• Recently Browsing

    No registered users viewing this page.

  • Posts

    • That was where my confusion layed. There was no mention of SlotItemHandler anywhere whatsoever on the capabilities page, or really anywhere I did prior research before creating this thread. Knowing this now, it makes more sense and I've since resolved the issue No error, slot has been added and all works as intended. Do keep in mind that I'm going into these APIs entirely blind so I'm unfamiliar with them. What may seem simple to you in Forge 1.14 I have yet to learn. I appreciate the assistance.   I needed an instance of the horse from both the container and the screen when registering the slot in order to grab its capabilities, so that as well I had to retrieve reflectively. With all those changes being made, I've finalized on the following (and will be making changes to ensure the reflection is properly cached). private static final ResourceLocation HORSE_GUI_TEXTURES = new ResourceLocation("textures/gui/container/horse.png"); private static final Function<IItemHandler, Slot> SLOT_CREATOR = i -> new SlotItemHandler(i, 0, 8, 54); @SubscribeEvent public static void attachHorseCapabilities(AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof AbstractHorseEntity) { event.addCapability(new ResourceLocation(MOD_ID, "my_custom_capability_id"), new MyCustomIItemHandlerProvider()); } } @SubscribeEvent public static void addSlotServerSide(PlayerContainerEvent.Open event) { Container container = event.getContainer(); if (container instanceof HorseInventoryContainer) { AbstractHorseEntity horse = getHorseFromField(container, "horse"); IItemHandler itemHandler = horse.getCapability(MyCustomCapabilities.MY_CUSTOM_CAPABILITY).orElseThrow(IllegalStateException::new); addSlotToHorseContainer((HorseInventoryContainer) container, itemHandler, SLOT_CREATOR); } } @SubscribeEvent public static void addSlotClientSide(GuiOpenEvent event) { Screen screen = event.getGui(); if (screen instanceof HorseInventoryScreen) { AbstractHorseEntity horse = getHorseFromField(screen, "horseEntity"); IItemHandler itemHandler = horse.getCapability(MyCustomCapabilities.MY_CUSTOM_CAPABILITY).orElseThrow(IllegalStateException::new); addSlotToHorseContainer(((HorseInventoryScreen) screen).getContainer(), itemHandler, SLOT_CREATOR); } } @SubscribeEvent public static void renderExtraSlot(GuiContainerEvent.DrawBackground event) { ContainerScreen<?> screen = event.getGuiContainer(); if (screen instanceof HorseInventoryScreen) { GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F); Minecraft.getInstance().getTextureManager().bindTexture(HORSE_GUI_TEXTURES); int ySize = screen.getYSize(); int i = (screen.width - screen.getXSize()) / 2; int j = (screen.height - ySize) / 2; // screen x, screen y, texture x, texture y, width, height screen.blit(i + 7, j + 53, 0, ySize, 18, 18); } } private static void addSlotToHorseContainer(HorseInventoryContainer container, IItemHandler handler, Function<IItemHandler, Slot> slotCreator) { try { Method methodAddSlot = Container.class.getDeclaredMethod("addSlot", Slot.class); methodAddSlot.setAccessible(true); methodAddSlot.invoke(container, slotCreator.apply(handler)); } catch (ReflectiveOperationException e) { e.printStackTrace(); } } private static AbstractHorseEntity getHorseFromField(Object instance, String fieldName) { try { Field field = instance.getClass().getDeclaredField(fieldName); return (AbstractHorseEntity) ReflectionUtil.getFieldValue(field, instance); } catch (ReflectiveOperationException e) { e.printStackTrace(); } return null; } } If, aside from reflection caching, there is anything you feel needs changing, I'm open to critiques. Of course the name of my mod and a few classes were renamed. "MyCustomCapabilities" and other obviously poorly named classes and constants are named much more appropriately.
    • If only the Slot constructor had a way to tell it which inventory to use... Oh, it does. It has an IInventory parameter. Use SlotItemHandler for the IItemHandler variant.   Incorrect. The slot modifies whatever inventory you give it.   Yes.   If it's the horse inventory, then how is the slot supposed to know that it should look anywhere else?
    • I do have an ItemStackHandler and it is attached  It's actually attached in that same class (it was something I left out because it was irrelevant at the time). @SubscribeEvent public static void attachHorseCapabilities(AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof AbstractHorseEntity) { event.addCapability(new ResourceLocation(MOD_ID, "capability_mycustomslots"), new ThisIsntTheClassNameButYouGetTheIdea()); } } It's implemented correctly, supplies an additional slot index, serializes and deserializes to/from NBT, sure. Here's my confusion. How am I supposed to say "Hey, when you interact with this slot, you should be modifying this capability" because adding a slot reflectively to the horse container modifies what I assume is the horse inventory, correct? Should I be passing that capability somewhere so my slot knows to interact with that capability instead of the default, vanilla inventoryContents supplied by the inventory? I see only the IInventory passed to the Slot but as far as I'm concerned, that should be the horse inventory.
    • Since you said this, I assumed you had already done that... You need a capability attached to horses, which stores an IItemHandler for your additional inventory space.
    • Correct, although that was my question to begin with. How am I meant to modify the inventory of an existing entity? I was under the impression that this would be addressed as well, though that mustn't be the case. The way you had described it in your initial reply had made it seem like the slots would have allowed the user to interact with them. Perhaps I misunderstood. I understand I am interacting with the 3rd slot (index 2) and I understand why I'm getting this exception, I'm just unsure how to address it. Overriding one of the existing slot indexes surely isn't an option. I want to maintain the vanilla functionality of a horse.   So to rephrase the question in the OP, what would be the correct approach to add a slot to a horse inventory with which a player may interact and place/take items?
  • Topics

  • Who's Online (See full list)