Jump to content

Recommended Posts

Posted (edited)

Hey,

how can i associate my custom ContainerScreen to my own entity? Like horse inventory.

 

In order to when i click E to open inventory my custom container should display. But that doesn't append.

 

I have already register the screen in the ScreenManager.

 

Here is my custom entity:


public class RidableDolphinEnitity extends DolphinEntity implements INamedContainerProvider {

    protected Inventory inventory = new Inventory(2);

    public RidableDolphinEnitity(EntityType<RidableDolphinEnitity> type, World worldIn) {
        super(type, worldIn);
    }


    public Inventory getInventory() {
        return inventory;
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory inventory, PlayerEntity player) {
        return new RidableDolphinContainer(id, inventory, getInventory());

    }

    @Override
    public ITextComponent getDisplayName() {
        return TextComponentUtils.toTextComponent(() -> "Dolphin Inventory");
    }
}

 

 

Edited by ArcaneDiver
Posted

I tried to implement this by modifing the container on GuiOpenEvent and PlayerContainerEvent.Open  but i sadly discovered that  PlayerContainerEvent.Open doesn't fire for the Container of the player then how can i say "when i press E open my container"? 

 

Here is the code of events:

@SubscribeEvent
    public static void onDolphinContainerOpen(PlayerContainerEvent.Open event) {
		// Never fires :C
        ServerPlayerEntity playerEntity = (ServerPlayerEntity) event.getPlayer();

        if(playerEntity.getRidingEntity() instanceof RidableDolphinEnitity) {
            RidableDolphinEnitity dolphin = (RidableDolphinEnitity) playerEntity.getRidingEntity();


            playerEntity.openContainer = new RidableDolphinContainer(playerEntity.currentWindowId, playerEntity.inventory, dolphin.getInventory());
        }
    }

    @SubscribeEvent
    public static void onDolphinGuiOpen(GuiOpenEvent event) {
        if(event.getGui() instanceof ContainerScreen) {
            ClientPlayerEntity player = Minecraft.getInstance().player;

            if (player != null && player.getRidingEntity() instanceof RidableDolphinEnitity) {

                RidableDolphinEnitity dolphin = (RidableDolphinEnitity) player.getRidingEntity();

                if(!(event.getGui() instanceof RidableDolphinScreen)) {

                    ContainerScreen<?> oldScreen = (ContainerScreen) event.getGui();
                    Container oldContainer = oldScreen.getContainer();

                    RidableDolphinContainer container = new RidableDolphinContainer(oldContainer.windowId, player.inventory, dolphin.getInventory());
                    RidableDolphinScreen screen = new RidableDolphinScreen(container, player.inventory, dolphin.getDisplayName());

                    player.openContainer = container;
                    event.setGui(screen);

                }
            }
        }
    }

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.