• Recently Browsing

    No registered users viewing this page.

  • Posts

    • I'm pretty sure I've left some comments on his videos about the mistakes he's perpetuating, but its not like anyone reads those.
    • You should use ObfuscationReflectionHelper for your reflection code and use SRG names, otherwise your reflection code won't work outside the development environment.
    • 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.
  • Topics

  • Who's Online (See full list)