From 9fd8b307c2f46fed357b0c64bc81c083ae1e1a7e Mon Sep 17 00:00:00 2001 From: plusls Date: Sun, 9 Aug 2020 18:49:03 +0800 Subject: [PATCH] =?UTF-8?q?update=20to=201.0.2-alpha.2=20=E8=AE=A9?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E9=A2=84=E8=A7=88=E6=94=AF=E6=8C=81=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++ build.gradle | 2 +- gradle.properties | 5 ++- .../plusls/MasaGadget/MasaGadgetMod.java | 18 +++++--- .../IMixinClientPlayNetworkHandler.java | 2 +- .../client/MixinClientPlayNetworkHandler.java | 34 +++++++++++++++ .../MixinCustomPayloadS2CPacket.java | 2 +- .../mixin/client/MixinMinecraftClient.java | 20 +++++++++ .../MixinPlayerRespawnS2CPacket.java | 2 +- .../malilib}/MixinDataStorage.java | 2 +- .../client/malilib/MixinInventoryUtils.java | 20 +++++++++ .../MixinWidgetListConfigOptions.java | 2 +- .../client/tweakeroo/MixinRenderHandler.java | 37 ++++++++++++++++ .../mixin/server/MixinPlayerManager.java | 31 +++++++++++++ .../MixinAbstractFurnaceBlockEntity.java | 40 +++++++++++++++++ .../inventory/MixinDispenserBlockEntity.java | 37 ++++++++++++++++ .../inventory/MixinHopperBlockEntity.java | 38 ++++++++++++++++ .../inventory/MixinShulkerBoxBlockEntity.java | 38 ++++++++++++++++ .../network/ClientNetworkHandler.java | 23 ++++++++++ .../MasaGadget/network/DataAccessor.java | 34 +++++++++++++++ .../network/ServerNetworkHandler.java | 43 +++++++++++++++++++ src/main/resources/fabric.mod.json | 3 ++ .../resources/masa_gadget_mod.mixins.json | 19 +++++--- 23 files changed, 438 insertions(+), 18 deletions(-) rename src/main/java/io/github/plusls/MasaGadget/mixin/{ => client}/IMixinClientPlayNetworkHandler.java (87%) create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinClientPlayNetworkHandler.java rename src/main/java/io/github/plusls/MasaGadget/mixin/{ => client}/MixinCustomPayloadS2CPacket.java (98%) create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinMinecraftClient.java rename src/main/java/io/github/plusls/MasaGadget/mixin/{ => client}/MixinPlayerRespawnS2CPacket.java (96%) rename src/main/java/io/github/plusls/MasaGadget/mixin/{ => client/malilib}/MixinDataStorage.java (96%) create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinInventoryUtils.java rename src/main/java/io/github/plusls/MasaGadget/mixin/{ => client/malilib}/MixinWidgetListConfigOptions.java (97%) create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/client/tweakeroo/MixinRenderHandler.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/server/MixinPlayerManager.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinAbstractFurnaceBlockEntity.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinDispenserBlockEntity.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinHopperBlockEntity.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinShulkerBoxBlockEntity.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/network/ClientNetworkHandler.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/network/DataAccessor.java create mode 100644 src/main/java/io/github/plusls/MasaGadget/network/ServerNetworkHandler.java diff --git a/README.md b/README.md index aabc427b..1ee65a98 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ malilib 创建的配置界面没有考虑到汉化的情况,在加载汉化资 minihud 的查看结构和史莱姆区块渲染需要服务端安装了 carpet 或者 servux 才能正常使用(史莱姆区块可以手动输入种子),在安装此 mod 后,若是服务器未安装上述 2个 mod 但是安装了 bbor, 该 mod 可以自动从服务器获取 bbor 的数据并导入 minihud,从而让 minihud 在多人游戏中也能渲染结构和史莱姆区块。 +### 多人游戏容器预览 + +tweakeroo 的容器预览功能在多人游戏中是不可用的,在安装此 mod 后可以让该功能在多人游戏中使用(需要服务端也安装 MasaGadget) + ## 安装 该 mod 依赖 malilib, minihud, fabric-api diff --git a/build.gradle b/build.gradle index a5682d5d..0a545291 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ dependencies { modCompile "fi.dy.masa.malilib:malilib-fabric-${project.minecraft_version}:${project.malilib_version}" modCompile "fi.dy.masa.minihud:minihud-fabric-${project.minecraft_version}:${project.minihud_version}" - + modCompile "fi.dy.masa.tweakeroo:tweakeroo-fabric-${project.minecraft_version}:${project.tweakeroo_version}" } processResources { diff --git a/gradle.properties b/gradle.properties index 0b146ec1..6c9af2ca 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.9.0+build.204 # Mod Properties - mod_version = 1.0.1-alpha.2 + mod_version = 1.0.2-alpha.2 maven_group = io.github.plusls archives_base_name = MasaGadget @@ -16,6 +16,9 @@ org.gradle.jvmargs=-Xmx1G malilib_version = 0.10.0-dev.21+arne.1 # minihud version minihud_version = 0.19.0-dev.20200720.162605 +# tweakeroo version + tweakeroo_version = 0.10.0-dev.20200720.162627 + # Dependencies # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api diff --git a/src/main/java/io/github/plusls/MasaGadget/MasaGadgetMod.java b/src/main/java/io/github/plusls/MasaGadget/MasaGadgetMod.java index 440054f0..e2c829f2 100644 --- a/src/main/java/io/github/plusls/MasaGadget/MasaGadgetMod.java +++ b/src/main/java/io/github/plusls/MasaGadget/MasaGadgetMod.java @@ -1,6 +1,9 @@ package io.github.plusls.MasaGadget; +import io.github.plusls.MasaGadget.network.ClientNetworkHandler; +import io.github.plusls.MasaGadget.network.ServerNetworkHandler; import io.netty.buffer.Unpooled; +import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.network.PacketByteBuf; @@ -11,19 +14,22 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configurator; -public class MasaGadgetMod implements ModInitializer { +public class MasaGadgetMod implements ModInitializer, ClientModInitializer { public static final String MODID = "masa_gadget_mod"; public static final Logger LOGGER = LogManager.getLogger("MasaGadgetMod"); public static boolean bborCompat = false; public static CustomPayloadC2SPacket BBOR_SUBSCRIBE_PACKET = null; public static String level = "INFO"; + public static boolean masaGagdetInServer = false; @Override public void onInitialize() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - // However, some things (like resources) may still be uninitialized. - // Proceed with mild caution. - // System.out.println("Hello Fabric world!"); + Configurator.setLevel(LOGGER.getName(), Level.toLevel(MasaGadgetMod.level)); + ServerNetworkHandler.init(); + } + + @Override + public void onInitializeClient() { if (FabricLoader.getInstance().isModLoaded("bbor")) { LOGGER.info("BBOR detected."); bborCompat = true; @@ -32,7 +38,7 @@ public void onInitialize() { new Identifier("bbor", "subscribe"), new PacketByteBuf(Unpooled.buffer())); } - Configurator.setLevel(LOGGER.getName(), Level.toLevel(MasaGadgetMod.level)); + ClientNetworkHandler.init(); } public static Identifier id(String id) { diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/IMixinClientPlayNetworkHandler.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/IMixinClientPlayNetworkHandler.java similarity index 87% rename from src/main/java/io/github/plusls/MasaGadget/mixin/IMixinClientPlayNetworkHandler.java rename to src/main/java/io/github/plusls/MasaGadget/mixin/client/IMixinClientPlayNetworkHandler.java index 2ebe5f99..aa8e4b72 100644 --- a/src/main/java/io/github/plusls/MasaGadget/mixin/IMixinClientPlayNetworkHandler.java +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/IMixinClientPlayNetworkHandler.java @@ -1,4 +1,4 @@ -package io.github.plusls.MasaGadget.mixin; +package io.github.plusls.MasaGadget.mixin.client; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinClientPlayNetworkHandler.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinClientPlayNetworkHandler.java new file mode 100644 index 00000000..dede4723 --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinClientPlayNetworkHandler.java @@ -0,0 +1,34 @@ +package io.github.plusls.MasaGadget.mixin.client; + +import net.minecraft.block.entity.*; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.listener.ClientPlayPacketListener; +import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; +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; + +@Mixin(ClientPlayNetworkHandler.class) +public abstract class MixinClientPlayNetworkHandler implements ClientPlayPacketListener { + + @Shadow + private MinecraftClient client; + + @Inject(method = "onBlockEntityUpdate(Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;)V", + at = @At(value = "RETURN")) + private void postOnBlockEntityUpdate(BlockEntityUpdateS2CPacket packet, CallbackInfo info) { + int blockEntityType = packet.getBlockEntityType(); + BlockEntity blockEntity = this.client.world.getBlockEntity(packet.getPos()); + if (blockEntityType == 0 && ( + blockEntity instanceof ShulkerBoxBlockEntity || + blockEntity instanceof HopperBlockEntity || + blockEntity instanceof AbstractFurnaceBlockEntity || + blockEntity instanceof DispenserBlockEntity // 包括了投掷器 + )) { + blockEntity.fromTag(this.client.world.getBlockState(blockEntity.getPos()), packet.getCompoundTag()); + } + } +} diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinCustomPayloadS2CPacket.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinCustomPayloadS2CPacket.java similarity index 98% rename from src/main/java/io/github/plusls/MasaGadget/mixin/MixinCustomPayloadS2CPacket.java rename to src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinCustomPayloadS2CPacket.java index 7478894e..c3e3f922 100644 --- a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinCustomPayloadS2CPacket.java +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinCustomPayloadS2CPacket.java @@ -1,4 +1,4 @@ -package io.github.plusls.MasaGadget.mixin; +package io.github.plusls.MasaGadget.mixin.client; import fi.dy.masa.minihud.util.DataStorage; import io.github.plusls.MasaGadget.MasaGadgetMod; diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinMinecraftClient.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinMinecraftClient.java new file mode 100644 index 00000000..d0d9cd77 --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinMinecraftClient.java @@ -0,0 +1,20 @@ +package io.github.plusls.MasaGadget.mixin.client; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin({MinecraftClient.class}) +public abstract class MixinMinecraftClient { + @Inject( + method = {"disconnect(Lnet/minecraft/client/gui/screen/Screen;)V"}, + at = {@At("HEAD")} + ) + private void onDisconnectPre(Screen screen, CallbackInfo ci) { + MasaGadgetMod.masaGagdetInServer = false; + } +} diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinPlayerRespawnS2CPacket.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinPlayerRespawnS2CPacket.java similarity index 96% rename from src/main/java/io/github/plusls/MasaGadget/mixin/MixinPlayerRespawnS2CPacket.java rename to src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinPlayerRespawnS2CPacket.java index 143ecde3..b45f12f6 100644 --- a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinPlayerRespawnS2CPacket.java +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinPlayerRespawnS2CPacket.java @@ -1,4 +1,4 @@ -package io.github.plusls.MasaGadget.mixin; +package io.github.plusls.MasaGadget.mixin.client; import fi.dy.masa.minihud.util.DataStorage; import io.github.plusls.MasaGadget.util.ParseBborPacket; diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinDataStorage.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinDataStorage.java similarity index 96% rename from src/main/java/io/github/plusls/MasaGadget/mixin/MixinDataStorage.java rename to src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinDataStorage.java index 5869d335..21e959d4 100644 --- a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinDataStorage.java +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinDataStorage.java @@ -1,4 +1,4 @@ -package io.github.plusls.MasaGadget.mixin; +package io.github.plusls.MasaGadget.mixin.client.malilib; import fi.dy.masa.minihud.util.DataStorage; import io.github.plusls.MasaGadget.util.ParseBborPacket; diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinInventoryUtils.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinInventoryUtils.java new file mode 100644 index 00000000..e786c27c --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinInventoryUtils.java @@ -0,0 +1,20 @@ +package io.github.plusls.MasaGadget.mixin.client.malilib; + +import fi.dy.masa.malilib.util.InventoryUtils; +import io.github.plusls.MasaGadget.network.DataAccessor; +import net.minecraft.inventory.Inventory; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(value = InventoryUtils.class, remap = false) +public abstract class MixinInventoryUtils { + @Inject(method = "getInventory", + at = @At(value = "HEAD")) + private static void preGetInventory(World world, BlockPos pos, CallbackInfoReturnable info) { + DataAccessor.requestBlockEntity(pos); + } +} diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinWidgetListConfigOptions.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinWidgetListConfigOptions.java similarity index 97% rename from src/main/java/io/github/plusls/MasaGadget/mixin/MixinWidgetListConfigOptions.java rename to src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinWidgetListConfigOptions.java index 7be22dc6..84c20fca 100644 --- a/src/main/java/io/github/plusls/MasaGadget/mixin/MixinWidgetListConfigOptions.java +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinWidgetListConfigOptions.java @@ -1,4 +1,4 @@ -package io.github.plusls.MasaGadget.mixin; +package io.github.plusls.MasaGadget.mixin.client.malilib; import com.google.common.collect.ImmutableList; import fi.dy.masa.malilib.config.IConfigBase; diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/client/tweakeroo/MixinRenderHandler.java b/src/main/java/io/github/plusls/MasaGadget/mixin/client/tweakeroo/MixinRenderHandler.java new file mode 100644 index 00000000..3d09e672 --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/client/tweakeroo/MixinRenderHandler.java @@ -0,0 +1,37 @@ +package io.github.plusls.MasaGadget.mixin.client.tweakeroo; + +import fi.dy.masa.malilib.hotkeys.IKeybind; +import fi.dy.masa.malilib.interfaces.IRenderer; +import fi.dy.masa.tweakeroo.event.RenderHandler; +import io.github.plusls.MasaGadget.MasaGadgetMod; +import io.github.plusls.MasaGadget.network.DataAccessor; +import io.github.plusls.MasaGadget.network.ServerNetworkHandler; +import io.netty.buffer.Unpooled; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + + +@Mixin(value = RenderHandler.class, remap = false) +public abstract class MixinRenderHandler implements IRenderer { + // 未按下按键时若是 lastBlockPos 不为空, 则告诉服务端不需要更新 block entity + @Redirect(method = "onRenderGameOverlayPost", + at = @At(value = "INVOKE", + target = "Lfi/dy/masa/malilib/hotkeys/IKeybind;isKeybindHeld()Z", + ordinal = 2)) + private boolean redirectIsKeyBindHeld(IKeybind iKeybind) { + boolean ret = iKeybind.isKeybindHeld(); + + if (!ret && DataAccessor.lastBlockPos != null) { + DataAccessor.lastBlockPos = null; + MasaGadgetMod.LOGGER.debug("cancel requestBlockEntity"); + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); + buf.writeBoolean(false); + MinecraftClient.getInstance().getNetworkHandler().getConnection().send(new CustomPayloadC2SPacket(ServerNetworkHandler.REQUEST_BLOCK_ENTITY, buf)); + } + return ret; + } +} diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/server/MixinPlayerManager.java b/src/main/java/io/github/plusls/MasaGadget/mixin/server/MixinPlayerManager.java new file mode 100644 index 00000000..d613445a --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/server/MixinPlayerManager.java @@ -0,0 +1,31 @@ +package io.github.plusls.MasaGadget.mixin.server; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import io.github.plusls.MasaGadget.network.ClientNetworkHandler; +import io.github.plusls.MasaGadget.network.ServerNetworkHandler; +import io.netty.buffer.Unpooled; +import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; +import net.minecraft.network.ClientConnection; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.PlayerManager; +import net.minecraft.server.network.ServerPlayerEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(PlayerManager.class) +public abstract class MixinPlayerManager { + + @Inject(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;)V", + at = @At(value = "INVOKE", + target = "Lnet/minecraft/network/packet/s2c/play/DifficultyS2CPacket;(Lnet/minecraft/world/Difficulty;Z)V", + ordinal = 0 + ) + ) + private void onOnPlayerConnect(ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci) { + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); + ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, ClientNetworkHandler.HELLO, buf); + MasaGadgetMod.LOGGER.debug("send hello!"); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinAbstractFurnaceBlockEntity.java b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinAbstractFurnaceBlockEntity.java new file mode 100644 index 00000000..c95ba9fc --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinAbstractFurnaceBlockEntity.java @@ -0,0 +1,40 @@ +package io.github.plusls.MasaGadget.mixin.server.inventory; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import io.github.plusls.MasaGadget.network.ServerNetworkHandler; +import net.minecraft.block.entity.AbstractFurnaceBlockEntity; +import net.minecraft.block.entity.LockableContainerBlockEntity; +import net.minecraft.inventory.SidedInventory; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; +import net.minecraft.recipe.RecipeInputProvider; +import net.minecraft.recipe.RecipeUnlocker; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Tickable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(AbstractFurnaceBlockEntity.class) +public abstract class MixinAbstractFurnaceBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider, Tickable { + + @Shadow + public abstract CompoundTag toTag(CompoundTag tag); + + public MixinAbstractFurnaceBlockEntity() { + super(null); + } + + @Override + public void markDirty() { + super.markDirty(); + if (ServerNetworkHandler.lastBlockPosMap.containsValue(this.pos)) { + ((ServerWorld) this.getWorld()).getChunkManager().markForUpdate(this.getPos()); + MasaGadgetMod.LOGGER.debug("update AbstractFurnaceBlockEntity: {}", this.pos); + } + } + + @Override + public BlockEntityUpdateS2CPacket toUpdatePacket() { + return new BlockEntityUpdateS2CPacket(this.pos, 0, this.toTag(new CompoundTag())); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinDispenserBlockEntity.java b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinDispenserBlockEntity.java new file mode 100644 index 00000000..953c05a1 --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinDispenserBlockEntity.java @@ -0,0 +1,37 @@ +package io.github.plusls.MasaGadget.mixin.server.inventory; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import io.github.plusls.MasaGadget.network.ServerNetworkHandler; +import net.minecraft.block.entity.DispenserBlockEntity; +import net.minecraft.block.entity.LootableContainerBlockEntity; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; +import net.minecraft.server.world.ServerWorld; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + + +@Mixin(DispenserBlockEntity.class) +public abstract class MixinDispenserBlockEntity extends LootableContainerBlockEntity { + + @Shadow + public abstract CompoundTag toTag(CompoundTag tag); + + public MixinDispenserBlockEntity() { + super(null); + } + + @Override + public void markDirty() { + super.markDirty(); + if (ServerNetworkHandler.lastBlockPosMap.containsValue(this.pos)) { + ((ServerWorld) this.getWorld()).getChunkManager().markForUpdate(this.getPos()); + MasaGadgetMod.LOGGER.debug("update DispenserBlockEntity: {}", this.pos); + } + } + + @Override + public BlockEntityUpdateS2CPacket toUpdatePacket() { + return new BlockEntityUpdateS2CPacket(this.pos, 0, this.toTag(new CompoundTag())); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinHopperBlockEntity.java b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinHopperBlockEntity.java new file mode 100644 index 00000000..d0d17374 --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinHopperBlockEntity.java @@ -0,0 +1,38 @@ +package io.github.plusls.MasaGadget.mixin.server.inventory; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import io.github.plusls.MasaGadget.network.ServerNetworkHandler; +import net.minecraft.block.entity.Hopper; +import net.minecraft.block.entity.HopperBlockEntity; +import net.minecraft.block.entity.LootableContainerBlockEntity; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Tickable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(HopperBlockEntity.class) +public abstract class MixinHopperBlockEntity extends LootableContainerBlockEntity implements Hopper, Tickable { + + @Shadow + public abstract CompoundTag toTag(CompoundTag tag); + + public MixinHopperBlockEntity() { + super(null); + } + + @Override + public void markDirty() { + super.markDirty(); + if (ServerNetworkHandler.lastBlockPosMap.containsValue(this.pos)) { + ((ServerWorld) this.getWorld()).getChunkManager().markForUpdate(this.getPos()); + MasaGadgetMod.LOGGER.debug("update HopperBlockEntity: {}", this.pos); + } + } + + @Override + public BlockEntityUpdateS2CPacket toUpdatePacket() { + return new BlockEntityUpdateS2CPacket(this.pos, 0, this.toTag(new CompoundTag())); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinShulkerBoxBlockEntity.java b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinShulkerBoxBlockEntity.java new file mode 100644 index 00000000..851a3b70 --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/mixin/server/inventory/MixinShulkerBoxBlockEntity.java @@ -0,0 +1,38 @@ +package io.github.plusls.MasaGadget.mixin.server.inventory; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import io.github.plusls.MasaGadget.network.ServerNetworkHandler; +import net.minecraft.block.entity.LootableContainerBlockEntity; +import net.minecraft.block.entity.ShulkerBoxBlockEntity; +import net.minecraft.inventory.SidedInventory; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Tickable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(ShulkerBoxBlockEntity.class) +public abstract class MixinShulkerBoxBlockEntity extends LootableContainerBlockEntity implements SidedInventory, Tickable { + + @Shadow + public abstract CompoundTag toTag(CompoundTag tag); + + public MixinShulkerBoxBlockEntity() { + super(null); + } + + @Override + public void markDirty() { + super.markDirty(); + if (ServerNetworkHandler.lastBlockPosMap.containsValue(this.pos)) { + ((ServerWorld) this.getWorld()).getChunkManager().markForUpdate(this.getPos()); + MasaGadgetMod.LOGGER.debug("update ShulkerBoxBlockEntity: {}", this.pos); + } + } + + @Override + public BlockEntityUpdateS2CPacket toUpdatePacket() { + return new BlockEntityUpdateS2CPacket(this.pos, 0, this.toTag(new CompoundTag())); + } +} diff --git a/src/main/java/io/github/plusls/MasaGadget/network/ClientNetworkHandler.java b/src/main/java/io/github/plusls/MasaGadget/network/ClientNetworkHandler.java new file mode 100644 index 00000000..9cc6cf79 --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/network/ClientNetworkHandler.java @@ -0,0 +1,23 @@ +package io.github.plusls.MasaGadget.network; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; +import net.fabricmc.fabric.api.network.PacketContext; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.Identifier; + +public class ClientNetworkHandler { + public static final Identifier HELLO = MasaGadgetMod.id("hello"); + + public static void init() { + ClientSidePacketRegistry.INSTANCE.register(HELLO, ClientNetworkHandler::helloHandler); + } + + private static void helloHandler(PacketContext packetContext, PacketByteBuf packetByteBuf) { + if (!MinecraftClient.getInstance().isIntegratedServerRunning()) { + MasaGadgetMod.LOGGER.info("MasaGadget detected."); + MasaGadgetMod.masaGagdetInServer = true; + } + } +} diff --git a/src/main/java/io/github/plusls/MasaGadget/network/DataAccessor.java b/src/main/java/io/github/plusls/MasaGadget/network/DataAccessor.java new file mode 100644 index 00000000..312b395d --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/network/DataAccessor.java @@ -0,0 +1,34 @@ +package io.github.plusls.MasaGadget.network; + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import io.netty.buffer.Unpooled; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; +import net.minecraft.client.MinecraftClient; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.math.BlockPos; + + +public class DataAccessor { + static public BlockPos lastBlockPos = null; + + @Environment(EnvType.CLIENT) + static public void requestBlockEntity(BlockPos pos) { + if (!MasaGadgetMod.masaGagdetInServer) { + return; + } + if (MinecraftClient.getInstance().getNetworkHandler() == null) { + return; + } + if (lastBlockPos != null && lastBlockPos.equals(pos)) { + return; + } + MasaGadgetMod.LOGGER.debug("requestBlockEntity: {}", pos); + lastBlockPos = pos; + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); + buf.writeBoolean(true); + buf.writeBlockPos(pos); + ClientSidePacketRegistry.INSTANCE.sendToServer(ServerNetworkHandler.REQUEST_BLOCK_ENTITY, buf); + } +} diff --git a/src/main/java/io/github/plusls/MasaGadget/network/ServerNetworkHandler.java b/src/main/java/io/github/plusls/MasaGadget/network/ServerNetworkHandler.java new file mode 100644 index 00000000..1800cbed --- /dev/null +++ b/src/main/java/io/github/plusls/MasaGadget/network/ServerNetworkHandler.java @@ -0,0 +1,43 @@ +package io.github.plusls.MasaGadget.network; + + +import io.github.plusls.MasaGadget.MasaGadgetMod; +import net.fabricmc.fabric.api.network.PacketContext; +import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; +import net.minecraft.block.entity.*; +import net.minecraft.inventory.Inventory; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.BlockPos; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class ServerNetworkHandler { + + public static final Identifier REQUEST_BLOCK_ENTITY = MasaGadgetMod.id("request_block_entity"); + public static Map lastBlockPosMap = new HashMap<>(); + + public static void init() { + ServerSidePacketRegistry.INSTANCE.register(REQUEST_BLOCK_ENTITY, ServerNetworkHandler::requestBlockEntityHandler); + } + + private static void requestBlockEntityHandler(PacketContext packetContext, PacketByteBuf packetByteBuf) { + ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer(); + ServerWorld world = (ServerWorld) player.world; + if (packetByteBuf.readBoolean()) { + BlockPos pos = packetByteBuf.readBlockPos(); + BlockEntity blockEntity = world.getBlockEntity(pos); + MasaGadgetMod.LOGGER.debug("watch blockpos! {}: {}", pos, blockEntity); + MasaGadgetMod.LOGGER.debug("mark update!"); + world.getChunkManager().markForUpdate(pos); + lastBlockPosMap.put(player.getUuid(), pos); + } else { + MasaGadgetMod.LOGGER.debug("cancel watch blockpos!"); + lastBlockPosMap.remove(player.getUuid()); + } + } +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1fa50a21..fce819cd 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -18,6 +18,9 @@ "entrypoints": { "main": [ "io.github.plusls.MasaGadget.MasaGadgetMod" + ], + "client": [ + "io.github.plusls.MasaGadget.MasaGadgetMod" ] }, "mixins": [ diff --git a/src/main/resources/masa_gadget_mod.mixins.json b/src/main/resources/masa_gadget_mod.mixins.json index 9fc4022e..2f80b0de 100644 --- a/src/main/resources/masa_gadget_mod.mixins.json +++ b/src/main/resources/masa_gadget_mod.mixins.json @@ -4,13 +4,22 @@ "package": "io.github.plusls.MasaGadget.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "server.MixinPlayerManager", + "server.inventory.MixinShulkerBoxBlockEntity", + "server.inventory.MixinHopperBlockEntity", + "server.inventory.MixinAbstractFurnaceBlockEntity", + "server.inventory.MixinDispenserBlockEntity" ], "client": [ - "MixinWidgetListConfigOptions", - "MixinCustomPayloadS2CPacket", - "IMixinClientPlayNetworkHandler", - "MixinPlayerRespawnS2CPacket", - "MixinDataStorage" + "client.malilib.MixinWidgetListConfigOptions", + "client.malilib.MixinDataStorage", + "client.malilib.MixinInventoryUtils", + "client.tweakeroo.MixinRenderHandler", + "client.IMixinClientPlayNetworkHandler", + "client.MixinCustomPayloadS2CPacket", + "client.MixinPlayerRespawnS2CPacket", + "client.MixinMinecraftClient", + "client.MixinClientPlayNetworkHandler" ], "injectors": { "defaultRequire": 1