Jump to content

[1.10.2] data beetween gui and tileEntity


personnedu13

Recommended Posts

Hi,

I want to create a complex tileentity  updated when a button is pressed in the gui.

But I've got some problems and I just dont understand how set the tileentitydata from the gui.

Here is my class :

I just want to send and receive the sideout value beetween gui and tileentity.

GuiTeleporter :

Spoiler

package com.personnedu13.test.client.gui;

@SideOnly(Side.CLIENT)
public class GuiTeleporter extends GuiContainer{
    
    private static final ResourceLocation TRANSLOCATOR_GUI_TEXTURES = new ResourceLocation(Main.MODID + ":textures/gui/container/teleporter.png");
    private final IInventory tileTeleporter;
    
    public GuiTeleporter(IInventory playerInv, IInventory teInv) {
        super(new ContainerTeleporter(playerInv, teInv));
        this.tileTeleporter = teInv;
        this.xSize = 176;
        this.ySize = 185;
    }
    
    
    public void initGui() {
        super.initGui();
        this.buttonList.add(new NorthButton(0, 24 + this.guiLeft, 26 + this.guiTop));
        this.buttonList.add(new WestButton(1, 12 + this.guiLeft, 38 + this.guiTop));
        this.buttonList.add(new SouthButton(2, 24 + this.guiLeft, 55 + this.guiTop));
        this.buttonList.add(new EastButton(3, 41 + this.guiLeft, 38 + this.guiTop));
        this.buttonList.add(new TopButton(4, 24 + this.guiLeft, 38 + this.guiTop));
        
        int i = this.tileTeleporter.getField(0);
        GuiTeleporter.DirectionnalButton j = (GuiTeleporter.DirectionnalButton) buttonList.get(i);
        j.setSelected(true);
    }
    
    public void updateScreen() {
        super.updateScreen();
    }
    
    protected void actionPerformed(GuiButton button) throws IOException {
        
        if (button instanceof DirectionnalButton) {
            GuiTeleporter.DirectionnalButton dirButton = (GuiTeleporter.DirectionnalButton)button;
            if (dirButton.isSelected())
                return;
            this.tileTeleporter.setField(0, dirButton.id);
            this.buttonList.clear();
            this.initGui();
            ModPacketHandler.INSTANCE.sendToServer(new UpdateTe());
        }
    }
    
    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(TRANSLOCATOR_GUI_TEXTURES);
        int i = (this.width - this.xSize) / 2;
        int j = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
    }
    
    //==========Button class==========
    @SideOnly(Side.CLIENT)
    static class SelectableButton extends GuiButton {
    
        private final int textureX;
        private final int textureY;
    
        boolean selected = false;
        protected SelectableButton(int buttonId, int x, int y, int width, int height, int textureXIn, int textureYIn) {
            super(buttonId, x , y, width, height, "");
            this.textureX = textureXIn;
            this.textureY = textureYIn;
        }
    
        public void drawButton(Minecraft mc, int mouseX, int mouseY)
        {
            if (this.visible)
            {
                mc.getTextureManager().bindTexture(GuiTeleporter.TRANSLOCATOR_GUI_TEXTURES);
                GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
                this.hovered = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
                int i = this.textureX;
                int j = this.textureY;
                if (!this.enabled)
                {
                    j += this.height * 3;
                }
                else if (this.selected)
                {
                    j += this.height * 2;
                }
                else if (this.hovered)
                {
                    j += this.height * 1;
                }
                this.drawTexturedModalRect(this.xPosition, this.yPosition, i, j, this.width, this.height);
            }
        }
        public boolean isSelected()
        {
            return this.selected;
        }
        public void setSelected(boolean selectedIn)
        {
            this.selected = selectedIn;
        }
    }
    class DirectionnalButton extends GuiTeleporter.SelectableButton {
        protected DirectionnalButton(int buttonId, int x, int y, int width, int height, int textureXIn, int textureYIn) {
            super(buttonId, x, y, width, height, textureXIn, textureYIn);
        }
    }
    
    @SideOnly(Side.CLIENT)
    class NorthButton extends GuiTeleporter.DirectionnalButton {
        public NorthButton(int buttonId, int x, int y) {
            super(buttonId, x, y, 15, 10, 211, 0);
        }
    }
    
    @SideOnly(Side.CLIENT)
    class WestButton extends GuiTeleporter.DirectionnalButton {
        public WestButton(int buttonId, int x, int y) {
            super(buttonId, x, y, 10, 15, 201, 0);
        }
    }
    
    @SideOnly(Side.CLIENT)
    class SouthButton extends GuiTeleporter.DirectionnalButton {
        public SouthButton(int buttonId, int x, int y) {
            super(buttonId, x, y, 15, 10, 226, 0);
        }
    }
    
    @SideOnly(Side.CLIENT)
    class EastButton extends GuiTeleporter.DirectionnalButton {
        public EastButton(int buttonId, int x, int y) {
            super(buttonId, x, y, 10, 15, 191, 0);
        }
    }
    
    @SideOnly(Side.CLIENT)
    class TopButton extends GuiTeleporter.DirectionnalButton {
        public TopButton(int buttonId, int x, int y) {
            super(buttonId, x, y, 15, 15, 176, 0);
        }
    }
}

 

 

