Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package thinh.eligibleadapter.tile;
- import cofh.api.energy.ItemEnergyContainer;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.init.SoundEvents;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.crafting.IRecipe;
- import net.minecraft.item.crafting.ShapedRecipes;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.util.SoundCategory;
- import net.minecraftforge.oredict.OreDictionary;
- import net.minecraftforge.oredict.ShapelessOreRecipe;
- import thinh.eligibleadapter.crafting.solder.SolderCraftingManager;
- import thinh.eligibleadapter.gui.ContainerSolderingStation;
- import thinh.eligibleadapter.gui.GuiSolderingStation;
- import thinh.eligibleadapter.network.PacketHandler;
- import thinh.eligibleadapter.network.PacketSyncPersistentCrafting;
- import thinh.eligibleadapter.tile.inventory.ItemHandlerPersistentCrafting;
- import thinh.eligibleadapter.utils.InventoryUtils;
- import javax.annotation.Nonnull;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- public class TileSolderingStation extends AbstractTileInventoryPowered implements IAbstractTileHandler, INeedsUpdateTile {
- public final int SOLDER_TIME_REQUIRED = 200;
- public int processTicks = 0;
- public static final ItemHandlerPersistentCrafting craftMatrix = new ItemHandlerPersistentCrafting(null, new TileSolderingStation(), TileSolderingStation.contents, 9, 3, 3);
- private ItemStack OUTPUT = null;
- public TileSolderingStation() {
- super(11, "soldering_station");
- }
- /**
- * NBT
- */
- @Override public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- this.processTicks = compound.getInteger("processTicks");
- }
- @Nonnull @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- compound.setInteger("processTicks", this.processTicks);
- return super.writeToNBT(compound);
- }
- /**
- * ITickable
- */
- @Override public void update() {
- if (!this.worldObj.isRemote) {
- boolean flag = false;
- int SOLDER_ENERGY_REQUIRED = 2000;
- if (this.energy.getEnergyStored() >= SOLDER_ENERGY_REQUIRED) {
- if (this.canSolder()) {
- ++this.processTicks;
- if (this.processTicks >= this.SOLDER_TIME_REQUIRED) {
- this.worldObj.playSound(null, this.pos, SoundEvents.BLOCK_REDSTONE_TORCH_BURNOUT, SoundCategory.NEUTRAL, 0.3F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
- this.processTicks = 0;
- this.solderItems();
- flag = true;
- this.extractEnergy(SOLDER_ENERGY_REQUIRED);
- }
- } else {
- this.processTicks = 0;
- }
- } else {
- this.processTicks = 0;
- }
- if (flag) {
- this.markDirty();
- }
- }
- }
- /**
- * ItemHandlerBase
- */
- @Override public boolean isItemValidForSlot(int index, ItemStack stack) {
- if (index == 0) {
- return stack.getItem() == new ItemEnergyContainer();
- } else {
- if (index >= 2) {
- if (this.OUTPUT != null) {
- ItemStack stackInSlot = TileSolderingStation.contents.getStackInSlot(index);
- return stackInSlot != null && stackInSlot.isItemEqual(stack);
- }
- return this.isSolderingInput(index - 2, stack);
- }
- }
- return false;
- }
- @Override public boolean canExtractItem(int index, ItemStack stack) {
- return index == 1;
- }
- @Override public void markDirty() {
- super.markDirty();
- if (TileSolderingStation.craftMatrix.container != null) {
- TileSolderingStation.craftMatrix.container.detectAndSendChanges();
- }
- PacketHandler.sendToServer(new PacketSyncPersistentCrafting());
- }
- /**
- * IAbstractTileHandler
- */
- @Override public String getName() {
- return this.name;
- }
- @Override public String setName(String name) {
- return this.name = name;
- }
- @Override public Object getGuiServer(EntityPlayer player, int ID, int x, int y, int z) {
- return new ContainerSolderingStation(this, player.inventory);
- }
- @Override public Object getGuiClient(EntityPlayer player, int ID, int x, int y, int z) {
- return new GuiSolderingStation(this, player.inventory);
- }
- /**
- * INeedsUpdateTile
- */
- @Override public int getField(int id) {
- switch (id) {
- case 0:
- return this.processTicks;
- case 1:
- return this.getEnergyStored();
- default:
- return 0;
- }
- }
- @Override public void setField(int id, int value) {
- switch (id) {
- case 0:
- this.processTicks = value;
- break;
- case 1:
- this.setEnergyStored(value);
- break;
- }
- }
- @Override public int getFieldCount() {
- return 2;
- }
- /**
- * ================================ HELPERS START ================================
- */
- private boolean canSolder() {
- ItemStack itemstack = this.OUTPUT;
- if (itemstack == null) {
- return false;
- }
- if (TileSolderingStation.contents.getStackInSlot(1) == null) {
- return true;
- }
- if (!TileSolderingStation.contents.getStackInSlot(1).isItemEqual(itemstack)) {
- return false;
- }
- int result = TileSolderingStation.contents.getStackInSlot(1).stackSize + itemstack.stackSize;
- return result <= TileSolderingStation.contents.getStackLimit() && result <= itemstack.getMaxStackSize();
- }
- public void updateInput() {
- this.OUTPUT = SolderCraftingManager.findMatchingRecipe(TileSolderingStation.craftMatrix, this.worldObj);
- }
- private void solderItems() {
- if (this.canSolder()) {
- ItemStack result = this.OUTPUT;
- if (TileSolderingStation.contents.getStackInSlot(1) == null) {
- TileSolderingStation.contents.setStackInSlot(1, result.copy());
- } else {
- if (TileSolderingStation.contents.getStackInSlot(1).isItemEqual(result)) {
- if (TileSolderingStation.contents.getStackInSlot(1).stackSize + result.stackSize > 64) {
- for (int i = 0; i < TileSolderingStation.contents.getStackInSlot(1).stackSize + result.stackSize - 64; ++i) {
- InventoryUtils.dropItemStack(this.worldObj, this.pos, result);
- }
- TileSolderingStation.contents.getStackInSlot(1).stackSize = 64;
- } else {
- TileSolderingStation.contents.getStackInSlot(1).stackSize += result.stackSize;
- }
- }
- }
- for (int i = 0; i < TileSolderingStation.craftMatrix.getSlots(); ++i) {
- if (TileSolderingStation.craftMatrix.getStackInSlot(i) != null && ItemStack.areItemStacksEqual(TileSolderingStation.craftMatrix.getStackInSlot(i), (new ItemStack(Items.WATER_BUCKET)))) {
- TileSolderingStation.craftMatrix.setStackInSlot(i, new ItemStack(Items.BUCKET));
- } else {
- TileSolderingStation.craftMatrix.extractItem(i,
- 1, false);
- }
- }
- this.updateInput();
- }
- }
- private boolean isSolderingInput(int index, ItemStack stack) {
- for (IRecipe recipe : SolderCraftingManager.recipes) {
- if (recipe instanceof ShapedRecipes) {
- if (index >= ((ShapedRecipes) recipe).recipeItems.length) {
- continue;
- }
- ItemStack itemstack1 = ((ShapedRecipes) recipe).recipeItems[index];
- if (stack.getItem() == itemstack1.getItem() && (itemstack1.getItemDamage() == 32767 || stack.getItemDamage() == itemstack1.getItemDamage())) {
- for (int i = 0; i < ((ShapedRecipes) recipe).recipeItems.length; i++) {
- if (i == index) {
- continue;
- }
- ItemStack itemstack2 = ((ShapedRecipes) recipe).recipeItems[i];
- if (stack.getItem() == itemstack2.getItem() && (itemstack2.getItemDamage() == 32767 || stack.getItemDamage() == itemstack2.getItemDamage())) {
- ItemStack itemStack3 = TileSolderingStation.contents.getStackInSlot(index + 2);
- ItemStack itemStack4 = TileSolderingStation.contents.getStackInSlot(i + 2);
- return itemStack3 == null || itemStack4 != null && itemStack3.stackSize < itemStack4.stackSize;
- }
- }
- return true;
- }
- } else {
- if (recipe instanceof ShapelessOreRecipe) {
- List<Object> required = new ArrayList<>(((ShapelessOreRecipe) recipe).getInput());
- Iterator<Object> req = required.iterator();
- int match = 0;
- while (req.hasNext()) {
- Object next = req.next();
- if (next instanceof ItemStack) {
- if (OreDictionary.itemMatches((ItemStack) next,
- stack,
- false)) {
- match++;
- }
- } else {
- if (next instanceof List) {
- for (ItemStack itemStack : ((List<ItemStack>) next)) {
- if (OreDictionary.itemMatches(itemStack,
- stack,
- false)) {
- match++;
- break;
- }
- }
- }
- }
- }
- if (match == 0) {
- continue;
- }
- if (match == 1) {
- return true;
- }
- // Shapeless recipe can go into (match) number of slots
- int slotsFilled = 0;
- for (int i = 2; i <= 11; ++i) {
- ItemStack inMatrix = TileSolderingStation.contents.getStackInSlot(i);
- if (inMatrix != null && inMatrix.isItemEqual(stack)) {
- slotsFilled++;
- }
- }
- if (slotsFilled < match) {
- return TileSolderingStation.contents.getStackInSlot(index + 2) == null;
- }
- return this.worldObj.rand.nextInt(match) == 0;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement