From 7af1522157dba9a6458b0b80e975c903c691d64e Mon Sep 17 00:00:00 2001 From: Frank van der Heijden Date: Mon, 11 Dec 2023 14:20:26 +0100 Subject: [PATCH 1/2] Add 1.20.4 support --- .../insights/nms/core/InsightsNMSVersion.java | 1 + Insights-NMS/v1_20_R3/build.gradle.kts | 3 + .../insights/nms/impl/InsightsNMSImpl.java | 189 ++++++++++++++++++ .../dev/frankheijden/insights/Insights.java | 4 +- buildSrc/src/main/kotlin/VersionConstants.kt | 2 +- 5 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 Insights-NMS/v1_20_R3/build.gradle.kts create mode 100644 Insights-NMS/v1_20_R3/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java diff --git a/Insights-NMS/Core/src/main/java/dev/frankheijden/insights/nms/core/InsightsNMSVersion.java b/Insights-NMS/Core/src/main/java/dev/frankheijden/insights/nms/core/InsightsNMSVersion.java index af68c040..0b45f122 100644 --- a/Insights-NMS/Core/src/main/java/dev/frankheijden/insights/nms/core/InsightsNMSVersion.java +++ b/Insights-NMS/Core/src/main/java/dev/frankheijden/insights/nms/core/InsightsNMSVersion.java @@ -7,4 +7,5 @@ public enum InsightsNMSVersion { v1_19_4_R3, v1_20_R1, v1_20_R2, + v1_20_R3, } diff --git a/Insights-NMS/v1_20_R3/build.gradle.kts b/Insights-NMS/v1_20_R3/build.gradle.kts new file mode 100644 index 00000000..7c25f4ee --- /dev/null +++ b/Insights-NMS/v1_20_R3/build.gradle.kts @@ -0,0 +1,3 @@ +dependencies { + paperweight.paperDevBundle("1.20.4-R0.1-SNAPSHOT") +} diff --git a/Insights-NMS/v1_20_R3/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java b/Insights-NMS/v1_20_R3/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java new file mode 100644 index 00000000..a5239f5e --- /dev/null +++ b/Insights-NMS/v1_20_R3/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java @@ -0,0 +1,189 @@ +package dev.frankheijden.insights.nms.impl; + +import ca.spottedleaf.concurrentutil.executor.standard.PrioritisedExecutor; +import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; +import dev.frankheijden.insights.nms.core.ChunkEntity; +import dev.frankheijden.insights.nms.core.ChunkSection; +import dev.frankheijden.insights.nms.core.InsightsNMS; +import io.papermc.paper.chunk.system.io.RegionFileIOThread; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.NbtOps; +import net.minecraft.nbt.Tag; +import net.minecraft.util.Mth; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.ChunkStatus; +import net.minecraft.world.level.chunk.LevelChunkSection; +import net.minecraft.world.level.chunk.PalettedContainer; +import net.minecraft.world.level.chunk.storage.ChunkSerializer; +import org.bukkit.Chunk; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_20_R3.CraftChunk; +import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; +import org.bukkit.craftbukkit.v1_20_R3.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_20_R3.util.CraftMagicNumbers; +import org.bukkit.entity.EntityType; +import java.io.IOException; +import java.util.Optional; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +public class InsightsNMSImpl extends InsightsNMS { + + @Override + public void getLoadedChunkSections(Chunk chunk, Consumer sectionConsumer) { + LevelChunkSection[] levelChunkSections = ((CraftChunk) chunk).getHandle(ChunkStatus.FULL).getSections(); + for (int i = 0; i < levelChunkSections.length; i++) { + sectionConsumer.accept(new ChunkSectionImpl(levelChunkSections[i], i)); + } + } + + @Override + public void getUnloadedChunkSections(World world, int chunkX, int chunkZ, Consumer sectionConsumer) { + var serverLevel = ((CraftWorld) world).getHandle(); + int sectionsCount = serverLevel.getSectionsCount(); + var chunkMap = serverLevel.getChunkSource().chunkMap; + var chunkPos = new ChunkPos(chunkX, chunkZ); + + Optional tagOptional = chunkMap.read(chunkPos).join(); + if (tagOptional.isEmpty()) return; + CompoundTag tag = tagOptional.get(); + + ListTag sectionsTagList = tag.getList("sections", Tag.TAG_COMPOUND); + + DataResult> dataResult; + int nonNullSectionCount = 0; + for (int i = 0; i < sectionsTagList.size(); i++) { + CompoundTag sectionTag = sectionsTagList.getCompound(i); + var chunkSectionPart = sectionTag.getByte("Y"); + var sectionIndex = serverLevel.getSectionIndexFromSectionY(chunkSectionPart); + if (sectionIndex < 0 || sectionIndex >= sectionsCount) continue; + + PalettedContainer blockStateContainer; + if (sectionTag.contains("block_states", Tag.TAG_COMPOUND)) { + Codec> blockStateCodec = ChunkSerializer.BLOCK_STATE_CODEC; + dataResult = blockStateCodec.parse( + NbtOps.INSTANCE, + sectionTag.getCompound("block_states") + ).promotePartial(message -> logger.severe(String.format( + CHUNK_ERROR, + chunkX, + chunkSectionPart, + chunkZ, + message + ))); + blockStateContainer = dataResult.getOrThrow(false, logger::severe); + } else { + blockStateContainer = new PalettedContainer<>( + Block.BLOCK_STATE_REGISTRY, + Blocks.AIR.defaultBlockState(), + PalettedContainer.Strategy.SECTION_STATES + ); + } + + LevelChunkSection chunkSection = new LevelChunkSection(blockStateContainer, null); + sectionConsumer.accept(new ChunkSectionImpl(chunkSection, sectionIndex)); + nonNullSectionCount++; + } + + for (int i = nonNullSectionCount; i < sectionsCount; i++) { + sectionConsumer.accept(null); + } + } + + @Override + public void getLoadedChunkEntities(Chunk chunk, Consumer entityConsumer) { + for (org.bukkit.entity.Entity bukkitEntity : chunk.getEntities()) { + Entity entity = ((CraftEntity) bukkitEntity).getHandle(); + entityConsumer.accept(new ChunkEntity( + bukkitEntity.getType(), + entity.getBlockX(), + entity.getBlockY(), + entity.getBlockZ() + )); + } + } + + @Override + public void getUnloadedChunkEntities( + World world, + int chunkX, + int chunkZ, + Consumer entityConsumer + ) throws IOException { + var serverLevel = ((CraftWorld) world).getHandle(); + CompoundTag tag = RegionFileIOThread.loadData( + serverLevel, + chunkX, + chunkZ, + RegionFileIOThread.RegionFileType.ENTITY_DATA, + PrioritisedExecutor.Priority.BLOCKING + ); + if (tag == null) return; + + readChunkEntities(tag.getList("Entities", Tag.TAG_COMPOUND), entityConsumer); + } + + private void readChunkEntities(ListTag listTag, Consumer entityConsumer) { + for (Tag tag : listTag) { + readChunkEntities((CompoundTag) tag, entityConsumer); + } + } + + private void readChunkEntities(CompoundTag nbt, Consumer entityConsumer) { + var typeOptional = net.minecraft.world.entity.EntityType.by(nbt); + if (typeOptional.isPresent()) { + String entityTypeName = net.minecraft.world.entity.EntityType.getKey(typeOptional.get()).getPath(); + ListTag posList = nbt.getList("Pos", Tag.TAG_DOUBLE); + entityConsumer.accept(new ChunkEntity( + EntityType.fromName(entityTypeName), + Mth.floor(Mth.clamp(posList.getDouble(0), -3E7D, 3E7D)), + Mth.floor(Mth.clamp(posList.getDouble(1), -2E7D, 2E7D)), + Mth.floor(Mth.clamp(posList.getDouble(2), -3E7D, 3E7D)) + )); + } + + if (nbt.contains("Passengers", Tag.TAG_LIST)) { + readChunkEntities(nbt.getList("Passengers", Tag.TAG_COMPOUND), entityConsumer); + } + } + + public static class ChunkSectionImpl implements ChunkSection { + + private final LevelChunkSection chunkSection; + private final int index; + + public ChunkSectionImpl(LevelChunkSection chunkSection, int index) { + this.chunkSection = chunkSection; + this.index = index; + } + + @Override + public int index() { + return index; + } + + @Override + public boolean isNull() { + return chunkSection == null; + } + + @Override + public Material blockAt(int x, int y, int z) { + return CraftMagicNumbers.getMaterial(chunkSection.getBlockState(x, y, z).getBlock()); + } + + @Override + public void countBlocks(BiConsumer consumer) { + chunkSection.getStates().count((state, count) -> { + consumer.accept(CraftMagicNumbers.getMaterial(state.getBlock()), count); + }); + } + } +} diff --git a/Insights/src/main/java/dev/frankheijden/insights/Insights.java b/Insights/src/main/java/dev/frankheijden/insights/Insights.java index 0fac7330..081c611a 100644 --- a/Insights/src/main/java/dev/frankheijden/insights/Insights.java +++ b/Insights/src/main/java/dev/frankheijden/insights/Insights.java @@ -122,7 +122,9 @@ public void onEnable() { } } if (isMin(20) && PaperLib.isPaper()) { - if (isMin(20, 2)) { + if (isMin(20, 3)) { + nms = InsightsNMS.get(InsightsNMSVersion.v1_20_R3); + } else if (isMin(20, 2)) { nms = InsightsNMS.get(InsightsNMSVersion.v1_20_R2); } else { nms = InsightsNMS.get(InsightsNMSVersion.v1_20_R1); diff --git a/buildSrc/src/main/kotlin/VersionConstants.kt b/buildSrc/src/main/kotlin/VersionConstants.kt index fd1f18cb..cc1ca24b 100644 --- a/buildSrc/src/main/kotlin/VersionConstants.kt +++ b/buildSrc/src/main/kotlin/VersionConstants.kt @@ -1,5 +1,5 @@ object VersionConstants { - const val userdevVersion = "1.5.8" + const val userdevVersion = "1.5.11" const val pluginYmlVersion = "0.6.0" const val shadowVersion = "8.1.1" const val minecraftVersion = "1.19.4-R0.1-SNAPSHOT" From 5967b7b8b525c4be789e08afeec8f077e1c7af03 Mon Sep 17 00:00:00 2001 From: Frank van der Heijden Date: Sat, 23 Dec 2023 13:46:50 +0100 Subject: [PATCH 2/2] Bump adventure --- buildSrc/src/main/kotlin/VersionConstants.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buildSrc/src/main/kotlin/VersionConstants.kt b/buildSrc/src/main/kotlin/VersionConstants.kt index cc1ca24b..d0781cfd 100644 --- a/buildSrc/src/main/kotlin/VersionConstants.kt +++ b/buildSrc/src/main/kotlin/VersionConstants.kt @@ -4,8 +4,8 @@ object VersionConstants { const val shadowVersion = "8.1.1" const val minecraftVersion = "1.19.4-R0.1-SNAPSHOT" const val cloudVersion = "1.8.4" - const val adventureVersion = "4.14.0" - const val adventurePlatformVersion = "4.3.1" + const val adventureVersion = "4.15.0" + const val adventurePlatformVersion = "4.3.2" const val paperLibVersion = "1.0.8" const val bStatsVersion = "3.0.0" const val minecraftReflectionVersion = "123e2f546c"