TileEntityTeleporter :

Spoiler

public class TileEntityTeleporter  extends TileEntity implements ITickable, IInventory, IInteractionObject {
    
    private ItemStack EMPTY = new ItemStack((Item)null);
    
    private int sideout;
    private ItemStack[] inventory;
    private String customName;
    
    public TileEntityTeleporter() {
        this.inventory = new ItemStack[this.getSizeInventory()] ;
        this.sideout = 4;
    }
    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        
        NBTTagList list = nbt.getTagList("Items", 10);
           for (int i = 0; i < list.tagCount(); ++i) {
               NBTTagCompound stackTag = list.getCompoundTagAt(i);
               int slot = stackTag.getByte("Slot") & 255;
               this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
           }
        this.sideout = nbt.getInteger("SideOut");
    }
    
    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        NBTTagList list = new NBTTagList();
        for (int i = 0; i < this.getSizeInventory(); i++) {
            if (this.getStackInSlot(i) != null) {
                NBTTagCompound stackTag = new NBTTagCompound();
                stackTag.setByte("Slot", (byte) i);
                this.getStackInSlot(i).writeToNBT(stackTag);
                list.appendTag(stackTag);
            }
        }
        
        nbt.setTag("Items", list);
        nbt.setInteger("SideOut", this.sideout);
        
        return super.writeToNBT(nbt);
        
    }
    @Override
    public void update() {
        if (this.worldObj != null) {
            if (!this.worldObj.isRemote) {
                System.out.println(sideout);
            }
        }
    }
    
    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
        NBTTagCompound nbt = new NBTTagCompound();
        this.writeToNBT(nbt);
        int metadata = getBlockMetadata();
        return new SPacketUpdateTileEntity(this.pos, metadata, nbt);
    }
    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
        this.readFromNBT(pkt.getNbtCompound());
    }
    @Override
    public NBTTagCompound getUpdateTag() {
        NBTTagCompound nbt = new NBTTagCompound();
        this.writeToNBT(nbt);
        return nbt;
    }
    @Override
    public void handleUpdateTag(NBTTagCompound tag) {
        this.readFromNBT(tag);
    }
    @Override
    public NBTTagCompound getTileData() {
        NBTTagCompound nbt = new NBTTagCompound();
        this.writeToNBT(nbt);
        return nbt;
    }
    
    
    public String getCustomName() {
        return this.customName;
    }
    public void setCustomName(String customName) {
        this.customName = customName;
    }
    
    public static void getSideOut() {
        
    }
    
    public final void setSideOut(int id) {
        this.sideout = id;
        this.markDirty();
    }
    
    @Override
    public String getName() {
        return this.hasCustomName() ? this.customName : "container.tileentity_tile_entity";
    }
    @Override
    public boolean hasCustomName() {
        return this.customName != null && !this.customName.equals("");
    }
    
    @Override
    public ITextComponent getDisplayName()
    {
        return (ITextComponent)(this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName(), new Object[0]));
    }

    @Override
    public int getSizeInventory() {
        return 2;
    }
    private boolean isInventoryEmpty()
    {
        for (int i = 0; i < this.getInventoryStackLimit();i++)
        {
            if (!(this.inventory[i].equals(null)))
            {
                return false;
            }
        }
        return true;
    }
    
    public boolean isEmpty()
    {
        return this.isInventoryEmpty();
    }
    @Override
    public ItemStack getStackInSlot(int index) {
        return this.inventory[index];
    }
    @Override
    public ItemStack decrStackSize(int index, int count) {
        ItemStack itemstack = ItemStackHelper.getAndSplit(this.inventory, index, count);
        this.markDirty();
        return itemstack;
    }
    @Override
    public ItemStack removeStackFromSlot(int index) {
        ItemStack itemstack = ItemStackHelper.getAndRemove(this.inventory, index);
        this.markDirty();
        return itemstack;
    }
    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        if (index < 0 || index >= this.getSizeInventory()) {
            System.out.println("ERROR : INDEX INADAPTé : " + index);
            return;
        }
        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
            stack.stackSize = this.getInventoryStackLimit();
            
        if (stack != null && stack.stackSize == 0)
            stack = null;
        this.inventory[index] = stack;
        this.markDirty();
    }
    @Override
    public int getInventoryStackLimit() {
        return 1;
    }
    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
    }
    @Override
    public void openInventory(EntityPlayer player) {
        
    }
    @Override
    public void closeInventory(EntityPlayer player) {
        
    }
    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return true;
    }
    @Override
    public int getField(int id)
    {
        switch (id)
        {
            case 0:
                return sideout;
            default:
                return 0;
        }
    }
    @Override
    public void setField(int id, int value)
    {
        switch (id)
        {
            case 0:
                sideout = value;
                break;
        default:
            break;
        }
    }
    @Override
    public int getFieldCount()
    {
        return 1;
    }
    @Override
    public void clear() {
        for (int i = 0; i < this.getSizeInventory(); i++)
            this.setInventorySlotContents(i, null);
    }
    @Override
    public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
        return new ContainerTeleporter(playerInventory, this);
    }
    @Override
    public String getGuiID() {
        return Main.MODID + ":translocator";
    }
}

 

 

