From 503f4ecba64c103cae486d2c2ecc742f78a9df25 Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Mon, 31 May 2021 16:09:39 +0200 Subject: [PATCH] Switch to Packet Tweaker for getting player --- build.gradle | 5 ++- gradle.properties | 2 +- .../interfaces/PlayerContextInterface.java | 8 ---- .../block/BlockUpdateS2CPacketAccessor.java | 16 ------- .../mixin/block/ServerChunkManagerMixin.java | 8 +++- .../block/ServerPlayNetworkHandlerMixin.java | 7 ++- .../mixin/item/PacketByteBufMixin.java | 5 +-- .../other/PacketByteBufContextMixin.java | 23 ---------- .../mixin/other/PacketContextMixin.java | 44 ------------------- .../ServerPlayNetworkHandlerContextMixin.java | 28 ------------ src/main/resources/polymer.mixins.json | 6 +-- src/testmod/resources/test.mixins.json | 2 +- 12 files changed, 17 insertions(+), 137 deletions(-) delete mode 100644 src/main/java/eu/pb4/polymer/interfaces/PlayerContextInterface.java delete mode 100644 src/main/java/eu/pb4/polymer/mixin/block/BlockUpdateS2CPacketAccessor.java delete mode 100644 src/main/java/eu/pb4/polymer/mixin/other/PacketByteBufContextMixin.java delete mode 100644 src/main/java/eu/pb4/polymer/mixin/other/PacketContextMixin.java delete mode 100644 src/main/java/eu/pb4/polymer/mixin/other/ServerPlayNetworkHandlerContextMixin.java diff --git a/build.gradle b/build.gradle index 617cd5ae..fbf2023a 100644 --- a/build.gradle +++ b/build.gradle @@ -57,7 +57,7 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - modRuntime "com.github.SuperCoder7979:databreaker:0.2.6" + //modRuntime "com.github.SuperCoder7979:databreaker:0.2.6" modCompileOnly "com.github.CaffeineMC.lithium-fabric:lithium:d2a5d9e" @@ -65,7 +65,8 @@ dependencies { exclude module: 'artifice' } - //modImplementation include("fr.catcore:server-translations-api:1.4.1") + modImplementation include("fr.catcore:server-translations-api:1.4.2+1.17") + modImplementation include("xyz.nucleoid:packet-tweaker:0.2.0-1.17-pre1") // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. // You may need to force-disable transitiveness on them. diff --git a/gradle.properties b/gradle.properties index 5c29714d..96f7ce04 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.11.3 # Mod Properties - mod_version = 0.1.0-pre0 + mod_version = 0.1.0-pre0-1.17-pre1 maven_group = eu.pb4 archives_base_name = polymer diff --git a/src/main/java/eu/pb4/polymer/interfaces/PlayerContextInterface.java b/src/main/java/eu/pb4/polymer/interfaces/PlayerContextInterface.java deleted file mode 100644 index 14bf479e..00000000 --- a/src/main/java/eu/pb4/polymer/interfaces/PlayerContextInterface.java +++ /dev/null @@ -1,8 +0,0 @@ -package eu.pb4.polymer.interfaces; - -import net.minecraft.server.network.ServerPlayerEntity; - -public interface PlayerContextInterface { - void setPolymerPlayer(ServerPlayerEntity player); - ServerPlayerEntity getPolymerPlayer(); -} diff --git a/src/main/java/eu/pb4/polymer/mixin/block/BlockUpdateS2CPacketAccessor.java b/src/main/java/eu/pb4/polymer/mixin/block/BlockUpdateS2CPacketAccessor.java deleted file mode 100644 index 4c8477d2..00000000 --- a/src/main/java/eu/pb4/polymer/mixin/block/BlockUpdateS2CPacketAccessor.java +++ /dev/null @@ -1,16 +0,0 @@ -package eu.pb4.polymer.mixin.block; - -import net.minecraft.block.BlockState; -import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -@Mixin(BlockUpdateS2CPacket.class) -public interface BlockUpdateS2CPacketAccessor { - @Accessor(value = "state") - BlockState getStateServer(); - - @Accessor(value = "pos") - BlockPos getPosServer(); -} diff --git a/src/main/java/eu/pb4/polymer/mixin/block/ServerChunkManagerMixin.java b/src/main/java/eu/pb4/polymer/mixin/block/ServerChunkManagerMixin.java index 560ab8d7..ea075dc2 100644 --- a/src/main/java/eu/pb4/polymer/mixin/block/ServerChunkManagerMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/block/ServerChunkManagerMixin.java @@ -26,6 +26,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import java.util.BitSet; import java.util.Set; import java.util.stream.Collectors; @@ -47,6 +48,7 @@ public abstract class ServerChunkManagerMixin { @Nullable public abstract WorldChunk getWorldChunk(int chunkX, int chunkZ); + @Shadow @Final private ServerLightingProvider lightProvider; @Unique private final Object2LongArrayMap lastUpdates = new Object2LongArrayMap<>(); @@ -86,12 +88,14 @@ private void sendLightUpdates(LightType type, ChunkSectionPos pos, CallbackInfo } if (sendUpdate) { - //Packet packet = new LightUpdateS2CPacket(pos.toChunkPos(), this.getLightingProvider(), true); + BitSet bitSet = new BitSet(); + bitSet.set(pos.getSectionY() - this.lightProvider.getBottomY()); + Packet packet = new LightUpdateS2CPacket(pos.toChunkPos(), this.getLightingProvider(), new BitSet(this.world.getTopSectionCoord() + 2), bitSet, true); Set players = this.threadedAnvilChunkStorage.getPlayersWatchingChunk(pos.toChunkPos(), false).collect(Collectors.toSet()); if (players.size() > 0) { this.lastUpdates.put(pos.toChunkPos(), System.currentTimeMillis()); for (ServerPlayerEntity player : players) { - //player.networkHandler.sendPacket(packet); + player.networkHandler.sendPacket(packet); } } } diff --git a/src/main/java/eu/pb4/polymer/mixin/block/ServerPlayNetworkHandlerMixin.java b/src/main/java/eu/pb4/polymer/mixin/block/ServerPlayNetworkHandlerMixin.java index 17d5d406..efe06a5d 100644 --- a/src/main/java/eu/pb4/polymer/mixin/block/ServerPlayNetworkHandlerMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/block/ServerPlayNetworkHandlerMixin.java @@ -39,12 +39,11 @@ public abstract class ServerPlayNetworkHandlerMixin { @Inject(method = "sendPacket(Lnet/minecraft/network/Packet;Lio/netty/util/concurrent/GenericFutureListener;)V", at = @At("TAIL")) private void catchBlockUpdates(Packet packet, GenericFutureListener> listener, CallbackInfo cb) { try { - if (packet instanceof BlockUpdateS2CPacket) { - BlockUpdateS2CPacketAccessor b = (BlockUpdateS2CPacketAccessor) packet; - BlockState blockState = b.getStateServer(); + if (packet instanceof BlockUpdateS2CPacket blockUpdatePacket) { + BlockState blockState = blockUpdatePacket.getState(); if (blockState.getBlock() instanceof VirtualBlock) { - BlockPos pos = ((BlockUpdateS2CPacketAccessor) packet).getPosServer(); + BlockPos pos = blockUpdatePacket.getPos(); ((VirtualBlock) blockState.getBlock()).sendPacketsAfterCreation(this.player, pos, blockState); } } else if (packet instanceof ChunkDataS2CPacket) { diff --git a/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java index 45021df0..52d3a473 100644 --- a/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java @@ -1,6 +1,5 @@ package eu.pb4.polymer.mixin.item; -import eu.pb4.polymer.interfaces.PlayerContextInterface; import eu.pb4.polymer.item.ItemHelper; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketByteBuf; @@ -9,13 +8,13 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import xyz.nucleoid.packettweaker.PacketContext; @Mixin(value = PacketByteBuf.class, priority = 500) public class PacketByteBufMixin { - @ModifyVariable(method = "writeItemStack", at = @At("HEAD"), ordinal = 0) private ItemStack replaceWithVanillaItem(ItemStack itemStack) { - return ItemHelper.getVirtualItemStack(itemStack, ((PlayerContextInterface) this).getPolymerPlayer()); + return ItemHelper.getVirtualItemStack(itemStack, PacketContext.get().getTarget()); } @Inject(method = "readItemStack", at = @At("RETURN"), cancellable = true) diff --git a/src/main/java/eu/pb4/polymer/mixin/other/PacketByteBufContextMixin.java b/src/main/java/eu/pb4/polymer/mixin/other/PacketByteBufContextMixin.java deleted file mode 100644 index 5cbbb92e..00000000 --- a/src/main/java/eu/pb4/polymer/mixin/other/PacketByteBufContextMixin.java +++ /dev/null @@ -1,23 +0,0 @@ -package eu.pb4.polymer.mixin.other; - -import eu.pb4.polymer.interfaces.PlayerContextInterface; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.network.ServerPlayerEntity; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; - -@Mixin(PacketByteBuf.class) -public class PacketByteBufContextMixin implements PlayerContextInterface { - @Unique - ServerPlayerEntity player; - - @Override - public void setPolymerPlayer(ServerPlayerEntity player) { - this.player = player; - } - - @Override - public ServerPlayerEntity getPolymerPlayer() { - return this.player; - } -} diff --git a/src/main/java/eu/pb4/polymer/mixin/other/PacketContextMixin.java b/src/main/java/eu/pb4/polymer/mixin/other/PacketContextMixin.java deleted file mode 100644 index e5806efb..00000000 --- a/src/main/java/eu/pb4/polymer/mixin/other/PacketContextMixin.java +++ /dev/null @@ -1,44 +0,0 @@ -package eu.pb4.polymer.mixin.other; - -import eu.pb4.polymer.interfaces.PlayerContextInterface; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.s2c.play.*; -import net.minecraft.server.network.ServerPlayerEntity; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -// Modified version of PolyMC mixin - https://github.com/TheEpicBlock/PolyMc/blob/master/src/main/java/io/github/theepicblock/polymc/mixins/context/PacketPlayerContextContainer.java - -@Mixin({AdvancementUpdateS2CPacket.class, - EntityEquipmentUpdateS2CPacket.class, - InventoryS2CPacket.class, - ScreenHandlerSlotUpdateS2CPacket.class, - SynchronizeRecipesS2CPacket.class, - SetTradeOffersS2CPacket.class, - EntityTrackerUpdateS2CPacket.class, - ParticleS2CPacket.class, - SynchronizeTagsS2CPacket.class}) - -public class PacketContextMixin implements PlayerContextInterface { - @Unique - private ServerPlayerEntity player; - - @Override - public ServerPlayerEntity getPolymerPlayer() { - return this.player; - } - - @Override - public void setPolymerPlayer(ServerPlayerEntity player) { - this.player = player; - } - - - @Inject(method = "write(Lnet/minecraft/network/PacketByteBuf;)V", at = @At("HEAD")) - private void writeInject(PacketByteBuf buf, CallbackInfo ci) { - ((PlayerContextInterface)buf).setPolymerPlayer(this.player); - } -} \ No newline at end of file diff --git a/src/main/java/eu/pb4/polymer/mixin/other/ServerPlayNetworkHandlerContextMixin.java b/src/main/java/eu/pb4/polymer/mixin/other/ServerPlayNetworkHandlerContextMixin.java deleted file mode 100644 index bb4c2def..00000000 --- a/src/main/java/eu/pb4/polymer/mixin/other/ServerPlayNetworkHandlerContextMixin.java +++ /dev/null @@ -1,28 +0,0 @@ -package eu.pb4.polymer.mixin.other; - -import eu.pb4.polymer.interfaces.PlayerContextInterface; -import io.netty.util.concurrent.GenericFutureListener; -import net.minecraft.network.Packet; -import net.minecraft.network.packet.c2s.play.RequestCommandCompletionsC2SPacket; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.server.network.ServerPlayerEntity; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import java.util.concurrent.Future; - -@Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerContextMixin { - @Shadow - public ServerPlayerEntity player; - - @Inject(method = "sendPacket(Lnet/minecraft/network/Packet;Lio/netty/util/concurrent/GenericFutureListener;)V", at = @At("HEAD")) - public void setPlayerInPacket(Packet packet, GenericFutureListener> listener, CallbackInfo ci) { - if (packet instanceof PlayerContextInterface) { - ((PlayerContextInterface) packet).setPolymerPlayer(this.player); - } - } -} diff --git a/src/main/resources/polymer.mixins.json b/src/main/resources/polymer.mixins.json index cfe1e34e..7d46d67d 100644 --- a/src/main/resources/polymer.mixins.json +++ b/src/main/resources/polymer.mixins.json @@ -2,14 +2,13 @@ "required": true, "minVersion": "0.8", "package": "eu.pb4.polymer.mixin", - "compatibilityLevel": "JAVA_8", + "compatibilityLevel": "JAVA_16", "plugin": "eu.pb4.polymer.mixin.MixinPlugin", "mixins": [ "block.AbstractBlockAccessor", "block.AbstractBlockMixin", "block.AbstractBlockSettingAccessor", "block.BlockPaletteMixin", - "block.BlockUpdateS2CPacketAccessor", "block.BlockUpdateS2CPacketMixin", "block.ChunkDataS2CPacketMixin", "block.ChunkDeltaUpdateS2CPacketAccessor", @@ -30,9 +29,6 @@ "item.ServerPlayNetworkHandlerMixin", "item.ItemStackContentMixin", "other.DimensionTypeAccessor", - "other.PacketByteBufContextMixin", - "other.PacketContextMixin", - "other.ServerPlayNetworkHandlerContextMixin", "other.SynchronizeRecipesS2CPacketMixin", "other.RegistrySyncManagerMixin", "polymc.BlockPolyGeneratorMixin" diff --git a/src/testmod/resources/test.mixins.json b/src/testmod/resources/test.mixins.json index 40001031..119e78fb 100644 --- a/src/testmod/resources/test.mixins.json +++ b/src/testmod/resources/test.mixins.json @@ -2,7 +2,7 @@ "required": true, "minVersion": "0.8", "package": "eu.pb4.polymertest.mixin", - "compatibilityLevel": "JAVA_8", + "compatibilityLevel": "JAVA_16", "mixins": [ "VillagerEntityAccessor", "AECAccessor"