Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 1.20.5 #369

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ jobs:
key: ${{ runner.os }}-maven-${{ secrets.CACHE_VERSION }}-${{ hashFiles('./.github/workflows/buildtools.sh') }}
restore-keys: |
${{ runner.os }}-maven-${{ secrets.CACHE_VERSION }}-
- name: Set up JDK 8/17
- name: Set up JDK 8/17/21
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: |
8
17
21
- name: Run BuildTools
run: |
bash ./.github/workflows/buildtools.sh
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/buildtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ checkVersion "1.19.4" "17"
checkVersion "1.20.1" "17"
checkVersion "1.20.2" "17"
checkVersion "1.20.4" "17"
checkVersion "1.20.5" "21"
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ jobs:
key: ${{ runner.os }}-maven-${{ secrets.CACHE_VERSION }}-${{ hashFiles('./.github/workflows/buildtools.sh') }}
restore-keys: |
${{ runner.os }}-maven-${{ secrets.CACHE_VERSION }}-
- name: Set up JDK 8/17
- name: Set up JDK 8/17/21
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: |
8
17
21
- name: Run BuildTools
run: |
bash ./.github/workflows/buildtools.sh
Expand Down
81 changes: 81 additions & 0 deletions orebfuscator-nms/orebfuscator-nms-v1_20_R4/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_R4</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.5-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_R4</pattern>
<shadedPattern>net.imprex.orebfuscator.nms.v1_20_R4_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.5-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>org.spigotmc:spigot:1.20.5-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.5-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>org.spigotmc:spigot:1.20.5-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,176 @@
package net.imprex.orebfuscator.nms.v1_20_R4;

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

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R4.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R4.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_20_R4.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();
BlockProperties.Builder builder = BlockProperties.builder(namespacedKey);

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())
.withIsDefaultState(Objects.equals(block.defaultBlockState(), blockState))
.build();

builder.withBlockState(properties);
}

this.registerBlockProperties(builder.build());
}
}

@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_R4;

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_R4;

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_R4.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(null, 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 @@ -34,5 +34,6 @@
<module>orebfuscator-nms-v1_20_R1</module>
<module>orebfuscator-nms-v1_20_R2</module>
<module>orebfuscator-nms-v1_20_R3</module>
<module>orebfuscator-nms-v1_20_R4</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 @@ -295,5 +295,18 @@
<version>${revision}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>orebfuscator-nms-v1_20_R4</artifactId>
<version>${revision}</version>
<classifier>mojang-mapped</classifier>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>orebfuscator-nms-v1_20_R4</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Loading