and ContainerTelepoter :

Spoiler


public class ContainerTeleporter extends Container {

	private IInventory te;
	
	public ContainerTeleporter(IInventory playerInv,IInventory te) {
		this.te = te;
		this.addSlotToContainer(new Slot(te, 0 , 8 , 72));
		this.addSlotToContainer(new Slot(te, 1 , 26, 72));

        for (int y = 0; y < 3; y++)
        {
            for (int x = 0; x < 9; x++)
            {
                this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 ));
            }
        }

        for (int x = 0; x < 9; x++)
        {
            this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 161));
        }
	}
	
	public void addListener(IContainerListener listener)
	   {
	        super.addListener(listener);
	        listener.sendAllWindowProperties(this, this.te);
	   }
	
	@SideOnly(Side.CLIENT)
    public void updateProgressBar(int id, int data)
    {
        this.te.setField(id, data);
    }

    public void onContainerClosed(EntityPlayer playerIn)
    {
        super.onContainerClosed(playerIn);
    }
    
	public IInventory getTileEntity()
    {
        return this.te;
    }
	
	@Override
    public boolean canInteractWith(EntityPlayer playerIn) {
		return !playerIn.isSpectator();
    }

	@Override
	public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
	    ItemStack previous = null;
	    Slot slot = (Slot) this.inventorySlots.get(fromSlot);

	    if (slot != null && slot.getHasStack()) {
	        ItemStack current = slot.getStack();
	        previous = current.copy();

	        if (fromSlot < 2) {
	        	 // From Player Inventory to TE Inventory
	            if (!this.mergeItemStack(current, 0, 1, false))
	                return null;
	            
	        } else {
	        	// From TE Inventory to Player Inventory
	            if (!this.mergeItemStack(current, 2, 38, true))
	                return null;
	        }

	        if (current.stackSize == 0)
	            slot.putStack((ItemStack) null);
	        else
	            slot.onSlotChanged();

	        if (current.stackSize == previous.stackSize)
	            return null;
	        slot.onPickupFromSlot(playerIn, current);
	    }
	    return previous;
	}
}

 

Help !

Link to comment
Share on other sites

here is the old code :

Spoiler

public class UpdateTe implements IMessage {
    private BlockPos blockPos;
    private int integer;
    
    @Override
    public void fromBytes(ByteBuf buf) {
        blockPos = new BlockPos (buf.readInt(), buf.readInt(), buf.readInt());
        integer = buf.readInt();
    }
    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(blockPos.getX());
        buf.writeInt(blockPos.getY());
        buf.writeInt(blockPos.getZ());
        buf.writeInt(integer);
    }
    public UpdateTe() {
        integer = GuiTeleporter.getSideOut;
        blockPos = GuiTeleporter.getPos();
    }
    public static class Handler implements IMessageHandler<UpdateTe, IMessage> {
        @Override
        public IMessage onMessage(UpdateTe message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }
        private void handle(UpdateTe message, MessageContext ctx) {
            EntityPlayerMP player = ctx.getServerHandler().playerEntity;
            World world = player.worldObj;
            TileEntityTeleporter te = (TileEntityTeleporter) world.getTileEntity(message.blockPos);
            te.setField(0, integer);
        }
    }
}

 

