Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16.4] How i can open a container by clicking on my mob
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 2
Klarks

[1.16.4] How i can open a container by clicking on my mob

By Klarks, January 16 in Modder Support

  • Reply to this topic
  • Start new topic
  • Prev
  • 1
  • 2
  • Next
  • Page 1 of 2  

Recommended Posts

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 16

I know that i have to somehow open gui on server but i dont know what exactly i need to do

Container



public class MyModContainer extends Container implements INamedContainerProvider {
    private  IItemHandler playerInventory;

    protected MyModContainer(int id, PlayerInventory playerInventory) {
        super(RegObj.MOD_CONTAINER.get(), id);

        this.playerInventory = new InvWrapper(playerInventory);
        playerInventory.player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {

            addSlot(new SlotItemHandler(h,0,79,18));
        });
        layoutPlayerInventory(7,54);
    }


    private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx)
    {
        for(int i = 0; i < amount;i++)
        {
            addSlot(new SlotItemHandler(handler,index,x,y));
            x+=dx;
            index++;
        }
        return index;
    }

    private int addSlotBox(IItemHandler handler,int index,int x,int y,int horAmount,int dx,int verAmount,int dy)
    {
        for (int j = 0;j<verAmount;j++)
        {
            index = addSlotRange(handler,index,x,y,horAmount,dx);
            y+=dy;
        }
        return index;
    }

    private void layoutPlayerInventory(int leftCol, int topRow)
    {
        addSlotBox(playerInventory,9,leftCol,topRow,9,18,3,18);
        topRow += 58;
        addSlotRange(playerInventory,0,leftCol,topRow,9,18);
    }


    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return true;
    }

    @Override
    public ITextComponent getDisplayName() {
        return new StringTextComponent("MY_INV");
    }

    @Nullable
    @Override
    public Container createMenu(int p_createMenu_1_, PlayerInventory p_createMenu_2_, PlayerEntity p_createMenu_3_) {
        return null;
    }
}

 

Screen

public class MyModInv extends ContainerScreen<MyModContainer> {
    private static final int WIDTH =179;
    private static final int HEIGHT =151;

    private ResourceLocation GUI = new ResourceLocation(Main.MODID,"textures/gui/my_inv.png");

    public MyModInv(MyModContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
        super(screenContainer, inv, titleIn);
    }


    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        this.renderBackground(matrixStack);
        this.renderHoveredTooltip(matrixStack, mouseX, mouseY);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int x, int y) {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bindTexture(GUI);
        int i = (this.width - WIDTH) / 2;
        int j = (this.height - HEIGHT) / 2;
        this.blit(matrixStack, i, j, 0, 0, WIDTH, HEIGHT);
    }


    @Override
    public boolean isPauseScreen() {
        return false;
    }


}
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    118

poopoodice

poopoodice    118

  • Dragon Slayer
  • poopoodice
  • Members
  • 118
  • 928 posts
Posted January 16

Override MobEntity#func_230254_b_ and the you can open the container via NetworkHooks.openGui().

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17 (edited)

How i can get my container here

public ActionResultType func_230254_b_(PlayerEntity playerEntity, Hand hand) {
    if (!this.world.isRemote) {
       String en = getOwner();
       String pl = playerEntity.getScoreboardName();





     if(en.equals(pl))

     {
         NetworkHooks.openGui((ServerPlayerEntity) playerEntity,*container*);
     }
     else
         playerEntity.sendMessage(new TranslationTextComponent("You are not  "+en),Util.DUMMY_UUID);

    }

    return ActionResultType.func_233537_a_(this.world.isRemote);
}
Edited January 17 by Klarks
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    118

poopoodice

poopoodice    118

  • Dragon Slayer
  • poopoodice
  • Members
  • 118
  • 928 posts
Posted January 17

Make a new instance of your container.

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

like this?


 

NetworkHooks.openGui((ServerPlayerEntity) playerEntity,new MyModContainer(0,playerEntity.inventory));
  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

it stil doest work

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 17

Show what you have tried.

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

i am trying to show my gui by clicking on my entity

public ActionResultType func_230254_b_(PlayerEntity playerEntity, Hand hand) {
    if (!this.world.isRemote) {
       String en = getOwner();
       String pl = playerEntity.getScoreboardName();





     if(en.equals(pl))

     {
         NetworkHooks.openGui((ServerPlayerEntity) playerEntity,new MyModContainer(0,playerEntity.inventory));
     }
     else
         playerEntity.sendMessage(new TranslationTextComponent("You are not  "+en),Util.DUMMY_UUID);

    }

    return ActionResultType.func_233537_a_(this.world.isRemote);
}
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 17
  • Storing the username is a bad idea - you should use UUIDs. Usernames can and will change.
  • openGui requires an INamedContainerProvider. It looks like you made your container implement that, which doesn't make much sense.
  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

where i can get uuid. from player entity? Should my entity implement it?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 17
1 minute ago, Klarks said:

where i can get uuid. from player entity?

Yes, use getUniqueId instead of the name.

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

Is it ok that i am saving a player uuid as a string in datamanager

private static final DataParameter<String> OWNER = EntityDataManager.createKey(MyEntity.class, DataSerializers.STRING);


@OnlyIn(Dist.CLIENT)

public UUID getOwner() {
    return UUID.fromString(this.dataManager.get(OWNER));
}

public void setOwner(UUID name) {
    this.dataManager.set(OWNER ,name.toString());

}
  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

So what needs to be implemented? My Entity?

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 17

Do not use @OnlyIn.

 

1 hour ago, Klarks said:

So what needs to be implemented? My Entity?

I do not understand this question.

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17
7 hours ago, diesieben07 said:
  • openGui requires an INamedContainerProvider. It looks like you made your container implement that, which doesn't make much sense.

