diff options
author | Draknyte1 <Draknyte1@hotmail.com> | 2017-05-28 11:28:05 +1000 |
---|---|---|
committer | Draknyte1 <Draknyte1@hotmail.com> | 2017-05-28 11:28:05 +1000 |
commit | f9fbf60c29f502f90659f68e30bb264d3b1fd873 (patch) | |
tree | adf206c6c581690dfd36de6e66639a721c748d19 /src/Java/gtPlusPlus | |
parent | c122962e3973990b937dce47c96a7758b6c5e18e (diff) | |
download | GT5-Unofficial-f9fbf60c29f502f90659f68e30bb264d3b1fd873.tar.gz GT5-Unofficial-f9fbf60c29f502f90659f68e30bb264d3b1fd873.tar.bz2 GT5-Unofficial-f9fbf60c29f502f90659f68e30bb264d3b1fd873.zip |
$ Fixed another sneaky NPE crash from loot addition to the Fish Trap.
Diffstat (limited to 'src/Java/gtPlusPlus')
-rw-r--r-- | src/Java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java | 190 |
1 files changed, 101 insertions, 89 deletions
diff --git a/src/Java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java b/src/Java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java index 30dbe2ba5a..3f6f2676a0 100644 --- a/src/Java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java +++ b/src/Java/gtPlusPlus/core/tileentities/general/TileEntityFishTrap.java @@ -13,7 +13,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; -public class TileEntityFishTrap extends TileEntity{ +public class TileEntityFishTrap extends TileEntity { private int tickCount = 0; private boolean isInWater = false; @@ -22,16 +22,18 @@ public class TileEntityFishTrap extends TileEntity{ private int locationY; private int locationZ; private int waterSides = 0; - private int baseTickRate = 600*5; + private int baseTickRate = 600 * 5; - public TileEntityFishTrap(){ - this.inventoryContents = new InventoryFishTrap();//number of slots - without product slot + public TileEntityFishTrap() { + this.inventoryContents = new InventoryFishTrap();// number of slots - + // without product + // slot this.setTileLocation(); } - public boolean setTileLocation(){ - if (this.hasWorldObj()){ - if (!this.getWorldObj().isRemote){ + public boolean setTileLocation() { + if (this.hasWorldObj()) { + if (!this.getWorldObj().isRemote) { this.locationX = this.xCoord; this.locationY = this.yCoord; this.locationZ = this.zCoord; @@ -41,22 +43,24 @@ public class TileEntityFishTrap extends TileEntity{ return false; } - public final boolean isSurroundedByWater(){ + 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); + 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){ + 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 { @@ -65,42 +69,48 @@ public class TileEntityFishTrap extends TileEntity{ } } } - if ((waterCount >= 2) && (trapCount <= 4)){ + if ((waterCount >= 2) && (trapCount <= 4)) { this.waterSides = waterCount; return true; } - else if ((waterCount >= 2) && (trapCount > 4)){ + else if ((waterCount >= 2) && (trapCount > 4)) { Utils.LOG_WARNING("Too many fish traps surrounding this one."); - Utils.LOG_WARNING("Not adding Loot to the fishtrap at x["+this.locationX+"] y["+this.locationY+"] z["+this.locationZ+"] (Ticking for loot every "+this.baseTickRate+" ticks)"); + Utils.LOG_WARNING("Not adding Loot to the fishtrap at x[" + this.locationX + "] y[" + this.locationY + + "] z[" + this.locationZ + "] (Ticking for loot every " + this.baseTickRate + " ticks)"); } } } - //Utils.LOG_WARNING("Error finding water"); + // Utils.LOG_WARNING("Error finding water"); return false; } - public InventoryFishTrap getInventory(){ + public InventoryFishTrap getInventory() { return this.inventoryContents; } - public boolean tryAddLoot(){ - if (this.getInventory().getInventory() != null){ + public boolean tryAddLoot() { + if (this.getInventory().getInventory() != null) { int checkingSlot = 0; final ItemStack loot = this.generateLootForFishTrap(); - for (final ItemStack contents : this.getInventory().getInventory()){ - if (contents == null){ - this.getInventory().setInventorySlotContents(checkingSlot, loot); - this.markDirty(); - return true; - } - else if (contents.getItem() == loot.getItem() && contents.stackSize <= contents.getMaxStackSize()-1){ - if (contents.stackSize < contents.getMaxStackSize()){ - contents.stackSize++; + try { + for (final ItemStack contents : this.getInventory().getInventory()) { + if (contents == null) { + this.getInventory().setInventorySlotContents(checkingSlot, loot); this.markDirty(); return true; } + else if (contents.getItem() == loot.getItem() + && contents.stackSize <= contents.getMaxStackSize() - 1) { + if (contents.stackSize < contents.getMaxStackSize()) { + contents.stackSize++; + this.markDirty(); + return true; + } + } + checkingSlot++; } - checkingSlot++; + } + catch (NullPointerException n) { } } this.markDirty(); @@ -108,32 +118,33 @@ public class TileEntityFishTrap extends TileEntity{ } private ItemStack generateLootForFishTrap() { - final int lootWeight = MathUtils.randInt(0, 100); + final int lootWeight = MathUtils.randInt(0, 100); ItemStack loot; - if (lootWeight <= 5){ + if (lootWeight <= 5) { loot = ItemUtils.getSimpleStack(Items.slime_ball); } - else if (lootWeight <= 15){ + else if (lootWeight <= 15) { loot = ItemUtils.getSimpleStack(Items.bone); } - else if (lootWeight <= 25){ + else if (lootWeight <= 25) { loot = ItemUtils.getSimpleStack(Blocks.sand); } - else if (lootWeight <= 35){ + else if (lootWeight <= 35) { loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken(seaweed, 1); } - //Pam Fish - else if (lootWeight <= 70){ - if (LoadedMods.PamsHarvestcraft){ - loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken(prefix+harvestcraftFish[MathUtils.randInt(0, harvestcraftFish.length-1)]+suffix, 1); + // Pam Fish + else if (lootWeight <= 70) { + if (LoadedMods.PamsHarvestcraft) { + loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken( + prefix + harvestcraftFish[MathUtils.randInt(0, harvestcraftFish.length - 1)] + suffix, 1); } else { - loot = ItemUtils.getSimpleStack(minecraftFish[MathUtils.randInt(0, minecraftFish.length-1)], 1); + loot = ItemUtils.getSimpleStack(minecraftFish[MathUtils.randInt(0, minecraftFish.length - 1)], 1); } } - //Minecraft Fish - else if (lootWeight <= 100){ - loot = ItemUtils.getSimpleStack(minecraftFish[MathUtils.randInt(0, minecraftFish.length-1)], 1); + // Minecraft Fish + else if (lootWeight <= 100) { + loot = ItemUtils.getSimpleStack(minecraftFish[MathUtils.randInt(0, minecraftFish.length - 1)], 1); } else { loot = ItemUtils.getSimpleStack(Blocks.diamond_ore); @@ -142,106 +153,107 @@ public class TileEntityFishTrap extends TileEntity{ } @Override - public void updateEntity(){ - if (!this.worldObj.isRemote){ + public void updateEntity() { + if (!this.worldObj.isRemote) { this.tickCount++; - //Utils.LOG_WARNING("Ticking "+this.tickCount); - //Check if the Tile is within water once per second. - if ((this.tickCount%20)==0){ + // Utils.LOG_WARNING("Ticking "+this.tickCount); + // Check if the Tile is within water once per second. + if ((this.tickCount % 20) == 0) { this.isInWater = this.isSurroundedByWater(); } else { } - if (this.isInWater){ + if (this.isInWater) { this.calculateTickrate(); } - //Try add some loot once every 30 seconds. - if ((this.tickCount%this.baseTickRate)==0){ - if (this.isInWater){ - //Add loot - //Utils.LOG_WARNING("Adding Loot to the fishtrap at x["+this.locationX+"] y["+this.locationY+"] z["+this.locationZ+"] (Ticking for loot every "+this.baseTickRate+" ticks)"); + // Try add some loot once every 30 seconds. + if ((this.tickCount % this.baseTickRate) == 0) { + if (this.isInWater) { + // Add loot + // Utils.LOG_WARNING("Adding Loot to the fishtrap at + // x["+this.locationX+"] y["+this.locationY+"] + // z["+this.locationZ+"] (Ticking for loot every + // "+this.baseTickRate+" ticks)"); this.tryAddLoot(); this.markDirty(); } else { Utils.LOG_WARNING("This Trap does not have enough water around it."); - Utils.LOG_WARNING("Not adding Loot to the fishtrap at x["+this.locationX+"] y["+this.locationY+"] z["+this.locationZ+"] (Ticking for loot every "+this.baseTickRate+" ticks)"); + Utils.LOG_WARNING("Not adding Loot to the fishtrap at x[" + this.locationX + "] y[" + this.locationY + + "] z[" + this.locationZ + "] (Ticking for loot every " + this.baseTickRate + " ticks)"); this.markDirty(); } this.tickCount = 0; } - if (this.tickCount > (this.baseTickRate+500)){ + if (this.tickCount > (this.baseTickRate + 500)) { this.tickCount = 0; } - } } - public void calculateTickrate(){ + public void calculateTickrate() { int calculateTickrate = 0; - if (this.waterSides < 2){ + if (this.waterSides < 2) { calculateTickrate = 0; } - else if ((this.waterSides >= 2) && (this.waterSides < 4)){ + else if ((this.waterSides >= 2) && (this.waterSides < 4)) { calculateTickrate = 3000; } - else if ((this.waterSides >= 4) && (this.waterSides < 6)){ + else if ((this.waterSides >= 4) && (this.waterSides < 6)) { calculateTickrate = 2000; } - else if (this.waterSides == 6){ + else if (this.waterSides == 6) { calculateTickrate = 900; } this.baseTickRate = calculateTickrate; } - public boolean anyPlayerInRange(){ + 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)){ + 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){ + public void writeToNBT(final NBTTagCompound nbt) { super.writeToNBT(nbt); - //Utils.LOG_WARNING("Trying to write NBT data to TE."); + // Utils.LOG_WARNING("Trying to write NBT data to TE."); final NBTTagCompound chestData = new NBTTagCompound(); this.inventoryContents.writeToNBT(chestData); nbt.setTag("ContentsChest", chestData); } @Override - public void readFromNBT(final NBTTagCompound nbt){ + public void readFromNBT(final NBTTagCompound nbt) { super.readFromNBT(nbt); - //Utils.LOG_WARNING("Trying to read NBT data from TE."); + // Utils.LOG_WARNING("Trying to read NBT data from TE."); this.inventoryContents.readFromNBT(nbt.getCompoundTag("ContentsChest")); } - + final static String prefix = "food"; final static String suffix = "raw"; final static String seaweed = "cropSeaweed"; final static 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"}; - - private static final ItemStack[] minecraftFish = { - ItemUtils.getItemStack("minecraft:fish", 1), ItemUtils.getItemStack("minecraft:fish:1", 1), - ItemUtils.getItemStack("minecraft:fish:2", 1), ItemUtils.getItemStack("minecraft:fish:3", 1) - }; - - public static void pamsHarvestCraftCompat(){ - for (int i = 0; i < harvestcraftFish.length; i++){ - + 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" }; + + private static final ItemStack[] minecraftFish = { ItemUtils.getItemStack("minecraft:fish", 1), + ItemUtils.getItemStack("minecraft:fish:1", 1), ItemUtils.getItemStack("minecraft:fish:2", 1), + ItemUtils.getItemStack("minecraft:fish:3", 1) }; + + public static void pamsHarvestCraftCompat() { + for (int i = 0; i < harvestcraftFish.length; i++) { + } } |