Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Status widget for machines and covers #1303

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
17be9d1
Add Widget to show cover functional status
PrototypeTrousers Nov 12, 2020
f2a9f17
handle null capabilty issue and show that on the GUI
PrototypeTrousers Nov 12, 2020
193bf92
Trigger issue diagnose on cover self capability update
PrototypeTrousers Nov 16, 2020
8f0f561
Functioning Registry add/get
PrototypeTrousers Nov 25, 2020
f886fc4
new icons and update references
PrototypeTrousers Nov 25, 2020
616f97f
Add some hooks in power and recipe logic
PrototypeTrousers Nov 25, 2020
43a1fd6
renaming
PrototypeTrousers Nov 25, 2020
44f4d70
add idle logic
PrototypeTrousers Nov 25, 2020
1e98dee
tweakidle logic and add full destination inventory status
PrototypeTrousers Nov 25, 2020
a4b5852
error texture goes in the registry
PrototypeTrousers Nov 26, 2020
1217f2a
Set up basic machine logic
PrototypeTrousers Nov 27, 2020
54129f1
Split Status Registry and Entries
PrototypeTrousers Nov 29, 2020
5861f40
rename resource to match statustype entry
PrototypeTrousers Nov 29, 2020
61c9ab4
rename to match Situation and Situations values
PrototypeTrousers Nov 29, 2020
3f59f98
add machine controller situation logic to covers and machines
PrototypeTrousers Nov 29, 2020
110da14
fix recipe logic not updating on world reload
PrototypeTrousers Nov 30, 2020
7d794b3
Move Situation to its own folder and update its references
PrototypeTrousers Dec 6, 2020
c9018f2
moved covers situation to coverbehaviors
PrototypeTrousers Dec 6, 2020
1830fd7
move hooveringtooltip to widget class
PrototypeTrousers Dec 6, 2020
8af62d7
generate localename from situation name
PrototypeTrousers Dec 6, 2020
a9a4262
missing import
PrototypeTrousers Dec 6, 2020
3a03b77
if to switch
PrototypeTrousers Dec 6, 2020
9a00da7
make loop cheaper
PrototypeTrousers Dec 8, 2020
b73ed95
move away from int supplier
PrototypeTrousers Dec 8, 2020
f705c6d
simplify switch, move !isWorkingAllowed back
PrototypeTrousers Dec 16, 2020
a7e3ca9
fix declarations and string for cover localized names
PrototypeTrousers Dec 16, 2020
667cb11
cleanup unused functions
PrototypeTrousers Dec 16, 2020
3dab8f8
fix switch
PrototypeTrousers Dec 16, 2020
e146794
reverted unintended logic changes and remove unused functions
PrototypeTrousers Dec 16, 2020
57ebefa
fix widget not syncing on cover GUI
PrototypeTrousers Dec 18, 2020
d83c93b
dont check if our capaibily is null
PrototypeTrousers Dec 18, 2020
1ae038f
add more situations to the recipe logic
PrototypeTrousers Dec 18, 2020
ac5f094
implement fisher situation widget
PrototypeTrousers Jan 11, 2021
0ae37a6
reworked idle logic
PrototypeTrousers Feb 3, 2021
6d2af09
get error type as string
PrototypeTrousers Feb 17, 2021
8be3ae5
Add to Air collector logic
PrototypeTrousers Feb 24, 2021
8e7c421
initial steam support
PrototypeTrousers Mar 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/gregtech/GregTechMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import codechicken.lib.CodeChickenLib;
import crafttweaker.CraftTweakerAPI;
import gregtech.api.GTValues;
import gregtech.api.situation.Situations;
import gregtech.api.capability.SimpleCapabilityManager;
import gregtech.api.cover.CoverBehaviorUIFactory;
import gregtech.api.items.gui.PlayerInventoryUIFactory;
Expand Down Expand Up @@ -82,6 +83,7 @@ public void onConstruction(FMLConstructionEvent event) {
public void onPreInit(FMLPreInitializationEvent event) {
GTLog.init(event.getModLog());
NetworkHandler.init();
Situations.init();
MetaTileEntityUIFactory.INSTANCE.init();
PlayerInventoryUIFactory.INSTANCE.init();
CoverBehaviorUIFactory.INSTANCE.init();
Expand Down
56 changes: 42 additions & 14 deletions src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import java.util.List;
import java.util.Random;

import static gregtech.api.situation.Situations.*;

public abstract class AbstractRecipeLogic extends MTETrait implements IWorkable {

public final RecipeMap<?> recipeMap;

protected boolean forceRecipeRecheck;
protected ItemStack[] lastItemInputs;
protected FluidStack[] lastFluidInputs;
protected Recipe currentRecipe;
protected Recipe previousRecipe;
protected boolean allowOverclocking = true;

Expand All @@ -48,6 +51,7 @@ public abstract class AbstractRecipeLogic extends MTETrait implements IWorkable
protected boolean workingEnabled = true;
protected boolean hasNotEnoughEnergy;
protected boolean wasActiveAndNeedsUpdate;
protected boolean isInputsEmpty;

public AbstractRecipeLogic(MetaTileEntity tileEntity, RecipeMap<?> recipeMap) {
super(tileEntity);
Expand Down Expand Up @@ -107,7 +111,16 @@ public void update() {
}
if (progressTime == 0) {
trySearchNewRecipe();
if (currentRecipe == null) {
if (isInputsEmpty) {
metaTileEntity.setSituation(IDLE);
} else {
metaTileEntity.setSituation(NO_MATCHING_RECIPE);
}
}
}
} else {
metaTileEntity.setSituation(DISABLED_BY_CONTROLLER);
}
if (wasActiveAndNeedsUpdate) {
this.wasActiveAndNeedsUpdate = false;
Expand All @@ -120,13 +133,15 @@ protected void updateRecipeProgress() {
boolean drawEnergy = drawEnergy(recipeEUt);
if (drawEnergy || (recipeEUt < 0)) {
//as recipe starts with progress on 1 this has to be > only not => to compensate for it
metaTileEntity.setSituation(WORKING);
if (++progressTime > maxProgressTime) {
completeRecipe();
}
} else if (recipeEUt > 0) {
//only set hasNotEnoughEnergy if this recipe is consuming recipe
//generators always have enough energy
this.hasNotEnoughEnergy = true;
metaTileEntity.setSituation(INSUFFICIENT_POWER);
//if current progress value is greater than 2, decrement it by 2
if (progressTime >= 2) {
if (ConfigHolder.insufficientEnergySupplyWipesRecipeProgress) {
Expand All @@ -140,25 +155,24 @@ protected void updateRecipeProgress() {

protected void trySearchNewRecipe() {
long maxVoltage = getMaxVoltage();
Recipe currentRecipe = null;
IItemHandlerModifiable importInventory = getInputInventory();
IMultipleTankHandler importFluids = getInputTank();
if (previousRecipe != null && previousRecipe.matches(false, importInventory, importFluids)) {
if (this.previousRecipe != null && this.previousRecipe.matches(false, importInventory, importFluids)) {
//if previous recipe still matches inputs, try to use it
currentRecipe = previousRecipe;
this.currentRecipe = this.previousRecipe;
} else {
boolean dirty = checkRecipeInputsDirty(importInventory, importFluids);
if (dirty || forceRecipeRecheck) {
if (dirty || this.forceRecipeRecheck) {
this.forceRecipeRecheck = false;
//else, try searching new recipe for given inputs
currentRecipe = findRecipe(maxVoltage, importInventory, importFluids);
if (currentRecipe != null) {
this.previousRecipe = currentRecipe;
this.currentRecipe = findRecipe(maxVoltage, importInventory, importFluids);
if (this.currentRecipe != null) {
this.previousRecipe = this.currentRecipe;
}
}
}
if (currentRecipe != null && setupAndConsumeRecipeInputs(currentRecipe)) {
setupRecipe(currentRecipe);
if (this.currentRecipe != null && setupAndConsumeRecipeInputs(this.currentRecipe)) {
setupRecipe(this.currentRecipe);
}
}

Expand All @@ -183,6 +197,7 @@ protected Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs, IMul

protected boolean checkRecipeInputsDirty(IItemHandler inputs, IMultipleTankHandler fluidInputs) {
boolean shouldRecheckRecipe = false;
this.isInputsEmpty = true;
if (lastItemInputs == null || lastItemInputs.length != inputs.getSlots()) {
this.lastItemInputs = new ItemStack[inputs.getSlots()];
Arrays.fill(lastItemInputs, ItemStack.EMPTY);
Expand All @@ -199,6 +214,8 @@ protected boolean checkRecipeInputsDirty(IItemHandler inputs, IMultipleTankHandl
} else if (currentStack.getCount() != lastStack.getCount()) {
lastStack.setCount(currentStack.getCount());
shouldRecheckRecipe = true;
} else if (!currentStack.isEmpty()) {
this.isInputsEmpty = false;
}
}
for (int i = 0; i < lastFluidInputs.length; i++) {
Expand All @@ -212,6 +229,8 @@ protected boolean checkRecipeInputsDirty(IItemHandler inputs, IMultipleTankHandl
currentStack.amount != lastStack.amount) {
lastStack.amount = currentStack.amount;
shouldRecheckRecipe = true;
} else if (currentStack != null) {
this.isInputsEmpty = false;
}
}
return shouldRecheckRecipe;
Expand All @@ -230,11 +249,20 @@ protected boolean setupAndConsumeRecipeInputs(Recipe recipe) {
IItemHandlerModifiable exportInventory = getOutputInventory();
IMultipleTankHandler importFluids = getInputTank();
IMultipleTankHandler exportFluids = getOutputTank();
return (totalEUt >= 0 ? getEnergyStored() >= (totalEUt > getEnergyCapacity() / 2 ? resultOverclock[0] : totalEUt) :
(getEnergyStored() - resultOverclock[0] <= getEnergyCapacity())) &&
MetaTileEntity.addItemsToItemHandler(exportInventory, true, recipe.getAllItemOutputs(exportInventory.getSlots())) &&
MetaTileEntity.addFluidsToFluidHandler(exportFluids, true, recipe.getFluidOutputs()) &&
recipe.matches(true, importInventory, importFluids);
if (!(totalEUt >= 0 ? getEnergyStored() >= (totalEUt > getEnergyCapacity() / 2 ? resultOverclock[0] : totalEUt) :
(getEnergyStored() - resultOverclock[0] <= getEnergyCapacity()))) {
metaTileEntity.setSituation(INSUFFICIENT_POWER_TO_START);
return false;
}
if (!MetaTileEntity.addItemsToItemHandler(exportInventory, true, recipe.getAllItemOutputs(exportInventory.getSlots()))) {
metaTileEntity.setSituation(OUTPUT_SLOTS_FULL);
return false;
}
if (!MetaTileEntity.addFluidsToFluidHandler(exportFluids, true, recipe.getFluidOutputs())) {
metaTileEntity.setSituation(OUTPUT_TANKS_FULL);
return false;
}
return recipe.matches(true, importInventory, importFluids);
}

protected int[] calculateOverclock(int EUt, long voltage, int duration) {
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/gregtech/api/capability/impl/RecipeLogicSteam.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.GTValues;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.damagesources.DamageSources;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.recipes.Recipe;
Expand All @@ -9,6 +10,7 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EntitySelectors;
Expand All @@ -18,7 +20,13 @@
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.items.IItemHandler;

import java.util.Arrays;

import static gregtech.api.situation.Situations.*;

public class RecipeLogicSteam extends AbstractRecipeLogic {

Expand Down Expand Up @@ -143,10 +151,56 @@ public void update() {
return;
if (this.needsVenting && metaTileEntity.getTimer() % 10 == 0) {
tryDoVenting();
if (isVentingStuck()){
metaTileEntity.setSituation(BLOCKED_VENT);
}
}
super.update();
}

protected boolean checkRecipeInputsDirty(IItemHandler inputs, IMultipleTankHandler fluidInputs) {
boolean shouldRecheckRecipe = false;
this.isInputsEmpty = true;
if (lastItemInputs == null || lastItemInputs.length != inputs.getSlots()) {
this.lastItemInputs = new ItemStack[inputs.getSlots()];
Arrays.fill(lastItemInputs, ItemStack.EMPTY);
}
if (lastFluidInputs == null || lastFluidInputs.length != fluidInputs.getTanks()) {
this.lastFluidInputs = new FluidStack[fluidInputs.getTanks()];
}
for (int i = 0; i < lastItemInputs.length; i++) {
ItemStack currentStack = inputs.getStackInSlot(i);
ItemStack lastStack = lastItemInputs[i];
if (!areItemStacksEqual(currentStack, lastStack)) {
this.lastItemInputs[i] = currentStack.isEmpty() ? ItemStack.EMPTY : currentStack.copy();
shouldRecheckRecipe = true;
} else if (currentStack.getCount() != lastStack.getCount()) {
lastStack.setCount(currentStack.getCount());
shouldRecheckRecipe = true;
} else if (!currentStack.isEmpty()) {
this.isInputsEmpty = false;
}
}
for (int i = 0; i < lastFluidInputs.length; i++) {
if (fluidInputs.getTankAt(i) == steamFluidTank)
continue;
FluidStack currentStack = fluidInputs.getTankAt(i).getFluid();
FluidStack lastStack = lastFluidInputs[i];
if ((currentStack == null && lastStack != null) ||
(currentStack != null && !currentStack.isFluidEqual(lastStack))) {
this.lastFluidInputs[i] = currentStack == null ? null : currentStack.copy();
shouldRecheckRecipe = true;
} else if (currentStack != null && lastStack != null &&
currentStack.amount != lastStack.amount) {
lastStack.amount = currentStack.amount;
shouldRecheckRecipe = true;
} else if (currentStack != null) {
this.isInputsEmpty = false;
}
}
return shouldRecheckRecipe;
}

@Override
protected boolean setupAndConsumeRecipeInputs(Recipe recipe) {
return !this.needsVenting && super.setupAndConsumeRecipeInputs(recipe);
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/gregtech/api/cover/CoverBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import com.google.common.collect.Lists;
import gregtech.api.GTValues;
import gregtech.api.gui.IUIHolder;
import gregtech.api.metatileentity.SimpleMachineMetaTileEntity;
import gregtech.api.render.SimpleSidedCubeRenderer.RenderSide;
import gregtech.api.render.Textures;
import gregtech.api.situation.Situation;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
Expand All @@ -28,6 +28,9 @@
import java.util.List;
import java.util.function.Consumer;

import static gregtech.api.situation.Situations.DISABLED_BY_CONTROLLER;
import static gregtech.api.situation.Situations.IDLE;

/**
* Represents cover instance attached on the specific side of meta tile entity
* Cover filters out interaction and logic of meta tile entity
Expand All @@ -41,6 +44,7 @@ public abstract class CoverBehavior implements IUIHolder {
public final ICoverable coverHolder;
public final EnumFacing attachedSide;
private int redstoneSignalOutput;
private Situation situation;

public CoverBehavior(ICoverable coverHolder, EnumFacing attachedSide) {
this.coverHolder = coverHolder;
Expand Down Expand Up @@ -209,6 +213,14 @@ public boolean isRemote() {
return coverHolder.getWorld().isRemote;
}

public Situation getSituation() {
return this.situation;
}

public void setSituation(Situation situation) {
this.situation = situation;
}

@Override
public final void markAsDirty() {
coverHolder.markDirty();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/gregtech/api/gui/GuiTextures.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public class GuiTextures {
//WIDGET UI RELATED
public static final TextureArea SLIDER_BACKGROUND = TextureArea.fullImage("textures/gui/widget/slider_background.png");
public static final TextureArea SLIDER_ICON = TextureArea.fullImage("textures/gui/widget/slider.png");
public static final TextureArea STATUS_IDLING = TextureArea.fullImage("textures/gui/widget/idling.png");
public static final TextureArea STATUS_WORKING = TextureArea.fullImage("textures/gui/widget/working.png");
public static final TextureArea STATUS_ERROR =TextureArea.fullImage("textures/gui/widget/error.png");
public static final TextureArea STATUS_WARNING =TextureArea.fullImage("textures/gui/widget/warning.png");


//BRONZE
public static final TextureArea BRONZE_BACKGROUND = TextureArea.fullImage("textures/gui/steam/bronze/bronze_gui.png");
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/gregtech/api/gui/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
Expand All @@ -25,6 +26,7 @@

import javax.annotation.Nullable;
import java.awt.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -44,6 +46,8 @@ public abstract class Widget {
private Position selfPosition;
private Position position;
private Size size;
protected long hoverStartTime = -1L;
protected boolean isMouseHovered;

public Widget(Position selfPosition, Size size) {
Preconditions.checkNotNull(selfPosition, "selfPosition");
Expand Down Expand Up @@ -229,6 +233,25 @@ protected final void writeClientAction(int id, Consumer<PacketBuffer> packetBuff
}
}

@SideOnly(Side.CLIENT)
public void drawHoveringTooltip(int mouseX, int mouseY, String tooltipHoverString) {
boolean isHovered = isMouseOverElement(mouseX, mouseY);
boolean wasHovered = isMouseHovered;
if (isHovered && !wasHovered) {
this.isMouseHovered = true;
this.hoverStartTime = System.currentTimeMillis();
} else if (!isHovered && wasHovered) {
this.isMouseHovered = false;
this.hoverStartTime = 0L;
} else if (isHovered) {
long timeSinceHover = System.currentTimeMillis() - hoverStartTime;
if (timeSinceHover > 1000L && tooltipHoverString != null) {
List<String> hoverList = Arrays.asList(I18n.format(tooltipHoverString).split("/n"));
drawHoveringText(ItemStack.EMPTY, hoverList, 300, mouseX, mouseY);
}
}
}

@SideOnly(Side.CLIENT)
protected void drawHoveringText(ItemStack itemStack, List<String> tooltip, int maxTextWidth, int mouseX, int mouseY) {
Minecraft mc = Minecraft.getMinecraft();
Expand Down
19 changes: 1 addition & 18 deletions src/main/java/gregtech/api/gui/widgets/CycleButtonWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public class CycleButtonWidget extends Widget {
private IntConsumer setOptionExecutor;
protected int currentOption;
protected String tooltipHoverString;
protected long hoverStartTime = -1L;
protected boolean isMouseHovered;

public CycleButtonWidget(int xPosition, int yPosition, int width, int height, String[] optionNames, IntSupplier currentOptionSupplier, IntConsumer setOptionExecutor) {
super(new Position(xPosition, yPosition), new Size(width, height));
Expand Down Expand Up @@ -93,21 +91,7 @@ public void drawInBackground(int mouseX, int mouseY, IRenderContext context) {

@Override
public void drawInForeground(int mouseX, int mouseY) {
boolean isHovered = isMouseOverElement(mouseX, mouseY);
boolean wasHovered = isMouseHovered;
if (isHovered && !wasHovered) {
this.isMouseHovered = true;
this.hoverStartTime = System.currentTimeMillis();
} else if (!isHovered && wasHovered) {
this.isMouseHovered = false;
this.hoverStartTime = 0L;
} else if (isHovered) {
long timeSinceHover = System.currentTimeMillis() - hoverStartTime;
if (timeSinceHover > 1000L && tooltipHoverString != null) {
List<String> hoverList = Arrays.asList(I18n.format(tooltipHoverString).split("/n"));
drawHoveringText(ItemStack.EMPTY, hoverList, 300, mouseX, mouseY);
}
}
drawHoveringTooltip(mouseX,mouseY,tooltipHoverString);
}

@Override
Expand Down Expand Up @@ -140,7 +124,6 @@ public boolean mouseClicked(int mouseX, int mouseY, int button) {
return false;
}


@Override
public void handleClientAction(int id, PacketBuffer buffer) {
super.handleClientAction(id, buffer);
Expand Down
Loading