What should implement INamedContainerProvider 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 17
2 hours ago, Klarks said:

What should implement INamedContainerProvider 

Make a new class. An anonymous inner class is usually the most sensible choice here.

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

I dont quite understand what you mean by this. I created a new class. 

 

 

NetworkHooks.openGui((ServerPlayerEntity) playerEntity,new MyModContainer(0,new anonymousInnerClass()
{

},playerEntity.inventory));

 

 



public class anonymousInnerClass implements INamedContainerProvider {

    @Override
    public ITextComponent getDisplayName() {
        return new StringTextComponent("Name");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new MyModContainer(id,playerInventory);
    }
}
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 17

Cool. An anonymous inner class is a Java concept - please learn Java before modding.


What is your issue now?

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 17

I ve seen an example of it but i dont understand how i can apply this to my problem. Do I need to call createmenu from new class in networkhooks.opengui method? 

  • Quote

Share this post


Link to post
Share on other sites

poopoodice    118

poopoodice

poopoodice    118

  • Dragon Slayer
  • poopoodice
  • Members
  • 118
  • 928 posts
Posted January 17

No, it is looking for a container provider, so just pass an instance of it.

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 18

is it correct
 

INamedContainerProvider in = new INamedContainerProvider() {
    @Override
    public ITextComponent getDisplayName() {
        return new StringTextComponent("Name");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new MyModContainer(id,playerInventory);
    }
};

         if(en.equals(pl))

         {
             NetworkHooks.openGui((ServerPlayerEntity) playerEntity, in,(Consumer<PacketBuffer>) playerEntity.inventory);
         }
         else
             playerEntity.sendMessage(new TranslationTextComponent("You are not  "+en),Util.DUMMY_UUID);

        }
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 18
57 minutes ago, Klarks said:

(Consumer<PacketBuffer>) playerEntity.inventory

How do you expect this to work at all, ever?

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 18

With out it java gave an error "Cant resolve method openGui". What i did  wrong here?

1 hour ago, Klarks said:


 


INamedContainerProvider in = new INamedContainerProvider() {
    @Override
    public ITextComponent getDisplayName() {
        return new StringTextComponent("Name");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new MyModContainer(id,playerInventory);
    }
};

         if(en.equals(pl))

         {
             NetworkHooks.openGui((ServerPlayerEntity) playerEntity, in,(Consumer<PacketBuffer>) playerEntity.inventory);
         }
         else
             playerEntity.sendMessage(new TranslationTextComponent("You are not  "+en),Util.DUMMY_UUID);

        }

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56420 posts
Posted January 18

Look at the parameters of openGui. Then give it the correct objects.

  • Quote

Share this post


Link to post
Share on other sites

Klarks    0

Klarks

Klarks    0

  • Stone Miner
  • Klarks
  • Members
  • 0
  • 95 posts
Posted January 18

Ok i messed up few things, idk where did i get a player inventory parameter in openGui. So thanks it's working now
But how i can correctly get the entity inside the container class to get capabilities to add a slot. I know how to do it with the player i can just get the player from the player inventory but with a mob i am not sure. Should i get the entity from world somehow?
 

  • Quote

Share this post


Link to post
Share on other sites
  • Prev
  • 1
  • 2
  • Next
  • Page 1 of 2  

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 2
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • diesieben07
      When I try to run my mod after I just added Deferred Registries it crashes

      By diesieben07 · Posted 3 minutes ago

      You need to change one of them, yes.
    • ThisIsNotOriginal
      When I try to run my mod after I just added Deferred Registries it crashes

      By ThisIsNotOriginal · Posted 11 minutes ago

      so do i change the name in my ItemsList?
    • diesieben07
      [1.16.5] Help with custom Backpack (slot background and mouse wheel move)

      By diesieben07 · Posted 21 minutes ago

      I do not understand. How is the offhand slot relevant regarding the background color of your slot?   Assuming you want a keyboard key to open the GUI: Make a KeyBinding. Use ClientTickEvent to check if its pressed. If it is, send a packet to the server, which then calls NetworkHooks#openGui. You can preemptively also open it on the client to hide the latency of the two network roundtrips.
    • diesieben07
      When I try to run my mod after I just added Deferred Registries it crashes

      By diesieben07 · Posted 23 minutes ago

      That is not how you make a Git repo. At all.   This creates a BlockItem for all of your blocks. One of your blocks has the registry name "copper_ore", hence this will create a BlockItem with registry name "copper_ore". This then creates another Item with registry name "copper_ore". This is not allowed, registry names must be unique.
    • Extrupt
      Forge won't fully download

      By Extrupt · Posted 25 minutes ago

      thanks for the help! it worked!
  • Topics

    • ThisIsNotOriginal
      19
      When I try to run my mod after I just added Deferred Registries it crashes

      By ThisIsNotOriginal
      Started 16 hours ago

    • Luis_ST
      3
      [1.16.5] Help with custom Backpack (slot background and mouse wheel move)

      By Luis_ST
      Started Yesterday at 07:47 AM

    • Extrupt
      5
      Forge won't fully download

      By Extrupt
      Started 57 minutes ago

    • HDMI_Steckdose
      1
      Forge error Forge won't open

      By HDMI_Steckdose
      Started 32 minutes ago

    • Retsal
      7
      [1.16.x] Custom block as a shop

      By Retsal
      Started Yesterday at 01:04 PM

  • Who's Online (See full list)

    • PlasmaPig13
    • diesieben07
    • WooBoo
    • Katie328
    • ThisIsNotOriginal
    • reu_24
    • troublemaker_47
    • CAS_ual_TY
    • Extrupt
    • Coloringgamer
    • HDMI_Steckdose
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16.4] How i can open a container by clicking on my mob
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community