From 05b751a60e4d26841d22738a6200ffb0024de622 Mon Sep 17 00:00:00 2001 From: Kosma Moczek Date: Tue, 23 May 2023 01:11:00 +0200 Subject: [PATCH] fabric-1.20: update code to 1.20 --- .../org/dynmap/fabric_1_20/DynmapPlugin.java | 16 ++++++------- .../fabric_1_20/FabricCommandSender.java | 3 +-- .../org/dynmap/fabric_1_20/FabricPlayer.java | 8 ++++--- .../org/dynmap/fabric_1_20/FabricServer.java | 2 +- .../dynmap/fabric_1_20/event/BlockEvents.java | 7 +++--- .../mixin/ServerPlayNetworkHandlerMixin.java | 24 ++++++++++++------- .../mixin/ServerPlayerEntityMixin.java | 2 +- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/DynmapPlugin.java b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/DynmapPlugin.java index 6657cc76..5ab8aa87 100644 --- a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/DynmapPlugin.java +++ b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/DynmapPlugin.java @@ -10,9 +10,9 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.FluidBlock; -import net.minecraft.block.Material; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; +import net.minecraft.registry.tag.BlockTags; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.server.MinecraftServer; @@ -53,6 +53,7 @@ import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.regex.Pattern; + public class DynmapPlugin { // FIXME: Fix package-private fields after splitting is done DynmapCore core; @@ -155,7 +156,6 @@ public class DynmapPlugin { String bn = ui.getNamespace() + ":" + ui.getPath(); // Only do defined names, and not "air" if (!bn.equals(DynmapBlockState.AIR_BLOCK)) { - Material mat = bs.getMaterial(); String statename = ""; for (net.minecraft.state.property.Property p : bs.getProperties()) { if (statename.length() > 0) { @@ -166,11 +166,11 @@ public class DynmapPlugin { int lightAtten = bs.isOpaqueFullCube(EmptyBlockView.INSTANCE, BlockPos.ORIGIN) ? 15 : (bs.isTransparent(EmptyBlockView.INSTANCE, BlockPos.ORIGIN) ? 0 : 1); //Log.info("statename=" + bn + "[" + statename + "], lightAtten=" + lightAtten); // Fill in base attributes - bld.setBaseState(basebs).setStateIndex(idx - baseidx).setBlockName(bn).setStateName(statename).setMaterial(mat.toString()).setLegacyBlockID(idx).setAttenuatesLight(lightAtten); - if (mat.isSolid()) { bld.setSolid(); } - if (mat == Material.AIR) { bld.setAir(); } - if (mat == Material.WOOD) { bld.setLog(); } - if (mat == Material.LEAVES) { bld.setLeaves(); } + bld.setBaseState(basebs).setStateIndex(idx - baseidx).setBlockName(bn).setStateName(statename).setLegacyBlockID(idx).setAttenuatesLight(lightAtten); + if (bs.isSolid()) { bld.setSolid(); } + if (bs.isAir()) { bld.setAir(); } + if (bs.isIn(BlockTags.LOGS)) { bld.setLog(); } + if (bs.isIn(BlockTags.LEAVES)) { bld.setLeaves(); } if ((!bs.getFluidState().isEmpty()) && !(bs.getBlock() instanceof FluidBlock)) { bld.setWaterlogged(); } @@ -634,7 +634,7 @@ public class DynmapPlugin { ChunkSection[] sections = chunk.getSectionArray(); for (int i = 0; i < sections.length; i++) { if ((sections[i] != null) && (!sections[i].isEmpty())) { - int sy = sections[i].getYOffset(); + int sy = chunk.getBottomY() + i * ChunkSection.field_31407 /* Mojmap: SECTION_HEIGHT */; if (sy < ymin) ymin = sy; if ((sy+16) > ymax) ymax = sy + 16; } diff --git a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricCommandSender.java b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricCommandSender.java index 87765c75..1af27d3e 100644 --- a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricCommandSender.java +++ b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricCommandSender.java @@ -25,8 +25,7 @@ public class FabricCommandSender implements DynmapCommandSender { @Override public void sendMessage(String msg) { if (sender != null) { - Text ichatcomponent = Text.literal(msg); - sender.sendFeedback(ichatcomponent, false); + sender.sendFeedback(() -> Text.literal(msg), false); } } diff --git a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricPlayer.java b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricPlayer.java index 14e1b0b1..9532648f 100644 --- a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricPlayer.java +++ b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricPlayer.java @@ -16,6 +16,7 @@ import net.minecraft.text.LiteralTextContent; import net.minecraft.text.Text; import net.minecraft.util.Util; import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; import org.dynmap.DynmapLocation; import org.dynmap.common.DynmapPlayer; @@ -100,7 +101,7 @@ public class FabricPlayer extends FabricCommandSender implements DynmapPlayer { } Vec3d pos = player.getPos(); - return FabricAdapter.toDynmapLocation(plugin, player.getWorld(), pos.getX(), pos.getY(), pos.getZ()); + return FabricAdapter.toDynmapLocation(plugin, player.getServerWorld(), pos.getX(), pos.getY(), pos.getZ()); } @Override @@ -109,8 +110,9 @@ public class FabricPlayer extends FabricCommandSender implements DynmapPlayer { return null; } - if (player.world != null) { - return plugin.getWorld(player.world).getName(); + World world = player.getWorld(); + if (world != null) { + return plugin.getWorld(world).getName(); } return null; diff --git a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricServer.java b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricServer.java index fbdd8956..6f2fed66 100644 --- a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricServer.java +++ b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/FabricServer.java @@ -271,7 +271,7 @@ public class FabricServer extends DynmapServerInterface { break; case SIGN_CHANGE: - BlockEvents.SIGN_CHANGE_EVENT.register((world, pos, lines, material, player) -> { + BlockEvents.SIGN_CHANGE_EVENT.register((world, pos, lines, player, front) -> { plugin.core.processSignChange("fabric", FabricWorld.getWorldName(plugin, world), pos.getX(), pos.getY(), pos.getZ(), lines, player.getName().getString()); }); diff --git a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/event/BlockEvents.java b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/event/BlockEvents.java index e2ed62dd..9ff9334f 100644 --- a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/event/BlockEvents.java +++ b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/event/BlockEvents.java @@ -2,7 +2,6 @@ package org.dynmap.fabric_1_20.event; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; -import net.minecraft.block.Material; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; @@ -21,9 +20,9 @@ public class BlockEvents { ); public static Event SIGN_CHANGE_EVENT = EventFactory.createArrayBacked(SignChangeCallback.class, - (listeners) -> (world, pos, lines, material, player) -> { + (listeners) -> (world, pos, lines, player, front) -> { for (SignChangeCallback callback : listeners) { - callback.onSignChange(world, pos, lines, material, player); + callback.onSignChange(world, pos, lines, player, front); } } ); @@ -35,6 +34,6 @@ public class BlockEvents { @FunctionalInterface public interface SignChangeCallback { - void onSignChange(ServerWorld world, BlockPos pos, String[] lines, Material material, ServerPlayerEntity player); + void onSignChange(ServerWorld world, BlockPos pos, String[] lines, ServerPlayerEntity player, boolean front); } } diff --git a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayNetworkHandlerMixin.java b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayNetworkHandlerMixin.java index 0ef2fe5f..0bf80ae2 100644 --- a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayNetworkHandlerMixin.java +++ b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayNetworkHandlerMixin.java @@ -3,6 +3,7 @@ package org.dynmap.fabric_1_20.mixin; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.SignBlockEntity; +import net.minecraft.network.message.FilterMask; import net.minecraft.network.message.SignedMessage; import net.minecraft.network.packet.c2s.play.UpdateSignC2SPacket; import net.minecraft.server.filter.FilteredMessage; @@ -14,6 +15,7 @@ import net.minecraft.text.LiteralTextContent; import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; +import java.util.Arrays; import java.util.List; import org.dynmap.fabric_1_20.event.BlockEvents; import org.dynmap.fabric_1_20.event.ServerChatEvents; @@ -43,13 +45,14 @@ public abstract class ServerPlayNetworkHandlerMixin { method = "onSignUpdate", at = @At( value = "INVOKE", - target = "Lnet/minecraft/block/entity/SignBlockEntity;markDirty()V", + target = "Lnet/minecraft/block/entity/SignBlockEntity;tryChangeText(Lnet/minecraft/entity/player/PlayerEntity;ZLjava/util/List;)V", shift = At.Shift.BEFORE ), - locals = LocalCapture.CAPTURE_FAILHARD + locals = LocalCapture.CAPTURE_FAILHARD, + cancellable = true ) - public void onSignUpdate(UpdateSignC2SPacket packet, List signText, CallbackInfo info, - ServerWorld serverWorld, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity, SignBlockEntity signBlockEntity) + public void onSignUpdate(UpdateSignC2SPacket packet, List signText, CallbackInfo ci, + ServerWorld serverWorld, BlockPos blockPos, BlockEntity blockEntity, SignBlockEntity signBlockEntity) { // Pull the raw text from the input. String[] rawTexts = new String[4]; @@ -57,10 +60,15 @@ public abstract class ServerPlayNetworkHandlerMixin { rawTexts[i] = signText.get(i).raw(); // Fire the event. - BlockEvents.SIGN_CHANGE_EVENT.invoker().onSignChange(serverWorld, blockPos, rawTexts, blockState.getMaterial(), player); + BlockEvents.SIGN_CHANGE_EVENT.invoker().onSignChange(serverWorld, blockPos, rawTexts, player, packet.isFront()); - // Put the (possibly updated) texts in the sign. Ignore filtering (is this OK?). - for (int i=0; i newSignText = Arrays.stream(rawTexts).map((raw) -> new FilteredMessage(raw, FilterMask.PASS_THROUGH)).toList(); + + // Execute the setting of the texts with the edited values. + signBlockEntity.tryChangeText(this.player, packet.isFront(), newSignText); + + // Cancel the original tryChangeText() since we're calling it ourselves above. + ci.cancel(); } } \ No newline at end of file diff --git a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayerEntityMixin.java b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayerEntityMixin.java index 41892ea0..c303c267 100644 --- a/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayerEntityMixin.java +++ b/fabric-1.20/src/main/java/org/dynmap/fabric_1_20/mixin/ServerPlayerEntityMixin.java @@ -15,7 +15,7 @@ public class ServerPlayerEntityMixin { @Inject(method = "teleport(Lnet/minecraft/server/world/ServerWorld;DDDFF)V", at = @At("RETURN")) public void teleport(ServerWorld targetWorld, double x, double y, double z, float yaw, float pitch, CallbackInfo info) { ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; - if (targetWorld != player.world) { + if (targetWorld != player.getServerWorld()) { PlayerEvents.PLAYER_CHANGED_DIMENSION.invoker().onPlayerChangedDimension(player); } }