Skip to content

Commit

Permalink
use machine owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Arborsm committed Dec 6, 2024
1 parent 3ad93a9 commit 180f761
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static EntryTypes<? extends VirtualEntry> fromString(String name) {
}

@Nullable
public static EntryTypes<? extends VirtualEntry> fromLocation(String location) {
return TYPES_MAP.getOrDefault(new ResourceLocation(location), null);
public static EntryTypes<? extends VirtualEntry> fromLocation(ResourceLocation location) {
return TYPES_MAP.getOrDefault(location, null);
}

public static <E extends VirtualEntry> EntryTypes<E> addEntryType(ResourceLocation location, Supplier<E> supplier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompoundTag> {

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<? extends VirtualEntry> getType();

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -69,7 +70,8 @@ public Set<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,23 +40,21 @@
import java.util.UUID;
import java.util.regex.Pattern;

public abstract class CoverAbstractEnderLink<T extends VirtualEntry> extends CoverBehavior
public abstract class AbstractEnderLinkCover<T extends VirtualEntry> 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
Expand All @@ -73,7 +73,7 @@ public abstract class CoverAbstractEnderLink<T extends VirtualEntry> 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);
}
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,10 +30,10 @@
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class EnderFluidLinkCover extends CoverAbstractEnderLink<VirtualTank> {
public class EnderFluidLinkCover extends AbstractEnderLinkCover<VirtualTank> {

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
Expand Down Expand Up @@ -63,13 +61,18 @@ protected EntryTypes<VirtualTank> 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());
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,6 +22,8 @@ public sealed interface IMachineOwner permits PlayerOwner, ArgonautsOwner, FTBOw

void displayInfo(List<Component> compList);

UUID getPlayerUUID();

static IMachineOwner create(CompoundTag tag) {
MachineOwnerType type = MachineOwnerType.VALUES[tag.getInt("type")];
if (!type.isAvailable()) {
Expand Down

0 comments on commit 180f761

Please sign in to comment.