Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
miozune committed Oct 28, 2023
1 parent 2961fe2 commit 07e1700
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.util.Random;

import javax.annotation.Nullable;

import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
Expand All @@ -16,7 +18,6 @@

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;
Expand All @@ -28,125 +29,77 @@ 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;
}

public InventoryFishTrap getInventory() {
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;
Expand All @@ -162,7 +115,7 @@ private ItemStack generateLootForFishTrap() {
// 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);
}
Expand All @@ -185,67 +138,48 @@ else if (lootWeight <= 99) {
} 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) {
Expand All @@ -259,26 +193,11 @@ public void calculateTickrate() {
} 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);
Expand All @@ -290,25 +209,12 @@ public void writeToNBT(final NBTTagCompound nbt) {
@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();
Expand Down Expand Up @@ -399,6 +305,6 @@ public String getInventoryName() {

@Override
public boolean hasCustomInventoryName() {
return (this.customName != null) && !this.customName.equals("");
return (this.customName != null) && !this.customName.isEmpty();
}
}
16 changes: 5 additions & 11 deletions src/main/java/gtPlusPlus/core/util/minecraft/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,16 @@ public static ItemStack getSimpleStack(final Block x, int meta, int i) {
}

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;
Expand Down

0 comments on commit 07e1700

Please sign in to comment.