Any idea for a syntax ?

Edited by personnedu13
Link to comment
Share on other sites

9 minutes ago, Jay Avery said:

What is this? It's not even valid Java, this code should not compile. You are trying to call a field and a method which do not exist.

this is the old version of my code, in the actual code, I just reset the packet and this code is how the old code look like.

the actual is empty and look like this : 

Spoiler

public class UpdateTe implements IMessage {
    
    
    @Override
    public void fromBytes(ByteBuf buf) {
        
    }
    @Override
    public void toBytes(ByteBuf buf) {
      
    }
    public UpdateTe() {
    }
    public static class Handler implements IMessageHandler<UpdateTe, IMessage> {
        @Override
        public IMessage onMessage(UpdateTe message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }
        private void handle(UpdateTe message, MessageContext ctx) {
            
        }
    }
}

 

How should I wrote the packet if I want to send to the TileEntity the actual side ?

Edited by personnedu13
Link to comment
Share on other sites

Here is my code if someone want an example :

GuiTeleporter :

Spoiler

package com.personnedu13.test.client.gui;

import java.io.IOException;

import com.personnedu13.test.Main;
import com.personnedu13.test.inventory.ContainerTeleporter;
import com.personnedu13.test.network.ModPacketHandler;
import com.personnedu13.test.network.UpdateTe;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GuiTeleporter extends GuiContainer{
	
    private static final ResourceLocation TRANSLOCATOR_GUI_TEXTURES = new ResourceLocation(Main.MODID + ":textures/gui/container/translocator.png");

	private final IInventory tileTeleporter;
	private final BlockPos pos;
	
	public GuiTeleporter(IInventory playerInv, IInventory teInv, BlockPos block) {
		super(new ContainerTeleporter(playerInv, teInv));
		this.tileTeleporter = teInv;
		this.pos = block;
		this.xSize = 176;
		this.ySize = 185;
	}
	
	
    public void initGui() {
    	super.initGui();
    	this.buttonList.add(new NorthButton(0, 24 + this.guiLeft, 26 + this.guiTop));
    	this.buttonList.add(new WestButton(1, 12 + this.guiLeft, 38 + this.guiTop));
    	this.buttonList.add(new SouthButton(2, 24 + this.guiLeft, 55 + this.guiTop));
    	this.buttonList.add(new EastButton(3, 41 + this.guiLeft, 38 + this.guiTop));
    	this.buttonList.add(new TopButton(4, 24 + this.guiLeft, 38 + this.guiTop));
    	
    	int i = this.tileTeleporter.getField(0);
    	GuiTeleporter.DirectionnalButton j = (GuiTeleporter.DirectionnalButton) buttonList.get(i);
    	j.setSelected(true);
    }
    
    public void updateScreen() {
    	super.updateScreen();
    }
    
    protected void actionPerformed(GuiButton button) throws IOException {
    	
    	if (button instanceof DirectionnalButton) {
    		GuiTeleporter.DirectionnalButton dirButton = (GuiTeleporter.DirectionnalButton)button;
    		if (dirButton.isSelected())
    			return;
    		this.tileTeleporter.setField(0, dirButton.id);
    		this.buttonList.clear();
            this.initGui();
            ModPacketHandler.INSTANCE.sendToServer(new UpdateTe(dirButton.id,this.pos));
    	}
    }
    
	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
		GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
		this.mc.getTextureManager().bindTexture(TRANSLOCATOR_GUI_TEXTURES);
		int i = (this.width - this.xSize) / 2;
        int j = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
	}
	
	//Button class
	@SideOnly(Side.CLIENT)
	static class SelectableButton extends GuiButton {
	
		private final int textureX;
		private final int textureY;
    
		boolean selected = false;

		protected SelectableButton(int buttonId, int x, int y, int width, int height, int textureXIn, int textureYIn) {
			super(buttonId, x , y, width, height, "");
			this.textureX = textureXIn;
			this.textureY = textureYIn;
		}
	
		/**
     	* Draws this button to the screen.
     	*/
    	public void drawButton(Minecraft mc, int mouseX, int mouseY)
    	{
        	if (this.visible)
        	{
            	mc.getTextureManager().bindTexture(GuiTeleporter.TRANSLOCATOR_GUI_TEXTURES);
            	GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
            	this.hovered = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
            	int i = this.textureX;
            	int j = this.textureY;

            	if (!this.enabled)
            	{
            		j += this.height * 3;
            	}
            	else if (this.selected)
            	{
                	j += this.height * 2;
            	}
            	else if (this.hovered)
            	{
                	j += this.height * 1;
            	}

            	this.drawTexturedModalRect(this.xPosition, this.yPosition, i, j, this.width, this.height);
        	}
    	}

    	public boolean isSelected()
    	{
    		return this.selected;
    	}

    	public void setSelected(boolean selectedIn)
    	{
        	this.selected = selectedIn;
    	}
	}

	class DirectionnalButton extends GuiTeleporter.SelectableButton {

		protected DirectionnalButton(int buttonId, int x, int y, int width, int height, int textureXIn, int textureYIn) {
			super(buttonId, x, y, width, height, textureXIn, textureYIn);
		}
	}
	
	@SideOnly(Side.CLIENT)
	class NorthButton extends GuiTeleporter.DirectionnalButton {

		public NorthButton(int buttonId, int x, int y) {
			super(buttonId, x, y, 15, 10, 211, 0);
		}
	}
	
	@SideOnly(Side.CLIENT)
	class WestButton extends GuiTeleporter.DirectionnalButton {

		public WestButton(int buttonId, int x, int y) {
			super(buttonId, x, y, 10, 15, 201, 0);
		}
	}
	
	@SideOnly(Side.CLIENT)
	class SouthButton extends GuiTeleporter.DirectionnalButton {

		public SouthButton(int buttonId, int x, int y) {
			super(buttonId, x, y, 15, 10, 226, 0);
		}
	}
	
	@SideOnly(Side.CLIENT)
	class EastButton extends GuiTeleporter.DirectionnalButton {

		public EastButton(int buttonId, int x, int y) {
			super(buttonId, x, y, 10, 15, 191, 0);
		}
	}
	
	@SideOnly(Side.CLIENT)
	class TopButton extends GuiTeleporter.DirectionnalButton {

		public TopButton(int buttonId, int x, int y) {
			super(buttonId, x, y, 15, 15, 176, 0);
		}
	}
}

 

