Skip to content

Commit

Permalink
Allow machine interface to pass a signal through to the multiblock
Browse files Browse the repository at this point in the history
  • Loading branch information
BluSunrize committed Apr 25, 2024
1 parent a65ee29 commit d24967c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.IntSupplier;
import java.util.function.ToIntFunction;

import static blusunrize.immersiveengineering.client.gui.IEContainerScreen.makeTextureLocation;

Expand Down Expand Up @@ -157,6 +155,15 @@ public void init()
}
));

// input color button
this.addRenderableWidget(new GuiButtonDyeColor(
guiLeft+xSize-40, guiTop+163, 16, 16,
() -> blockEntity.inputColor.getId(), TEXTURE, 192, 18,
btn -> sendInputColor(btn.getNextState()),
ItemBatcherScreen::gatherRedstoneTooltip
));

// scroll buttons
this.addRenderableWidget(new GuiButtonIE(
guiLeft+xSize-20, guiTop+12, 16, 12, Component.empty(), TEXTURE, 224, 18,
(IIEPressable<Button>)btn -> this.rowList.scrollUp()
Expand Down Expand Up @@ -203,6 +210,16 @@ private void sendConfig(int idx, @Nullable MachineInterfaceConfig<?> config)
PacketDistributor.SERVER.noArg().send(new MessageBlockEntitySync(blockEntity, message));
}

private void sendInputColor(DyeColor col)
{
//update client
blockEntity.inputColor = col;
//update server
CompoundTag message = new CompoundTag();
message.putInt("inputColor", col.getId());
PacketDistributor.SERVER.noArg().send(new MessageBlockEntitySync(blockEntity, message));
}

@Override
protected void drawGuiContainerBackgroundLayer(GuiGraphics graphics, int mouseX, int mouseY, float partialTick)
{
Expand Down Expand Up @@ -231,6 +248,14 @@ protected void drawGuiContainerForegroundLayer(GuiGraphics graphics, int mouseX,
guiLeft+12, yPos+i*font.lineHeight, 0x555555, false
);
}
else
{
Component text = Component.translatable(Lib.GUI_CONFIG+"machine_interface.input_color");
int textWidth = font.width(text);
graphics.drawString(font, text,
guiLeft+xSize-48-textWidth, guiTop+167, 0xE0E0E0, false
);
}

ArrayList<Component> tooltip = new ArrayList<>();
for(GuiEventListener w : children())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import blusunrize.immersiveengineering.common.blocks.BlockCapabilityRegistration.BECapabilityRegistrar;
import blusunrize.immersiveengineering.common.blocks.IEBaseBlockEntity;
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IPlayerInteraction;
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IRedstoneOutput;
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IStateBasedDirectional;
import blusunrize.immersiveengineering.common.blocks.PlacementLimitation;
import blusunrize.immersiveengineering.common.blocks.ticking.IEServerTickableBE;
Expand All @@ -41,7 +42,7 @@
import java.util.List;

public class MachineInterfaceBlockEntity extends IEBaseBlockEntity implements IEServerTickableBE,
IPlayerInteraction, IStateBasedDirectional
IPlayerInteraction, IStateBasedDirectional, IRedstoneOutput
{
public final IEBlockCapabilityCache<IMachineInterfaceConnection> machine = IEBlockCapabilityCaches.forNeighbor(
IMachineInterfaceConnection.CAPABILITY, this, this::getFacing
Expand All @@ -51,6 +52,9 @@ public class MachineInterfaceBlockEntity extends IEBaseBlockEntity implements IE

private final int[] outputs = new int[DyeColor.values().length];

public DyeColor inputColor = DyeColor.WHITE;
private byte inputSignalStrength = 0;

public MachineInterfaceBlockEntity(BlockPos pos, BlockState state)
{
super(IEBlockEntities.MACHINE_INTERFACE.get(), pos, state);
Expand Down Expand Up @@ -83,6 +87,7 @@ public void readCustomNBT(CompoundTag nbt, boolean descPacket)
configurations.clear();
for(int i = 0; i < list.size(); i++)
configurations.add(MachineInterfaceConfig.readFromNBT(list.getCompound(i)));
inputColor = DyeColor.byId(nbt.getInt("inputColor"));
}

@Override
Expand All @@ -91,6 +96,7 @@ public void writeCustomNBT(CompoundTag nbt, boolean descPacket)
ListTag list = new ListTag();
configurations.forEach(conf -> list.add(conf.writeToNBT()));
nbt.put("configurations", list);
nbt.putInt("inputColor", inputColor.getId());
}

@Override
Expand All @@ -106,6 +112,8 @@ public void receiveMessageFromClient(CompoundTag message)
}
else if(message.getBoolean("delete"))
this.configurations.remove(message.getInt("idx"));
else if(message.contains("inputColor"))
this.inputColor = DyeColor.byId(message.getInt("inputColor"));
setChanged();
this.markContainingBlockForUpdate(null);
}
Expand Down Expand Up @@ -133,6 +141,17 @@ public Property<Direction> getFacingProperty()

private final RedstoneBundleConnection redstoneCap = new RedstoneBundleConnection()
{
@Override
public void onChange(byte[] externalInputs, Direction side)
{
if(externalInputs[inputColor.getId()]!=inputSignalStrength)
{
inputSignalStrength = externalInputs[inputColor.getId()];
markChunkDirty();
markContainingBlockForUpdate(getBlockState());
}
}

@Override
public void updateInput(byte[] signals, Direction side)
{
Expand All @@ -149,6 +168,18 @@ public static void registerCapabilities(BECapabilityRegistrar<MachineInterfaceBl
);
}

@Override
public int getStrongRSOutput(Direction side)
{
return canConnectRedstone(side)?inputSignalStrength: 0;
}

@Override
public boolean canConnectRedstone(Direction side)
{
return side==getFacing().getOpposite();
}

public static final class MachineInterfaceConfig<T>
{
private int selectedCheck;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
"gui.immersiveengineering.config.machine_interface.not_connected": "The Machine Interface is not attached to a valid machine.\nPlease ensure that the interface was placed against the redstone control port of an Immersive Engineering multiblock.",
"gui.immersiveengineering.config.machine_interface.add": "Add Condition",
"gui.immersiveengineering.config.machine_interface.remove": "Remove Condition",
"gui.immersiveengineering.config.machine_interface.input_color": "Control Signal Input:",
"gui.immersiveengineering.config.machine_interface.check.basic.active": "Machine Active",
"gui.immersiveengineering.config.machine_interface.check.basic.item_input": "Item Input",
"gui.immersiveengineering.config.machine_interface.check.basic.item_output": "Item Output",
Expand Down

0 comments on commit d24967c

Please sign in to comment.