Доброго времени суток. Я тут систему энергии склепал, а там фигня. Собранная система после перезахода перестает работать. Что делать? Вот код.
MAgical GEm (нужен для линковки)
UBatteryEntity
MAgical GEm (нужен для линковки)
Код:
package rc.common.items;
import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import rc.common.tile.UBatteryEntity;
import rc.common.tile.UGeneratorEntity;
public class CMagicalGem extends Item
{
public CMagicalGem()
{
this.setMaxStackSize(1);
}
public void onUpdate(ItemStack itemStack, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_)
{
if(!itemStack.hasTagCompound())
{
itemStack.stackTagCompound = new NBTTagCompound();
itemStack.stackTagCompound.setBoolean("key", false);
itemStack.stackTagCompound.setInteger("x", 0);
itemStack.stackTagCompound.setInteger("y", 0);
itemStack.stackTagCompound.setInteger("z", 0);
itemStack.stackTagCompound.setString("type", "connector");
test(itemStack, p_77663_2_);
}
}
public void addInformation(ItemStack itemStack, EntityPlayer player,
List list, boolean par4) {
if (itemStack.stackTagCompound != null && itemStack.stackTagCompound.hasKey("key")) {
if(itemStack.getTagCompound().getBoolean("key")==true)
{
int x = itemStack.stackTagCompound.getInteger("x");
int y = itemStack.stackTagCompound.getInteger("y");
int z = itemStack.stackTagCompound.getInteger("z");
list.add(EnumChatFormatting.GREEN + "x: " + x + " y: " + y + " z: " + z);
if(player.getEntityWorld().getTileEntity(x, y, z) instanceof UBatteryEntity)
{
UBatteryEntity tile = (UBatteryEntity) player.getEntityWorld().getTileEntity(x, y, z);
list.add(EnumChatFormatting.BLUE + "Power: " + tile.getPower() + "/" + tile.getMaxPower());
}else if(player.getEntityWorld().getTileEntity(x, y, z) instanceof UGeneratorEntity)
{
UGeneratorEntity tile = (UGeneratorEntity) player.getEntityWorld().getTileEntity(x, y, z);
list.add(EnumChatFormatting.BLUE + "Power: " + tile.getPower() + "/" + tile.getMaxPower());
}
}else{
list.add(EnumChatFormatting.RED + "No connection.");
}
}
}
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world,
int x, int y, int z, int side, float hitX, float hitY, float hitZ)
{
TileEntity tile = world.getTileEntity(x, y, z);
if(tile instanceof UGeneratorEntity)
{
stack.getTagCompound().setBoolean("key", true);
stack.getTagCompound().setInteger("x", x);
stack.getTagCompound().setInteger("y", y);
stack.getTagCompound().setInteger("z", z);
player.addChatMessage(new ChatComponentText("Connected!"));
}else if(tile instanceof UBatteryEntity)
{
stack.getTagCompound().setBoolean("key", true);
stack.getTagCompound().setInteger("x", x);
stack.getTagCompound().setInteger("y", y);
stack.getTagCompound().setInteger("z", z);
player.addChatMessage(new ChatComponentText("Connected!"));
}else{
stack.getTagCompound().setBoolean("key", false);
player.addChatMessage(new ChatComponentText("Unconnected!"));
}
return true;
}
public void test(ItemStack itemStack, World world)
{
int x = itemStack.stackTagCompound.getInteger("x");
int y = itemStack.stackTagCompound.getInteger("y");
int z = itemStack.stackTagCompound.getInteger("z");
TileEntity tile = world.getTileEntity(x, y, z);
if(tile instanceof UGeneratorEntity || tile instanceof UBatteryEntity)
{
itemStack.stackTagCompound.setBoolean("key", true);
}else{
itemStack.stackTagCompound.setBoolean("key", false);
itemStack.stackTagCompound.setInteger("x", 0);
itemStack.stackTagCompound.setInteger("y", 0);
itemStack.stackTagCompound.setInteger("z", 0);
}
}
}
Код:
package rc.common.tile;
import rc.common.items.CMagicalGem;
import rc.core.Items;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
public class UBatteryEntity extends PowerEntity implements IInventory
{
private int maxPower;
private int power;
private ItemStack[] stack = new ItemStack[13];
private String field_145958_o;
public UBatteryEntity()
{
super();
this.canUpdate();
maxPower = 1000;
}
@Override
public void writeToNBT(NBTTagCompound p1)
{
super.writeToNBT(p1);
p1.setInteger("maxPower", maxPower);
p1.setInteger("power", power);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.stack.length; ++i)
{
if (this.stack[i] != null)
{
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte)i);
this.stack[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
p1.setTag("Items", nbttaglist);
if (this.hasCustomInventoryName())
{
p1.setString("CustomName", this.field_145958_o);
}
}
@Override
public void readFromNBT(NBTTagCompound p1)
{
super.readFromNBT(p1);
this.maxPower = p1.getInteger("maxPower");
this.power = p1.getInteger("power");
NBTTagList nbttaglist = p1.getTagList("Items", 10);
this.stack = new ItemStack[this.getSizeInventory()];
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
byte b0 = nbttagcompound1.getByte("Slot");
if (b0 >= 0 && b0 < this.stack.length)
{
this.stack[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
if (p1.hasKey("CustomName", 8))
{
this.field_145958_o = p1.getString("CustomName");
}
}
@Override
public void updateEntity()
{
for(int i=0; i < stack.length-3; i++)
{
if(this.stack[i]!=null)
{
ItemStack it = this.getStackInSlot(i);
if(it.hasTagCompound())
{
if(it.getTagCompound().getString("type") == "connector")
{
int x = it.stackTagCompound.getInteger("x");
int y = it.stackTagCompound.getInteger("y");
int z = it.stackTagCompound.getInteger("z");
TileEntity tile = this.getWorldObj().getTileEntity(x, y, z);
if(tile instanceof UGeneratorEntity)
{
if(((UGeneratorEntity) tile).getPower() !=0 && this.power != this.maxPower)
{
this.power=this.power+1;
((UGeneratorEntity) tile).setPower(((UGeneratorEntity) tile).getPower()-1);
}
}else if(tile instanceof UBatteryEntity)
{
int P=((UBatteryEntity) tile).getPower();
if(P !=0 && this.power != this.maxPower)
{
P=P-1;
this.power=this.power+1;
((UBatteryEntity) tile).setPower(P);
}
}
}
}
}
}
}
public int getPower()
{
return power;
}
public int getMaxPower()
{
return maxPower;
}
public boolean setPower(int i)
{
this.power=i;
return true;
}
public boolean setMaxPower(int i)
{
this.maxPower=i;
return true;
}
@Override
public int getSizeInventory() {
return this.stack.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return this.stack[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amt) {
ItemStack itemstack = getStackInSlot(slot);
if (itemstack != null) {
if (itemstack.stackSize <= amt) {
setInventorySlotContents(slot, null);
} else {
itemstack = itemstack.splitStack(amt);
if (itemstack.stackSize == 0) {
setInventorySlotContents(slot, null);
}
}
}
return itemstack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack itemstack = getStackInSlot(slot);
if (itemstack != null) {
setInventorySlotContents(slot, null);
}
return itemstack;
}
@Override
public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {
this.stack[p_70299_1_] = p_70299_2_;
if (p_70299_2_ != null && p_70299_2_.stackSize > this.getInventoryStackLimit())
{
p_70299_2_.stackSize = this.getInventoryStackLimit();
}
}
@Override
public String getInventoryName() {
return this.hasCustomInventoryName() ? this.field_145958_o : "container.furnace";
}
@Override
public boolean hasCustomInventoryName() {
return this.field_145958_o != null && this.field_145958_o.length() > 0;
}
@Override
public int getInventoryStackLimit() {
return 1;
}
@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
return true;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack ist)
{
return false;/*
int i = 0;
if(slot <= 9)
{
i=1;
}else if(slot >=10 && slot <=12)
{
i=2;
}else if(slot >=13 || slot<0)
{
i=0;
}
if(i==1)
{
if(ist.getItem() instanceof CMagicalGem)
{
return true;
}else return false;
}else if(i==2)
{
if(ist.getItem() instanceof CMagicalGem)
{
return true;
}else return false;
}else
{
return false;
}*/
}
}