TileEntityTeleporter :

Spoiler

package com.personnedu13.test.tileentity;

import com.personnedu13.test.Main;
import com.personnedu13.test.client.gui.GuiTeleporter;
import com.personnedu13.test.inventory.ContainerTeleporter;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.IInteractionObject;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityTeleporter  extends TileEntity implements ITickable, IInventory, IInteractionObject {
	
	private ItemStack EMPTY = new ItemStack((Item)null);
	
	private int sideout;
	private ItemStack[] inventory;
	private String customName;
	
	public TileEntityTeleporter() {
		this.inventory = new ItemStack[this.getSizeInventory()] ;
		this.sideout = 4;
	}

	@Override
	public void readFromNBT(NBTTagCompound nbt) {
		super.readFromNBT(nbt);
		
		NBTTagList list = nbt.getTagList("Items", 10);
		   for (int i = 0; i < list.tagCount(); ++i) {
		       NBTTagCompound stackTag = list.getCompoundTagAt(i);
		       int slot = stackTag.getByte("Slot") & 255;
		       this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
		   }
		this.sideout = nbt.getInteger("SideOut");

	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
		NBTTagList list = new NBTTagList();
		for (int i = 0; i < this.getSizeInventory(); i++) {
			if (this.getStackInSlot(i) != null) {
				NBTTagCompound stackTag = new NBTTagCompound();
	            stackTag.setByte("Slot", (byte) i);
	            this.getStackInSlot(i).writeToNBT(stackTag);
	            list.appendTag(stackTag);
			}
		}
		
		nbt.setTag("Items", list);
		nbt.setInteger("SideOut", this.sideout);
		
		return super.writeToNBT(nbt);
		
	}

	@Override
	public void update() {
		if (this.worldObj != null) {
			if (!this.worldObj.isRemote) {
				System.out.println(sideout);
			}
		}
	}
	
	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound nbt = new NBTTagCompound();
		this.writeToNBT(nbt);
		int metadata = getBlockMetadata();
		return new SPacketUpdateTileEntity(this.pos, metadata, nbt);
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		this.readFromNBT(pkt.getNbtCompound());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound nbt = new NBTTagCompound();
		this.writeToNBT(nbt);
		return nbt;
	}

	@Override
	public void handleUpdateTag(NBTTagCompound tag) {
		this.readFromNBT(tag);
	}

	@Override
	public NBTTagCompound getTileData() {
		NBTTagCompound nbt = new NBTTagCompound();
		this.writeToNBT(nbt);
		return nbt;
	}
	
	
	public String getCustomName() {
        return this.customName;
    }

    public void setCustomName(String customName) {
        this.customName = customName;
    }
    
    public static void getSideOut() {
    	
    }
    
    public final void setSideOut(int id) {
    	this.sideout = id;
    	this.markDirty();
    }
	
    @Override
	public String getName() {
	    return this.hasCustomName() ? this.customName : "container.tileentity_tile_entity";
	}

	@Override
	public boolean hasCustomName() {
		return this.customName != null && !this.customName.equals("");
	}
	
	@Override
	public ITextComponent getDisplayName()
    {
        return (ITextComponent)(this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName(), new Object[0]));
    }


	@Override
	public int getSizeInventory() {
		return 2;
	}

	private boolean isInventoryEmpty()
    {
        for (int i = 0; i < this.getInventoryStackLimit();i++)
        {
            if (!(this.inventory[i].equals(null)))
            {
                return false;
            }
        }

        return true;
    }
	
	public boolean isEmpty()
    {
        return this.isInventoryEmpty();
    }

	@Override
	public ItemStack getStackInSlot(int index) {
		return this.inventory[index];
	}

	@Override
	public ItemStack decrStackSize(int index, int count) {
		ItemStack itemstack = ItemStackHelper.getAndSplit(this.inventory, index, count);
		this.markDirty();
        return itemstack;
	}

	@Override
	public ItemStack removeStackFromSlot(int index) {
		ItemStack itemstack = ItemStackHelper.getAndRemove(this.inventory, index);
		this.markDirty();
        return itemstack;
	}

	@Override
	public void setInventorySlotContents(int index, ItemStack stack) {
	    if (index < 0 || index >= this.getSizeInventory()) {
	    	System.out.println("ERROR : INDEX INADAPTé : " + index);
	        return;
	    }
	    if (stack != null && stack.stackSize > this.getInventoryStackLimit())
	        stack.stackSize = this.getInventoryStackLimit();
	        
	    if (stack != null && stack.stackSize == 0)
	        stack = null;

	    this.inventory[index] = stack;
	    this.markDirty();
	}

	@Override
	public int getInventoryStackLimit() {
		return 1;
	}

	@Override
	public boolean isUseableByPlayer(EntityPlayer player) {
		return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
	}

	@Override
	public void openInventory(EntityPlayer player) {
		
	}

	@Override
	public void closeInventory(EntityPlayer player) {
		
	}

	@Override
	public boolean isItemValidForSlot(int index, ItemStack stack) {
		return true;
	}

	@Override
    public int getField(int id)
    {
        switch (id)
        {
            case 0:
                return sideout;
            default:
                return 0;
        }
    }

    @Override
    public void setField(int id, int value)
    {
        switch (id)
        {
            case 0:
                sideout = value;
                break;
        default:
            break;
        }
    }

    @Override
    public int getFieldCount()
    {
        return 1;
    }

	@Override
	public void clear() {
		for (int i = 0; i < this.getSizeInventory(); i++)
	        this.setInventorySlotContents(i, null);
	}

	@Override
	public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
		return new ContainerTeleporter(playerInventory, this);
	}

	@Override
	public String getGuiID() {
		return Main.MODID + ":translocator";
	}
}

 

