Skip to content

Commit

Permalink
feat: add support for 1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingrim4 committed Dec 10, 2023
1 parent 87fc5a4 commit 25eef37
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/buildtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ checkVersion "1.19.3" "17"
checkVersion "1.19.4" "17"
checkVersion "1.20.1" "17"
checkVersion "1.20.2" "17"
checkVersion "1.20.4" "17"
81 changes: 81 additions & 0 deletions orebfuscator-nms/orebfuscator-nms-v1_20_R3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>net.imprex</groupId>
<artifactId>orebfuscator-nms</artifactId>
<version>${revision}</version>
</parent>

<artifactId>orebfuscator-nms-v1_20_R3</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>orebfuscator-nms-api</artifactId>
<version>${revision}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<classifier>remapped-mojang</classifier>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>mojang-mapped</shadedClassifierName>
<relocations>
<relocation>
<pattern>net.imprex.orebfuscator.nms.v1_20_R3</pattern>
<shadedPattern>net.imprex.orebfuscator.nms.v1_20_R3_mojang</shadedPattern>
</relocation>
</relocations>
</configuration>
</plugin>
<plugin>
<groupId>net.md-5</groupId>
<artifactId>specialsource-maven-plugin</artifactId>
<version>${plugin.specialsource.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>remap</goal>
</goals>
<id>remap-obf</id>
<configuration>
<srgIn>org.spigotmc:minecraft-server:1.20.4-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>org.spigotmc:spigot:1.20.4-R0.1-SNAPSHOT:jar:remapped-mojang</remappedDependencies>
<remappedArtifactAttached>true</remappedArtifactAttached>
<remappedClassifierName>remapped-obf</remappedClassifierName>
</configuration>
</execution>
<execution>
<phase>package</phase>
<goals>
<goal>remap</goal>
</goals>
<id>remap-spigot</id>
<configuration>
<inputFile>${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar</inputFile>
<srgIn>org.spigotmc:minecraft-server:1.20.4-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>org.spigotmc:spigot:1.20.4-R0.1-SNAPSHOT:jar:remapped-obf</remappedDependencies>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package net.imprex.orebfuscator.nms.v1_20_R3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R3.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;

import com.google.common.collect.ImmutableList;

import it.unimi.dsi.fastutil.shorts.Short2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
import net.imprex.orebfuscator.config.Config;
import net.imprex.orebfuscator.nms.AbstractNmsManager;
import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.imprex.orebfuscator.util.BlockProperties;
import net.imprex.orebfuscator.util.BlockStateProperties;
import net.imprex.orebfuscator.util.NamespacedKey;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection;

public class NmsManager extends AbstractNmsManager {

private static final int BLOCK_ID_AIR = Block.getId(Blocks.AIR.defaultBlockState());

static int getBlockState(LevelChunk chunk, int x, int y, int z) {
LevelChunkSection[] sections = chunk.getSections();

int sectionIndex = chunk.getSectionIndex(y);
if (sectionIndex >= 0 && sectionIndex < sections.length) {
LevelChunkSection section = sections[sectionIndex];
if (section != null && !section.hasOnlyAir()) {
return Block.getId(section.getBlockState(x & 0xF, y & 0xF, z & 0xF));
}
}

return BLOCK_ID_AIR;
}

private static ServerLevel level(World world) {
return ((CraftWorld) world).getHandle();
}

private static ServerPlayer player(Player player) {
return ((CraftPlayer) player).getHandle();
}

public NmsManager(Config config) {
super(Block.BLOCK_STATE_REGISTRY.size(), new RegionFileCache(config.cache()));

for (Map.Entry<ResourceKey<Block>, Block> entry : BuiltInRegistries.BLOCK.entrySet()) {
NamespacedKey namespacedKey = NamespacedKey.fromString(entry.getKey().location().toString());
Block block = entry.getValue();

ImmutableList<BlockState> possibleBlockStates = block.getStateDefinition().getPossibleStates();
List<BlockStateProperties> possibleBlockStateProperties = new ArrayList<>();

for (BlockState blockState : possibleBlockStates) {
Material material = CraftBlockData.fromData(blockState).getMaterial();

BlockStateProperties properties = BlockStateProperties.builder(Block.getId(blockState))
.withIsAir(blockState.isAir())
// check if material is occluding and use blockData check for rare edge cases like barrier, spawner, slime_block, ...
.withIsOccluding(material.isOccluding() && blockState.canOcclude())
.withIsBlockEntity(blockState.hasBlockEntity())
.build();

possibleBlockStateProperties.add(properties);
this.registerBlockStateProperties(properties);
}

int defaultBlockStateId = Block.getId(block.defaultBlockState());
BlockStateProperties defaultBlockState = getBlockStateProperties(defaultBlockStateId);

BlockProperties blockProperties = BlockProperties.builder(namespacedKey)
.withDefaultBlockState(defaultBlockState)
.withPossibleBlockStates(ImmutableList.copyOf(possibleBlockStateProperties))
.build();

this.registerBlockProperties(blockProperties);
}
}

@Override
public ReadOnlyChunk getReadOnlyChunk(World world, int chunkX, int chunkZ) {
ServerChunkCache serverChunkCache = level(world).getChunkSource();
LevelChunk chunk = serverChunkCache.getChunk(chunkX, chunkZ, true);
return new ReadOnlyChunkWrapper(chunk);
}

@Override
public int getBlockState(World world, int x, int y, int z) {
ServerChunkCache serverChunkCache = level(world).getChunkSource();
if (!serverChunkCache.isChunkLoaded(x >> 4, z >> 4)) {
return BLOCK_ID_AIR;
}

LevelChunk chunk = serverChunkCache.getChunk(x >> 4, z >> 4, true);
if (chunk == null) {
return BLOCK_ID_AIR;
}

return getBlockState(chunk, x, y, z);
}

@Override
public void sendBlockUpdates(World world, Iterable<net.imprex.orebfuscator.util.BlockPos> iterable) {
ServerChunkCache serverChunkCache = level(world).getChunkSource();
BlockPos.MutableBlockPos position = new BlockPos.MutableBlockPos();

for (net.imprex.orebfuscator.util.BlockPos pos : iterable) {
position.set(pos.x, pos.y, pos.z);
serverChunkCache.blockChanged(position);
}
}

@Override
public void sendBlockUpdates(Player player, Iterable<net.imprex.orebfuscator.util.BlockPos> iterable) {
ServerPlayer serverPlayer = player(player);
ServerLevel level = serverPlayer.serverLevel();
ServerChunkCache serverChunkCache = level.getChunkSource();

BlockPos.MutableBlockPos position = new BlockPos.MutableBlockPos();
Map<SectionPos, Short2ObjectMap<BlockState>> sectionPackets = new HashMap<>();
List<Packet<ClientGamePacketListener>> blockEntityPackets = new ArrayList<>();

for (net.imprex.orebfuscator.util.BlockPos pos : iterable) {
if (!serverChunkCache.isChunkLoaded(pos.x >> 4, pos.z >> 4)) {
continue;
}

position.set(pos.x, pos.y, pos.z);
BlockState blockState = level.getBlockState(position);

sectionPackets.computeIfAbsent(SectionPos.of(position), key -> new Short2ObjectLinkedOpenHashMap<>())
.put(SectionPos.sectionRelativePos(position), blockState);

if (blockState.hasBlockEntity()) {
BlockEntity blockEntity = level.getBlockEntity(position);
if (blockEntity != null) {
blockEntityPackets.add(blockEntity.getUpdatePacket());
}
}
}

for (Map.Entry<SectionPos, Short2ObjectMap<BlockState>> entry : sectionPackets.entrySet()) {
Short2ObjectMap<BlockState> blockStates = entry.getValue();
if (blockStates.size() == 1) {
Short2ObjectMap.Entry<BlockState> blockEntry = blockStates.short2ObjectEntrySet().iterator().next();
BlockPos blockPosition = entry.getKey().relativeToBlockPos(blockEntry.getShortKey());
serverPlayer.connection.send(new ClientboundBlockUpdatePacket(blockPosition, blockEntry.getValue()));
} else {
serverPlayer.connection.send(new ClientboundSectionBlocksUpdatePacket(entry.getKey(),
blockStates.keySet(), blockStates.values().toArray(BlockState[]::new)));
}
}

for (Packet<ClientGamePacketListener> packet : blockEntityPackets) {
serverPlayer.connection.send(packet);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.imprex.orebfuscator.nms.v1_20_R3;

import net.imprex.orebfuscator.nms.ReadOnlyChunk;
import net.minecraft.world.level.chunk.LevelChunk;

public class ReadOnlyChunkWrapper implements ReadOnlyChunk {

private final LevelChunk chunk;

ReadOnlyChunkWrapper(LevelChunk chunk) {
this.chunk = chunk;
}

@Override
public int getBlockState(int x, int y, int z) {
return NmsManager.getBlockState(chunk, x, y, z);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.imprex.orebfuscator.nms.v1_20_R3;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.file.Path;

import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_20_R3.CraftServer;

import net.imprex.orebfuscator.config.CacheConfig;
import net.imprex.orebfuscator.nms.AbstractRegionFileCache;
import net.imprex.orebfuscator.util.ChunkPosition;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.RegionFile;
import net.minecraft.world.level.chunk.storage.RegionFileVersion;

public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {

RegionFileCache(CacheConfig cacheConfig) {
super(cacheConfig);
}

@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
}

@Override
protected void closeRegionFile(RegionFile t) throws IOException {
t.close();
}

@Override
protected DataInputStream createInputStream(RegionFile t, ChunkPosition key) throws IOException {
return t.getChunkDataInputStream(new ChunkPos(key.x, key.z));
}

@Override
protected DataOutputStream createOutputStream(RegionFile t, ChunkPosition key) throws IOException {
return t.getChunkDataOutputStream(new ChunkPos(key.x, key.z));
}
}
1 change: 1 addition & 0 deletions orebfuscator-nms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
<module>orebfuscator-nms-v1_19_R3</module>
<module>orebfuscator-nms-v1_20_R1</module>
<module>orebfuscator-nms-v1_20_R2</module>
<module>orebfuscator-nms-v1_20_R3</module>
</modules>
</project>
13 changes: 13 additions & 0 deletions orebfuscator-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,18 @@
<version>${revision}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>orebfuscator-nms-v1_20_R3</artifactId>
<version>${revision}</version>
<classifier>mojang-mapped</classifier>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>orebfuscator-nms-v1_20_R3</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

0 comments on commit 25eef37

Please sign in to comment.