Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fr.rissalfa.slimemod.init.ItemInit;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.math.MathHelper;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- import net.minecraftforge.items.IItemHandler;
- import javax.annotation.Nonnull;
- public class TileEntityMachineBase extends TileEntity implements IItemHandler, ITickable {
- private NonNullList<ItemStack> machineItemStacks = NonNullList.withSize(4, ItemStack.EMPTY);
- private int machineRunTime;
- private int currentItemRunTime;
- private int processTime;
- private int totalProcessTime;
- private String machineCustomName;
- public void setCustomName(String name)
- {
- machineCustomName = name;
- return;
- }
- public int getSizeInventory()
- {
- return machineItemStacks.size();
- }
- @Override
- public int getSlots()
- {
- return 4;
- }
- public boolean hasCustomName()
- {
- return this.machineCustomName != null && !this.machineCustomName.isEmpty();
- }
- @Override
- public ItemStack getStackInSlot(int index)
- {
- return this.machineItemStacks.get(index);
- }
- @Nonnull
- @Override
- public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
- return null;
- }
- @Nonnull
- @Override
- public ItemStack extractItem(int slot, int amount, boolean simulate) {
- return null;
- }
- @Override
- public int getSlotLimit(int slot) {
- return 64;
- }
- @Override
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- this.machineItemStacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
- ItemStackHelper.loadAllItems(compound, this.machineItemStacks);
- this.machineRunTime = compound.getInteger("RunTime");
- this.processTime = compound.getInteger("ProcessTime");
- this.totalProcessTime = compound.getInteger("ProcessTimeTotal");
- this.currentItemRunTime = getItemRunTime(this.machineItemStacks.get(2));
- if (compound.hasKey("CustomName", 8)) this.setCustomName("CustomName");
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- compound.setInteger("RunTime", (short) this.machineRunTime);
- compound.setInteger("ProcessTime", (short) this.processTime);
- compound.setInteger("ProcessTimeTotal", (short) this.totalProcessTime);
- ItemStackHelper.saveAllItems(compound, this.machineItemStacks);
- if (this.hasCustomName())compound.setString("CustomName", this.machineCustomName);
- return compound;
- }
- public boolean isRunning() {
- return this.machineRunTime > 0;
- }
- @SideOnly(Side.CLIENT)
- public static boolean isRunning(IItemHandler itemHandler) {return itemHandler.getStackInSlot(0).getCount() > 0;}
- @Override
- public void update()
- {
- boolean flag = this.isRunning();
- boolean flag1 = false;
- if(this.isRunning()) --this.machineRunTime;
- if(!this.world.isRemote)
- {
- ItemStack stack = (ItemStack)this.machineItemStacks.get(2);
- if(this.isRunning() || !stack.isEmpty() && !((((ItemStack)this.machineItemStacks.get(0)).isEmpty()) || ((ItemStack)this.machineItemStacks.get(1)).isEmpty()))
- {
- if(!this.isRunning() && this.canProcess())
- {
- this.machineRunTime = getItemRunTime(stack);
- this.currentItemRunTime = this.machineRunTime;
- if(this.isRunning())
- {
- flag1 = true;
- if(!stack.isEmpty())
- {
- Item item = stack.getItem();
- stack.shrink(1);
- if(stack.isEmpty())
- {
- ItemStack item1 = item.getContainerItem(stack);
- this.machineItemStacks.set(2, item1);
- }
- }
- }
- }
- if(this.isRunning() && this.canProcess())
- {
- ++this.processTime;
- if(this.processTime == this.totalProcessTime)
- {
- this.processTime = 0;
- this.totalProcessTime = this.getProcessTime((ItemStack)this.machineItemStacks.get(0), (ItemStack)this.machineItemStacks.get(1));
- this.processItem();
- flag1 = true;
- }
- }
- else this.processTime = 0;
- }
- else if(!this.isRunning() && this.processTime > 0)
- {
- this.processTime = MathHelper.clamp(this.processTime - 2, 0, this.totalProcessTime);
- }
- if(flag != this.isRunning())
- {
- flag1 = true;
- MachineBaseBlock.setState(this.world, this.pos);
- }
- }
- if(flag1) this.markDirty();
- }
- public int getProcessTime(ItemStack stack, ItemStack stack1)
- {
- return 200;
- }
- private boolean canProcess()
- {
- if ((this.machineItemStacks.get(0)).isEmpty() || (this.machineItemStacks.get(1)).isEmpty()) return false;
- else
- {
- ItemStack itemstack = MachineBaseRecipes.instance().getProcessResult(this.machineItemStacks.get(0), this.machineItemStacks.get(1));
- if (itemstack.isEmpty()) return false;
- else
- {
- ItemStack itemstack1 = this.machineItemStacks.get(3);
- if (itemstack1.isEmpty()) return true;
- else if (!itemstack1.isItemEqual(itemstack)) return false;
- else if (itemstack1.getCount() + itemstack.getCount() <= this.getSlotLimit(3) && itemstack1.getCount() + itemstack.getCount() <= itemstack1.getMaxStackSize()) return true;
- else return itemstack1.getCount() + itemstack.getCount() <= itemstack.getMaxStackSize();
- }
- }
- }
- public void processItem()
- {
- if (this.canProcess())
- {
- ItemStack input1 = this.machineItemStacks.get(0);
- ItemStack input2 = this.machineItemStacks.get(1);
- ItemStack result = MachineBaseRecipes.instance().getProcessResult(input1, input2);
- ItemStack output = this.machineItemStacks.get(3);
- if (output.isEmpty()) this.machineItemStacks.set(3, result.copy());
- else if (output.getItem() == result.getItem()) output.grow(result.getCount());
- input1.shrink(1);
- input2.shrink(2);
- }
- }
- public static int getItemRunTime(ItemStack energy)
- {
- if (energy.isEmpty()) return 0;
- else
- {
- Item item = energy.getItem();
- if (item == ItemInit.BATTERY)return 200;
- else return 0;
- }
- }
- public static boolean isItemEnergy(ItemStack stack) {
- return getItemRunTime(stack) > 0;
- }
- }
Add Comment
Please, Sign In to add comment