Skip to content

Commit

Permalink
Moved functionality of ClearChestContents to tutmodMessage - NullPoin…
Browse files Browse the repository at this point in the history
…ter on line 65 - Not sure how.
  • Loading branch information
Excaliburns committed Nov 8, 2017
1 parent 1e1a3b1 commit f64cf1f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
38 changes: 7 additions & 31 deletions src/main/java/items/HuskItem.java
Expand Up @@ -17,7 +17,6 @@
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import network.tutmodMessage;
import network.tutmodPacketHandler;
import utils.ChestCoordStorage;
Expand All @@ -29,7 +28,7 @@ public class HuskItem extends Item
{

private ArrayList<ItemStack> newlist = new ArrayList<>();
private ChestCoordStorage[] chestList = new ChestCoordStorage[2];
private ChestCoordStorage[] chestList = new ChestCoordStorage[3];
private ArrayList<Integer> airList = new ArrayList<>();
private int totalItemUses = 0;
private boolean itemisUsed = false;
Expand Down Expand Up @@ -65,10 +64,9 @@ public EnumActionResult onItemUseFirst(EntityPlayer player, World worldIn, Block
if (!locatedChest.getTileData().hasKey("hasBeenAccessed") && totalItemUses <= 1)
{

chestList[totalItemUses] = new ChestCoordStorage(pos.getX(), pos.getY(), pos.getZ());
totalItemUses++;

chestList[totalItemUses - 1] = new ChestCoordStorage(pos.getX(), pos.getY(), pos.getZ());

locatedChest.getTileData().setBoolean("hasBeenAccessed", true);

for (int i = 0; i < ((TileEntityChest) locatedChest).getSizeInventory(); i++)
Expand All @@ -86,6 +84,7 @@ public EnumActionResult onItemUseFirst(EntityPlayer player, World worldIn, Block

if (!locatedChest.getTileData().getBoolean("hasBeenAccessed") && itemisUsed)
{
chestList[2] = new ChestCoordStorage(pos.getX(), pos.getY(), pos.getZ());
System.out.println("Using item!");

for (int i = 0; i < ((TileEntityChest) locatedChest).getSizeInventory(); i++)
Expand All @@ -98,18 +97,13 @@ public EnumActionResult onItemUseFirst(EntityPlayer player, World worldIn, Block

if (airList.size() > newlist.size())
{
System.out.println(newlist.toString());
System.out.println(newlist);
System.out.println(newlist.size());

tutmodPacketHandler.INSTANCE.sendToServer(new tutmodMessage(pos.getX(), pos.getY(), pos.getZ(), newlist, airList));


//ClearChestStorage is called before the packet is handled by the server, breaking everything. Will be moving this functionality to tutmodMessage.

//ClearChestStorage(chestList[0].getX(), chestList[0].getY(), chestList[0].getZ(), worldIn, player);
//ClearChestStorage(chestList[1].getX(), chestList[1].getY(), chestList[1].getZ(), worldIn, player);
tutmodPacketHandler.INSTANCE.sendToServer(new tutmodMessage(chestList, newlist, airList));

totalItemUses = 0;

} else
{
System.out.println("Not enough space!");
Expand All @@ -127,22 +121,4 @@ public EnumActionResult onItemUseFirst(EntityPlayer player, World worldIn, Block
}
return super.onItemUseFirst(player, worldIn, pos, side, hitX, hitY, hitZ, hand);
}
}
/*
private void ClearChestStorage(int x, int y, int z, World worldIn, EntityPlayer playerIn)
{
BlockPos chestBlock = new BlockPos(x, y, z);
TileEntity locatedChest = worldIn.getTileEntity(chestBlock);
for (int i = 0; i < ((TileEntityChest) locatedChest).getSizeInventory(); i++)
{
locatedChest.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).extractItem(i, ((TileEntityChest) locatedChest).getStackInSlot(i).getCount(), false);
}
locatedChest.getTileData().setBoolean("hasBeenAccessed", false);
}
}
*/
}
47 changes: 32 additions & 15 deletions src/main/java/network/tutmodMessage.java
@@ -1,6 +1,7 @@
package network;

import io.netty.buffer.ByteBuf;
import jdk.nashorn.internal.ir.Block;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
Expand All @@ -12,24 +13,26 @@
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import utils.ChestCoordStorage;

import java.util.ArrayList;

public class tutmodMessage implements IMessage
{
private int[] chestPos = new int[9];
private ChestCoordStorage[] chestPos = new ChestCoordStorage[3];
private ArrayList<ItemStack> itemsList;
private ArrayList<Integer> airSlots;

public tutmodMessage()
{
}

public tutmodMessage(int x, int y, int z, ArrayList<ItemStack> newList, ArrayList<Integer> airList)
public tutmodMessage(ChestCoordStorage[] chestCoords, ArrayList<ItemStack> newList, ArrayList<Integer> airList)
{
chestPos[0] = x;
chestPos[1] = y;
chestPos[2] = z;
for(int i = 0; i < 3 ; i++)
{
chestPos[i] = chestCoords[i];
}
itemsList = newList;
airSlots = airList;
}
Expand All @@ -47,9 +50,11 @@ public void toBytes(ByteBuf buf)
buf.writeInt(airSlots.get(x));
}

for(int x = 0; x < 3; x++)
for(int x = 0; x < 3 ; x++)
{
buf.writeInt(chestPos[x]);
buf.writeInt(chestPos[x].getX());
buf.writeInt(chestPos[x].getY());
buf.writeInt(chestPos[x].getZ());
}

}
Expand All @@ -67,9 +72,11 @@ public void fromBytes(ByteBuf buf)
airSlots.set(x, buf.readInt());
}

for(int x = 0; x < 9; x++)
for(int x = 0; x < 3; x++)
{
chestPos[x] = buf.readInt();
chestPos[x].setX(buf.readInt());
chestPos[x].setY(buf.readInt());
chestPos[x].setZ(buf.readInt());
}

}
Expand All @@ -79,25 +86,35 @@ public static class Handler implements IMessageHandler<tutmodMessage, IMessage>
@Override
public IMessage onMessage(tutmodMessage message, MessageContext ctx)
{
EntityPlayerMP serverPlayer = ctx.getServerHandler().player;
FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
return null;
}

private void handle(tutmodMessage message, MessageContext ctx)
{
TileEntity chestArray[] = new TileEntity[3];
EntityPlayerMP EntityPlayer = ctx.getServerHandler().player;
World world = EntityPlayer.getEntityWorld();

BlockPos chestBlock = new BlockPos(message.chestPos[0], message.chestPos[1], message.chestPos[2]);

TileEntity locatedChest = world.getTileEntity(chestBlock);
for(int i = 0; i < 3 ; i ++)
{
chestArray[i] = world.getTileEntity(new BlockPos(message.chestPos[i].getX(), message.chestPos[i].getY(), message.chestPos[i].getZ()));
}

if (world.isBlockLoaded(locatedChest.getPos()))
if (world.isBlockLoaded(chestArray[2].getPos()))
{
for (int i = 0; i < message.itemsList.size(); i++)
{
((TileEntityChest) locatedChest).setInventorySlotContents(message.airSlots.get(i), message.itemsList.get(i));
((TileEntityChest) chestArray[2]).setInventorySlotContents(message.airSlots.get(i), message.itemsList.get(i));
}
for (int i = 0; i < 2 ; i++)
{
for(int x = 0; i < ((TileEntityChest) chestArray[i]).getSizeInventory() ; x++)
{
((TileEntityChest) chestArray[i]).setInventorySlotContents(x, null );
}

chestArray[i].getTileData().setBoolean("hasBeenAccessed", false);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/utils/ChestCoordStorage.java
Expand Up @@ -13,6 +13,21 @@ public ChestCoordStorage(int xCoord, int yCoord, int zCoord)
this.z = zCoord;
}

public void setX(int x)
{
this.x = x;
}

public void setY(int y)
{
this.y = y;
}

public void setZ(int z)
{
this.z = z;
}

public int getX()
{
return x;
Expand Down

0 comments on commit f64cf1f

Please sign in to comment.