ContainerTeleporter :

Spoiler

package com.personnedu13.test.inventory;

import com.personnedu13.test.network.ModPacketHandler;

import com.personnedu13.test.tileentity.TileEntityTeleporter;

import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ContainerTeleporter extends Container {

	private IInventory te;
	
	public ContainerTeleporter(IInventory playerInv,IInventory te) {
		this.te = te;
		this.addSlotToContainer(new Slot(te, 0 , 8 , 72));
		this.addSlotToContainer(new Slot(te, 1 , 26, 72));

        for (int y = 0; y < 3; y++)
        {
            for (int x = 0; x < 9; x++)
            {
                this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 ));
            }
        }

        for (int x = 0; x < 9; x++)
        {
            this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 161));
        }
	}
	
	public void addListener(IContainerListener listener)
	   {
	        super.addListener(listener);
	        listener.sendAllWindowProperties(this, this.te);
	   }
	
	@SideOnly(Side.CLIENT)
    public void updateProgressBar(int id, int data)
    {
        this.te.setField(id, data);
    }

    public void onContainerClosed(EntityPlayer playerIn)
    {
        super.onContainerClosed(playerIn);
    }
    
	public IInventory getTileEntity()
    {
        return this.te;
    }
	
	@Override
    public boolean canInteractWith(EntityPlayer playerIn) {
		return !playerIn.isSpectator();
    }

	@Override
	public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
	    ItemStack previous = null;
	    Slot slot = (Slot) this.inventorySlots.get(fromSlot);

	    if (slot != null && slot.getHasStack()) {
	        ItemStack current = slot.getStack();
	        previous = current.copy();

	        if (fromSlot < 2) {
	        	 // From Player Inventory to TE Inventory
	            if (!this.mergeItemStack(current, 0, 1, false))
	                return null;
	            
	        } else {
	        	// From TE Inventory to Player Inventory
	            if (!this.mergeItemStack(current, 2, 38, true))
	                return null;
	        }

	        if (current.stackSize == 0)
	            slot.putStack((ItemStack) null);
	        else
	            slot.onSlotChanged();

	        if (current.stackSize == previous.stackSize)
	            return null;
	        slot.onPickupFromSlot(playerIn, current);
	    }
	    return previous;
	}
}

 

