From a991e01721e8ee0a683e228d748c121c2a439fe2 Mon Sep 17 00:00:00 2001 From: Eugene Rybalkin Date: Tue, 16 Apr 2024 22:45:37 +0300 Subject: [PATCH] modified: src/main/java/com/youpe/test/Testing.java modified: src/main/java/com/youpe/test/TestingClient.java modified: src/main/java/com/youpe/test/client/CustomAbstractInventoryScreen.java new file: src/main/java/com/youpe/test/client/CustomCraftingScreenHandler.java modified: src/main/java/com/youpe/test/client/CustomHandledScreen.java modified: src/main/java/com/youpe/test/client/CustomInventoryScreen.java new file: src/main/java/com/youpe/test/client/CustomPlayerScreenHandler.java new file: src/main/java/com/youpe/test/client/TestScreenHandler.java modified: src/main/java/com/youpe/test/event/DirtBrokenAfter.java modified: src/main/java/com/youpe/test/event/KeyInputHandler.java modified: src/main/java/com/youpe/test/item/custom/CustomItem.java modified: src/main/java/com/youpe/test/networking/ModNetworkingManager.java --- src/main/java/com/youpe/test/Testing.java | 21 +- .../java/com/youpe/test/TestingClient.java | 5 +- .../client/CustomAbstractInventoryScreen.java | 14 +- .../client/CustomCraftingScreenHandler.java | 199 ++++++++++++++ .../test/client/CustomHandledScreen.java | 19 +- .../test/client/CustomInventoryScreen.java | 9 +- .../client/CustomPlayerScreenHandler.java | 258 ++++++++++++++++++ .../youpe/test/client/TestScreenHandler.java | 61 +++++ .../com/youpe/test/event/DirtBrokenAfter.java | 1 - .../com/youpe/test/event/KeyInputHandler.java | 3 +- .../youpe/test/item/custom/CustomItem.java | 1 - .../test/networking/ModNetworkingManager.java | 1 - 12 files changed, 556 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/youpe/test/client/CustomCraftingScreenHandler.java create mode 100644 src/main/java/com/youpe/test/client/CustomPlayerScreenHandler.java create mode 100644 src/main/java/com/youpe/test/client/TestScreenHandler.java diff --git a/src/main/java/com/youpe/test/Testing.java b/src/main/java/com/youpe/test/Testing.java index 4bcc12b..b0ab68d 100644 --- a/src/main/java/com/youpe/test/Testing.java +++ b/src/main/java/com/youpe/test/Testing.java @@ -1,30 +1,33 @@ package com.youpe.test; +import com.youpe.test.client.TestScreenHandler; import com.youpe.test.event.DirtBrokenAfter; import com.youpe.test.event.JoinServerHandler; import com.youpe.test.item.ModItems; import com.youpe.test.networking.ModNetworkingManager; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.block.Blocks; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; +import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; +import net.minecraft.registry.Registries; +import net.minecraft.registry.Registry; +import net.minecraft.screen.ScreenHandlerType; import net.minecraft.util.Identifier; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.youpe.test.server.PlayerData; -import com.youpe.test.server.StateSaverAndLoader; - public class Testing implements ModInitializer { public static final String MOD_ID = "testing"; + public static final ScreenHandlerType BOX_SCREEN_HANDLER; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + static { + Registry.register(Registries.SCREEN_HANDLER, new Identifier(Testing.MOD_ID, name), item); + BOX_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(BOX, BoxScreenHandler::new) + } + @Override public void onInitialize() { diff --git a/src/main/java/com/youpe/test/TestingClient.java b/src/main/java/com/youpe/test/TestingClient.java index 0799821..dbf4916 100644 --- a/src/main/java/com/youpe/test/TestingClient.java +++ b/src/main/java/com/youpe/test/TestingClient.java @@ -6,8 +6,7 @@ import com.youpe.test.event.TestHudRender; import com.youpe.test.networking.ModNetworkingManager; import com.youpe.test.server.PlayerData; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.minecraft.text.Text; +import net.minecraft.client.MinecraftClient; public class TestingClient implements ClientModInitializer{ @@ -22,6 +21,8 @@ public class TestingClient implements ClientModInitializer{ TestHudRender.registerModRenders(); + MinecraftClient client = MinecraftClient.getInstance(); + } } diff --git a/src/main/java/com/youpe/test/client/CustomAbstractInventoryScreen.java b/src/main/java/com/youpe/test/client/CustomAbstractInventoryScreen.java index 78d63db..973d7d0 100644 --- a/src/main/java/com/youpe/test/client/CustomAbstractInventoryScreen.java +++ b/src/main/java/com/youpe/test/client/CustomAbstractInventoryScreen.java @@ -8,6 +8,7 @@ import net.minecraft.entity.effect.StatusEffect; import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffectUtil; import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.screen.PlayerScreenHandler; import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.MutableText; @@ -20,12 +21,11 @@ import java.util.Optional; public abstract class CustomAbstractInventoryScreen extends CustomHandledScreen { - public CustomAbstractInventoryScreen(T screenHandler, PlayerInventory inventory, Text title) { + public CustomAbstractInventoryScreen(PlayerScreenHandler screenHandler, PlayerInventory inventory, Text title) { super(screenHandler, inventory, title); } public void render(DrawContext context, int mouseX, int mouseY, float delta) { super.render(context, mouseX, mouseY, delta); - this.drawStatusEffects(context, mouseX, mouseY); } public boolean hideStatusEffectHud() { @@ -34,6 +34,7 @@ public abstract class CustomAbstractInventoryScreen ex return j >= 32; } + @SuppressWarnings("unused") private void drawStatusEffects(DrawContext context, int mouseX, int mouseY) { int i = this.x + this.backgroundWidth + 2; int j = this.width - i; @@ -54,7 +55,7 @@ public abstract class CustomAbstractInventoryScreen ex int l = this.y; StatusEffectInstance statusEffectInstance = null; - for(Iterator var12 = iterable.iterator(); var12.hasNext(); l += k) { + for(Iterator var12 = iterable.iterator(); var12.hasNext(); l += k) { StatusEffectInstance statusEffectInstance2 = (StatusEffectInstance)var12.next(); if (mouseY >= l && mouseY <= l + k) { statusEffectInstance = statusEffectInstance2; @@ -73,8 +74,7 @@ public abstract class CustomAbstractInventoryScreen ex private void drawStatusEffectBackgrounds(DrawContext context, int x, int height, Iterable statusEffects, boolean wide) { int i = this.y; - for(Iterator var7 = statusEffects.iterator(); var7.hasNext(); i += height) { - StatusEffectInstance statusEffectInstance = (StatusEffectInstance)var7.next(); + for(Iterator var7 = statusEffects.iterator(); var7.hasNext(); i += height) { if (wide) { context.drawTexture(BACKGROUND_TEXTURE, x, i, 0, 166, 120, 32); } else { @@ -88,7 +88,7 @@ public abstract class CustomAbstractInventoryScreen ex StatusEffectSpriteManager statusEffectSpriteManager = this.client.getStatusEffectSpriteManager(); int i = this.y; - for(Iterator var8 = statusEffects.iterator(); var8.hasNext(); i += height) { + for(Iterator var8 = statusEffects.iterator(); var8.hasNext(); i += height) { StatusEffectInstance statusEffectInstance = (StatusEffectInstance)var8.next(); StatusEffect statusEffect = statusEffectInstance.getEffectType(); Sprite sprite = statusEffectSpriteManager.getSprite(statusEffect); @@ -100,7 +100,7 @@ public abstract class CustomAbstractInventoryScreen ex private void drawStatusEffectDescriptions(DrawContext context, int x, int height, Iterable statusEffects) { int i = this.y; - for(Iterator var6 = statusEffects.iterator(); var6.hasNext(); i += height) { + for(Iterator var6 = statusEffects.iterator(); var6.hasNext(); i += height) { StatusEffectInstance statusEffectInstance = (StatusEffectInstance)var6.next(); Text text = this.getStatusEffectDescription(statusEffectInstance); context.drawTextWithShadow(this.textRenderer, text, x + 10 + 18, i + 6, 16777215); diff --git a/src/main/java/com/youpe/test/client/CustomCraftingScreenHandler.java b/src/main/java/com/youpe/test/client/CustomCraftingScreenHandler.java new file mode 100644 index 0000000..8a6b2d5 --- /dev/null +++ b/src/main/java/com/youpe/test/client/CustomCraftingScreenHandler.java @@ -0,0 +1,199 @@ +// Source code is decompiled from a .class file using FernFlower decompiler. +package com.youpe.test.client; + +import java.util.Optional; +import net.minecraft.block.Blocks; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.CraftingInventory; +import net.minecraft.inventory.CraftingResultInventory; +import net.minecraft.inventory.Inventory; +import net.minecraft.inventory.RecipeInputInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket; +import net.minecraft.recipe.CraftingRecipe; +import net.minecraft.recipe.Recipe; +import net.minecraft.recipe.RecipeMatcher; +import net.minecraft.recipe.RecipeType; +import net.minecraft.recipe.book.RecipeBookCategory; +import net.minecraft.screen.AbstractRecipeScreenHandler; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerContext; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.screen.slot.CraftingResultSlot; +import net.minecraft.screen.slot.Slot; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.world.World; + +public class CustomCraftingScreenHandler extends AbstractRecipeScreenHandler { + public static final int field_30781 = 0; + private static final int field_30782 = 1; + private static final int field_30783 = 10; + private static final int field_30784 = 10; + private static final int field_30785 = 37; + private static final int field_30786 = 37; + private static final int field_30787 = 46; + private final RecipeInputInventory input; + private final CraftingResultInventory result; + private final ScreenHandlerContext context; + private final PlayerEntity player; + + public CustomCraftingScreenHandler(int syncId, PlayerInventory playerInventory) { + this(syncId, playerInventory, ScreenHandlerContext.EMPTY); + } + + public CustomCraftingScreenHandler(int syncId, PlayerInventory playerInventory, ScreenHandlerContext context) { + super(ScreenHandlerType.CRAFTING, syncId); + this.input = new CraftingInventory(this, 3, 3); + this.result = new CraftingResultInventory(); + this.context = context; + this.player = playerInventory.player; + this.addSlot(new CraftingResultSlot(playerInventory.player, this.input, this.result, 0, 124, 35)); + + int i; + int j; + for(i = 0; i < 3; ++i) { + for(j = 0; j < 3; ++j) { + this.addSlot(new Slot(this.input, j + i * 3, 30 + j * 18, 17 + i * 18)); + } + } + + for(i = 0; i < 3; ++i) { + for(j = 0; j < 9; ++j) { + this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); + } + } + + for(i = 0; i < 9; ++i) { + this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142)); + } + + } + + protected static void updateResult(ScreenHandler handler, World world, PlayerEntity player, RecipeInputInventory craftingInventory, CraftingResultInventory resultInventory) { + if (!world.isClient) { + ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity)player; + ItemStack itemStack = ItemStack.EMPTY; + Optional optional = world.getServer().getRecipeManager().getFirstMatch(RecipeType.CRAFTING, craftingInventory, world); + if (optional.isPresent()) { + CraftingRecipe craftingRecipe = (CraftingRecipe)optional.get(); + if (resultInventory.shouldCraftRecipe(world, serverPlayerEntity, craftingRecipe)) { + ItemStack itemStack2 = craftingRecipe.craft(craftingInventory, world.getRegistryManager()); + if (itemStack2.isItemEnabled(world.getEnabledFeatures())) { + itemStack = itemStack2; + } + } + } + + resultInventory.setStack(0, itemStack); + handler.setPreviousTrackedSlot(0, itemStack); + serverPlayerEntity.networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(handler.syncId, handler.nextRevision(), 0, itemStack)); + } + } + + public void onContentChanged(Inventory inventory) { + this.context.run((world, pos) -> { + updateResult(this, world, this.player, this.input, this.result); + }); + } + + public void populateRecipeFinder(RecipeMatcher finder) { + this.input.provideRecipeInputs(finder); + } + + public void clearCraftingSlots() { + this.input.clear(); + this.result.clear(); + } + + public boolean matches(Recipe recipe) { + return recipe.matches(this.input, this.player.getWorld()); + } + + public void onClosed(PlayerEntity player) { + super.onClosed(player); + this.context.run((world, pos) -> { + this.dropInventory(player, this.input); + }); + } + + public boolean canUse(PlayerEntity player) { + return canUse(this.context, player, Blocks.CRAFTING_TABLE); + } + + public ItemStack quickMove(PlayerEntity player, int slot) { + ItemStack itemStack = ItemStack.EMPTY; + Slot slot2 = (Slot)this.slots.get(slot); + if (slot2 != null && slot2.hasStack()) { + ItemStack itemStack2 = slot2.getStack(); + itemStack = itemStack2.copy(); + if (slot == 0) { + this.context.run((world, pos) -> { + itemStack2.getItem().onCraft(itemStack2, world, player); + }); + if (!this.insertItem(itemStack2, 10, 46, true)) { + return ItemStack.EMPTY; + } + + slot2.onQuickTransfer(itemStack2, itemStack); + } else if (slot >= 10 && slot < 46) { + if (!this.insertItem(itemStack2, 1, 10, false)) { + if (slot < 37) { + if (!this.insertItem(itemStack2, 37, 46, false)) { + return ItemStack.EMPTY; + } + } else if (!this.insertItem(itemStack2, 10, 37, false)) { + return ItemStack.EMPTY; + } + } + } else if (!this.insertItem(itemStack2, 10, 46, false)) { + return ItemStack.EMPTY; + } + + if (itemStack2.isEmpty()) { + slot2.setStack(ItemStack.EMPTY); + } else { + slot2.markDirty(); + } + + if (itemStack2.getCount() == itemStack.getCount()) { + return ItemStack.EMPTY; + } + + slot2.onTakeItem(player, itemStack2); + if (slot == 0) { + player.dropItem(itemStack2, false); + } + } + + return itemStack; + } + + public boolean canInsertIntoSlot(ItemStack stack, Slot slot) { + return slot.inventory != this.result && super.canInsertIntoSlot(stack, slot); + } + + public int getCraftingResultSlotIndex() { + return 0; + } + + public int getCraftingWidth() { + return this.input.getWidth(); + } + + public int getCraftingHeight() { + return this.input.getHeight(); + } + + public int getCraftingSlotCount() { + return 10; + } + + public RecipeBookCategory getCategory() { + return RecipeBookCategory.CRAFTING; + } + + public boolean canInsertIntoSlot(int index) { + return index != this.getCraftingResultSlotIndex(); + } +} diff --git a/src/main/java/com/youpe/test/client/CustomHandledScreen.java b/src/main/java/com/youpe/test/client/CustomHandledScreen.java index 5222153..d1861ef 100644 --- a/src/main/java/com/youpe/test/client/CustomHandledScreen.java +++ b/src/main/java/com/youpe/test/client/CustomHandledScreen.java @@ -14,6 +14,7 @@ import net.minecraft.client.texture.Sprite; import net.minecraft.client.util.InputUtil; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; +import net.minecraft.screen.PlayerScreenHandler; import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.slot.Slot; import net.minecraft.screen.slot.SlotActionType; @@ -30,7 +31,8 @@ import java.util.Set; @Environment(EnvType.CLIENT) public abstract class CustomHandledScreen extends Screen implements ScreenHandlerProvider { - public static final Identifier BACKGROUND_TEXTURE = new Identifier("testing:textures/inventory.png"); + // public static final Identifier BACKGROUND_TEXTURE = new Identifier("testing:textures/inventory.png"); + public static final Identifier BACKGROUND_TEXTURE = new Identifier("textures/gui/container/inventory.png"); private static final float field_32318 = 100.0F; private static final int field_32319 = 500; public static final int field_32322 = 100; @@ -41,7 +43,7 @@ public abstract class CustomHandledScreen extends Scree protected int titleY; protected int playerInventoryTitleX; protected int playerInventoryTitleY; - protected final T handler; + protected final PlayerScreenHandler handler; protected final Text playerInventoryTitle; @Nullable protected Slot focusedSlot; @@ -73,13 +75,13 @@ public abstract class CustomHandledScreen extends Scree private boolean doubleClicking; private ItemStack quickMovingStack; - public CustomHandledScreen(T handler, PlayerInventory inventory, Text title) { + public CustomHandledScreen(PlayerScreenHandler screenHandler, PlayerInventory inventory, Text title) { super(title); this.touchDragStack = ItemStack.EMPTY; this.touchDropReturningStack = ItemStack.EMPTY; this.cursorDragSlots = Sets.newHashSet(); this.quickMovingStack = ItemStack.EMPTY; - this.handler = handler; + this.handler = screenHandler; this.playerInventoryTitle = inventory.getDisplayName(); this.cancelNextRelease = true; this.titleX = 8; @@ -124,7 +126,6 @@ public abstract class CustomHandledScreen extends Scree this.drawForeground(context, mouseX, mouseY); ItemStack itemStack = this.touchDragStack.isEmpty() ? this.handler.getCursorStack() : this.touchDragStack; if (!itemStack.isEmpty()) { - boolean n = true; l = this.touchDragStack.isEmpty() ? 8 : 16; String string = null; if (!this.touchDragStack.isEmpty() && this.touchIsRightClickDrag) { @@ -188,7 +189,7 @@ public abstract class CustomHandledScreen extends Scree protected abstract void drawBackground(DrawContext context, float delta, int mouseX, int mouseY); - private void drawSlot(DrawContext context, Slot slot) { + private void drawSlot(DrawContext context, Slot slot) { int i = slot.x; int j = slot.y; ItemStack itemStack = slot.getStack(); @@ -254,7 +255,7 @@ public abstract class CustomHandledScreen extends Scree int i; int k; - for(Iterator var2 = this.cursorDragSlots.iterator(); var2.hasNext(); this.draggedStackRemainder -= k - i) { + for(Iterator var2 = this.cursorDragSlots.iterator(); var2.hasNext(); this.draggedStackRemainder -= k - i) { Slot slot = (Slot)var2.next(); ItemStack itemStack2 = slot.getStack(); i = itemStack2.isEmpty() ? 0 : itemStack2.getCount(); @@ -425,7 +426,7 @@ public abstract class CustomHandledScreen extends Scree } Slot slot2; - Iterator var13; + Iterator var13; if (this.doubleClicking && slot != null && button == 0 && this.handler.canInsertIntoSlot(ItemStack.EMPTY, slot)) { if (hasShiftDown()) { if (!this.quickMovingStack.isEmpty()) { @@ -606,7 +607,7 @@ public abstract class CustomHandledScreen extends Scree protected void handledScreenTick() { } - public T getScreenHandler() { + public PlayerScreenHandler getScreenHandler() { return this.handler; } diff --git a/src/main/java/com/youpe/test/client/CustomInventoryScreen.java b/src/main/java/com/youpe/test/client/CustomInventoryScreen.java index 7d43d9d..e67e110 100644 --- a/src/main/java/com/youpe/test/client/CustomInventoryScreen.java +++ b/src/main/java/com/youpe/test/client/CustomInventoryScreen.java @@ -16,14 +16,13 @@ import org.jetbrains.annotations.Nullable; import org.joml.Matrix4f; import org.joml.Quaternionf; -public class CustomInventoryScreen extends CustomAbstractInventoryScreen { +public class CustomInventoryScreen extends CustomAbstractInventoryScreen { private float mouseX; private float mouseY; - private boolean narrow; private boolean mouseDown; public CustomInventoryScreen(PlayerEntity player) { - super(player.playerScreenHandler, player.getInventory(), Text.translatable("container.crafting")); + super(new CustomPlayerScreenHandler(player.getInventory(), !player.getWorld().isClient(), player), player.getInventory(), Text.translatable("container.crafting")); this.titleX = 97; } @@ -47,6 +46,7 @@ public class CustomInventoryScreen extends CustomAbstractInventoryScreen getBackgroundSprite() { + return Pair.of(CustomPlayerScreenHandler.BLOCK_ATLAS_TEXTURE, CustomPlayerScreenHandler.EMPTY_ARMOR_SLOT_TEXTURES[equipmentSlot.getEntitySlotId()]); + } + }); + } + + for(i = 0; i < 3; ++i) { + for(j = 0; j < 9; ++j) { + this.addSlot(new Slot(inventory, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18)); + } + } + + for(i = 0; i < 9; ++i) { + this.addSlot(new Slot(inventory, i, 8 + i * 18, 142)); + } + + this.addSlot(new Slot(inventory, 40, 77, 62) { + public void setStack(ItemStack stack) { + CustomPlayerScreenHandler.onEquipStack(owner, EquipmentSlot.OFFHAND, stack, this.getStack()); + super.setStack(stack); + } + + public Pair getBackgroundSprite() { + return Pair.of(CustomPlayerScreenHandler.BLOCK_ATLAS_TEXTURE, CustomPlayerScreenHandler.EMPTY_OFFHAND_ARMOR_SLOT); + } + }); + } + + static void onEquipStack(PlayerEntity player, EquipmentSlot slot, ItemStack newStack, ItemStack currentStack) { + Equipment equipment = Equipment.fromStack(newStack); + if (equipment != null) { + player.onEquipStack(slot, currentStack, newStack); + } + + } + + public static boolean isInHotbar(int slot) { + return slot >= 36 && slot < 45 || slot == 45; + } + + public void populateRecipeFinder(RecipeMatcher finder) { + this.craftingInput.provideRecipeInputs(finder); + } + + public void clearCraftingSlots() { + this.craftingResult.clear(); + this.craftingInput.clear(); + } + + public boolean matches(Recipe recipe) { + return recipe.matches(this.craftingInput, this.owner.getWorld()); + } + + public void onContentChanged(Inventory inventory) { + CustomCraftingScreenHandler.updateResult(this, this.owner.getWorld(), this.owner, this.craftingInput, this.craftingResult); + } + + @SuppressWarnings("resource") + public void onClosed(PlayerEntity player) { + super.onClosed(player); + this.craftingResult.clear(); + if (!player.getWorld().isClient) { + this.dropInventory(player, this.craftingInput); + } + } + + public boolean canUse(PlayerEntity player) { + return true; + } + + public ItemStack quickMove(PlayerEntity player, int slot) { + ItemStack itemStack = ItemStack.EMPTY; + Slot slot2 = (Slot)this.slots.get(slot); + if (slot2.hasStack()) { + ItemStack itemStack2 = slot2.getStack(); + itemStack = itemStack2.copy(); + EquipmentSlot equipmentSlot = MobEntity.getPreferredEquipmentSlot(itemStack); + if (slot == 0) { + if (!this.insertItem(itemStack2, 9, 45, true)) { + return ItemStack.EMPTY; + } + + slot2.onQuickTransfer(itemStack2, itemStack); + } else if (slot >= 1 && slot < 5) { + if (!this.insertItem(itemStack2, 9, 45, false)) { + return ItemStack.EMPTY; + } + } else if (slot >= 5 && slot < 9) { + if (!this.insertItem(itemStack2, 9, 45, false)) { + return ItemStack.EMPTY; + } + } else if (equipmentSlot.getType() == Type.ARMOR && !((Slot)this.slots.get(8 - equipmentSlot.getEntitySlotId())).hasStack()) { + int i = 8 - equipmentSlot.getEntitySlotId(); + if (!this.insertItem(itemStack2, i, i + 1, false)) { + return ItemStack.EMPTY; + } + } else if (equipmentSlot == EquipmentSlot.OFFHAND && !((Slot)this.slots.get(45)).hasStack()) { + if (!this.insertItem(itemStack2, 45, 46, false)) { + return ItemStack.EMPTY; + } + } else if (slot >= 9 && slot < 36) { + if (!this.insertItem(itemStack2, 36, 45, false)) { + return ItemStack.EMPTY; + } + } else if (slot >= 36 && slot < 45) { + if (!this.insertItem(itemStack2, 9, 36, false)) { + return ItemStack.EMPTY; + } + } else if (!this.insertItem(itemStack2, 9, 45, false)) { + return ItemStack.EMPTY; + } + + if (itemStack2.isEmpty()) { + slot2.setStack(ItemStack.EMPTY); + } else { + slot2.markDirty(); + } + + if (itemStack2.getCount() == itemStack.getCount()) { + return ItemStack.EMPTY; + } + + slot2.onTakeItem(player, itemStack2); + if (slot == 0) { + player.dropItem(itemStack2, false); + } + } + + return itemStack; + } + + public boolean canInsertIntoSlot(ItemStack stack, Slot slot) { + return slot.inventory != this.craftingResult && super.canInsertIntoSlot(stack, slot); + } + + public int getCraftingResultSlotIndex() { + return 0; + } + + public int getCraftingWidth() { + return this.craftingInput.getWidth(); + } + + public int getCraftingHeight() { + return this.craftingInput.getHeight(); + } + + public int getCraftingSlotCount() { + return 5; + } + + public RecipeInputInventory getCraftingInput() { + return this.craftingInput; + } + + public RecipeBookCategory getCategory() { + return RecipeBookCategory.CRAFTING; + } + + public boolean canInsertIntoSlot(int index) { + return index != this.getCraftingResultSlotIndex(); + } + + static { + EMPTY_ARMOR_SLOT_TEXTURES = new Identifier[]{EMPTY_BOOTS_SLOT_TEXTURE, EMPTY_LEGGINGS_SLOT_TEXTURE, EMPTY_CHESTPLATE_SLOT_TEXTURE, EMPTY_HELMET_SLOT_TEXTURE}; + EQUIPMENT_SLOT_ORDER = new EquipmentSlot[]{EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET}; + } +} \ No newline at end of file diff --git a/src/main/java/com/youpe/test/client/TestScreenHandler.java b/src/main/java/com/youpe/test/client/TestScreenHandler.java new file mode 100644 index 0000000..a781a06 --- /dev/null +++ b/src/main/java/com/youpe/test/client/TestScreenHandler.java @@ -0,0 +1,61 @@ +package com.youpe.test.client; + +import com.youpe.test.Testing; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.Inventory; +import net.minecraft.inventory.SimpleInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.screen.slot.Slot; + +public class TestScreenHandler extends ScreenHandler { + private final Inventory inventory; + + public TestScreenHandler(int syncId, PlayerInventory playerInventory) { + this(syncId, playerInventory, new SimpleInventory(9)); + } + + public TestScreenHandler(int syncId, PlayerInventory playerInventory, SimpleInventory simpleInventory) { + super(Testing.BOX_SCREEN_HANDLER, syncId); + checkSize(inventory, 9); + this.inventory = inventory; + //some inventories do custom logic when a player opens it. + inventory.onOpen(playerInventory.player); + + //This will place the slot in the correct locations for a 3x3 Grid. The slots exist on both server and client! + //This will not render the background of the slots however, this is the Screens job + int m; + int l; + //Our inventory + for (m = 0; m < 3; ++m) { + for (l = 0; l < 3; ++l) { + this.addSlot(new Slot(inventory, l + m * 3, 62 + l * 18, 17 + m * 18)); + } + } + //The player inventory + for (m = 0; m < 3; ++m) { + for (l = 0; l < 9; ++l) { + this.addSlot(new Slot(playerInventory, l + m * 9 + 9, 8 + l * 18, 84 + m * 18)); + } + } + //The player Hotbar + for (m = 0; m < 9; ++m) { + this.addSlot(new Slot(playerInventory, m, 8 + m * 18, 142)); + } + + } + + @Override + public boolean canUse(PlayerEntity player) { + throw new UnsupportedOperationException("Unimplemented method 'canUse'"); + } + + @Override + public ItemStack quickMove(PlayerEntity player, int slot) { + throw new UnsupportedOperationException("Unimplemented method 'quickMove'"); + } + +} diff --git a/src/main/java/com/youpe/test/event/DirtBrokenAfter.java b/src/main/java/com/youpe/test/event/DirtBrokenAfter.java index c1c26e5..458eb1b 100644 --- a/src/main/java/com/youpe/test/event/DirtBrokenAfter.java +++ b/src/main/java/com/youpe/test/event/DirtBrokenAfter.java @@ -3,7 +3,6 @@ package com.youpe.test.event; import com.youpe.test.networking.ModNetworkingManager; import com.youpe.test.server.PlayerData; import com.youpe.test.server.StateSaverAndLoader; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; diff --git a/src/main/java/com/youpe/test/event/KeyInputHandler.java b/src/main/java/com/youpe/test/event/KeyInputHandler.java index 4af7422..69e28b9 100644 --- a/src/main/java/com/youpe/test/event/KeyInputHandler.java +++ b/src/main/java/com/youpe/test/event/KeyInputHandler.java @@ -4,8 +4,6 @@ import com.youpe.test.Testing; import com.youpe.test.client.CustomInventoryScreen; import org.lwjgl.glfw.GLFW; -import com.youpe.test.client.GUI; - import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.minecraft.client.MinecraftClient; @@ -20,6 +18,7 @@ public class KeyInputHandler { public static KeyBinding modkey; + @SuppressWarnings("resource") public static void registerKeyInputs() { registerKeyBindings(); ClientTickEvents.END_CLIENT_TICK.register(client -> { diff --git a/src/main/java/com/youpe/test/item/custom/CustomItem.java b/src/main/java/com/youpe/test/item/custom/CustomItem.java index d7e4112..da6433f 100644 --- a/src/main/java/com/youpe/test/item/custom/CustomItem.java +++ b/src/main/java/com/youpe/test/item/custom/CustomItem.java @@ -7,7 +7,6 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Formatting; diff --git a/src/main/java/com/youpe/test/networking/ModNetworkingManager.java b/src/main/java/com/youpe/test/networking/ModNetworkingManager.java index 84c8319..9e2aa03 100644 --- a/src/main/java/com/youpe/test/networking/ModNetworkingManager.java +++ b/src/main/java/com/youpe/test/networking/ModNetworkingManager.java @@ -5,7 +5,6 @@ import com.youpe.test.Testing; import com.youpe.test.networking.packet.DirtBrokenS2CPacket; import com.youpe.test.networking.packet.InitSyncS2CPacket; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.util.Identifier; public class ModNetworkingManager {