/
TileEntityFishTrap.java
398 lines (353 loc) · 12.2 KB
/
TileEntityFishTrap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package gtPlusPlus.core.tileentities.general;
import java.util.Random;
import gregtech.api.util.GT_Utility;
import gtPlusPlus.core.block.ModBlocks;
import gtPlusPlus.core.inventories.InventoryFishTrap;
import gtPlusPlus.core.lib.LoadedMods;
import gtPlusPlus.core.util.Utils;
import gtPlusPlus.core.util.item.ItemUtils;
import gtPlusPlus.core.util.math.MathUtils;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.FishingHooks;
public class TileEntityFishTrap extends TileEntity implements ISidedInventory {
private int tickCount = 0;
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();
}
public boolean setTileLocation() {
if (this.hasWorldObj()) {
if (!this.getWorldObj().isRemote) {
this.locationX = this.xCoord;
this.locationY = this.yCoord;
this.locationZ = this.zCoord;
return true;
}
}
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)) {
this.waterSides = waterCount;
return true;
}
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("Error finding water");
return false;
}
public InventoryFishTrap getInventory() {
return this.inventoryContents;
}
public boolean tryAddLoot() {
if (this.getInventory().getInventory() != null) {
int checkingSlot = 0;
final ItemStack loot = this.generateLootForFishTrap().copy();
try {
//Utils.LOG_WARNING("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_WARNING("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_WARNING("Adding Item To Empty Slot. "+(checkingSlot+1));
this.getInventory().setInventorySlotContents(checkingSlot, loot);
this.markDirty();
return true;
}
checkingSlot++;
}
}
catch (final NullPointerException n) {
}
}
this.markDirty();
return false;
}
private ItemStack generateLootForFishTrap() {
final int lootWeight = MathUtils.randInt(0, 100);
ItemStack loot;
if (lootWeight <= 5) {
loot = ItemUtils.getSimpleStack(Items.slime_ball);
}
else if (lootWeight <= 10) {
loot = ItemUtils.getSimpleStack(Items.bone);
}
else if (lootWeight <= 15) {
loot = ItemUtils.getSimpleStack(Blocks.sand);
}
else if (lootWeight <= 20) {
loot = ItemUtils.simpleMetaStack(Items.dye, 0, 1);
}
// Junk Loot
else if (lootWeight <= 23) {
if (LoadedMods.PamsHarvestcraft) {
loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken(seaweed, 1);
}
else {
loot = ItemUtils.getSimpleStack(Blocks.dirt);
}
}
// Pam Fish
else if (lootWeight <= 99) {
final Random xstr = new Random();
loot = FishingHooks.getRandomFishable(xstr, 100);
}
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);
}
}
else if (rareLoot <= 7) {
loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken("nuggetGold", 1);
if (loot == null){
loot = ItemUtils.getItemStackOfAmountFromOreDictNoBroken("ingotGold", 1);
}
}
else if (rareLoot <= 9){
loot = ItemUtils.getSimpleStack(Items.emerald);
}
else {
loot = ItemUtils.getSimpleStack(Items.diamond);
}
}
else {
loot = ItemUtils.getSimpleStack(Blocks.diamond_ore);
}
loot.stackSize=1;
Utils.LOG_WARNING("Adding x"+loot.stackSize+" "+loot.getDisplayName()+".");
return loot;
}
@Override
public void updateEntity() {
try{
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) {
this.isInWater = this.isSurroundedByWater();
}
else {
}
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)");
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)");
this.markDirty();
}
this.tickCount = 0;
}
if (this.tickCount > (this.baseTickRate + 500)) {
this.tickCount = 0;
}
}
}
catch (final Throwable t){}
}
public void calculateTickrate() {
int calculateTickrate = 0;
if (this.waterSides <= 2) {
calculateTickrate = 0;
}
else if ((this.waterSides > 2) && (this.waterSides < 4)) {
calculateTickrate = 3600;
}
else if ((this.waterSides >= 4) && (this.waterSides < 6)) {
calculateTickrate = 2400;
}
else if (this.waterSides == 6) {
calculateTickrate = 1200;
}
this.baseTickRate = calculateTickrate;
}
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_WARNING("Trying to write NBT data to TE.");
final NBTTagCompound chestData = new NBTTagCompound();
this.inventoryContents.writeToNBT(chestData);
nbt.setTag("ContentsChest", chestData);
if (this.hasCustomInventoryName()) {
nbt.setString("CustomName", this.getCustomName());
}
}
@Override
public void readFromNBT(final NBTTagCompound nbt) {
super.readFromNBT(nbt);
// Utils.LOG_WARNING("Trying to read NBT data from TE.");
this.inventoryContents.readFromNBT(nbt.getCompoundTag("ContentsChest"));
if (nbt.hasKey("CustomName", 8)) {
this.setCustomName(nbt.getString("CustomName"));
}
}
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" };
public static void pamsHarvestCraftCompat() {
for (int i = 0; i < harvestcraftFish.length; i++) {
}
}
@Override
public int getSizeInventory() {
return this.getInventory().getSizeInventory();
}
@Override
public ItemStack getStackInSlot(final int slot) {
return this.getInventory().getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(final int slot, final int count) {
return this.getInventory().decrStackSize(slot, count);
}
@Override
public ItemStack getStackInSlotOnClosing(final int slot) {
return this.getInventory().getStackInSlotOnClosing(slot);
}
@Override
public void setInventorySlotContents(final int slot, final ItemStack stack) {
this.getInventory().setInventorySlotContents(slot, stack);
}
@Override
public int getInventoryStackLimit() {
return this.getInventory().getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(final EntityPlayer entityplayer) {
return this.getInventory().isUseableByPlayer(entityplayer);
}
@Override
public void openInventory() {
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, 1);
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
this.getInventory().openInventory();
}
@Override
public void closeInventory() {
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, 1);
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
this.getInventory().closeInventory();
}
@Override
public boolean isItemValidForSlot(final int slot, final ItemStack itemstack) {
return this.getInventory().isItemValidForSlot(slot, itemstack);
}
@Override
public int[] getAccessibleSlotsFromSide(final int p_94128_1_) {
final int[] accessibleSides = new int[this.getSizeInventory()];
for (int r=0; r<this.getInventory().getSizeInventory(); r++){
accessibleSides[r]=r;
}
return accessibleSides;
}
@Override
public boolean canInsertItem(final int p_102007_1_, final ItemStack p_102007_2_, final int p_102007_3_) {
return false;
}
@Override
public boolean canExtractItem(final int p_102008_1_, final ItemStack p_102008_2_, final int p_102008_3_) {
return true;
}
public String getCustomName() {
return this.customName;
}
public void setCustomName(final String customName) {
this.customName = customName;
}
@Override
public String getInventoryName() {
return this.hasCustomInventoryName() ? this.customName : "container.fishrap";
}
@Override
public boolean hasCustomInventoryName() {
return (this.customName != null) && !this.customName.equals("");
}
}