Jump to content

Recommended Posts

Posted

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 !

Posted (edited)

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

public UpdateTe() {
    integer = GuiTeleporter.getSideOut;
    blockPos = GuiTeleporter.getPos();
}

 

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.

Posted (edited)
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
Posted

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

 

 

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.