ModPacketHandler :

Spoiler

package com.personnedu13.test.network;

import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;

public class ModPacketHandler {
	private static int packetId = 0;
    public static SimpleNetworkWrapper INSTANCE = null;
	
	public static int nextID() {
        return packetId++;
    }

    public static void registerMessages(String channelName) {
        INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(channelName);
        registerMessages();
    }

    public static void registerMessages() {
        // Register messages which are sent from the client to the server here:
        INSTANCE.registerMessage(UpdateTe.Handler.class, UpdateTe.class, nextID(), Side.SERVER);

    }
}

 

UpdateTe :

Spoiler

package com.personnedu13.test.network;
import com.personnedu13.test.tileentity.TileEntityTeleporter;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;


public class UpdateTe implements IMessage {
	private BlockPos blockPos;
	private int integer;
	
    @Override
    public void fromBytes(ByteBuf buf) {
    	blockPos = new BlockPos (buf.readInt(), buf.readInt(), buf.readInt());
    	integer = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
    	buf.writeInt(blockPos.getX());
    	buf.writeInt(blockPos.getY());
    	buf.writeInt(blockPos.getZ());
    	buf.writeInt(integer);
    }

    public UpdateTe() {
    }

    
    public UpdateTe(int side, BlockPos pos) {
    	integer = side;
    	blockPos = pos;
    }

