Enhance compatibility and feature support for Minecraft 1.21: Add ProtocolWrapper21, update gamerule management, streamline entity tracking, and refine chunk hider logic.

This commit is contained in:
2025-07-31 11:34:56 +02:00
parent cf52b50333
commit e7803dcf82
18 changed files with 135 additions and 35 deletions
@@ -87,8 +87,8 @@ public class TechHiderCommand extends SWCommand implements Listener, ScoreboardE
Set<String> hiddenBlockEntities = Collections.unmodifiableSet(new HashSet<>(config.getStringList("Techhider.HiddenBlockEntities"))); Set<String> hiddenBlockEntities = Collections.unmodifiableSet(new HashSet<>(config.getStringList("Techhider.HiddenBlockEntities")));
TechHider current = new TechHider((TechHider.LocationEvaluator) (p, cX, cY) -> { TechHider current = new TechHider((TechHider.LocationEvaluator) (p, cX, cY) -> {
Chunk playerChunk = p.getLocation().getChunk(); if (rg.buildChunkOutside(cX, cY)) return true;
return playerChunk.getX() == cX && playerChunk.getZ() == cY; return !hidden.get(rg).contains(p);
}, Material.valueOf(obfuscateWith.toUpperCase()), hiddenBlocks.stream().map(String::toUpperCase).map(Material::valueOf).collect(Collectors.toSet()), hiddenBlockEntities); }, Material.valueOf(obfuscateWith.toUpperCase()), hiddenBlocks.stream().map(String::toUpperCase).map(Material::valueOf).collect(Collectors.toSet()), hiddenBlockEntities);
current.enable(); current.enable();
@@ -55,4 +55,7 @@ public class CraftbukkitWrapper10 implements CraftbukkitWrapper {
public Stream<?> entityIterator() { public Stream<?> entityIterator() {
return ((CraftWorld) Config.world).getHandle().entityList.stream(); return ((CraftWorld) Config.world).getHandle().entityList.stream();
} }
@Override
public void setupGamerule() { }
} }
@@ -55,4 +55,7 @@ public class CraftbukkitWrapper12 implements CraftbukkitWrapper {
public Stream<?> entityIterator() { public Stream<?> entityIterator() {
return ((CraftWorld) Config.world).getHandle().entityList.stream(); return ((CraftWorld) Config.world).getHandle().entityList.stream();
} }
@Override
public void setupGamerule() { }
} }
@@ -56,4 +56,7 @@ public class CraftbukkitWrapper14 implements CraftbukkitWrapper {
public Stream<?> entityIterator() { public Stream<?> entityIterator() {
return ((CraftWorld) Config.world).getHandle().entitiesById.values().stream(); return ((CraftWorld) Config.world).getHandle().entitiesById.values().stream();
} }
@Override
public void setupGamerule() { }
} }
@@ -56,4 +56,7 @@ public class CraftbukkitWrapper15 implements CraftbukkitWrapper {
public Stream<?> entityIterator() { public Stream<?> entityIterator() {
return ((CraftWorld) Config.world).getHandle().entitiesById.values().stream(); return ((CraftWorld) Config.world).getHandle().entitiesById.values().stream();
} }
@Override
public void setupGamerule() { }
} }
@@ -62,4 +62,7 @@ public class CraftbukkitWrapper18 implements CraftbukkitWrapper {
public Stream<?> entityIterator() { public Stream<?> entityIterator() {
return StreamSupport.stream(((Iterable<?>) getIterable.invoke(getWorldEntities.invoke(getWorld.invoke(Config.world)))).spliterator(), false); return StreamSupport.stream(((Iterable<?>) getIterable.invoke(getWorldEntities.invoke(getWorld.invoke(Config.world)))).spliterator(), false);
} }
@Override
public void setupGamerule() { }
} }
@@ -41,4 +41,5 @@ dependencies {
} }
compileOnly(libs.fastutil) compileOnly(libs.fastutil)
compileOnly(libs.authlib)
} }
@@ -19,34 +19,24 @@
package de.steamwar.fightsystem.utils; package de.steamwar.fightsystem.utils;
import de.steamwar.Reflection; import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.fightsystem.Config; import com.mojang.authlib.GameProfile;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import de.steamwar.core.ProtocolWrapper;
import de.steamwar.fightsystem.FightSystem;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlockState; import org.bukkit.craftbukkit.block.CraftBlockState;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class BlockIdWrapper21 implements BlockIdWrapper { public class BlockIdWrapper21 implements BlockIdWrapper {
static Int2ObjectMap<ChunkMap.TrackedEntity> tracker;
static Reflection.Field<Object> chunkMap = Reflection.getField(ServerChunkCache.class, "chunkMap", Object.class);
static {
ServerChunkCache chunkCache = ((CraftWorld) Config.world).getHandle().getChunkSource();
Object map = chunkMap.get(chunkCache);
Reflection.Field<Int2ObjectMap> eMapField = Reflection.getField(map.getClass(), "entityMap", Int2ObjectMap.class);
tracker = eMapField.get(map);
}
@Override @Override
public Material idToMaterial(int blockState) { public Material idToMaterial(int blockState) {
@@ -71,17 +61,17 @@ public class BlockIdWrapper21 implements BlockIdWrapper {
@Override @Override
public void trackEntity(Player player, Entity entity) { public void trackEntity(Player player, Entity entity) {
ChunkMap.TrackedEntity trackedEntity = tracker.get(entity.getEntityId()); if(entity instanceof Player)
if (trackedEntity != null) { TinyProtocol.instance.sendPacket(player, ProtocolWrapper.impl.playerInfoPacketConstructor(ProtocolWrapper.PlayerInfoAction.REMOVE, new GameProfile(entity.getUniqueId(), entity.getName()), GameMode.CREATIVE));
trackedEntity.updatePlayer(((CraftPlayer) player).getHandle());
} player.showEntity(FightSystem.getPlugin(), entity);
} }
@Override @Override
public void untrackEntity(Player player, Entity entity) { public void untrackEntity(Player player, Entity entity) {
ChunkMap.TrackedEntity trackedEntity = tracker.get(entity.getEntityId()); player.hideEntity(FightSystem.getPlugin(), entity);
if (trackedEntity != null) {
trackedEntity.removePlayer(((CraftPlayer) player).getHandle()); if(entity instanceof Player)
} TinyProtocol.instance.sendPacket(player, ProtocolWrapper.impl.playerInfoPacketConstructor(ProtocolWrapper.PlayerInfoAction.ADD, new GameProfile(entity.getUniqueId(), entity.getName()), GameMode.CREATIVE));
} }
} }
@@ -19,6 +19,8 @@
package de.steamwar.fightsystem.utils; package de.steamwar.fightsystem.utils;
import de.steamwar.fightsystem.Config;
import org.bukkit.GameRule;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
public class CraftbukkitWrapper21 extends CraftbukkitWrapper18 { public class CraftbukkitWrapper21 extends CraftbukkitWrapper18 {
@@ -27,4 +29,9 @@ public class CraftbukkitWrapper21 extends CraftbukkitWrapper18 {
public float headRotation(Entity e) { public float headRotation(Entity e) {
return getEntity(e).getYHeadRot(); return getEntity(e).getYHeadRot();
} }
@Override
public void setupGamerule() {
Config.world.setGameRule(GameRule.LOCATOR_BAR, false);
}
} }
@@ -52,4 +52,7 @@ public class CraftbukkitWrapper8 implements CraftbukkitWrapper {
public Stream<?> entityIterator() { public Stream<?> entityIterator() {
return ((CraftWorld) Config.world).getHandle().entityList.stream(); return ((CraftWorld) Config.world).getHandle().entityList.stream();
} }
@Override
public void setupGamerule() { }
} }
@@ -55,4 +55,7 @@ public class CraftbukkitWrapper9 implements CraftbukkitWrapper {
public Stream<?> entityIterator() { public Stream<?> entityIterator() {
return ((CraftWorld) Config.world).getHandle().entityList.stream(); return ((CraftWorld) Config.world).getHandle().entityList.stream();
} }
@Override
public void setupGamerule() { }
} }
@@ -188,6 +188,8 @@ public class FightSystem extends JavaPlugin {
}else if(Config.mode == ArenaMode.PREPARE) { }else if(Config.mode == ArenaMode.PREPARE) {
Fight.getUnrotated().setSchem(SchematicNode.getSchematicNode(Config.PrepareSchemID)); Fight.getUnrotated().setSchem(SchematicNode.getSchematicNode(Config.PrepareSchemID));
} }
CraftbukkitWrapper.impl.setupGamerule();
} }
@Override @Override
@@ -33,4 +33,6 @@ public interface CraftbukkitWrapper {
float headRotation(Entity e); float headRotation(Entity e);
Stream<?> entityIterator(); Stream<?> entityIterator();
void setupGamerule();
} }
@@ -59,7 +59,7 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati
private final SecretKey key; private final SecretKey key;
public TechHiderWrapper() { public TechHiderWrapper() {
super(ENABLED, FightState.All); super(ENABLED, FightState.Schem);
techHider = new TechHider(this, Config.ObfuscateWith, Config.HiddenBlocks, Config.HiddenBlockEntities); techHider = new TechHider(this, Config.ObfuscateWith, Config.HiddenBlocks, Config.HiddenBlockEntities);
try { try {
@@ -68,7 +68,7 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati
throw new SecurityException(e); throw new SecurityException(e);
} }
new StateDependentListener(ENABLED, FightState.All, this); new StateDependentListener(ENABLED, FightState.Schem, this);
register(); register();
} }
@@ -0,0 +1,66 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.core;
import com.mojang.authlib.GameProfile;
import com.mojang.datafixers.util.Pair;
import de.steamwar.Reflection;
import net.minecraft.Util;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.world.level.GameType;
import org.bukkit.GameMode;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.function.LongSupplier;
public class ProtocolWrapper21 implements ProtocolWrapper {
private static final Reflection.Field<List> equipmentStack = Reflection.getField(equipmentPacket, List.class, 0);
@Override
public void setEquipmentPacketStack(Object packet, Object slot, Object stack) {
equipmentStack.set(packet, Collections.singletonList(new Pair<>(slot, stack)));
}
private static final Reflection.Constructor removePacketConstructor = Reflection.getConstructor(ClientboundPlayerInfoRemovePacket.class, List.class);
@Override
@SuppressWarnings("deprecation")
public Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode) {
if(action == PlayerInfoAction.REMOVE)
return removePacketConstructor.invoke(Collections.singletonList(profile.getId()));
return switch (action) {
case ADD -> new ClientboundPlayerInfoUpdatePacket(EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_GAME_MODE), new ClientboundPlayerInfoUpdatePacket.Entry(
profile.getId(), profile, true, 0, GameType.byId(mode.getValue()), null, true, 0, null
));
case GAMEMODE -> new ClientboundPlayerInfoUpdatePacket(EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_GAME_MODE), new ClientboundPlayerInfoUpdatePacket.Entry(
profile.getId(), profile, true, 0, GameType.byId(mode.getValue()), null, true, 0, null
));
default -> null;
};
}
@Override
public void initTPSWarp(LongSupplier longSupplier) {
Util.timeSource = () -> System.nanoTime() + longSupplier.getAsLong();
}
}
@@ -123,7 +123,9 @@ public class ChunkHider21 implements ChunkHider {
for (int x = 0; x < 16; x++) { for (int x = 0; x < 16; x++) {
int pos = (((y * 16) + z) * 16) + x; int pos = (((y * 16) + z) * 16) + x;
switch (section.test(x, y, z)) { TechHider.State test = section.test(x, y, z);
switch (test) {
case SKIP: case SKIP:
break; break;
case CHECK: case CHECK:
@@ -157,14 +157,21 @@ public final class Reflection {
} else if(MAJOR_VERSION < 21 || MINOR_VERSION < 4) { } else if(MAJOR_VERSION < 21 || MINOR_VERSION < 4) {
return Class.forName(spigotClassnames.getOrDefault(name, name)); return Class.forName(spigotClassnames.getOrDefault(name, name));
} else { } else {
Class<?> clazz = Class.forName(name); Class<?> clazz = null;
if (clazz.getName().equals(name)) { try {
clazz = Class.forName(name);
} catch (ClassNotFoundException e) {}
if (clazz != null && clazz.getName().equals(name)) {
return clazz; return clazz;
} }
try { try {
return Core.class.getClassLoader().getParent().loadClass(name); return Core.class.getClassLoader().getParent().loadClass(name);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
if (clazz == null) {
throw e;
}
return clazz; return clazz;
} }
} }
@@ -147,8 +147,9 @@ public interface ChunkHider {
return; return;
} }
int longs = (int) (((long) entries * bitsPerBlock + 63) >> 6); char valuesPerLong = (char)(64 / bitsPerBlock);
out.writeBytes(in, longs * Long.BYTES); int i1 = (entries + valuesPerLong - 1) / valuesPerLong;
out.writeBytes(in, i1 * Long.BYTES);
} }
public long[] readDataArray() { public long[] readDataArray() {
@@ -164,9 +165,10 @@ public interface ChunkHider {
return new long[entries]; return new long[entries];
} }
int longs = (int) (((long) entries * bitsPerBlock + 63) >> 6); char valuesPerLong = (char) (64 / bitsPerBlock);
long[] array = new long[longs]; int i1 = (entries + valuesPerLong - 1) / valuesPerLong;
for(int i = 0; i < longs; i++) long[] array = new long[i1];
for(int i = 0; i < i1; i++)
array[i] = in.readLong(); array[i] = in.readLong();
return array; return array;