Skip to content

Commit

Permalink
move entry selector logic to abstract ender cover
Browse files Browse the repository at this point in the history
make abstract entry selector synchandler
misc + spotless
  • Loading branch information
ghzdude committed Jun 30, 2024
1 parent b4e687c commit 6a16bc1
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.INBTSerializable;

import org.jetbrains.annotations.NotNull;

public abstract class VirtualEntry implements INBTSerializable<NBTTagCompound> {

public static final String DEFAULT_COLOR = "FFFFFFFF";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public VirtualRegistryBase(String name) {
super(name);
}

protected static <T extends VirtualEntry> T getEntry(@Nullable UUID owner, EntryTypes<T> type, String name) {
public static <T extends VirtualEntry> T getEntry(@Nullable UUID owner, EntryTypes<T> type, String name) {
return getRegistry(owner).getEntry(type, name);
}

protected static void addEntry(@Nullable UUID owner, String name, VirtualEntry entry) {
getRegistry(owner).addEntry(name, entry);
}

protected static boolean hasEntry(@Nullable UUID owner, EntryTypes<?> type, String name) {
public static boolean hasEntry(@Nullable UUID owner, EntryTypes<?> type, String name) {
return getRegistry(owner).contains(type, name);
}

Expand Down Expand Up @@ -71,7 +71,7 @@ public static void clearMaps() {
VIRTUAL_REGISTRIES.clear();
}

public static VirtualRegistryMap getRegistry(UUID owner) {
private static VirtualRegistryMap getRegistry(UUID owner) {
return VIRTUAL_REGISTRIES.computeIfAbsent(owner, key -> new VirtualRegistryMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

import net.minecraftforge.fluids.IFluidTank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public final class VirtualTankRegistry extends VirtualRegistryBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public boolean canFillFluidType(FluidStack fluidStack) {
public boolean canDrainFluidType(FluidStack fluidStack) {
return true;
}

};
}
}
130 changes: 110 additions & 20 deletions src/main/java/gregtech/common/covers/ender/CoverAbstractEnderLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import gregtech.api.mui.GTGuis;
import gregtech.api.util.virtualregistry.EntryTypes;
import gregtech.api.util.virtualregistry.VirtualEntry;

import gregtech.api.util.virtualregistry.VirtualRegistryBase;

import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -26,24 +25,29 @@
import codechicken.lib.raytracer.CuboidRayTraceResult;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.drawable.DynamicDrawable;
import com.cleanroommc.modularui.drawable.GuiTextures;
import com.cleanroommc.modularui.drawable.Rectangle;
import com.cleanroommc.modularui.factory.SidedPosGuiData;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.Color;
import com.cleanroommc.modularui.value.sync.BooleanSyncValue;
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
import com.cleanroommc.modularui.value.sync.PanelSyncHandler;
import com.cleanroommc.modularui.widgets.ButtonWidget;
import com.cleanroommc.modularui.widgets.ListWidget;
import com.cleanroommc.modularui.widgets.ToggleButton;
import com.cleanroommc.modularui.widgets.layout.Column;
import com.cleanroommc.modularui.widgets.layout.Row;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.regex.Pattern;

@SuppressWarnings("SameParameterValue")
Expand Down Expand Up @@ -170,23 +174,6 @@ protected IWidget createPrivateButton() {
.value(isPrivate);
}

protected IWidget createEntryList(EntryTypes<T> type, WidgetCreator<T> widgetFunction) {
return ListWidget.builder(new ArrayList<>(VirtualRegistryBase.getEntryNames(getOwner(), type)),
name -> widgetFunction.create(name, VirtualRegistryBase.getRegistry(getOwner()).getEntry(type, name)))
.background(GTGuiTextures.DISPLAY.asIcon()
.width(168 - 8)
.height(112 - 20))
.paddingTop(1)
.size(168 - 12, 112 - 24)
.left(4)
.bottom(6);
}

@FunctionalInterface
protected interface WidgetCreator<T> {
IWidget create(String name, T entry);
}

protected IWidget createIoRow() {
var ioEnabled = new BooleanSyncValue(this::isIoEnabled, this::setIoEnabled);

Expand Down Expand Up @@ -262,4 +249,107 @@ public void writeToNBT(@NotNull NBTTagCompound nbt) {
nbt.setBoolean("WorkingAllowed", workingEnabled);
nbt.setString("PlacedUUID", playerUUID.toString());
}

protected abstract class EntrySelectorSH extends PanelSyncHandler {

private final EntryTypes<T> type;
private final List<String> names;

protected EntrySelectorSH(ModularPanel mainPanel, EntryTypes<T> type) {
super(mainPanel);
this.type = type;
this.names = new ArrayList<>(VirtualRegistryBase.getEntryNames(getOwner(), type));
}

@Override
public ModularPanel createUI(ModularPanel mainPanel, GuiSyncManager syncManager) {
return GTGuis.createPopupPanel("entry_selector", 168, 112)
.child(IKey.str("Known Channels") // todo lang
.color(UI_TITLE_COLOR).asWidget()
.top(6)
.left(4))
.child(ListWidget.builder(this.names, this::createRow)
.background(GTGuiTextures.DISPLAY.asIcon()
.width(168 - 8)
.height(112 - 20))
.paddingTop(1)
.size(168 - 12, 112 - 24)
.left(4)
.bottom(6));
}

protected IWidget createRow(String name) {
T entry = VirtualRegistryBase.getEntry(getOwner(), this.type, name);
return new Row()
.left(4)
.marginBottom(2)
.height(18)
.widthRel(0.98f)
.setEnabledIf(row -> VirtualRegistryBase.hasEntry(getOwner(), this.type, name))
.child(new Rectangle()
.setColor(entry.getColor())
.asWidget()
.marginRight(4)
.size(16)
.background(GTGuiTextures.SLOT.asIcon().size(18))
.top(1))
.child(IKey.str(entry.getColorStr())
.alignment(Alignment.CenterLeft)
.color(Color.WHITE.darker(1))
.asWidget()
.tooltipBuilder(tooltip -> {
String desc = entry.getDescription();
if (desc != null && !desc.isEmpty())
tooltip.addLine(desc);
})
.width(64)
.height(16)
.top(1)
.marginRight(4))
.child(new ButtonWidget<>()
.overlay(GuiTextures.GEAR)
// todo lang
.tooltipBuilder(tooltip -> tooltip.addLine("Set Description"))
.onMousePressed(i -> {
// open entry settings
Interactable.playButtonClickSound();
return true;
}))
.child(createSlotWidget(entry))
.child(new ButtonWidget<>()
.overlay(GTGuiTextures.BUTTON_CROSS)
// todo lang
.tooltipBuilder(tooltip -> tooltip.addLine("Delete Entry"))
.onMousePressed(i -> {
// todo option to force delete, maybe as a popup?
deleteEntry(name, entry);
syncToServer(1, buffer -> {
buffer.writeByte(name.length());
buffer.writeString(name);
});
Interactable.playButtonClickSound();
return true;
}));
}

@Override
public void readOnClient(int i, PacketBuffer packetBuffer) throws IOException {
super.readOnClient(i, packetBuffer);
}

@Override
public void readOnServer(int i, PacketBuffer packetBuffer) throws IOException {
super.readOnServer(i, packetBuffer);
if (i == 1) {
int len = packetBuffer.readByte();
String name = packetBuffer.readString(len);
T entry = VirtualRegistryBase.getEntry(getOwner(), this.type, name);
deleteEntry(name, entry);
}
}

protected abstract IWidget createSlotWidget(T entry);

protected abstract void deleteEntry(String name, T entry);
}
}
89 changes: 17 additions & 72 deletions src/main/java/gregtech/common/covers/ender/CoverEnderFluidLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import gregtech.api.cover.CoverWithUI;
import gregtech.api.cover.CoverableView;
import gregtech.api.mui.GTGuiTextures;
import gregtech.api.mui.GTGuis;
import gregtech.api.util.FluidTankSwitchShim;
import gregtech.api.util.GTTransferUtils;
import gregtech.api.util.virtualregistry.EntryTypes;
Expand All @@ -21,29 +20,21 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;

import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Matrix4;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.drawable.GuiTextures;
import com.cleanroommc.modularui.drawable.Rectangle;
import com.cleanroommc.modularui.factory.SidedPosGuiData;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.Color;
import com.cleanroommc.modularui.value.sync.EnumSyncValue;
import com.cleanroommc.modularui.value.sync.FluidSlotSyncHandler;
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
import com.cleanroommc.modularui.value.sync.PanelSyncHandler;
import com.cleanroommc.modularui.value.sync.StringSyncValue;
import com.cleanroommc.modularui.widgets.ButtonWidget;
import com.cleanroommc.modularui.widgets.FluidSlot;
Expand Down Expand Up @@ -148,70 +139,24 @@ protected Column createWidgets(ModularPanel panel, GuiSyncManager syncManager) {
var fluidTank = new FluidSlotSyncHandler(this.linkedTank);
fluidTank.updateCacheFromSource(true);

var panelSH = new PanelSyncHandler(panel) {
var entrySelectorSH = new EntrySelectorSH(panel, EntryTypes.ENDER_FLUID) {

@Override
public ModularPanel createUI(ModularPanel mainPanel, GuiSyncManager syncManager) {
return GTGuis.createPopupPanel("entry_selector", 168, 112)
.child(IKey.str("Known Channels") // todo lang
.color(UI_TITLE_COLOR).asWidget()
.top(6)
.left(4))
.child(createEntryList(EntryTypes.ENDER_FLUID,
(name, entry) -> new Row()
.left(4)
.marginBottom(2)
.height(18)
.widthRel(0.98f)
.setEnabledIf(row -> VirtualTankRegistry.hasTank(name, getOwner()))
.child(new Rectangle()
.setColor(entry.getColor())
.asWidget()
.marginRight(4)
.size(16)
.background(GTGuiTextures.SLOT.asIcon().size(18))
.top(1))
.child(IKey.str(entry.getColorStr())
.alignment(Alignment.CenterLeft)
.color(Color.WHITE.darker(1))
.asWidget()
.tooltipBuilder(tooltip -> {
String desc = entry.getDescription();
if (desc != null && !desc.isEmpty())
tooltip.addLine(desc);
})
.width(64)
.height(16)
.top(1)
.marginRight(4))
.child(new ButtonWidget<>()
.overlay(GuiTextures.GEAR)
// todo lang
.tooltipBuilder(tooltip -> tooltip.addLine("Set Description"))
.onMousePressed(i -> {
// open entry settings
Interactable.playButtonClickSound();
return true;
}))
.child(new FluidSlot()
.syncHandler(new FluidSlotSyncHandler(entry)
.canDrainSlot(false)
.canFillSlot(false))
.marginRight(2))
.child(new ButtonWidget<>()
.overlay(GTGuiTextures.BUTTON_CROSS)
// todo lang
.tooltipBuilder(tooltip -> tooltip.addLine("Delete Entry"))
.onMousePressed(i -> {
// todo option to force delete, maybe as a popup?
VirtualTankRegistry.delTank(name, getOwner(), false);
Interactable.playButtonClickSound();
return true;
}))));
protected IWidget createSlotWidget(VirtualTank entry) {
return new FluidSlot()
.syncHandler(new FluidSlotSyncHandler(entry)
.canDrainSlot(false)
.canFillSlot(false))
.marginRight(2);
}

@Override
protected void deleteEntry(String name, VirtualTank entry) {
VirtualTankRegistry.delTank(name, getOwner(), false);
}
};

syncManager.syncValue("entry_selector", panelSH);
syncManager.syncValue("entry_selector", entrySelectorSH);

return new Column().coverChildrenHeight().top(24)
.margin(7, 0).widthRel(1f)
Expand All @@ -232,10 +177,10 @@ public ModularPanel createUI(ModularPanel mainPanel, GuiSyncManager syncManager)
.background(GTGuiTextures.MC_BUTTON)
.hoverBackground(GuiTextures.MC_BUTTON_HOVERED)
.onMousePressed(i -> {
if (!panelSH.isPanelOpen()) {
panelSH.openPanel();
if (!entrySelectorSH.isPanelOpen()) {
entrySelectorSH.openPanel();
} else {
panelSH.closePanel();
entrySelectorSH.closePanel();
}
Interactable.playButtonClickSound();
return true;
Expand Down

0 comments on commit 6a16bc1

Please sign in to comment.