diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java | 269 | ||||
-rw-r--r-- | src/main/java/gtPlusPlus/core/util/minecraft/ItemUtils.java | 16 |
2 files changed, 90 insertions, 195 deletions
diff --git a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java index 48eb2945df..c3140eb53a 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java +++ b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java @@ -4,6 +4,8 @@ import static gregtech.api.enums.Mods.PamsHarvestCraft; import java.util.Random; +import javax.annotation.Nullable; + import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; @@ -14,8 +16,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.FishingHooks; +import gregtech.api.enums.Materials; import gregtech.api.util.GT_Utility; -import gtPlusPlus.api.objects.Logger; import gtPlusPlus.core.block.ModBlocks; import gtPlusPlus.core.inventories.InventoryFishTrap; import gtPlusPlus.core.util.math.MathUtils; @@ -27,81 +29,43 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { private boolean isInWater = false; private final InventoryFishTrap inventoryContents; private String customName; - private int locationX; - private int locationY; - private int locationZ; private int waterSides = 0; private int baseTickRate = 600 * 5; public TileEntityFishTrap() { - this.inventoryContents = new InventoryFishTrap(); // number of slots - - // without product - // slot - this.setTileLocation(); + this.inventoryContents = new InventoryFishTrap(); } - public boolean setTileLocation() { - if (this.hasWorldObj()) { - if (!this.getWorldObj().isRemote) { - this.locationX = this.xCoord; - this.locationY = this.yCoord; - this.locationZ = this.zCoord; - return true; - } + private boolean isSurroundedByWater() { + if (!this.hasWorldObj() || this.getWorldObj().isRemote) { + return false; } - return false; - } - - public final boolean isSurroundedByWater() { - this.setTileLocation(); final Block[] surroundingBlocks = new Block[6]; - if (this.hasWorldObj()) { - if (!this.getWorldObj().isRemote) { - surroundingBlocks[0] = this.worldObj.getBlock(this.locationX, this.locationY + 1, this.locationZ); // Above - surroundingBlocks[1] = this.worldObj.getBlock(this.locationX, this.locationY - 1, this.locationZ); // Below - surroundingBlocks[2] = this.worldObj.getBlock(this.locationX + 1, this.locationY, this.locationZ); - surroundingBlocks[3] = this.worldObj.getBlock(this.locationX - 1, this.locationY, this.locationZ); - surroundingBlocks[4] = this.worldObj.getBlock(this.locationX, this.locationY, this.locationZ + 1); - surroundingBlocks[5] = this.worldObj.getBlock(this.locationX, this.locationY, this.locationZ - 1); - int waterCount = 0; - int trapCount = 0; - for (final Block checkBlock : surroundingBlocks) { - if ((checkBlock == Blocks.water) || (checkBlock == Blocks.flowing_water) - || checkBlock.getUnlocalizedName().toLowerCase().contains("water") - || (checkBlock == ModBlocks.blockFishTrap)) { - if (checkBlock != ModBlocks.blockFishTrap) { - waterCount++; - } else { - waterCount++; - trapCount++; - } - } - } - if ((waterCount >= 2) && (trapCount <= 4)) { - int aCheck = trapCount + waterCount; - this.waterSides = MathUtils.balance(aCheck, 0, 6); - Logger.MACHINE_INFO( - "Valid Trap. " + waterCount - + " | " - + (this.tickCount / 20) - + "/" - + (this.baseTickRate / 20)); - return true; - } else if ((waterCount >= 2) && (trapCount > 4)) { - Logger.MACHINE_INFO("Too many fish traps surrounding this one."); - Logger.MACHINE_INFO( - "Not adding Loot to the fishtrap at x[" + this.locationX - + "] y[" - + this.locationY - + "] z[" - + this.locationZ - + "] (Ticking for loot every " - + this.baseTickRate - + " ticks)"); + surroundingBlocks[0] = this.worldObj.getBlock(this.xCoord, this.yCoord + 1, this.zCoord); // Above + surroundingBlocks[1] = this.worldObj.getBlock(this.xCoord, this.yCoord - 1, this.zCoord); // Below + surroundingBlocks[2] = this.worldObj.getBlock(this.xCoord + 1, this.yCoord, this.zCoord); + surroundingBlocks[3] = this.worldObj.getBlock(this.xCoord - 1, this.yCoord, this.zCoord); + surroundingBlocks[4] = this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord + 1); + surroundingBlocks[5] = this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord - 1); + int waterCount = 0; + int trapCount = 0; + for (final Block checkBlock : surroundingBlocks) { + if ((checkBlock == Blocks.water) || (checkBlock == Blocks.flowing_water) + || checkBlock.getUnlocalizedName().toLowerCase().contains("water") + || (checkBlock == ModBlocks.blockFishTrap)) { + if (checkBlock != ModBlocks.blockFishTrap) { + waterCount++; + } else { + waterCount++; + trapCount++; } } } - // Utils.LOG_MACHINE_INFO("Error finding water"); + if ((waterCount >= 2) && (trapCount <= 4)) { + int aCheck = trapCount + waterCount; + this.waterSides = MathUtils.balance(aCheck, 0, 6); + return true; + } return false; } @@ -109,43 +73,33 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { return this.inventoryContents; } - public boolean tryAddLoot() { - if (this.getInventory().getInventory() != null) { - int checkingSlot = 0; - ItemUtils.organiseInventory(getInventory()); - final ItemStack loot = this.generateLootForFishTrap().copy(); - try { - // Utils.LOG_MACHINE_INFO("Trying to add "+loot.getDisplayName()+" | "+loot.getItemDamage()); - for (final ItemStack contents : this.getInventory().getInventory()) { - - if (GT_Utility.areStacksEqual(loot, contents)) { - if (contents.stackSize < contents.getMaxStackSize()) { - // Utils.LOG_MACHINE_INFO("3-Trying to add one more "+loot.getDisplayName()+"meta: - // "+loot.getItemDamage()+" to an existing stack of "+contents.getDisplayName()+" with a - // size of "+contents.stackSize); - contents.stackSize++; - this.markDirty(); - return true; - } - } - checkingSlot++; - } - checkingSlot = 0; - for (final ItemStack contents : this.getInventory().getInventory()) { - if (contents == null) { - // Utils.LOG_MACHINE_INFO("Adding Item To Empty Slot. "+(checkingSlot+1)); - this.getInventory().setInventorySlotContents(checkingSlot, loot); - this.markDirty(); - return true; - } - checkingSlot++; + private void tryAddLoot() { + ItemUtils.organiseInventory(getInventory()); + final ItemStack loot = this.generateLootForFishTrap(); + if (loot == null) { + return; + } + for (final ItemStack contents : this.getInventory().getInventory()) { + if (GT_Utility.areStacksEqual(loot, contents)) { + if (contents.stackSize < contents.getMaxStackSize()) { + contents.stackSize++; + this.markDirty(); + return; } - } catch (final NullPointerException n) {} + } + } + int checkingSlot = 0; + for (final ItemStack contents : this.getInventory().getInventory()) { + if (contents == null) { + this.getInventory().setInventorySlotContents(checkingSlot, loot); + this.markDirty(); + return; + } + checkingSlot++; } - this.markDirty(); - return false; } + @Nullable private ItemStack generateLootForFishTrap() { final int lootWeight = MathUtils.randInt(0, 100); ItemStack loot; @@ -161,7 +115,7 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { // Junk Loot else if (lootWeight <= 23) { if (PamsHarvestCraft.isModLoaded()) { - loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken(seaweed, 1); + loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken("cropSeaweed", 1); } else { loot = ItemUtils.getSimpleStack(Blocks.dirt); } @@ -173,15 +127,9 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { } else if (lootWeight == 100) { final int rareLoot = MathUtils.randInt(1, 10); if (rareLoot <= 4) { - loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken("nuggetIron", 1); - if (loot == null) { - loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken("ingotIron", 1); - } + loot = Materials.Iron.getNuggets(1); } else if (rareLoot <= 7) { - loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken("nuggetGold", 1); - if (loot == null) { - loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken("ingotGold", 1); - } + loot = ItemUtils.getSimpleStack(Items.gold_nugget); } else if (rareLoot <= 9) { loot = ItemUtils.getSimpleStack(Items.emerald); } else { @@ -190,67 +138,48 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { } else { loot = ItemUtils.getSimpleStack(Blocks.diamond_ore); } - loot.stackSize = 1; - Logger.MACHINE_INFO("Adding x" + loot.stackSize + " " + loot.getDisplayName() + "."); + if (loot != null) { + loot.stackSize = 1; + } return loot; } @Override public void updateEntity() { - try { - if (!this.worldObj.isRemote) { - this.tickCount++; - // Logger.MACHINE_INFO("Ticking "+this.tickCount); - // Check if the Tile is within water once per second. - if ((this.tickCount % 20) == 0) { - this.isInWater = this.isSurroundedByWater(); - } + if (this.worldObj.isRemote) { + return; + } - if (this.isInWater) { - this.calculateTickrate(); - } + this.tickCount++; + if ((this.tickCount % 20) == 0) { + this.isInWater = this.isSurroundedByWater(); + } - // Try add some loot once every 30 seconds. - if (this.tickCount >= this.baseTickRate) { - if (this.isInWater) { - // Add loot - Logger.MACHINE_INFO( - "Adding Loot to the fishtrap at x[" + this.locationX - + "] y[" - + this.locationY - + "] z[" - + this.locationZ - + "] (Ticking for loot every " - + this.baseTickRate - + " ticks)"); - - int aExtraLootChance = MathUtils.randInt(1, 1000); - if (aExtraLootChance >= 999) { - this.tryAddLoot(); - this.tryAddLoot(); - this.tryAddLoot(); - } else { - this.tryAddLoot(); - } - - this.markDirty(); - } else { - Logger.MACHINE_INFO("Not in water."); - this.markDirty(); - } - this.tickCount = 0; - } - if (this.tickCount >= (this.baseTickRate + 500)) { - Logger.MACHINE_INFO("Resetting tick counter"); - this.tickCount = 0; + if (this.isInWater) { + this.calculateTickrate(); + } + + if (this.tickCount >= this.baseTickRate) { + if (this.isInWater) { + int aExtraLootChance = MathUtils.randInt(1, 1000); + if (aExtraLootChance >= 999) { + this.tryAddLoot(); + this.tryAddLoot(); + this.tryAddLoot(); + } else { + this.tryAddLoot(); } + this.markDirty(); } - } catch (final Throwable t) {} + this.tickCount = 0; + } + if (this.tickCount >= (this.baseTickRate + 500)) { + this.tickCount = 0; + } } public void calculateTickrate() { int water = this.waterSides; - // int variance = (int) ((MathUtils.randInt(-200, 200)/water)*0.5); if (water <= 1) { this.baseTickRate = 0; } else if (water == 2) { @@ -264,26 +193,11 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { } else { this.baseTickRate = 1750; } - if (water > 1) { - // this.baseTickRate += variance; - } - } - - public boolean anyPlayerInRange() { - return this.worldObj.getClosestPlayer(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D, 32) != null; - } - - public NBTTagCompound getTag(final NBTTagCompound nbt, final String tag) { - if (!nbt.hasKey(tag)) { - nbt.setTag(tag, new NBTTagCompound()); - } - return nbt.getCompoundTag(tag); } @Override public void writeToNBT(final NBTTagCompound nbt) { super.writeToNBT(nbt); - // Utils.LOG_MACHINE_INFO("Trying to write NBT data to TE."); final NBTTagCompound chestData = new NBTTagCompound(); this.inventoryContents.writeToNBT(chestData); nbt.setTag("ContentsChest", chestData); @@ -295,25 +209,12 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { @Override public void readFromNBT(final NBTTagCompound nbt) { super.readFromNBT(nbt); - // Utils.LOG_MACHINE_INFO("Trying to read NBT data from TE."); this.inventoryContents.readFromNBT(nbt.getCompoundTag("ContentsChest")); if (nbt.hasKey("CustomName", 8)) { this.setCustomName(nbt.getString("CustomName")); } } - static final String prefix = "food"; - static final String suffix = "raw"; - static final String seaweed = "cropSeaweed"; - static final String greenheartFish = "Greenheartfish"; - private static final String[] harvestcraftFish = { "Anchovy", "Bass", "Carp", "Catfish", "Charr", "Clam", "Crab", - "Crayfish", "Eel", "Frog", "Grouper", "Herring", "Jellyfish", "Mudfish", "Octopus", "Perch", "Scallop", - "Shrimp", "Snail", "Snapper", "Tilapia", "Trout", "Tuna", "Turtle", "Walleye" }; - - public static void pamsHarvestCraftCompat() { - for (int i = 0; i < harvestcraftFish.length; i++) {} - } - @Override public int getSizeInventory() { return this.getInventory().getSizeInventory(); @@ -404,6 +305,6 @@ public class TileEntityFishTrap extends TileEntity implements ISidedInventory { @Override public boolean hasCustomInventoryName() { - return (this.customName != null) && !this.customName.equals(""); + return (this.customName != null) && !this.customName.isEmpty(); } } diff --git a/src/main/java/gtPlusPlus/core/util/minecraft/ItemUtils.java b/src/main/java/gtPlusPlus/core/util/minecraft/ItemUtils.java index b07ca24990..5a7a014884 100644 --- a/src/main/java/gtPlusPlus/core/util/minecraft/ItemUtils.java +++ b/src/main/java/gtPlusPlus/core/util/minecraft/ItemUtils.java @@ -73,22 +73,16 @@ public class ItemUtils { } public static ItemStack getSimpleStack(final Item x, final int i) { - try { - final ItemStack r = new ItemStack(x, i); - return r.copy(); - } catch (final Throwable e) { - return null; - } + return new ItemStack(x, i); } public static ItemStack getSimpleStack(final ItemStack x, final int i) { - try { - final ItemStack r = x.copy(); - r.stackSize = i; - return r; - } catch (final Throwable e) { + if (x == null) { return null; } + final ItemStack r = x.copy(); + r.stackSize = i; + return r; } public static final int WILDCARD_VALUE = Short.MAX_VALUE; |