Jump to content

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


Recommended Posts

Posted

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;
    }


}
Posted (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 by Klarks
Posted

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);
}
Posted

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());

}
Posted
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 

Posted

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);
    }
}
Posted

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? 

Posted

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);

        }
Posted

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);

        }

 

Posted

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?
 

Posted (edited)

is it correct?

//MyModEntity     
public ITextComponent getDisplayName() {
         return new StringTextComponent("Name");
     }

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


 PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
 buf.writeVarInt(this.getEntityId());
NetworkHooks.openGui((ServerPlayerEntity) playerEntity,iNamedContainerProvider,(Consumer<PacketBuffer> buf);

 

and how can i get it in my container class

//MyModContainer
world.getEntityByID().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {

    addSlot(new SlotItemHandler(h,0,79,18));
});
Edited by Klarks
Posted

Thanks it seems to be working. 
But i didnt understand one thing. if this line sending the EntityId to my container

return new MyModContainer(id,playerInventory, world,extraDataWriter);

so what do that line with EntityId 

NetworkHooks.openGui((ServerPlayerEntity) playerEntity,iNamedContainerProvider, buf -> buf.writeInt(getEntityId()));

 

PacketBuffer extraDataWriter = new PacketBuffer(Unpooled.buffer());

 extraDataWriter.writeInt(getEntityId());
 INamedContainerProvider iNamedContainerProvider = new INamedContainerProvider() {
     @Override
     public ITextComponent getDisplayName() {
         return new StringTextComponent("loh");
     }

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



NetworkHooks.openGui((ServerPlayerEntity) playerEntity,iNamedContainerProvider, buf -> buf.writeInt(getEntityId()));
Posted

I get it but my container have a data parametr now
 

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

    this.playerInventory = new InvWrapper(playerInventory);


    world.getEntityByID( data.readInt()).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {

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

 

and my anonymous inner class asks for data parametr  too. if i'll leave it null container wont work

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

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

how else i can get my id. 

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

    this.playerInventory = new InvWrapper(playerInventory);


    world.getEntityByID( data.readInt()).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {

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

 

 

public static final RegistryObject<ContainerType<MyModContainer>> MOD_CONTAINER = CONTAINERS.register("my_con", () -> IForgeContainerType.create((windowId, inv,data) -> {
            World world = inv.player.getEntityWorld();
            return new MyModContainer(windowId, inv,world,data);
        }));
Posted

Well, since 

    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity playerEntity <--------) {
        return new MyModContainer(id,playerInventory, world,extraDataWriter);
    }

you already have the player, why don't you just pass the player in?

Also getEntityByID is using entityID not UUID, use ServerWorld#getEntityByUuid/getPlayerByUuid although it is not necessary because you've got the player already.

 

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.