Klarks Posted January 16, 2021 Share Posted January 16, 2021 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 Link to comment Share on other sites More sharing options...
poopoodice Posted January 16, 2021 Share Posted January 16, 2021 Override MobEntity#func_230254_b_ and the you can open the container via NetworkHooks.openGui(). Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 (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, 2021 by Klarks Quote Link to comment Share on other sites More sharing options...
poopoodice Posted January 17, 2021 Share Posted January 17, 2021 Make a new instance of your container. Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 like this? NetworkHooks.openGui((ServerPlayerEntity) playerEntity,new MyModContainer(0,playerEntity.inventory)); Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 it stil doest work Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 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 Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 where i can get uuid. from player entity? Should my entity implement it? Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 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 Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 So what needs to be implemented? My Entity? Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 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 Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 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 Link to comment Share on other sites More sharing options...
Klarks Posted January 17, 2021 Author Share Posted January 17, 2021 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 Link to comment Share on other sites More sharing options...
poopoodice Posted January 17, 2021 Share Posted January 17, 2021 No, it is looking for a container provider, so just pass an instance of it. Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 18, 2021 Author Share Posted January 18, 2021 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 Link to comment Share on other sites More sharing options...
Klarks Posted January 18, 2021 Author Share Posted January 18, 2021 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 Link to comment Share on other sites More sharing options...
Klarks Posted January 18, 2021 Author Share Posted January 18, 2021 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 Link to comment Share on other sites More sharing options...
Klarks Posted January 18, 2021 Author Share Posted January 18, 2021 (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 January 18, 2021 by Klarks Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 18, 2021 Author Share Posted January 18, 2021 In my IContainerFactory? Where i can get it in my container class Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 19, 2021 Author Share Posted January 19, 2021 I don't understand. Where i should have container factory Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 19, 2021 Author Share Posted January 19, 2021 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())); Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 19, 2021 Author Share Posted January 19, 2021 why Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 19, 2021 Author Share Posted January 19, 2021 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); } }; Quote Link to comment Share on other sites More sharing options...
Klarks Posted January 19, 2021 Author Share Posted January 19, 2021 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); })); Quote Link to comment Share on other sites More sharing options...
poopoodice Posted January 19, 2021 Share Posted January 19, 2021 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.