Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ender fluid cover #2505

Open
wants to merge 7 commits into
base: 1.20.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.gregtechceu.gtceu.api.gui.widget;

import com.lowdragmc.lowdraglib.gui.util.DrawerHelper;
import com.lowdragmc.lowdraglib.gui.widget.Widget;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;

import java.util.function.IntSupplier;

@Setter
@Accessors(chain = true)
public class ColorBlockWidget extends Widget {

private IntSupplier colorSupplier;
@Getter
private int currentColor;

public ColorBlockWidget(int x, int y, int width, int height) {
super(x, y, width, height);
this.currentColor = 0xFFFFFFFF;
}

@Override
public void updateScreen() {
super.updateScreen();
if (isClientSideWidget && colorSupplier != null) {
currentColor = colorSupplier.getAsInt();
}
}

@OnlyIn(Dist.CLIENT)
@Override
public void drawInBackground(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) {
int x = getPosition().x + 1;
int y = getPosition().y + 1;
int width = getSize().width - 2;
int height = getSize().height - 2;

if (colorSupplier != null) {
currentColor = colorSupplier.getAsInt();
}
final int BORDER_COLOR = 0xFF000000;

graphics.fill(x, y, x + width, y + height, currentColor);
DrawerHelper.drawBorder(graphics, x, y, width, height, BORDER_COLOR, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.gregtechceu.gtceu.api.gui.widget;

import com.gregtechceu.gtceu.api.gui.GuiTextures;

import com.lowdragmc.lowdraglib.gui.texture.GuiTextureGroup;
import com.lowdragmc.lowdraglib.gui.widget.ButtonWidget;
import com.lowdragmc.lowdraglib.gui.widget.TextFieldWidget;
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.function.Consumer;
import java.util.function.Function;

@Accessors(chain = true)
public class ConfirmTextInputWidget extends WidgetGroup {

private final Consumer<String> textResponder;
private final Function<String, String> validator;
@Getter
@Setter
private String text = "";
@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private String inputText = "";
private String hoverText = "";

public ConfirmTextInputWidget(int x, int y, int width, int height, String text, Consumer<String> textResponder,
Function<String, String> validator) {
super(x, y, width, height);
this.textResponder = textResponder;
this.validator = validator;
if (text != null) {
this.inputText = text;
}
}

public ConfirmTextInputWidget setInputBoxTooltips(String text) {
this.hoverText = text;
return this;
}

@Override
public void initWidget() {
super.initWidget();
this.addWidget(new ButtonWidget(
getSizeWidth() - getSizeHeight(),
0,
getSizeHeight(),
getSizeHeight(),
pressed -> textResponder.accept(inputText))
.setButtonTexture(
new GuiTextureGroup(GuiTextures.VANILLA_BUTTON,
GuiTextures.CLIPBOARD_BUTTON.getSubTexture(0, 0.25, 1, 0.25))));
this.addWidget(new TextFieldWidget(
1,
1,
getSizeWidth() - getSizeHeight() - 4,
getSizeHeight() - 2,
this::getInputText,
this::setInputText)
.setValidator(validator)
.setHoverTooltips(hoverText));
}
}
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
@@ -0,0 +1,64 @@
package com.gregtechceu.gtceu.api.misc.virtualregistry;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.misc.virtualregistry.entries.VirtualTank;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.function.Supplier;

public final class EntryTypes<T extends VirtualEntry> {

private static final Map<ResourceLocation, EntryTypes<?>> TYPES_MAP = new Object2ObjectOpenHashMap<>();
public static final EntryTypes<VirtualTank> ENDER_FLUID = addEntryType(GTCEu.id("ender_fluid"), VirtualTank::new);
// ENDER_ITEM("ender_item", null),
// ENDER_ENERGY("ender_energy", null),
// ENDER_REDSTONE("ender_redstone", null);
private final ResourceLocation location;
private final Supplier<T> factory;

private EntryTypes(ResourceLocation location, Supplier<T> supplier) {
this.location = location;
this.factory = supplier;
}

@Nullable
public static EntryTypes<? extends VirtualEntry> fromString(String name) {
return TYPES_MAP.getOrDefault(GTCEu.id(name), null);
}

@Nullable
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) {
var type = new EntryTypes<>(location, supplier);
if (!TYPES_MAP.containsKey(location)) {
TYPES_MAP.put(location, type);
} else {
GTCEu.LOGGER.warn("Entry \"{}\" is already registered!", location);
}
return type;
}

public T createInstance(CompoundTag nbt) {
var entry = createInstance();
entry.deserializeNBT(nbt);
return entry;
}

public T createInstance() {
return factory.get();
}

@Override
public String toString() {
return this.location.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.gregtechceu.gtceu.api.misc.virtualregistry;

import com.gregtechceu.gtceu.GTCEu;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraftforge.server.ServerLifecycleHooks;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;

public class VirtualEnderRegistry extends SavedData {

private static final String DATA_ID = GTCEu.MOD_ID + ".virtual_entry_data";
private static final String PUBLIC_KEY = "Public";
private static final String PRIVATE_KEY = "Private";
private static volatile VirtualEnderRegistry data;
private final Map<UUID, VirtualRegistryMap> VIRTUAL_REGISTRIES = new HashMap<>();

public VirtualEnderRegistry() {}

public VirtualEnderRegistry(CompoundTag name) {
readFromNBT(name);
}

public static VirtualEnderRegistry getInstance() {
if (data == null) {
var server = ServerLifecycleHooks.getCurrentServer();
if (server != null) {
data = server.overworld().getDataStorage()
.computeIfAbsent(VirtualEnderRegistry::new, VirtualEnderRegistry::new, DATA_ID);
}
}

return data;
}

/**
* To be called on server stopped event
*/
public static void release() {
if (data != null) {
data = null;
GTCEu.LOGGER.debug("VirtualEnderRegistry has been unloaded");
}
}

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

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

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

public @NotNull <T extends VirtualEntry> T getOrCreateEntry(@Nullable UUID owner, EntryTypes<T> type, String name) {
if (!hasEntry(owner, type, name)) addEntry(owner, name, type.createInstance());
return getEntry(owner, type, name);
}

/**
* Removes an entry from the registry. Use with caution!
*
* @param owner The uuid of the player the entry is private to, or null if the entry is public
* @param type Type of the registry to remove from
* @param name The name of the entry
*/
public void deleteEntry(@Nullable UUID owner, EntryTypes<?> type, String name) {
var registry = getRegistry(owner);
if (registry.contains(type, name)) {
registry.deleteEntry(type, name);
return;
}
GTCEu.LOGGER.warn("Attempted to delete {} entry {} of type {}, which does not exist",
owner == null ? "public" : String.format("private [%s]", owner), name, type);
}

public <T extends VirtualEntry> void deleteEntryIf(@Nullable UUID owner, EntryTypes<T> type, String name,
Predicate<T> shouldDelete) {
T entry = getEntry(owner, type, name);
if (entry != null && shouldDelete.test(entry)) deleteEntry(owner, type, name);
}

public Set<String> getEntryNames(UUID owner, EntryTypes<?> type) {
return getRegistry(owner).getEntryNames(type);
}

private VirtualRegistryMap getRegistry(UUID owner) {
if (data == null) getInstance();
return data.VIRTUAL_REGISTRIES.computeIfAbsent(owner, key -> new VirtualRegistryMap());
}

public final void readFromNBT(CompoundTag nbt) {
if (nbt.contains(PUBLIC_KEY)) {
VIRTUAL_REGISTRIES.put(null, new VirtualRegistryMap(nbt.getCompound(PUBLIC_KEY)));
}
if (nbt.contains(PRIVATE_KEY)) {
CompoundTag privateEntries = nbt.getCompound(PRIVATE_KEY);
for (String owner : privateEntries.getAllKeys()) {
var privateMap = privateEntries.getCompound(owner);
VIRTUAL_REGISTRIES.put(UUID.fromString(owner), new VirtualRegistryMap(privateMap));
}
}
}

@NotNull
@Override
public final CompoundTag save(@NotNull CompoundTag tag) {
var privateTag = new CompoundTag();
for (var owner : VIRTUAL_REGISTRIES.keySet()) {
var mapTag = VIRTUAL_REGISTRIES.get(owner).serializeNBT();
if (owner != null) {
privateTag.put(owner.toString(), mapTag);
} else {
tag.put(PUBLIC_KEY, mapTag);
}
}
tag.put(PRIVATE_KEY, privateTag);
return tag;
}

@Override
public boolean isDirty() {
// can't think of a good way to mark dirty other than always return true;
return true;
}
}
Loading