From 3bbbdc0b9ced6bc9dce1b63dde92d7cfbeb8128b Mon Sep 17 00:00:00 2001 From: Up Date: Sat, 13 Jan 2024 10:13:00 +0100 Subject: [PATCH] finish neo port --- .../network/c2s/EquipBackpackPacket.java | 66 +++++++++++++ .../network/c2s/OpenBackpackScreenPacket.java | 77 +++++++++++++++ .../network/c2s/PlaceBackpackPacket.java | 39 ++++++++ .../s2c/UpdateConfigurationPacket.java | 18 ++++ .../util/platform/service/MenuHelper.java | 4 + NeoForge/build.gradle | 7 ++ NeoForge/libs.versions.toml | 2 +- .../neoforge/entrypoints/Client.java | 1 + .../neoforge/entrypoints/Main.java | 4 +- .../neoforge/mixin/ConfigPacketMixin.java | 34 +++++++ .../neoforge/mixin/EquipPacketMixin.java | 21 ++++ .../neoforge/mixin/OpenPacketMixin.java | 20 ++++ .../neoforge/mixin/PlacePacketMixin.java | 21 ++++ .../neoforge/network/NetworkHandler.java | 54 +++++++++++ .../network/c2s/NFEquipBackpackPacket.java | 34 +++++++ .../c2s/NFOpenBackpackScreenPacket.java | 22 +++++ .../network/c2s/NFPlaceBackpackPacket.java | 30 ++++++ .../s2c/NFUpdateConfigurationPacket.java | 48 ++++++++++ .../camsbackpacks.neoforge.mixins.json | 16 ++++ .../compat/modmenu/ModMenuEntrypoint.java | 1 + .../network/c2s/QEquipBackpackPacket.java | 70 +------------- .../c2s/QOpenBackpackScreenPacket.java | 96 +------------------ .../network/c2s/QPlaceBackpackPacket.java | 43 +-------- .../s2c/QUpdateConfigurationPacket.java | 20 +--- .../quilt/services/QMenuHelper.java | 25 +++++ 25 files changed, 558 insertions(+), 215 deletions(-) create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/ConfigPacketMixin.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/EquipPacketMixin.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/OpenPacketMixin.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/PlacePacketMixin.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/NetworkHandler.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFEquipBackpackPacket.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFOpenBackpackScreenPacket.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFPlaceBackpackPacket.java create mode 100644 NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/s2c/NFUpdateConfigurationPacket.java create mode 100644 NeoForge/src/main/resources/camsbackpacks.neoforge.mixins.json diff --git a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/EquipBackpackPacket.java b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/EquipBackpackPacket.java index bb3d7fc..b41aa12 100644 --- a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/EquipBackpackPacket.java +++ b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/EquipBackpackPacket.java @@ -1,6 +1,22 @@ package dev.cammiescorner.camsbackpacks.network.c2s; +import dev.cammiescorner.camsbackpacks.block.BackpackBlock; +import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; +import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; +import dev.cammiescorner.camsbackpacks.item.BackpackItem; +import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.ContainerHelper; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.properties.BlockStateProperties; public class EquipBackpackPacket { @@ -8,4 +24,54 @@ public static void send(boolean isBlockEntity, BlockPos pos) { throw new UnsupportedOperationException(); } + public static void handle(ServerPlayer player, BlockPos pos, boolean isBlockEntity) { + Level world = player.level(); + + if (!world.mayInteract(player, pos)) { + player.closeContainer(); + MutableComponent message = isBlockEntity + ? Component.translatable("error.camsbackpacks.permission_pickup_at") + : Component.translatable("error.camsbackpacks.permission_place_at"); + player.sendSystemMessage(message.withStyle(ChatFormatting.RED), true); + return; + } + + if (isBlockEntity) { + if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) { + ItemStack stack = new ItemStack(world.getBlockState(pos).getBlock().asItem()); + CompoundTag tag = stack.getOrCreateTag(); + + ContainerHelper.saveAllItems(tag, blockEntity.inventory); + blockEntity.wasPickedUp = true; + player.setItemSlot(EquipmentSlot.CHEST, stack); + + if (blockEntity.hasCustomName()) + stack.setHoverName(blockEntity.getName()); + + world.destroyBlock(pos, false, player); + + if (!BackpacksConfig.allowInventoryGui) { + player.closeContainer(); + player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled")); + } + + } + } else { + ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); + + if (stack.getItem() instanceof BackpackItem backpackItem && BackpackBlock.isStateReplaceable(world, pos) && pos.closerThan(player.blockPosition(), 3)) { + world.playSound(null, pos, SoundEvents.WOOL_PLACE, SoundSource.BLOCKS, 1F, 1F); + world.setBlockAndUpdate(pos, backpackItem.getBlock().defaultBlockState().setValue(BackpackBlock.FACING, player.getDirection()).setValue(BlockStateProperties.WATERLOGGED, !world.getFluidState(pos).isEmpty())); + player.closeContainer(); + + if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) { + ContainerHelper.loadAllItems(stack.getOrCreateTag(), backpack.inventory); + backpack.setName(stack.getHoverName()); + } + + player.getItemBySlot(EquipmentSlot.CHEST).shrink(1); + } + } + } + } diff --git a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/OpenBackpackScreenPacket.java b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/OpenBackpackScreenPacket.java index ed18d7e..c94bc6e 100644 --- a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/OpenBackpackScreenPacket.java +++ b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/OpenBackpackScreenPacket.java @@ -1,8 +1,85 @@ package dev.cammiescorner.camsbackpacks.network.c2s; +import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; +import dev.cammiescorner.camsbackpacks.menu.BackpackMenu; +import dev.cammiescorner.camsbackpacks.util.platform.Services; +import net.minecraft.core.NonNullList; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.Container; +import net.minecraft.world.ContainerHelper; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.ContainerLevelAccess; +import net.minecraft.world.item.ItemStack; + public class OpenBackpackScreenPacket { public static void send() { throw new UnsupportedOperationException(); } + + public static void handle(ServerPlayer player) { + if (!BackpacksConfig.allowInventoryGui) { + player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled"), true); + return; + } + + final NonNullList stacks = NonNullList.withSize(36, ItemStack.EMPTY); + ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); + CompoundTag tag = stack.getOrCreateTag(); + ContainerHelper.loadAllItems(tag, stacks); + Container inventory = new Container() { + @Override + public void clearContent() { + stacks.clear(); + } + + @Override + public int getContainerSize() { + return stacks.size(); + } + + @Override + public boolean isEmpty() { + return stacks.isEmpty(); + } + + @Override + public ItemStack getItem(int slot) { + return stacks.get(slot); + } + + @Override + public ItemStack removeItem(int slot, int amount) { + return ContainerHelper.removeItem(stacks, slot, amount); + } + + @Override + public ItemStack removeItemNoUpdate(int slot) { + return ContainerHelper.takeItem(stacks, slot); + } + + @Override + public void setItem(int slot, ItemStack stack) { + stacks.set(slot, stack); + } + + @Override + public void setChanged() { + + } + + @Override + public boolean stillValid(Player player) { + return true; + } + }; + + Services.MENU.openMenu(player, stack, inventory); + } } diff --git a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/PlaceBackpackPacket.java b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/PlaceBackpackPacket.java index d6c053d..9e6f37d 100644 --- a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/PlaceBackpackPacket.java +++ b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/PlaceBackpackPacket.java @@ -1,5 +1,19 @@ package dev.cammiescorner.camsbackpacks.network.c2s; +import dev.cammiescorner.camsbackpacks.block.BackpackBlock; +import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; +import dev.cammiescorner.camsbackpacks.item.BackpackItem; +import net.minecraft.ChatFormatting; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.ContainerHelper; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.phys.BlockHitResult; public class PlaceBackpackPacket { @@ -7,4 +21,29 @@ public class PlaceBackpackPacket { public static void send(BlockHitResult hitResult) { throw new UnsupportedOperationException(); } + + public static void handle(ServerPlayer player, BlockHitResult hitResult) { + Level world = player.level(); + BlockPos pos = BackpackBlock.isStateReplaceable(world, hitResult.getBlockPos()) ? hitResult.getBlockPos() : hitResult.getBlockPos().relative(hitResult.getDirection()); + + if (!world.mayInteract(player, pos)) { + player.closeContainer(); + player.sendSystemMessage(Component.translatable("error.camsbackpacks.permission_place_at").withStyle(ChatFormatting.RED), true); + return; + } + + ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); + + if (BackpackBlock.isStateReplaceable(world, pos)) { + world.playSound(null, pos, SoundEvents.WOOL_PLACE, SoundSource.BLOCKS, 1F, 1F); + world.setBlockAndUpdate(pos, ((BackpackItem) stack.getItem()).getBlock().defaultBlockState().setValue(BackpackBlock.FACING, player.getDirection()).setValue(BlockStateProperties.WATERLOGGED, !world.getFluidState(pos).isEmpty())); + + if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) { + ContainerHelper.loadAllItems(stack.getOrCreateTag(), backpack.inventory); + backpack.setName(stack.getHoverName()); + } + + player.getItemBySlot(EquipmentSlot.CHEST).shrink(1); + } + } } diff --git a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/s2c/UpdateConfigurationPacket.java b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/s2c/UpdateConfigurationPacket.java index 254e29a..da0269b 100644 --- a/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/s2c/UpdateConfigurationPacket.java +++ b/Common/src/main/java/dev/cammiescorner/camsbackpacks/network/s2c/UpdateConfigurationPacket.java @@ -1,5 +1,9 @@ package dev.cammiescorner.camsbackpacks.network.s2c; +import dev.cammiescorner.camsbackpacks.client.CamsBackpacksClient; +import dev.cammiescorner.camsbackpacks.client.screen.BackpackScreen; +import net.minecraft.client.Minecraft; +import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; import java.util.Collection; @@ -7,9 +11,23 @@ public class UpdateConfigurationPacket { public static void sendTo(Collection players) { + throw new UnsupportedOperationException(); } public static void sendTo(ServerPlayer player) { + throw new UnsupportedOperationException(); } + public static void handle(boolean allowInventoryGui) { + CamsBackpacksClient.chestSlotUiEnabled = allowInventoryGui; + if (CamsBackpacksClient.chestSlotUiEnabled) { + var minecraft = Minecraft.getInstance(); + CamsBackpacksClient.backpackScreenIsOpen = false; + + if (minecraft.screen instanceof BackpackScreen screen && !screen.getMenu().isBlockEntity && minecraft.player != null) { + minecraft.setScreen(null); + minecraft.player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled")); + } + } + } } diff --git a/Common/src/main/java/dev/cammiescorner/camsbackpacks/util/platform/service/MenuHelper.java b/Common/src/main/java/dev/cammiescorner/camsbackpacks/util/platform/service/MenuHelper.java index 3bcc667..d9a4f02 100644 --- a/Common/src/main/java/dev/cammiescorner/camsbackpacks/util/platform/service/MenuHelper.java +++ b/Common/src/main/java/dev/cammiescorner/camsbackpacks/util/platform/service/MenuHelper.java @@ -2,11 +2,15 @@ import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.Container; import net.minecraft.world.MenuProvider; +import net.minecraft.world.item.ItemStack; public interface MenuHelper { MenuProvider getMenuProvider(BackpackBlockEntity backpack); void openMenu(ServerPlayer player, MenuProvider menu, BackpackBlockEntity backpack); + + void openMenu(ServerPlayer player, ItemStack backpackStack, Container inventory); } diff --git a/NeoForge/build.gradle b/NeoForge/build.gradle index f098ce1..59cc39a 100644 --- a/NeoForge/build.gradle +++ b/NeoForge/build.gradle @@ -17,6 +17,13 @@ dependencies { } } +mixin { + add(sourceSets.main, "${mod_id}.refmap.json") + + config("${mod_id}.mixins.json") + config("${mod_id}.neoforge.mixins.json") +} + jar { archiveClassifier.set("slim") } diff --git a/NeoForge/libs.versions.toml b/NeoForge/libs.versions.toml index 9d7d631..9e91ad3 100644 --- a/NeoForge/libs.versions.toml +++ b/NeoForge/libs.versions.toml @@ -1,6 +1,6 @@ [versions] neogradle = "6.+" -mixingradle = "0.7-SNAPSHOT" +mixingradle = "0.7.+" neoforge = "1.20.1-47.1.85" diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Client.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Client.java index b04b01b..2595d6c 100644 --- a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Client.java +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Client.java @@ -12,6 +12,7 @@ public class Client { public static void init() { + //FIXME this is handled by resourcefulconfig in 1.20.4+ ModLoadingContext.get().registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class, () -> new ConfigScreenHandler.ConfigScreenFactory( (minecraft, parent) -> new ConfigScreen(parent, null, CamsBackpacks.CONFIGURATOR.getConfig(BackpacksConfig.class)) diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Main.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Main.java index ae5afb2..6d09f02 100644 --- a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Main.java +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/entrypoints/Main.java @@ -1,6 +1,7 @@ package dev.cammiescorner.camsbackpacks.neoforge.entrypoints; import dev.cammiescorner.camsbackpacks.CamsBackpacks; +import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; import dev.cammiescorner.camsbackpacks.neoforge.services.NFRegistryHelper; import dev.cammiescorner.camsbackpacks.util.platform.Services; import net.minecraft.world.item.CreativeModeTabs; @@ -9,7 +10,6 @@ import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.DistExecutor; -import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @@ -25,6 +25,8 @@ public Main() { bus.register(this); CamsBackpacks.init(); + NetworkHandler.registerMessages(); + DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> Client::init); } diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/ConfigPacketMixin.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/ConfigPacketMixin.java new file mode 100644 index 0000000..8f16245 --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/ConfigPacketMixin.java @@ -0,0 +1,34 @@ +package dev.cammiescorner.camsbackpacks.neoforge.mixin; + +import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; +import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; +import dev.cammiescorner.camsbackpacks.neoforge.network.s2c.NFUpdateConfigurationPacket; +import dev.cammiescorner.camsbackpacks.network.s2c.UpdateConfigurationPacket; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.PacketDistributor; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +import java.util.Collection; + +@Mixin(UpdateConfigurationPacket.class) +public class ConfigPacketMixin { + + /** + * @author Up + * @reason I'm too lazy to set up multi loader networking rn + */ + @Overwrite + public static void sendTo(Collection players) { + NetworkHandler.INSTANCE.send(PacketDistributor.ALL.noArg(), new NFUpdateConfigurationPacket(BackpacksConfig.allowInventoryGui)); + } + + /** + * @author Up + * @reason I'm too lazy to set up multi loader networking rn + */ + @Overwrite + public static void sendTo(ServerPlayer player) { + NetworkHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), new NFUpdateConfigurationPacket(BackpacksConfig.allowInventoryGui)); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/EquipPacketMixin.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/EquipPacketMixin.java new file mode 100644 index 0000000..cc5d3dc --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/EquipPacketMixin.java @@ -0,0 +1,21 @@ +package dev.cammiescorner.camsbackpacks.neoforge.mixin; + +import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; +import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFEquipBackpackPacket; +import dev.cammiescorner.camsbackpacks.network.c2s.EquipBackpackPacket; +import net.minecraft.core.BlockPos; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(EquipBackpackPacket.class) +public class EquipPacketMixin { + + /** + * @author Up + * @reason I'm too lazy to set up multi loader networking rn + */ + @Overwrite + public static void send(boolean isBlockEntity, BlockPos pos) { + NetworkHandler.INSTANCE.sendToServer(new NFEquipBackpackPacket(pos, isBlockEntity)); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/OpenPacketMixin.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/OpenPacketMixin.java new file mode 100644 index 0000000..72e112f --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/OpenPacketMixin.java @@ -0,0 +1,20 @@ +package dev.cammiescorner.camsbackpacks.neoforge.mixin; + +import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; +import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFOpenBackpackScreenPacket; +import dev.cammiescorner.camsbackpacks.network.c2s.OpenBackpackScreenPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(OpenBackpackScreenPacket.class) +public class OpenPacketMixin { + + /** + * @author Up + * @reason I'm too lazy to set up multi loader networking rn + */ + @Overwrite + public static void send() { + NetworkHandler.INSTANCE.sendToServer(new NFOpenBackpackScreenPacket()); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/PlacePacketMixin.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/PlacePacketMixin.java new file mode 100644 index 0000000..53f1afb --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/PlacePacketMixin.java @@ -0,0 +1,21 @@ +package dev.cammiescorner.camsbackpacks.neoforge.mixin; + +import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; +import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFPlaceBackpackPacket; +import dev.cammiescorner.camsbackpacks.network.c2s.PlaceBackpackPacket; +import net.minecraft.world.phys.BlockHitResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(PlaceBackpackPacket.class) +public class PlacePacketMixin { + + /** + * @author Up + * @reason I'm too lazy to set up multi loader networking rn + */ + @Overwrite + public static void send(BlockHitResult hitResult) { + NetworkHandler.INSTANCE.sendToServer(new NFPlaceBackpackPacket(hitResult)); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/NetworkHandler.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/NetworkHandler.java new file mode 100644 index 0000000..a896420 --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/NetworkHandler.java @@ -0,0 +1,54 @@ +package dev.cammiescorner.camsbackpacks.neoforge.network; + +import com.mojang.logging.LogUtils; +import dev.cammiescorner.camsbackpacks.CamsBackpacks; +import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFEquipBackpackPacket; +import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFOpenBackpackScreenPacket; +import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFPlaceBackpackPacket; +import dev.cammiescorner.camsbackpacks.neoforge.network.s2c.NFUpdateConfigurationPacket; +import net.minecraftforge.network.NetworkRegistry; +import net.minecraftforge.network.simple.SimpleChannel; +import org.slf4j.Logger; + +public class NetworkHandler { + + private static final Logger logger = LogUtils.getLogger(); + private static final String PROTOCOL_VERSION = "1"; + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + CamsBackpacks.id("main"), + () -> PROTOCOL_VERSION, + PROTOCOL_VERSION::equals, + PROTOCOL_VERSION::equals + ); + + public static void registerMessages() { + + int id = 0; + + INSTANCE.messageBuilder(NFEquipBackpackPacket.class, id++) + .encoder(NFEquipBackpackPacket::encode) + .decoder(NFEquipBackpackPacket::decode) + .consumerMainThread(NFEquipBackpackPacket::handle) + .add(); + + INSTANCE.messageBuilder(NFOpenBackpackScreenPacket.class, id++) + .encoder(NFOpenBackpackScreenPacket::encode) + .decoder(NFOpenBackpackScreenPacket::decode) + .consumerMainThread(NFOpenBackpackScreenPacket::handle) + .add(); + + INSTANCE.messageBuilder(NFPlaceBackpackPacket.class, id++) + .encoder(NFPlaceBackpackPacket::encode) + .decoder(NFPlaceBackpackPacket::decode) + .consumerMainThread(NFPlaceBackpackPacket::handle) + .add(); + + INSTANCE.messageBuilder(NFUpdateConfigurationPacket.class, id++) + .encoder(NFUpdateConfigurationPacket::encode) + .decoder(NFUpdateConfigurationPacket::decode) + .consumerMainThread(NFUpdateConfigurationPacket::handle) + .add(); + + logger.debug("Registered {} messages", id); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFEquipBackpackPacket.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFEquipBackpackPacket.java new file mode 100644 index 0000000..d3f9fbb --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFEquipBackpackPacket.java @@ -0,0 +1,34 @@ +package dev.cammiescorner.camsbackpacks.neoforge.network.c2s; + +import dev.cammiescorner.camsbackpacks.network.c2s.EquipBackpackPacket; +import net.minecraft.core.BlockPos; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +import java.util.function.Supplier; + +public class NFEquipBackpackPacket { + + private final BlockPos pos; + private final boolean isBlockEntity; + + public NFEquipBackpackPacket(BlockPos pos, boolean isBlockEntity) { + this.pos = pos; + this.isBlockEntity = isBlockEntity; + } + + public void encode(FriendlyByteBuf buf) { + buf.writeBlockPos(pos); + buf.writeBoolean(isBlockEntity); + } + + public static NFEquipBackpackPacket decode(FriendlyByteBuf buf) { + var pos = buf.readBlockPos(); + var isBlockEntity = buf.readBoolean(); + return new NFEquipBackpackPacket(pos, isBlockEntity); + } + + public void handle(Supplier ctx) { + EquipBackpackPacket.handle(ctx.get().getSender(), this.pos, this.isBlockEntity); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFOpenBackpackScreenPacket.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFOpenBackpackScreenPacket.java new file mode 100644 index 0000000..8793237 --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFOpenBackpackScreenPacket.java @@ -0,0 +1,22 @@ +package dev.cammiescorner.camsbackpacks.neoforge.network.c2s; + +import dev.cammiescorner.camsbackpacks.network.c2s.OpenBackpackScreenPacket; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +import java.util.function.Supplier; + +public class NFOpenBackpackScreenPacket { + + public void encode(FriendlyByteBuf buf) { + // NO-OP + } + + public static NFOpenBackpackScreenPacket decode(FriendlyByteBuf buf) { + return new NFOpenBackpackScreenPacket(); + } + + public void handle(Supplier ctx) { + OpenBackpackScreenPacket.handle(ctx.get().getSender()); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFPlaceBackpackPacket.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFPlaceBackpackPacket.java new file mode 100644 index 0000000..829e4a3 --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/c2s/NFPlaceBackpackPacket.java @@ -0,0 +1,30 @@ +package dev.cammiescorner.camsbackpacks.neoforge.network.c2s; + +import dev.cammiescorner.camsbackpacks.network.c2s.PlaceBackpackPacket; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraftforge.network.NetworkEvent; + +import java.util.function.Supplier; + +public class NFPlaceBackpackPacket { + + private final BlockHitResult target; + + public NFPlaceBackpackPacket(BlockHitResult target) { + this.target = target; + } + + public void encode(FriendlyByteBuf buf) { + buf.writeBlockHitResult(target); + } + + public static NFPlaceBackpackPacket decode(FriendlyByteBuf buf) { + var target = buf.readBlockHitResult(); + return new NFPlaceBackpackPacket(target); + } + + public void handle(Supplier ctx) { + PlaceBackpackPacket.handle(ctx.get().getSender(), this.target); + } +} diff --git a/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/s2c/NFUpdateConfigurationPacket.java b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/s2c/NFUpdateConfigurationPacket.java new file mode 100644 index 0000000..3aff570 --- /dev/null +++ b/NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/network/s2c/NFUpdateConfigurationPacket.java @@ -0,0 +1,48 @@ +package dev.cammiescorner.camsbackpacks.neoforge.network.s2c; + +import com.mojang.logging.LogUtils; +import dev.cammiescorner.camsbackpacks.client.CamsBackpacksClient; +import dev.cammiescorner.camsbackpacks.client.screen.BackpackScreen; +import dev.cammiescorner.camsbackpacks.network.s2c.UpdateConfigurationPacket; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.fml.DistExecutor; +import net.minecraftforge.network.NetworkEvent; +import org.slf4j.Logger; + +import java.util.function.Supplier; + +public class NFUpdateConfigurationPacket { + + private static final Logger logger = LogUtils.getLogger(); + private final boolean allowInventoryGui; + + public NFUpdateConfigurationPacket(boolean allowInventoryGui) { + this.allowInventoryGui = allowInventoryGui; + } + + public void encode(FriendlyByteBuf buf) { + buf.writeBoolean(allowInventoryGui); + } + + public static NFUpdateConfigurationPacket decode(FriendlyByteBuf buf) { + var allowInventoryGui = buf.readBoolean(); + return new NFUpdateConfigurationPacket(allowInventoryGui); + } + + public void handle(Supplier ctx) { + logger.debug("Configuration received from server"); + DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handleMessage(this, ctx)); + } + + @OnlyIn(Dist.CLIENT) + private static class ClientHandler { + + public static void handleMessage(NFUpdateConfigurationPacket msg, Supplier ctx) { + UpdateConfigurationPacket.handle(msg.allowInventoryGui); + } + } +} diff --git a/NeoForge/src/main/resources/camsbackpacks.neoforge.mixins.json b/NeoForge/src/main/resources/camsbackpacks.neoforge.mixins.json new file mode 100644 index 0000000..0cdf0c9 --- /dev/null +++ b/NeoForge/src/main/resources/camsbackpacks.neoforge.mixins.json @@ -0,0 +1,16 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "dev.cammiescorner.camsbackpacks.neoforge.mixin", + "refmap": "${mod_id}.refmap.json", + "compatibilityLevel": "JAVA_17", + "mixins": [ + "ConfigPacketMixin", + "EquipPacketMixin", + "OpenPacketMixin", + "PlacePacketMixin" + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/compat/modmenu/ModMenuEntrypoint.java b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/compat/modmenu/ModMenuEntrypoint.java index 5f96150..feb5f90 100644 --- a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/compat/modmenu/ModMenuEntrypoint.java +++ b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/compat/modmenu/ModMenuEntrypoint.java @@ -6,6 +6,7 @@ import dev.cammiescorner.camsbackpacks.CamsBackpacks; import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; +//FIXME this is handled by resourcefulconfig in 1.20.4+ public class ModMenuEntrypoint implements ModMenuApi { @Override diff --git a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QEquipBackpackPacket.java b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QEquipBackpackPacket.java index e0628da..ab33bcb 100644 --- a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QEquipBackpackPacket.java +++ b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QEquipBackpackPacket.java @@ -1,28 +1,14 @@ package dev.cammiescorner.camsbackpacks.quilt.network.c2s; -import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; import dev.cammiescorner.camsbackpacks.CamsBackpacks; -import dev.cammiescorner.camsbackpacks.block.BackpackBlock; -import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; -import dev.cammiescorner.camsbackpacks.item.BackpackItem; -import io.netty.buffer.Unpooled; -import net.minecraft.ChatFormatting; +import dev.cammiescorner.camsbackpacks.network.c2s.EquipBackpackPacket; import net.minecraft.core.BlockPos; -import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; -import net.minecraft.sounds.SoundEvents; -import net.minecraft.sounds.SoundSource; -import net.minecraft.world.ContainerHelper; -import net.minecraft.world.entity.EquipmentSlot; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.state.properties.BlockStateProperties; +import org.quiltmc.qsl.networking.api.PacketByteBufs; import org.quiltmc.qsl.networking.api.PacketSender; import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking; @@ -30,7 +16,7 @@ public class QEquipBackpackPacket { public static final ResourceLocation ID = CamsBackpacks.id("equip_backpack"); public static void send(boolean isBlockEntity, BlockPos pos) { - FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + FriendlyByteBuf buf = PacketByteBufs.create(); buf.writeBlockPos(pos); buf.writeBoolean(isBlockEntity); ClientPlayNetworking.send(ID, buf); @@ -40,54 +26,6 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam BlockPos pos = buf.readBlockPos(); boolean isBlockEntity = buf.readBoolean(); - server.execute(() -> { - Level world = player.level(); - - if (!world.mayInteract(player, pos)) { - player.closeContainer(); - MutableComponent message = isBlockEntity - ? Component.translatable("error.camsbackpacks.permission_pickup_at") - : Component.translatable("error.camsbackpacks.permission_place_at"); - player.sendSystemMessage(message.withStyle(ChatFormatting.RED), true); - return; - } - - if (isBlockEntity) { - if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) { - ItemStack stack = new ItemStack(world.getBlockState(pos).getBlock().asItem()); - CompoundTag tag = stack.getOrCreateTag(); - - ContainerHelper.saveAllItems(tag, blockEntity.inventory); - blockEntity.wasPickedUp = true; - player.setItemSlot(EquipmentSlot.CHEST, stack); - - if (blockEntity.hasCustomName()) - stack.setHoverName(blockEntity.getName()); - - world.destroyBlock(pos, false, player); - - if(!BackpacksConfig.allowInventoryGui) { - player.closeContainer(); - player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled")); - } - - } - } else { - ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); - - if (stack.getItem() instanceof BackpackItem backpackItem && BackpackBlock.isStateReplaceable(world, pos) && pos.closerThan(player.blockPosition(), 3)) { - world.playSound(null, pos, SoundEvents.WOOL_PLACE, SoundSource.BLOCKS, 1F, 1F); - world.setBlockAndUpdate(pos, backpackItem.getBlock().defaultBlockState().setValue(BackpackBlock.FACING, player.getDirection()).setValue(BlockStateProperties.WATERLOGGED, !world.getFluidState(pos).isEmpty())); - player.closeContainer(); - - if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) { - ContainerHelper.loadAllItems(stack.getOrCreateTag(), backpack.inventory); - backpack.setName(stack.getHoverName()); - } - - player.getItemBySlot(EquipmentSlot.CHEST).shrink(1); - } - } - }); + server.execute(() -> EquipBackpackPacket.handle(player, pos, isBlockEntity)); } } diff --git a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QOpenBackpackScreenPacket.java b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QOpenBackpackScreenPacket.java index 14b621e..f29fab7 100644 --- a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QOpenBackpackScreenPacket.java +++ b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QOpenBackpackScreenPacket.java @@ -1,26 +1,13 @@ package dev.cammiescorner.camsbackpacks.quilt.network.c2s; import dev.cammiescorner.camsbackpacks.CamsBackpacks; -import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; -import dev.cammiescorner.camsbackpacks.menu.BackpackMenu; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; -import net.minecraft.core.NonNullList; -import net.minecraft.nbt.CompoundTag; +import dev.cammiescorner.camsbackpacks.network.c2s.OpenBackpackScreenPacket; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; -import net.minecraft.world.Container; -import net.minecraft.world.ContainerHelper; -import net.minecraft.world.entity.EquipmentSlot; -import net.minecraft.world.entity.player.Inventory; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.inventory.ContainerLevelAccess; -import net.minecraft.world.item.ItemStack; +import org.quiltmc.qsl.networking.api.PacketByteBufs; import org.quiltmc.qsl.networking.api.PacketSender; import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking; @@ -28,85 +15,10 @@ public class QOpenBackpackScreenPacket { public static final ResourceLocation ID = CamsBackpacks.id("open_backpack"); public static void send() { - FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); - ClientPlayNetworking.send(ID, buf); + ClientPlayNetworking.send(ID, PacketByteBufs.empty()); } public static void handle(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl network, FriendlyByteBuf buf, PacketSender sender) { - server.execute(() -> { - if(!BackpacksConfig.allowInventoryGui) { - player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled"), true); - return; - } - - final NonNullList stacks = NonNullList.withSize(36, ItemStack.EMPTY); - ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); - CompoundTag tag = stack.getOrCreateTag(); - ContainerHelper.loadAllItems(tag, stacks); - Container inventory = new Container() { - @Override - public void clearContent() { - stacks.clear(); - } - - @Override - public int getContainerSize() { - return stacks.size(); - } - - @Override - public boolean isEmpty() { - return stacks.isEmpty(); - } - - @Override - public ItemStack getItem(int slot) { - return stacks.get(slot); - } - - @Override - public ItemStack removeItem(int slot, int amount) { - return ContainerHelper.removeItem(stacks, slot, amount); - } - - @Override - public ItemStack removeItemNoUpdate(int slot) { - return ContainerHelper.takeItem(stacks, slot); - } - - @Override - public void setItem(int slot, ItemStack stack) { - stacks.set(slot, stack); - } - - @Override - public void setChanged() { - - } - - @Override - public boolean stillValid(Player player) { - return true; - } - }; - - player.openMenu(new ExtendedScreenHandlerFactory() { - @Override - public void writeScreenOpeningData(ServerPlayer player, FriendlyByteBuf buf) { - buf.writeBlockPos(player.blockPosition()); - buf.writeBoolean(false); - } - - @Override - public Component getDisplayName() { - return stack.getHoverName(); - } - - @Override - public AbstractContainerMenu createMenu(int syncId, Inventory playerInventory, Player player) { - return new BackpackMenu(syncId, playerInventory, inventory, ContainerLevelAccess.create(player.level(), player.blockPosition()), player.blockPosition(), false); - } - }); - }); + server.execute(() -> OpenBackpackScreenPacket.handle(player)); } } diff --git a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QPlaceBackpackPacket.java b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QPlaceBackpackPacket.java index a822a70..54476ec 100644 --- a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QPlaceBackpackPacket.java +++ b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/c2s/QPlaceBackpackPacket.java @@ -1,26 +1,14 @@ package dev.cammiescorner.camsbackpacks.quilt.network.c2s; import dev.cammiescorner.camsbackpacks.CamsBackpacks; -import dev.cammiescorner.camsbackpacks.block.BackpackBlock; -import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; -import dev.cammiescorner.camsbackpacks.item.BackpackItem; -import io.netty.buffer.Unpooled; -import net.minecraft.ChatFormatting; -import net.minecraft.core.BlockPos; +import dev.cammiescorner.camsbackpacks.network.c2s.PlaceBackpackPacket; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; -import net.minecraft.sounds.SoundEvents; -import net.minecraft.sounds.SoundSource; -import net.minecraft.world.ContainerHelper; -import net.minecraft.world.entity.EquipmentSlot; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.phys.BlockHitResult; +import org.quiltmc.qsl.networking.api.PacketByteBufs; import org.quiltmc.qsl.networking.api.PacketSender; import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking; @@ -28,7 +16,7 @@ public class QPlaceBackpackPacket { public static final ResourceLocation ID = CamsBackpacks.id("place_backpack"); public static void send(BlockHitResult hitResult) { - FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + FriendlyByteBuf buf = PacketByteBufs.create(); buf.writeBlockHitResult(hitResult); ClientPlayNetworking.send(ID, buf); } @@ -36,29 +24,6 @@ public static void send(BlockHitResult hitResult) { public static void handle(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl network, FriendlyByteBuf buf, PacketSender sender) { BlockHitResult hitResult = buf.readBlockHitResult(); - server.execute(() -> { - Level world = player.level(); - BlockPos pos = BackpackBlock.isStateReplaceable(world, hitResult.getBlockPos()) ? hitResult.getBlockPos() : hitResult.getBlockPos().relative(hitResult.getDirection()); - - if(!world.mayInteract(player, pos)) { - player.closeContainer(); - player.sendSystemMessage(Component.translatable("error.camsbackpacks.permission_place_at").withStyle(ChatFormatting.RED), true); - return; - } - - ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); - - if (BackpackBlock.isStateReplaceable(world, pos)) { - world.playSound(null, pos, SoundEvents.WOOL_PLACE, SoundSource.BLOCKS, 1F, 1F); - world.setBlockAndUpdate(pos, ((BackpackItem) stack.getItem()).getBlock().defaultBlockState().setValue(BackpackBlock.FACING, player.getDirection()).setValue(BlockStateProperties.WATERLOGGED, !world.getFluidState(pos).isEmpty())); - - if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) { - ContainerHelper.loadAllItems(stack.getOrCreateTag(), backpack.inventory); - backpack.setName(stack.getHoverName()); - } - - player.getItemBySlot(EquipmentSlot.CHEST).shrink(1); - } - }); + server.execute(() -> PlaceBackpackPacket.handle(player, hitResult)); } } diff --git a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/s2c/QUpdateConfigurationPacket.java b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/s2c/QUpdateConfigurationPacket.java index f8f4d45..2bfe7e0 100644 --- a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/s2c/QUpdateConfigurationPacket.java +++ b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/network/s2c/QUpdateConfigurationPacket.java @@ -1,14 +1,12 @@ package dev.cammiescorner.camsbackpacks.quilt.network.s2c; import com.mojang.logging.LogUtils; -import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; import dev.cammiescorner.camsbackpacks.CamsBackpacks; -import dev.cammiescorner.camsbackpacks.client.CamsBackpacksClient; -import dev.cammiescorner.camsbackpacks.client.screen.BackpackScreen; +import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; +import dev.cammiescorner.camsbackpacks.network.s2c.UpdateConfigurationPacket; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import org.quiltmc.loader.api.minecraft.ClientOnly; @@ -33,7 +31,7 @@ public static void sendTo(PacketSender sender) { } private static FriendlyByteBuf encode() { - var buf = PacketByteBufs.create(); + FriendlyByteBuf buf = PacketByteBufs.create(); buf.writeBoolean(BackpacksConfig.allowInventoryGui); return buf; @@ -43,16 +41,6 @@ private static FriendlyByteBuf encode() { public static void handle(Minecraft minecraft, ClientPacketListener packetListener, FriendlyByteBuf buf, PacketSender packetSender) { logger.debug("Configuration received from server"); var allowInvGui = buf.readBoolean(); - minecraft.execute(() -> { - CamsBackpacksClient.chestSlotUiEnabled = allowInvGui; - if(CamsBackpacksClient.chestSlotUiEnabled) { - CamsBackpacksClient.backpackScreenIsOpen = false; - - if(minecraft.screen instanceof BackpackScreen screen && !screen.getMenu().isBlockEntity) { - minecraft.setScreen(null); - minecraft.player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled")); - } - } - }); + minecraft.execute(() -> UpdateConfigurationPacket.handle(allowInvGui)); } } diff --git a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/services/QMenuHelper.java b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/services/QMenuHelper.java index 6cc6cbe..5b0509d 100644 --- a/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/services/QMenuHelper.java +++ b/Quilt/src/main/java/dev/cammiescorner/camsbackpacks/quilt/services/QMenuHelper.java @@ -1,15 +1,19 @@ package dev.cammiescorner.camsbackpacks.quilt.services; import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; +import dev.cammiescorner.camsbackpacks.menu.BackpackMenu; import dev.cammiescorner.camsbackpacks.util.platform.service.MenuHelper; import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.Container; import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.ContainerLevelAccess; +import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.Nullable; public class QMenuHelper implements MenuHelper { @@ -38,4 +42,25 @@ public AbstractContainerMenu createMenu(int syncId, Inventory inventory, Player public void openMenu(ServerPlayer player, MenuProvider menu, BackpackBlockEntity backpack) { player.openMenu(menu); } + + @Override + public void openMenu(ServerPlayer player, ItemStack backpackStack, Container inventory) { + player.openMenu(new ExtendedScreenHandlerFactory() { + @Override + public void writeScreenOpeningData(ServerPlayer player, FriendlyByteBuf buf) { + buf.writeBlockPos(player.blockPosition()); + buf.writeBoolean(false); + } + + @Override + public Component getDisplayName() { + return backpackStack.getHoverName(); + } + + @Override + public AbstractContainerMenu createMenu(int syncId, Inventory playerInventory, Player player) { + return new BackpackMenu(syncId, playerInventory, inventory, ContainerLevelAccess.create(player.level(), player.blockPosition()), player.blockPosition(), false); + } + }); + } }