Skip to content

Commit

Permalink
move away from int supplier
Browse files Browse the repository at this point in the history
syncing still is done by rebuild the situation from the registry by its id
  • Loading branch information
PrototypeTrousers committed Dec 8, 2020
1 parent 6201c8e commit af0b2ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/cover/CoverBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ public boolean isRemote() {
return coverHolder.getWorld().isRemote;
}

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

public void setSituation(Situation situation) {
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/gregtech/api/gui/widgets/SituationWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@
import gregtech.api.gui.IRenderContext;
import gregtech.api.gui.Widget;
import gregtech.api.gui.resources.TextureArea;
import gregtech.api.situation.Situation;
import gregtech.api.situation.SituationTypes;
import gregtech.api.util.Position;
import gregtech.api.util.Size;
import net.minecraft.client.resources.I18n;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.function.IntSupplier;

import static gregtech.api.situation.Situation.getSituationFromId;
import static gregtech.api.situation.SituationTypes.*;
import java.util.function.Supplier;

public class SituationWidget extends Widget {

private final IntSupplier currentSituationId;
private final Supplier<Situation> currentSituationSupplier;
private Situation currentSituation;
protected String tooltipHoverString;
protected int currentError;
protected int currentId;

protected TextureArea area;
private boolean isVisible = true;

public SituationWidget(int xPosition, int yPosition, int width, int height, IntSupplier getSituationId) {
public <T extends Situation> SituationWidget(int xPosition, int yPosition, int width, int height, Supplier <Situation> getSituation) {
super(new Position(xPosition, yPosition), new Size(width, height));
this.currentSituationId = () -> getSituationId.getAsInt();
this.currentSituationSupplier = getSituation;
this.currentSituation = getSituation.get();
setTooltipHoverString();
setImage();
}

public void setTooltipHoverString() {
this.tooltipHoverString = I18n.format(getSituationFromId(currentError).situationLocaleName);
this.tooltipHoverString = I18n.format(this.currentSituation.situationLocaleName);
}

public SituationWidget setImage() {
SituationTypes iconTextures = getSituationFromId(currentError).situationTypes;
SituationTypes iconTextures = this.currentSituation.situationTypes;
switch (iconTextures) {
case IDLE:
this.area = GuiTextures.STATUS_IDLING;
Expand All @@ -60,17 +60,18 @@ public SituationWidget setImage() {
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
if (currentSituationId.getAsInt() != currentError) {
this.currentError = currentSituationId.getAsInt();
writeUpdateInfo(1, buf -> buf.writeVarInt(currentError));
if (currentSituationSupplier.get().id != currentId) {
this.currentId = currentSituationSupplier.get().id;
writeUpdateInfo(1, buf -> buf.writeVarInt(currentId));
}
}

@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
super.readUpdateInfo(id, buffer);
if (id == 1) {
this.currentError = buffer.readVarInt();
this.currentId = buffer.readVarInt();
this.currentSituation = Situation.getSituationFromId(this.currentId);
setTooltipHoverString();
setImage();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,8 @@ public void setFragile(boolean fragile) {
}
}

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

public void setSituation(Situation situation) {
Expand Down

0 comments on commit af0b2ef

Please sign in to comment.