Привет!
Как сделать при заполнении слотов на 20 отключение энергии, но если хоть один слот не полон - энергию возвращать.
Мод : Ex-Astris
Как сделать при заполнении слотов на 20 отключение энергии, но если хоть один слот не полон - энергию возвращать.
Мод : Ex-Astris
TileEntityHammerAutomatic:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package ExAstris.Block.TileEntity;
import ExAstris.ExAstrisItem;
import ExAstris.Data.ModData;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyHandler;
import cofh.lib.util.helpers.ItemHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import exnihilo.registries.HammerRegistry;
import exnihilo.registries.helpers.Smashable;
import java.util.ArrayList;
import java.util.Iterator;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.EntityDiggingFX;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
public class TileEntityHammerAutomatic extends TileEntity implements IEnergyHandler, ISidedInventory {
public EnergyStorage storage = new EnergyStorage(64000);
private int energyPerCycle;
private float processingInterval;
protected ItemStack[] inventory;
public TileEntityHammerAutomatic.HammerMode mode;
public Block content;
public int contentMeta;
public ItemStack stackInProgress;
private float volume;
private boolean particleMode;
private boolean update;
private static final int UPDATE_INTERVAL = 20;
private int timer;
public TileEntityHammerAutomatic() {
this.energyPerCycle = ModData.hammerAutomaticBaseEnergy;
this.processingInterval = 0.005F;
this.volume = 0.0F;
this.update = false;
this.timer = 0;
this.inventory = new ItemStack[this.getSizeInventory()];
this.mode = TileEntityHammerAutomatic.HammerMode.EMPTY;
}
public void updateEntity() {
++this.timer;
if (this.timer >= 20) {
this.timer = 0;
if (this.update) {
// On craft?
this.update();
}
}
if (this.storage.getEnergyStored() > this.getEffectiveEnergy()) {
if (this.mode == TileEntityHammerAutomatic.HammerMode.EMPTY && this.inventory[0] != null) {
ItemStack held = this.inventory[0];
if (HammerRegistry.registered(held)) {
if (this.mode == TileEntityHammerAutomatic.HammerMode.EMPTY){
setEnergyStored(0);
}
this.addHammerable(Block.getBlockFromItem(held.getItem()), held.getItemDamage());
this.stackInProgress = held.copy();
this.stackInProgress.stackSize = 1;
this.decrStackSize(0, 1);
this.storage.extractEnergy(this.getEffectiveEnergy(), false);
}
} else if (this.mode != TileEntityHammerAutomatic.HammerMode.EMPTY) {
this.processContents();
}
}
}
public void addHammerable(Block block, int blockMeta) {
this.content = block;
this.contentMeta = blockMeta;
this.mode = TileEntityHammerAutomatic.HammerMode.FILLED;
this.volume = 1.0F;
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}
public void processContents() {
this.volume -= this.getEffectiveSpeed();
this.storage.extractEnergy(this.getEffectiveEnergy(), false);
if (this.volume <= 0.0F) {
this.mode = TileEntityHammerAutomatic.HammerMode.EMPTY;
if (!this.worldObj.isRemote) {
ArrayList<Smashable> rewards = HammerRegistry.getRewards(this.content, this.contentMeta);
if (rewards.size() > 0) {
Iterator it = rewards.iterator();
label50:
while(true) {
Smashable reward;
int size;
int inventoryIndex;
do {
if (!it.hasNext()) {
break label50;
}
reward = (Smashable)it.next();
size = this.getSizeInventory() - 2;
inventoryIndex = 0;
} while((double)this.worldObj.rand.nextFloat() > (double)reward.chance + (double)reward.luckMultiplier * this.getFortuneModifier());
for(int i = 1; i < size; ++i) {
if (this.inventory[i] == null) {
inventoryIndex = i;
break;
}
if (ItemHelper.itemsEqualWithMetadata(this.inventory[i], new ItemStack(reward.item, 1, reward.meta)) && this.inventory[i].stackSize < this.inventory[i].getMaxStackSize()) {
inventoryIndex = i;
break;
}
}
if (inventoryIndex != 0) {
if (this.inventory[inventoryIndex] != null) {
this.inventory[inventoryIndex] = new ItemStack(reward.item, this.inventory[inventoryIndex].stackSize + 1, reward.meta);
} else {
this.inventory[inventoryIndex] = new ItemStack(reward.item, 1, reward.meta);
}
} else {
// EntityItem entityitem = new EntityItem(this.worldObj, (double)this.xCoord + 0.5D, (double)this.yCoord + 1.5D, (double)this.zCoord + 0.5D, new ItemStack(reward.item, 1, reward.meta));
// double f3 = 0.05000000074505806D;
// entityitem.motionX = this.worldObj.rand.nextGaussian() * f3;
// entityitem.motionY = 0.2D;
// entityitem.motionZ = this.worldObj.rand.nextGaussian() * f3;
// this.worldObj.spawnEntityInWorld(entityitem);
// this.inventory[inventoryIndex] = new ItemStack(inventory[inventoryIndex].getItem(),)
// break ;
//this.inventory[21].stackSize = undecrStackSize(21, this.inventory[21].stackSize);
}
}
}
} else if (this.stackInProgress != null) { // add items to the hammer slot and spawn particles in the world
this.spawnCrushParticles();
}
} else {
this.particleMode = true;
}
this.update = true;
}
@SideOnly(Side.CLIENT)
private void spawnCrushParticles() {
for(int i = 0; i < 10; ++i) { // add items to the hammer slot and spawn particles in the world
EntityDiggingFX particle = new EntityDiggingFX(this.getWorldObj(), (double)this.xCoord + 0.5D, (double)this.yCoord + 0.3125D, (double)this.zCoord + 0.5D, 0.0D, 0.0D, 0.0D, Block.getBlockFromItem(this.stackInProgress.getItem()), this.stackInProgress.getItemDamage());
particle.setVelocity(this.worldObj.rand.nextDouble() / 2.0D - 0.25D, 0.0D, this.worldObj.rand.nextDouble() / 2.0D - 0.25D);
Minecraft.getMinecraft().effectRenderer.addEffect(particle);
}
}
private void update() {
// Works forever
this.update = false;
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}
public boolean canConnectEnergy(ForgeDirection arg0) {
return true;
}
public int getSizeInventory() {
return 23;
}
public ItemStack getStackInSlot(int slot) {
return this.inventory[slot];
}
public ItemStack decrStackSize(int slot, int amount) {
if (this.inventory[slot] != null) {
ItemStack itemstack1;
if (this.inventory[slot].stackSize <= amount) {
itemstack1 = this.inventory[slot];
this.inventory[slot] = null;
return itemstack1;
} else {
itemstack1 = this.inventory[slot].splitStack(amount);
if (this.inventory[slot].stackSize == 0) {
this.inventory[slot] = null;
}
return itemstack1;
}
} else {
return null;
}
}
public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
return null;
}
public void setInventorySlotContents(int slot, ItemStack stack) {
this.inventory[slot] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
stack.stackSize = this.getInventoryStackLimit();
}
}
public String getInventoryName() {
return null;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
public boolean isCustomInventoryName() {
return false;
}
public int getInventoryStackLimit() {
return 64;
}
public boolean isUseableByPlayer(EntityPlayer player) {
return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
public void openChest() {
}
public void closeChest() {
}
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return this.canInsertItem(slot, stack, 0);
}
public int[] getSlotsForFace(int p_94128_1_) {
int size = this.getSizeInventory() - 2;
int[] slots = new int[size];
for(int i = 0; i < size; slots[i] = i++) {
}
return slots;
}
@Override
public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
return new int[0];
}
public boolean canInsertItem(int slot, ItemStack item, int side) {
if (slot == 0) {
return HammerRegistry.registered(item);
} else if (slot == 21) {
return item.getItem() == ExAstrisItem.sieveUpgradeItem && item.getItemDamage() == 0;
} else if (slot != 22) {
return false;
} else {
return item.getItem() == ExAstrisItem.sieveUpgradeItem && item.getItemDamage() == 1;
}
}
public boolean canExtractItem(int slot, ItemStack item, int side) {
if (slot >= 1) {
return true;
} else {
return slot >= 1 && slot < 21;
}
}
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
switch(compound.getInteger("mode")) {
case 0:
this.mode = TileEntityHammerAutomatic.HammerMode.EMPTY;
break;
case 1:
this.storage.setMaxReceive(80000);
this.mode = TileEntityHammerAutomatic.HammerMode.FILLED;
}
if (!compound.getString("content").equals("")) {
this.content = (Block)Block.blockRegistry.getObject(compound.getString("content"));
} else {
this.content = null;
}
this.contentMeta = compound.getInteger("contentMeta");
this.volume = compound.getFloat("volume");
this.particleMode = compound.getBoolean("particles");
this.processingInterval = compound.getFloat("speed");
this.storage.readFromNBT(compound);
NBTTagList nbttaglist = compound.getTagList("Items", 10);
this.inventory = 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.inventory.length) {
this.inventory[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
}
public void writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setInteger("mode", this.mode.value);
if (this.content == null) {
compound.setString("content", "");
} else {
compound.setString("content", Block.blockRegistry.getNameForObject(this.content));
}
compound.setInteger("contentMeta", this.contentMeta);
compound.setFloat("volume", this.volume);
compound.setBoolean("particles", this.particleMode);
compound.setFloat("speed", this.processingInterval);
this.storage.writeToNBT(compound);
NBTTagList nbttaglist = new NBTTagList();
for(int i = 0; i < this.inventory.length; ++i) {
if (this.inventory[i] != null) {
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte)i);
this.inventory[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
compound.setTag("Items", nbttaglist);
}
public Packet getDescriptionPacket() {
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, this.blockMetadata, tag);
}
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
NBTTagCompound tag = pkt.func_148857_g();
this.readFromNBT(tag);
}
public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) {
return this.storage.extractEnergy(maxExtract, simulate);
}
public int getEnergyStored(ForgeDirection arg0) {
return this.storage.getEnergyStored();
}
public int getMaxEnergyStored(ForgeDirection arg0) {
return this.storage.getMaxEnergyStored();
}
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
return this.storage.receiveEnergy(maxReceive, simulate);
}
public float getEffectiveSpeed() {
float time = this.processingInterval;
if (this.inventory[21] != null) {
time += (float)this.inventory[21].stackSize / 1024.0F;
}
return time;
}
public int getEffectiveEnergy() {
int energy = this.energyPerCycle;
if (this.inventory[21] != null) {
energy = (int)((float)energy * (((float)this.inventory[21].stackSize / 1024.0F + this.processingInterval) / this.processingInterval));
}
if (this.inventory[22] != null) {
energy += this.inventory[22].stackSize * ModData.sieveFortuneRFIncrease;
}
return energy;
}
public double getFortuneModifier() {
return this.inventory[22] != null && ModData.sieveFortuneChance != 0 ? 0.09375D * (double)this.inventory[22].stackSize : 0.0D;
}
public float getVolume() {
return this.volume;
}
public void setEnergyStored(int energy) {
this.storage.setEnergyStored(energy);
}
public static enum HammerMode {
EMPTY(0),
FILLED(1);
public int value;
private HammerMode(int v) {
this.value = v;
}
}
}