Skip to content

Commit

Permalink
Add config to disable inventory usage of backpacks
Browse files Browse the repository at this point in the history
(closes #34)
  • Loading branch information
UpcraftLP committed Jan 3, 2024
1 parent 5dd0cab commit 0a373e4
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/main/java/dev/cammiescorner/camsbackpacks/CamsBackpacks.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ItemStack> stacks = NonNullList.withSize(36, ItemStack.EMPTY);
ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST);
CompoundTag tag = stack.getOrCreateTag();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServerPlayer> 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"));
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public class BackpacksConfig extends MidnightConfig {

@Entry
public static boolean sneakPlaceBackpack = true;

@Entry
public static boolean allowInventoryGui = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/camsbackpacks/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 0a373e4

Please sign in to comment.