diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/CamsBackpacks.java b/src/main/java/dev/cammiescorner/camsbackpacks/CamsBackpacks.java index c132f0e..4880f1d 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/CamsBackpacks.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/CamsBackpacks.java @@ -1,9 +1,9 @@ package dev.cammiescorner.camsbackpacks; -import com.google.common.base.MoreObjects; import dev.cammiescorner.camsbackpacks.common.network.EquipBackpackPacket; import dev.cammiescorner.camsbackpacks.common.network.OpenBackpackScreenPacket; import dev.cammiescorner.camsbackpacks.common.network.PlaceBackpackPacket; +import dev.cammiescorner.camsbackpacks.common.network.UpdateConfigurationPacket; import dev.cammiescorner.camsbackpacks.compat.universalgraves.UniversalGravesCompat; import dev.cammiescorner.camsbackpacks.core.BackpacksConfig; import dev.cammiescorner.camsbackpacks.core.registry.ModBlockEntities; @@ -12,11 +12,11 @@ import eu.midnightdust.lib.config.MidnightConfig; import net.minecraft.resources.ResourceLocation; import org.quiltmc.loader.api.ModContainer; -import org.quiltmc.loader.api.ModMetadata; import org.quiltmc.loader.api.QuiltLoader; -import org.quiltmc.loader.api.plugin.ModMetadataExt; import org.quiltmc.qsl.base.api.entrypoint.ModInitializer; +import org.quiltmc.qsl.networking.api.ServerPlayConnectionEvents; import org.quiltmc.qsl.networking.api.ServerPlayNetworking; +import org.quiltmc.qsl.resource.loader.api.ResourceLoaderEvents; public class CamsBackpacks implements ModInitializer { @@ -34,8 +34,17 @@ public void onInitialize(ModContainer mod) { ModBlockEntities.register(); ModScreenHandlers.register(); - if (QuiltLoader.isModLoaded("universal-graves")) + if (QuiltLoader.isModLoaded("universal-graves")) { UniversalGravesCompat.load(); + } + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> UpdateConfigurationPacket.sendTo(sender)); + ResourceLoaderEvents.END_DATA_PACK_RELOAD.register(context -> { + var server = context.server(); + if(server != null) { + UpdateConfigurationPacket.sendTo(server.getPlayerList().getPlayers()); + } + }); } public static ResourceLocation id(String path) { diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/client/CamsBackpacksClient.java b/src/main/java/dev/cammiescorner/camsbackpacks/client/CamsBackpacksClient.java index cf15b47..85ae3f9 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/client/CamsBackpacksClient.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/client/CamsBackpacksClient.java @@ -5,6 +5,7 @@ import dev.cammiescorner.camsbackpacks.client.screen.BackpackScreen; import dev.cammiescorner.camsbackpacks.common.blocks.BackpackBlock; import dev.cammiescorner.camsbackpacks.common.items.BackpackItem; +import dev.cammiescorner.camsbackpacks.common.network.UpdateConfigurationPacket; import dev.cammiescorner.camsbackpacks.core.registry.ModScreenHandlers; import dev.cammiescorner.camsbackpacks.core.util.BackpackHelper; import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; @@ -19,12 +20,14 @@ import org.quiltmc.loader.api.ModContainer; import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer; import org.quiltmc.qsl.block.extensions.api.client.BlockRenderLayerMap; +import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking; import static dev.cammiescorner.camsbackpacks.core.registry.ModBlocks.*; public class CamsBackpacksClient implements ClientModInitializer { public static final ModelLayerLocation BACKPACK = new ModelLayerLocation(CamsBackpacks.id("backpack"), "main"); public static boolean backpackScreenIsOpen = true; + public static boolean chestSlotUiEnabled = true; @Override public void onInitializeClient(ModContainer mod) { @@ -36,5 +39,6 @@ public void onInitializeClient(ModContainer mod) { ColorProviderRegistry.ITEM.register((stack, tintIndex) -> BackpackHelper.dyeToDecimal(((BackpackItem) stack.getItem()).getColour()), BuiltInRegistries.ITEM.stream().filter(item -> item instanceof BackpackItem backpack && (backpack.getColour() != DyeColor.WHITE || backpack == WHITE_BACKPACK.asItem())).toArray(Item[]::new)); BlockRenderLayerMap.put(RenderType.cutout(), BuiltInRegistries.BLOCK.stream().filter(BackpackBlock.class::isInstance).toArray(Block[]::new)); + ClientPlayNetworking.registerGlobalReceiver(UpdateConfigurationPacket.ID, UpdateConfigurationPacket::handle); } } diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java index ea3ee86..8c1496d 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java @@ -4,6 +4,7 @@ import dev.cammiescorner.camsbackpacks.common.blocks.BackpackBlock; import dev.cammiescorner.camsbackpacks.common.blocks.entities.BackpackBlockEntity; import dev.cammiescorner.camsbackpacks.common.items.BackpackItem; +import dev.cammiescorner.camsbackpacks.core.BackpacksConfig; import dev.cammiescorner.camsbackpacks.core.util.BackpackHelper; import io.netty.buffer.Unpooled; import net.minecraft.ChatFormatting; @@ -66,6 +67,11 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam 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); diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/OpenBackpackScreenPacket.java b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/OpenBackpackScreenPacket.java index cdb80cb..dea4f13 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/OpenBackpackScreenPacket.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/OpenBackpackScreenPacket.java @@ -2,6 +2,7 @@ import dev.cammiescorner.camsbackpacks.CamsBackpacks; import dev.cammiescorner.camsbackpacks.common.menu.BackpackMenu; +import dev.cammiescorner.camsbackpacks.core.BackpacksConfig; import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; import net.minecraft.core.NonNullList; @@ -33,6 +34,11 @@ public static void send() { 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(); diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/UpdateConfigurationPacket.java b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/UpdateConfigurationPacket.java new file mode 100644 index 0000000..5ce961b --- /dev/null +++ b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/UpdateConfigurationPacket.java @@ -0,0 +1,58 @@ +package dev.cammiescorner.camsbackpacks.common.network; + +import com.mojang.logging.LogUtils; +import dev.cammiescorner.camsbackpacks.CamsBackpacks; +import dev.cammiescorner.camsbackpacks.client.CamsBackpacksClient; +import dev.cammiescorner.camsbackpacks.client.screen.BackpackScreen; +import dev.cammiescorner.camsbackpacks.core.BackpacksConfig; +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; +import org.quiltmc.qsl.networking.api.PacketByteBufs; +import org.quiltmc.qsl.networking.api.PacketSender; +import org.quiltmc.qsl.networking.api.ServerPlayNetworking; +import org.slf4j.Logger; + +import java.util.Collection; + +public class UpdateConfigurationPacket { + + private static final Logger logger = LogUtils.getLogger(); + public static final ResourceLocation ID = CamsBackpacks.id("config"); + + public static void sendTo(Collection players) { + ServerPlayNetworking.send(players, ID, encode()); + } + + public static void sendTo(PacketSender sender) { + sender.sendPacket(ID, encode()); + } + + private static FriendlyByteBuf encode() { + var buf = PacketByteBufs.create(); + buf.writeBoolean(BackpacksConfig.allowInventoryGui); + + return buf; + } + + @ClientOnly + 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")); + } + } + }); + } +} diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/core/BackpacksConfig.java b/src/main/java/dev/cammiescorner/camsbackpacks/core/BackpacksConfig.java index b987c5c..109dd54 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/core/BackpacksConfig.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/core/BackpacksConfig.java @@ -6,4 +6,7 @@ public class BackpacksConfig extends MidnightConfig { @Entry public static boolean sneakPlaceBackpack = true; + + @Entry + public static boolean allowInventoryGui = true; } diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/InventoryScreenMixin.java b/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/InventoryScreenMixin.java index f367038..f11d8c2 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/InventoryScreenMixin.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/InventoryScreenMixin.java @@ -37,7 +37,7 @@ private InventoryScreenMixin(InventoryMenu screenHandler, Inventory playerInvent @Inject(method = "renderBg", at = @At("TAIL")) private void camsbackpacks$renderBg(GuiGraphics gui, float delta, int mouseX, int mouseY, CallbackInfo info) { - if (equippedStack.getItem() instanceof BackpackItem) { + if (equippedStack.getItem() instanceof BackpackItem && CamsBackpacksClient.chestSlotUiEnabled) { RenderSystem.setShaderTexture(0, BackpackScreen.TEXTURE); gui.blit(BackpackScreen.TEXTURE, leftPos + 1, topPos - 27, 0, 0, 190, 60, 30, 322, 220); gui.renderItem(playerInvIcon, leftPos + 8, topPos - 20); @@ -48,7 +48,7 @@ private InventoryScreenMixin(InventoryMenu screenHandler, Inventory playerInvent @Inject(method = "render", at = @At("TAIL")) private void camsbackpacks$render(GuiGraphics gui, int mouseX, int mouseY, float delta, CallbackInfo info) { - if (equippedStack.getItem() instanceof BackpackItem) { + if (equippedStack.getItem() instanceof BackpackItem && CamsBackpacksClient.chestSlotUiEnabled) { if (isHovering(3, -27, 26, 28, mouseX, mouseY)) gui.renderTooltip(font, Component.translatable("container.camsbackpacks.player_inv"), mouseX, mouseY); else if (isHovering(32, -27, 26, 28, mouseX, mouseY)) @@ -61,7 +61,7 @@ else if (isHovering(32, -27, 26, 28, mouseX, mouseY)) playerInvIcon = BackpackScreen.getPlayerHead(minecraft.player); equippedStack = minecraft.player.getItemBySlot(EquipmentSlot.CHEST); - if (equippedStack.getItem() instanceof BackpackItem) { + if (equippedStack.getItem() instanceof BackpackItem && CamsBackpacksClient.chestSlotUiEnabled) { this.addWidget(new Button.Builder(Component.empty(), this::openBackpackScreen) .bounds(this.leftPos + 31, this.topPos - 27, 28, 28).build()); } diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/MinecraftMixin.java b/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/MinecraftMixin.java index 8f24d22..e82f03f 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/MinecraftMixin.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/core/mixin/client/MinecraftMixin.java @@ -28,6 +28,10 @@ public abstract class MinecraftMixin { @Inject(method = "handleKeybinds", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;setScreen(Lnet/minecraft/client/gui/screens/Screen;)V", ordinal = 1), cancellable = true) private void camsbackpacks$setScreen(CallbackInfo info) { if (this.level != null && this.player != null && !this.player.isCreative() && CamsBackpacksClient.backpackScreenIsOpen && this.player.getItemBySlot(EquipmentSlot.CHEST).getItem() instanceof BackpackItem) { + if(!CamsBackpacksClient.chestSlotUiEnabled) { + CamsBackpacksClient.backpackScreenIsOpen = false; + return; + } OpenBackpackScreenPacket.send(); info.cancel(); } diff --git a/src/main/resources/assets/camsbackpacks/lang/en_us.json b/src/main/resources/assets/camsbackpacks/lang/en_us.json index 80c911b..3086f2e 100644 --- a/src/main/resources/assets/camsbackpacks/lang/en_us.json +++ b/src/main/resources/assets/camsbackpacks/lang/en_us.json @@ -47,8 +47,10 @@ "error.camsbackpacks.permission_place_at": "You do not have permission to place a backpack at this location!", "error.camsbackpacks.permission_pickup_at": "You do not have permission to pick up a backpack at this location!", "error.camsbackpacks.permission_use": "You do not have permission to use this backpack!", + "error.camsbackpacks.chest_slot_ui_disabled": "Chest slot UI is disabled on this server! Place the backpack on the ground to open it.", // Config "camsbackpacks.midnightconfig.title": "CamsBackpacks", - "camsbackpacks.midnightconfig.sneakPlaceBackpack": "Enable Sneak-Placing Backpacks" + "camsbackpacks.midnightconfig.sneakPlaceBackpack": "Enable Sneak-Placing Backpacks", + "camsbackpacks.midnightconfig.allowInventoryGui": "Allow using backpacks while equipped" }