Skip to content

Commit

Permalink
Allow Sided Components to have different valid facing modes, closes #80
Browse files Browse the repository at this point in the history
  • Loading branch information
Buuz135 committed Feb 13, 2021
1 parent 974d7b2 commit 3145d83
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.hrznstudio.titanium.component.fluid.FluidTankComponent;
import com.hrznstudio.titanium.component.inventory.SidedInventoryComponent;
import com.hrznstudio.titanium.component.progress.ProgressBarComponent;
import com.hrznstudio.titanium.component.sideness.IFacingComponent;
import com.hrznstudio.titanium.util.FacingUtil;
import net.minecraft.block.Block;
import net.minecraft.entity.player.PlayerEntity;
Expand Down Expand Up @@ -65,8 +66,10 @@ public class TestTile extends PoweredTile<TestTile> implements IRedstoneReader {
public TestTile() {
super(TestBlock.TEST);
this.addInventory(first = (SidedInventoryComponent<TestTile>) new SidedInventoryComponent<TestTile>("test", 80, 30, 1, 0)
.setValidFaceModes(IFacingComponent.FaceMode.ENABLED, IFacingComponent.FaceMode.NONE)
.setComponentHarness(this)
.setInputFilter((stack, integer) -> IItemStackQuery.ANYTHING.test(stack)));
.setInputFilter((stack, integer) -> IItemStackQuery.ANYTHING.test(stack))
);
this.addInventory(second = (SidedInventoryComponent<TestTile>) new SidedInventoryComponent<TestTile>("test2", 80, 60, 2, 1)
.setComponentHarness(this)
.setInputFilter((stack, integer) -> IItemStackQuery.ANYTHING.test(stack))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void handleButtonMessage(int id, PlayerEntity playerEntity, CompoundNBT c
if (id == -1) {
String name = compound.getString("Name");
FacingUtil.Sideness facing = FacingUtil.Sideness.valueOf(compound.getString("Facing"));
IFacingComponent.FaceMode faceMode = IFacingComponent.FaceMode.values()[compound.getInt("Next")];
int faceMode = compound.getInt("Next");
if (multiInventoryComponent != null && multiInventoryComponent.handleFacingChange(name, facing, faceMode)) {
markForUpdate();
} else if (multiTankComponent != null && multiTankComponent.handleFacingChange(name, facing, faceMode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -159,7 +160,11 @@ public void handleClick(Screen screen, int guiX, int guiY, double mouseX, double
for (FacingUtil.Sideness facing : FacingUtil.Sideness.values()) {
if (!handler.getFacingModes().containsKey(facing)) continue;
Point point = getPointFromFacing(facing, inventoryPoint);
StateButtonAddon addon = new StateButtonAddon(new ButtonComponent(point.x, point.y, 14, 14), IFacingComponent.FaceMode.NONE.getInfo(), IFacingComponent.FaceMode.ENABLED.getInfo(), IFacingComponent.FaceMode.PULL.getInfo(), IFacingComponent.FaceMode.PUSH.getInfo()) {
List<StateButtonInfo> stateButtonInfo = new ArrayList<>();
for (int i = 0; i < handler.getValidFacingModes().length; i++) {
stateButtonInfo.add(handler.getValidFacingModes()[i].getInfo(i));
}
StateButtonAddon addon = new StateButtonAddon(new ButtonComponent(point.x, point.y, 14, 14), stateButtonInfo.toArray(new StateButtonInfo[0])) {
@Override
public int getState() {
IFacingComponent handler = null;
Expand All @@ -175,7 +180,7 @@ public int getState() {
Titanium.LOGGER.warn("Container is not IObjectContainer. Could not get FacingComponent");
}
return handler != null && handler.getFacingModes().containsKey(facing) ?
handler.getFacingModes().get(facing).getIndex() : 0;
Arrays.asList(handler.getValidFacingModes()).indexOf(handler.getFacingModes().get(facing)) : 0;
}

@Override
Expand All @@ -184,8 +189,8 @@ public void handleClick(Screen gui, int guiX, int guiY, double mouseX, double mo
if (info != null && gui instanceof IHasContainer<?>) {
CompoundNBT compound = new CompoundNBT();
compound.putString("Facing", facing.name());
int faceMode = (getState() + (mouse == 0 ? 1 : -1)) % IFacingComponent.FaceMode.values().length;
if (faceMode < 0) faceMode = IFacingComponent.FaceMode.values().length - 1;
int faceMode = (getState() + (mouse == 0 ? 1 : -1)) % handler.getValidFacingModes().length;
if (faceMode < 0) faceMode = handler.getValidFacingModes().length - 1;
compound.putInt("Next", faceMode);
compound.putString("Name", handler.getName());
if (container instanceof ILocatable) {
Expand All @@ -194,14 +199,14 @@ public void handleClick(Screen gui, int guiX, int guiY, double mouseX, double mo
} else {
Titanium.LOGGER.warn("Failed to Find Locatable Instance for Container");
}
handler.getFacingModes().put(facing, IFacingComponent.FaceMode.values()[faceMode]);
handler.getFacingModes().put(facing, handler.getValidFacingModes()[faceMode]);
}
}

@Override
public List<ITextComponent> getTooltipLines() {
List<ITextComponent> strings = new ArrayList<>();
IFacingComponent.FaceMode mode = IFacingComponent.FaceMode.values()[getState()];
IFacingComponent.FaceMode mode = handler.getValidFacingModes()[getState()];
strings.add(new StringTextComponent(TextFormatting.GOLD + LangUtil.getString("tooltip.titanium.facing_handler.direction") +
TextFormatting.RESET + LangUtil.getString("tooltip.titanium.facing_handler." + facing.name().toLowerCase()) + TextFormatting.GRAY + " [" + LangUtil.getString("direction.titanium." + FacingUtil.getFacingFromSide(blockDirection, facing)) + "]"));
strings.add(new StringTextComponent(TextFormatting.GOLD + LangUtil.getString("tooltip.titanium.facing_handler.action") +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public LazyOptional<MultiTankCapabilityHandler<T>> getCapabilityForSide(@Nullabl
}

@Override
public boolean handleFacingChange(String handlerName, FacingUtil.Sideness facing, IFacingComponent.FaceMode mode) {
public boolean handleFacingChange(String handlerName, FacingUtil.Sideness facing, int mode) {
for (FluidTankComponent<T> tankHandler : tanks) {
if (tankHandler.getName().equals(handlerName) && tankHandler instanceof IFacingComponent) {
((IFacingComponent) tankHandler).getFacingModes().put(facing, mode);
((IFacingComponent) tankHandler).getFacingModes().put(facing, ((IFacingComponent) tankHandler).getValidFacingModes()[mode]);
rebuildCapability(new FacingUtil.Sideness[]{facing});
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SidedFluidTankComponent<T extends IComponentHarness> extends FluidT
private HashMap<FacingUtil.Sideness, FaceMode> facingModes;
private int pos;
private boolean hasFacingAddon;
private FaceMode[] validFaceModes;

public SidedFluidTankComponent(String name, int amount, int posX, int posY, int pos) {
super(name, amount, posX, posY);
Expand All @@ -51,6 +52,7 @@ public SidedFluidTankComponent(String name, int amount, int posX, int posY, int
this.facingModes.put(facing, FaceMode.ENABLED);
}
this.hasFacingAddon = true;
this.validFaceModes = FaceMode.values();
}

public SidedFluidTankComponent<T> disableFacingAddon() {
Expand Down Expand Up @@ -134,6 +136,20 @@ public SidedFluidTankComponent<T> setFacingHandlerPos(int x, int y) {
return this;
}


@Override
public FaceMode[] getValidFacingModes() {
return validFaceModes;
}

public SidedFluidTankComponent<T> setValidFaceModes(FaceMode... validFaceModes){
this.validFaceModes = validFaceModes;
for (FacingUtil.Sideness value : FacingUtil.Sideness.values()) {
this.facingModes.put(value, validFaceModes[0]);
}
return this;
}

private boolean transfer(IFluidHandler from, IFluidHandler to, int workAmount) {
FluidStack stack = from.drain(workAmount * 100, FluidAction.SIMULATE);
if (!stack.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public LazyOptional<MultiInvCapabilityHandler<T>> getCapabilityForSide(FacingUti
}

@Override
public boolean handleFacingChange(String handlerName, FacingUtil.Sideness facing, IFacingComponent.FaceMode mode) {
public boolean handleFacingChange(String handlerName, FacingUtil.Sideness facing, int mode) {
for (InventoryComponent<T> inventoryHandler : inventoryHandlers) {
if (inventoryHandler.getName().equals(handlerName) && inventoryHandler instanceof IFacingComponent) {
((IFacingComponent) inventoryHandler).getFacingModes().put(facing, mode);
((IFacingComponent) inventoryHandler).getFacingModes().put(facing, ((IFacingComponent) inventoryHandler).getValidFacingModes()[mode]);
rebuildCapability(new FacingUtil.Sideness[]{facing});
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SidedInventoryComponent<T extends IComponentHarness> extends Invent
private HashMap<FacingUtil.Sideness, Integer> slotCache;
private int position;
private boolean hasFacingAddon;
private FaceMode[] validFaceModes;

public SidedInventoryComponent(String name, int xPos, int yPos, int size, int position) {
super(name, xPos, yPos, size);
Expand All @@ -53,6 +54,7 @@ public SidedInventoryComponent(String name, int xPos, int yPos, int size, int po
this.position = position;
this.setColorGuiEnabled(true);
this.hasFacingAddon = true;
this.validFaceModes = FaceMode.values();
}

public SidedInventoryComponent<T> disableFacingAddon() {
Expand Down Expand Up @@ -156,6 +158,19 @@ public SidedInventoryComponent<T> setFacingHandlerPos(int x, int y) {
return this;
}

@Override
public FaceMode[] getValidFacingModes() {
return validFaceModes;
}

public SidedInventoryComponent<T> setValidFaceModes(FaceMode... validFaceModes){
this.validFaceModes = validFaceModes;
for (FacingUtil.Sideness value : FacingUtil.Sideness.values()) {
this.facingModes.put(value, validFaceModes[0]);
}
return this;
}

@Override
public CompoundNBT serializeNBT() {
CompoundNBT nbt = super.serializeNBT();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface ICapabilityHolder<T> {
@Nonnull
LazyOptional<T> getCapabilityForSide(@Nullable FacingUtil.Sideness sideness);

boolean handleFacingChange(String handlerName, FacingUtil.Sideness facing, IFacingComponent.FaceMode mode);
boolean handleFacingChange(String handlerName, FacingUtil.Sideness facing, int mode);

Collection<LazyOptional<T>> getLazyOptionals();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;

public interface IFacingComponent {

Map<FacingUtil.Sideness, FaceMode> getFacingModes();

int getColor();
Expand All @@ -37,6 +38,8 @@ public interface IFacingComponent {

IFacingComponent setFacingHandlerPos(int x, int y);

FaceMode[] getValidFacingModes();

enum FaceMode {
NONE(false, 0, AssetTypes.BUTTON_SIDENESS_DISABLED, TextFormatting.RED),
ENABLED(true, 1, AssetTypes.BUTTON_SIDENESS_ENABLED, TextFormatting.GREEN),
Expand All @@ -59,7 +62,7 @@ public boolean allowsConnection() {
return allowsConnection;
}

public StateButtonInfo getInfo() {
public StateButtonInfo getInfo(int index) {
return new StateButtonInfo(index, assetType, "tooltip.titanium.facing_handler." + this.name().toLowerCase());
}

Expand Down

0 comments on commit 3145d83

Please sign in to comment.