Skip to content

Commit

Permalink
Merge pull request #211 from InsightsPlugin/feature/1.20.4
Browse files Browse the repository at this point in the history
Add 1.20.4 support
  • Loading branch information
FrankHeijden authored Dec 23, 2023
2 parents e0dfe0d + 5967b7b commit 065b307
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public enum InsightsNMSVersion {
v1_19_4_R3,
v1_20_R1,
v1_20_R2,
v1_20_R3,
}
3 changes: 3 additions & 0 deletions Insights-NMS/v1_20_R3/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
paperweight.paperDevBundle("1.20.4-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
@@ -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<ChunkSection> 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<ChunkSection> sectionConsumer) {
var serverLevel = ((CraftWorld) world).getHandle();
int sectionsCount = serverLevel.getSectionsCount();
var chunkMap = serverLevel.getChunkSource().chunkMap;
var chunkPos = new ChunkPos(chunkX, chunkZ);

Optional<CompoundTag> tagOptional = chunkMap.read(chunkPos).join();
if (tagOptional.isEmpty()) return;
CompoundTag tag = tagOptional.get();

ListTag sectionsTagList = tag.getList("sections", Tag.TAG_COMPOUND);

DataResult<PalettedContainer<BlockState>> 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<BlockState> blockStateContainer;
if (sectionTag.contains("block_states", Tag.TAG_COMPOUND)) {
Codec<PalettedContainer<BlockState>> 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<ChunkEntity> 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<ChunkEntity> 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<ChunkEntity> entityConsumer) {
for (Tag tag : listTag) {
readChunkEntities((CompoundTag) tag, entityConsumer);
}
}

private void readChunkEntities(CompoundTag nbt, Consumer<ChunkEntity> 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<Material, Integer> consumer) {
chunkSection.getStates().count((state, count) -> {
consumer.accept(CraftMagicNumbers.getMaterial(state.getBlock()), count);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/VersionConstants.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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"
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"
Expand Down

0 comments on commit 065b307

Please sign in to comment.