From 180f7618bf9237b349ea5740a3b2c8c18a6e906b Mon Sep 17 00:00:00 2001 From: Arborsm Date: Sat, 7 Dec 2024 00:02:24 +0800 Subject: [PATCH] use machine owner --- .../api/machine/MachineCoverContainer.java | 1 + .../api/misc/virtualregistry/EntryTypes.java | 4 +-- .../virtualregistry/VirtualEnderRegistry.java | 1 - .../misc/virtualregistry/VirtualEntry.java | 18 +++------- .../virtualregistry/VirtualRegistryMap.java | 4 ++- ...rLink.java => AbstractEnderLinkCover.java} | 34 ++++++++++++------- .../cover/ender/EnderFluidLinkCover.java | 21 +++++------- .../common/machine/owner/IMachineOwner.java | 3 ++ 8 files changed, 44 insertions(+), 42 deletions(-) rename src/main/java/com/gregtechceu/gtceu/common/cover/ender/{CoverAbstractEnderLink.java => AbstractEnderLinkCover.java} (90%) diff --git a/src/main/java/com/gregtechceu/gtceu/api/machine/MachineCoverContainer.java b/src/main/java/com/gregtechceu/gtceu/api/machine/MachineCoverContainer.java index 12d216c0ef..326eaa0c03 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/machine/MachineCoverContainer.java +++ b/src/main/java/com/gregtechceu/gtceu/api/machine/MachineCoverContainer.java @@ -40,6 +40,7 @@ public class MachineCoverContainer implements ICoverable, IEnhancedManaged { public static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(MachineCoverContainer.class); @Getter private final FieldManagedStorage syncStorage = new FieldManagedStorage(this); + @Getter private final MetaMachine machine; @DescSynced @Persisted diff --git a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/EntryTypes.java b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/EntryTypes.java index c2e3e3b560..9910f108a8 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/EntryTypes.java +++ b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/EntryTypes.java @@ -33,8 +33,8 @@ public static EntryTypes fromString(String name) { } @Nullable - public static EntryTypes fromLocation(String location) { - return TYPES_MAP.getOrDefault(new ResourceLocation(location), null); + public static EntryTypes fromLocation(ResourceLocation location) { + return TYPES_MAP.getOrDefault(location, null); } public static EntryTypes addEntryType(ResourceLocation location, Supplier supplier) { diff --git a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEnderRegistry.java b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEnderRegistry.java index e38e3ef206..d76b54600b 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEnderRegistry.java +++ b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEnderRegistry.java @@ -35,7 +35,6 @@ public static VirtualEnderRegistry getInstance() { if (server != null) { data = server.overworld().getDataStorage() .computeIfAbsent(VirtualEnderRegistry::new, VirtualEnderRegistry::new, DATA_ID); - GTCEu.LOGGER.debug("VirtualEnderRegistry has been successfully loaded"); } } diff --git a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEntry.java b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEntry.java index 0c4cf56a9c..40307265b7 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEntry.java +++ b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualEntry.java @@ -4,20 +4,21 @@ import net.minecraftforge.common.util.INBTSerializable; import lombok.Getter; +import lombok.Setter; import org.jetbrains.annotations.NotNull; +@Getter public abstract class VirtualEntry implements INBTSerializable { public static final String DEFAULT_COLOR = "FFFFFFFF"; protected static final String COLOR_KEY = "color"; protected static final String DESC_KEY = "description"; - @Getter - private int color = 0xFFFFFFFF; - @Getter - private String colorStr = DEFAULT_COLOR; + @Setter @NotNull private String description = ""; + private int color = 0xFFFFFFFF; + private String colorStr = DEFAULT_COLOR; public abstract EntryTypes getType(); @@ -39,15 +40,6 @@ private int parseColor(String s) { return (int) tmp; } - @NotNull - public String getDescription() { - return this.description; - } - - public void setDescription(@NotNull String desc) { - this.description = desc; - } - @Override public boolean equals(Object o) { if (!(o instanceof VirtualEntry other)) return false; diff --git a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualRegistryMap.java b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualRegistryMap.java index af026dca63..3d48f8b020 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualRegistryMap.java +++ b/src/main/java/com/gregtechceu/gtceu/api/misc/virtualregistry/VirtualRegistryMap.java @@ -1,6 +1,7 @@ package com.gregtechceu.gtceu.api.misc.virtualregistry; import net.minecraft.nbt.CompoundTag; +import net.minecraft.resources.ResourceLocation; import net.minecraftforge.common.util.INBTSerializable; import org.jetbrains.annotations.NotNull; @@ -69,7 +70,8 @@ public Set getEntryNames(EntryTypes type) { @Override public void deserializeNBT(CompoundTag nbt) { for (String entryTypeString : nbt.getAllKeys()) { - EntryTypes type = entryTypeString.contains(":") ? EntryTypes.fromLocation(entryTypeString) : + EntryTypes type = entryTypeString.contains(":") ? + EntryTypes.fromLocation(ResourceLocation.tryParse(entryTypeString)) : EntryTypes.fromString(entryTypeString); if (type == null) continue; diff --git a/src/main/java/com/gregtechceu/gtceu/common/cover/ender/CoverAbstractEnderLink.java b/src/main/java/com/gregtechceu/gtceu/common/cover/ender/AbstractEnderLinkCover.java similarity index 90% rename from src/main/java/com/gregtechceu/gtceu/common/cover/ender/CoverAbstractEnderLink.java rename to src/main/java/com/gregtechceu/gtceu/common/cover/ender/AbstractEnderLinkCover.java index de8fb4558d..65bcc31237 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/cover/ender/CoverAbstractEnderLink.java +++ b/src/main/java/com/gregtechceu/gtceu/common/cover/ender/AbstractEnderLinkCover.java @@ -12,11 +12,13 @@ import com.gregtechceu.gtceu.api.gui.widget.EnumSelectorWidget; import com.gregtechceu.gtceu.api.gui.widget.ToggleButtonWidget; import com.gregtechceu.gtceu.api.machine.ConditionalSubscriptionHandler; +import com.gregtechceu.gtceu.api.machine.MachineCoverContainer; import com.gregtechceu.gtceu.api.misc.virtualregistry.EntryTypes; import com.gregtechceu.gtceu.api.misc.virtualregistry.VirtualEnderRegistry; import com.gregtechceu.gtceu.api.misc.virtualregistry.VirtualEntry; import com.gregtechceu.gtceu.api.misc.virtualregistry.entries.VirtualTank; import com.gregtechceu.gtceu.common.cover.data.ManualIOMode; +import com.gregtechceu.gtceu.common.machine.owner.IMachineOwner; import com.lowdragmc.lowdraglib.gui.widget.LabelWidget; import com.lowdragmc.lowdraglib.gui.widget.Widget; @@ -38,23 +40,21 @@ import java.util.UUID; import java.util.regex.Pattern; -public abstract class CoverAbstractEnderLink extends CoverBehavior +public abstract class AbstractEnderLinkCover extends CoverBehavior implements IUICover, IControllable { - public static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(CoverAbstractEnderLink.class, + public static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(AbstractEnderLinkCover.class, CoverBehavior.MANAGED_FIELD_HOLDER); public static final Pattern COLOR_INPUT_PATTERN = Pattern.compile("[0-9a-fA-F]*"); protected final ConditionalSubscriptionHandler subscriptionHandler; @Persisted @DescSynced - protected String channelName = VirtualEntry.DEFAULT_COLOR; + protected String color = VirtualEntry.DEFAULT_COLOR; @Persisted @DescSynced protected String description = VirtualEntry.DEFAULT_COLOR; - @Getter @Persisted - @DescSynced - protected UUID playerUUID = null; + protected IMachineOwner owner = null; @Getter @Setter @Persisted @@ -73,7 +73,7 @@ public abstract class CoverAbstractEnderLink extends Cov @RequireRerender protected IO io = IO.OUT; - public CoverAbstractEnderLink(CoverDefinition definition, ICoverable coverHolder, Direction attachedSide) { + public AbstractEnderLinkCover(CoverDefinition definition, ICoverable coverHolder, Direction attachedSide) { super(definition, coverHolder, attachedSide); subscriptionHandler = new ConditionalSubscriptionHandler(coverHolder, this::update, this::isSubscriptionActive); } @@ -100,12 +100,20 @@ public void onRemoved() { @Override public void onAttached(@NotNull ItemStack itemStack, @NotNull ServerPlayer player) { super.onAttached(itemStack, player); - playerUUID = player.getUUID(); + if (coverHolder instanceof MachineCoverContainer mcc) { + this.owner = mcc.getMachine().getHolder().getOwner(); + } } + protected final String getChannelName() { + return identifier() + this.color; + } + + protected abstract String identifier(); + protected void setChannelName(String name) { - beforeChannelNameChanging(this.channelName); - this.channelName = name; + beforeChannelNameChanging(getChannelName()); + this.color = name; } protected void beforeChannelNameChanging(String oldChannelName) { @@ -119,7 +127,7 @@ protected void beforeChannelNameChanging(String oldChannelName) { protected abstract void updateEntry(); public UUID getOwner() { - return isPrivate ? playerUUID : null; + return isPrivate ? owner.getPlayerUUID() : null; } protected void update() { @@ -186,7 +194,7 @@ public Widget createUIWidget() { int textInputWidth = channelGroupWidth - currentX - SMALL_WIDGET_WIDTH - 2; var confirmTextInputWidget = new ConfirmTextInputWidget(currentX, 0, textInputWidth, WIDGET_HEIGHT, - channelName, + this.color, text -> { if (text != null && !text.isEmpty()) { setChannelName(text); @@ -218,7 +226,7 @@ protected void buildAdditionalUI(WidgetGroup group) {} protected abstract String getUITitle(); protected int getColor() { - var colorString = this.channelName; + var colorString = this.color; colorString = String.format("%8s", colorString).replace(' ', '0'); if (colorString.length() > 8) { diff --git a/src/main/java/com/gregtechceu/gtceu/common/cover/ender/EnderFluidLinkCover.java b/src/main/java/com/gregtechceu/gtceu/common/cover/ender/EnderFluidLinkCover.java index 7af0abbd4e..de87f0b82a 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/cover/ender/EnderFluidLinkCover.java +++ b/src/main/java/com/gregtechceu/gtceu/common/cover/ender/EnderFluidLinkCover.java @@ -21,8 +21,6 @@ import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder; import net.minecraft.core.Direction; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidUtil; @@ -32,10 +30,10 @@ import javax.annotation.ParametersAreNonnullByDefault; @ParametersAreNonnullByDefault -public class EnderFluidLinkCover extends CoverAbstractEnderLink { +public class EnderFluidLinkCover extends AbstractEnderLinkCover { public static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(EnderFluidLinkCover.class, - CoverAbstractEnderLink.MANAGED_FIELD_HOLDER); + AbstractEnderLinkCover.MANAGED_FIELD_HOLDER); public static final int TRANSFER_RATE = 8000; // mB/t @Persisted @@ -63,13 +61,18 @@ protected EntryTypes getEntryType() { return EntryTypes.ENDER_FLUID; } + @Override + protected String identifier() { + return "EFLink#"; + } + @Override protected void updateEntry() { var reg = VirtualEnderRegistry.getInstance(); if (reg == null) return; - var tank = reg.getOrCreateEntry(getOwner(), EntryTypes.ENDER_FLUID, this.channelName); + var tank = reg.getOrCreateEntry(getOwner(), EntryTypes.ENDER_FLUID, getChannelName()); this.visualTank.setVirtualTank(tank); - this.visualTank.getVirtualTank().setColor(this.channelName); + this.visualTank.getVirtualTank().setColor(this.getColor()); markAsDirty(); markDirty("visualTank"); this.visualTank.setFluid(this.visualTank.getVirtualTank().getFluid()); @@ -108,12 +111,6 @@ private int doTransferFluids(int platformTransferLimit) { return 0; } - @Override - public void onAttached(ItemStack itemStack, ServerPlayer player) { - super.onAttached(itemStack, player); - playerUUID = player.getUUID(); - } - @Override public @NotNull ManagedFieldHolder getFieldHolder() { return MANAGED_FIELD_HOLDER; diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java b/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java index 9f90afd18f..924c34a899 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java @@ -9,6 +9,7 @@ import lombok.Getter; import java.util.List; +import java.util.UUID; import java.util.function.BooleanSupplier; public sealed interface IMachineOwner permits PlayerOwner, ArgonautsOwner, FTBOwner { @@ -21,6 +22,8 @@ public sealed interface IMachineOwner permits PlayerOwner, ArgonautsOwner, FTBOw void displayInfo(List compList); + UUID getPlayerUUID(); + static IMachineOwner create(CompoundTag tag) { MachineOwnerType type = MachineOwnerType.VALUES[tag.getInt("type")]; if (!type.isAvailable()) {