Skip to content

Commit a4b0822

Browse files
implement fisher situation widget
widget updates really slowly due to the ticking logic
1 parent 65d1a04 commit a4b0822

3 files changed

Lines changed: 42 additions & 26 deletions

File tree

src/main/java/gregtech/api/gui/widgets/SituationWidget.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ public SituationWidget setImage() {
6767
@Override
6868
public void detectAndSendChanges() {
6969
super.detectAndSendChanges();
70-
if (currentSituationSupplier.get().id != currentId) {
71-
this.currentId = currentSituationSupplier.get().id;
72-
writeUpdateInfo(1, buf -> buf.writeVarInt(currentId));
70+
if (currentSituationSupplier.get() != null) {
71+
if (currentSituationSupplier.get().id != currentId) {
72+
this.currentId = currentSituationSupplier.get().id;
73+
writeUpdateInfo(1, buf -> buf.writeVarInt(currentId));
74+
}
7375
}
7476
}
7577

src/main/java/gregtech/api/situation/Situations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Situations {
2222
public static Situation NO_IMPORT_TANK = new Situation(203, "no_import_tank", SituationTypes.ERROR);
2323
public static Situation NO_EXPORT_TANK = new Situation(204, "no_export_tank", SituationTypes.ERROR);
2424
public static Situation EXPECTED_CAPABILITY_UNAVAILABLE = new Situation(205, "null_capability", SituationTypes.ERROR);
25+
public static Situation WATER_CHECK_FAILED = new Situation(206, "water_check_failed", SituationTypes.ERROR);
2526

2627
public static void init() {
2728
GTLog.logger.info("Registering situations...");

src/main/java/gregtech/common/metatileentities/electric/MetaTileEntityFisher.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import gregtech.api.GTValues;
77
import gregtech.api.gui.GuiTextures;
88
import gregtech.api.gui.ModularUI;
9+
import gregtech.api.gui.widgets.SituationWidget;
910
import gregtech.api.gui.widgets.SlotWidget;
1011
import gregtech.api.metatileentity.MetaTileEntity;
1112
import gregtech.api.metatileentity.MetaTileEntityHolder;
@@ -33,6 +34,8 @@
3334
import javax.annotation.Nullable;
3435
import java.util.List;
3536

37+
import static gregtech.api.situation.Situations.*;
38+
3639
public class MetaTileEntityFisher extends TieredMetaTileEntity {
3740

3841
private static final int WATER_CHECK_SIZE = 25;
@@ -62,7 +65,8 @@ protected ModularUI createUI(EntityPlayer entityPlayer) {
6265
18 + 18 * rowSize + 94)
6366
.label(10, 5, getMetaFullName())
6467
.widget(new SlotWidget(importItems, 0, 18, 18, true, true)
65-
.setBackgroundTexture(GuiTextures.SLOT, GuiTextures.STRING_SLOT_OVERLAY));
68+
.setBackgroundTexture(GuiTextures.SLOT, GuiTextures.STRING_SLOT_OVERLAY))
69+
.widget(new SituationWidget(19,38,16,16,this::getSituation));
6670

6771
for (int y = 0; y < rowSize; y++) {
6872
for (int x = 0; x < rowSize; x++) {
@@ -80,32 +84,41 @@ protected ModularUI createUI(EntityPlayer entityPlayer) {
8084
public void update() {
8185
super.update();
8286
ItemStack baitStack = importItems.getStackInSlot(0);
83-
if (!getWorld().isRemote && energyContainer.getEnergyStored() >= energyAmountPerFish && getTimer() % fishingTicks == 0L && !baitStack.isEmpty()) {
84-
WorldServer world = (WorldServer) this.getWorld();
85-
int waterCount = 0;
86-
int edgeSize = (int) Math.sqrt(WATER_CHECK_SIZE);
87-
for (int x = 0; x < edgeSize; x++){
88-
for (int z = 0; z < edgeSize; z++){
89-
BlockPos waterCheckPos = getPos().down().add(x - edgeSize / 2, 0, z - edgeSize / 2);
90-
if (world.getBlockState(waterCheckPos).getBlock() instanceof BlockLiquid &&
91-
world.getBlockState(waterCheckPos).getMaterial() == Material.WATER) {
92-
waterCount++;
87+
if (!getWorld().isRemote) {
88+
if (baitStack.isEmpty()) {
89+
setSituation(IDLE);
90+
} else if (energyContainer.getEnergyStored() < energyAmountPerFish) {
91+
setSituation(INSUFFICIENT_POWER_TO_START);
92+
} else if (getTimer() % fishingTicks == 0L) {
93+
setSituation(WORKING);
94+
WorldServer world = (WorldServer) this.getWorld();
95+
int waterCount = 0;
96+
int edgeSize = (int) Math.sqrt(WATER_CHECK_SIZE);
97+
for (int x = 0; x < edgeSize; x++) {
98+
for (int z = 0; z < edgeSize; z++) {
99+
BlockPos waterCheckPos = getPos().down().add(x - edgeSize / 2, 0, z - edgeSize / 2);
100+
if (world.getBlockState(waterCheckPos).getBlock() instanceof BlockLiquid &&
101+
world.getBlockState(waterCheckPos).getMaterial() == Material.WATER) {
102+
waterCount++;
103+
}
93104
}
94105
}
95-
}
96-
if (waterCount == WATER_CHECK_SIZE) {
97-
LootTable table = world.getLootTableManager().getLootTableFromLocation(LootTableList.GAMEPLAY_FISHING);
98-
NonNullList<ItemStack> itemStacks = NonNullList.create();
99-
itemStacks.addAll(table.generateLootForPools(world.rand, new LootContext.Builder(world).build()));
100-
if(addItemsToItemHandler(exportItems, true, itemStacks)) {
101-
addItemsToItemHandler(exportItems, false, itemStacks);
102-
energyContainer.removeEnergy(energyAmountPerFish);
103-
baitStack.shrink(1);
106+
if (waterCount == WATER_CHECK_SIZE) {
107+
LootTable table = world.getLootTableManager().getLootTableFromLocation(LootTableList.GAMEPLAY_FISHING);
108+
NonNullList<ItemStack> itemStacks = NonNullList.create();
109+
itemStacks.addAll(table.generateLootForPools(world.rand, new LootContext.Builder(world).build()));
110+
if (addItemsToItemHandler(exportItems, true, itemStacks)) {
111+
addItemsToItemHandler(exportItems, false, itemStacks);
112+
energyContainer.removeEnergy(energyAmountPerFish);
113+
baitStack.shrink(1);
114+
}
115+
} else {
116+
setSituation(WATER_CHECK_FAILED);
104117
}
105118
}
106-
}
107-
if(!getWorld().isRemote && getTimer() % 5 == 0) {
108-
pushItemsIntoNearbyHandlers(getFrontFacing());
119+
if (getTimer() % 5 == 0) {
120+
pushItemsIntoNearbyHandlers(getFrontFacing());
121+
}
109122
}
110123
}
111124

0 commit comments

Comments
 (0)