    public static class Handler implements IMessageHandler<UpdateTe, IMessage> {
        @Override
        public IMessage onMessage(UpdateTe message, MessageContext ctx) {
        	FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        private void handle(UpdateTe message, MessageContext ctx) {
        	EntityPlayerMP player = ctx.getServerHandler().playerEntity;
        	World world = player.worldObj;
        	TileEntityTeleporter te = (TileEntityTeleporter) world.getTileEntity(message.blockPos);
        	te.setField(0, message.integer);
        }
    }
}

 

 

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • so my minecraft crashes when opening my world, i played without any troubles for about 5 days and today it started tweaking.. pls help me
    • Hi guys! I am having some issues with the server crashing over and over and I was hoping to get some guidance.  Thanks in advance! Crash 1: java.lang.Error: ServerHangWatchdog detected that a single server tick took 60.00 seconds (should be max 0.05)     at net.minecraft.server.dedicated.ServerWatchdog.run(ServerWatchdog.java:43) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:classloading}     at java.lang.Thread.run(Thread.java:840) ~[?:?] { Crash 2: java.lang.IllegalStateException: Capability missing for eeb7f026-34b4-42f5-9164-e7736461df83     at me.lucko.luckperms.forge.capabilities.UserCapabilityImpl.lambda$get$0(UserCapabilityImpl.java:66) ~[?:?] {re:classloading,re:classloading,re:classloading}     at net.minecraftforge.common.util.LazyOptional.orElseThrow(LazyOptional.java:261) ~[forge-1.20.1-47.3.10-universal.jar%23222!/:?] {re:mixin,re:classloading}     at me.lucko.luckperms.forge.capabilities.UserCapabilityImpl.get(UserCapabilityImpl.java:66) ~[?:?] {re:classloading,re:classloading,re:classloading}     at me.lucko.luckperms.forge.util.BrigadierInjector$InjectedPermissionRequirement.test(BrigadierInjector.java:143) ~[?:?] {}     at me.lucko.luckperms.forge.util.BrigadierInjector$InjectedPermissionRequirement.test(BrigadierInjector.java:129) ~[?:?] {}     at com.mojang.brigadier.tree.CommandNode.canUse(CommandNode.java:65) ~[brigadier-1.1.8.jar%2376!/:?] {}     at com.mojang.brigadier.CommandDispatcher.parseNodes(CommandDispatcher.java:359) ~[brigadier-1.1.8.jar%2376!/:?] {}     at com.mojang.brigadier.CommandDispatcher.parse(CommandDispatcher.java:349) ~[brigadier-1.1.8.jar%2376!/:?] {}     at com.mojang.brigadier.CommandDispatcher.parse(CommandDispatcher.java:317) ~[brigadier-1.1.8.jar%2376!/:?] {}     at net.minecraft.commands.Commands.m_230957_(Commands.java:237) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:classloading}     at net.mcreator.boh.procedures.TeleportbenProcedure.lambda$execute$2(TeleportbenProcedure.java:65) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading}     at net.mcreator.boh.BohMod.lambda$tick$2(BohMod.java:96) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading}     at java.util.ArrayList.forEach(ArrayList.java:1511) ~[?:?] {re:mixin}     at net.mcreator.boh.BohMod.tick(BohMod.java:96) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading}     at net.mcreator.boh.__BohMod_tick_ServerTickEvent.invoke(.dynamic) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading,pl:eventbus:B}     at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:73) ~[eventbus-6.0.5.jar%2352!/:?] {}     at net.minecraftforge.eventbus.EventBus.post(EventBus.java:315) ~[eventbus-6.0.5.jar%2352!/:?] {}     at net.minecraftforge.eventbus.EventBus.post(EventBus.java:296) ~[eventbus-6.0.5.jar%2352!/:?] {}     at net.minecraftforge.event.ForgeEventFactory.onPostServerTick(ForgeEventFactory.java:950) ~[forge-1.20.1-47.3.10-universal.jar%23222!/:?] {re:classloading}     at net.minecraft.server.MinecraftServer.m_5705_(MinecraftServer.java:835) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,pl:mixin:A}     at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:661) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,pl:mixin:A}     at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:251) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,pl:mixin:A}     at java.lang.Thread.run(Thread.java:840) ~[?:?] {}
    • Hello there! I am trying to make custom dimensions for a modpack I am making in an older minecraft version, 1.16.5. I like that version and it has a few other mods that have not been updated that I would still like to use. Anyway, I am having a terrible time with getting my dimension to work and have tried using code from other peoples projects to at least figure out what I'm supposed to be doing but it has not been as helpful as I would have liked. If anyone could help that would be greatly appreciated! Here is my github with all the code as I am using it: https://github.com/BladeColdsteel/InvigoratedDimensionsMod I have also included the last log, https://pastebin.com/zX9vsDSq, I had when I tried to load up a world, let me know if there is anything else I should send though, thank you!
    • Whether you are a fan of Hypixel Bedwars, SkyWars and PvP gamemodes like that, well you would enjoy this server! We have a very fun and unique style of PvP that a lot of our players really enjoy and we want to bring this server to more players like you! Yes you reading this post haha. Introducing, the Minezone Network, home of SUPER CRAFT BLOCKS. We've been working on this server for over 4 years now. Here is what we have to offer: SUPER CRAFT BLOCKS: This has 3 different gamemodes you can play, Classic, Duels and Frenzy. Each mode offers over 60 kits to choose from, along with a total of over 60 maps, allowing for various different playstyles on each map. There are also random powerups that spawn on the map which can include Health Pots, Bazookas, Nukes, Extra Lives and way way more! There is also double jump in this gamemode as well, which makes PvP a lot more fun & unique. You only need a minimum of 2 players to start any mode! Classic: Choose a kit, 5 lives for each player, fight it out and claim the #1 spot! Look out for lightning as they can spawn powerups to really give you an advantage in the game! Duels: Fight against another random player or one of your friends and see who is the best! Frenzy: Your kit is randomly selected for you, each life you will have a different kit. You can fight with up to 100 players in this mode and lets see who will be the best out of that 100! All the other stuff from Classic/Duels apply to this mode as well like powerups. We have 2 ranks on this server too, VIP and CAPTAIN which has a bunch of different perks for SCB and other things like Cosmetics and more.   SERVER IP: If this server has caught your interest in any way, please consider joining and you will NOT regret it! Bring some of your friends online for an even better experience and join in on the fun at: IP: minezone.club Hope to see you online!   SERVER TRAILER: https://www.youtube.com/watch?v=0phpMgu1mH0
  • Topics

×
×
  • Create New...

Important Information

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