Refactor slot configs

This commit is contained in:
modmuss50 2023-05-31 20:23:46 +01:00
parent 4ee192b4cd
commit 0adc67de8a
15 changed files with 320 additions and 366 deletions

View file

@ -34,88 +34,26 @@ import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.resource.language.I18n; import net.minecraft.client.resource.language.I18n;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid; import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot; import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Util;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
import reborncore.api.blockentity.IUpgradeable; import reborncore.api.blockentity.IUpgradeable;
import reborncore.client.gui.builder.slot.FluidConfigGui;
import reborncore.client.gui.builder.slot.GuiTab; import reborncore.client.gui.builder.slot.GuiTab;
import reborncore.client.gui.builder.slot.SlotConfigGui;
import reborncore.client.gui.builder.widget.GuiButtonHologram; import reborncore.client.gui.builder.widget.GuiButtonHologram;
import reborncore.client.gui.guibuilder.GuiBuilder; import reborncore.client.gui.guibuilder.GuiBuilder;
import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.screen.BuiltScreenHandler; import reborncore.common.screen.BuiltScreenHandler;
import reborncore.common.screen.slot.PlayerInventorySlot; import reborncore.common.screen.slot.PlayerInventorySlot;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Created by Prospector
*/
public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> { public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
public static FluidCellProvider fluidCellProvider = fluid -> ItemStack.EMPTY; public static FluidCellProvider fluidCellProvider = fluid -> ItemStack.EMPTY;
public static ItemStack wrenchStack = ItemStack.EMPTY; public static ItemStack wrenchStack = ItemStack.EMPTY;
private final List<GuiTab.Builder> tabBuilders = Util.make(new ArrayList<>(), builders -> {
builders.add(GuiTab.Builder.builder()
.name("reborncore.gui.tooltip.config_slots")
.enabled(guiTab -> guiTab.machine().hasSlotConfig())
.stack(guiTab -> wrenchStack)
.draw(SlotConfigGui::draw)
.click(SlotConfigGui::mouseClicked)
.hideGuiElements()
.keyPressed((guiBase, keyCode, scanCode, modifiers) -> {
if (hasControlDown() && keyCode == GLFW.GLFW_KEY_C) {
SlotConfigGui.copyToClipboard();
return true;
} else if (hasControlDown() && keyCode == GLFW.GLFW_KEY_V) {
SlotConfigGui.pasteFromClipboard();
return true;
} else if (keyCode == GLFW.GLFW_KEY_ESCAPE && SlotConfigGui.selectedSlot != -1) {
SlotConfigGui.reset();
return true;
}
return false;
})
.tips(tips -> {
tips.add("reborncore.gui.slotconfigtip.slot");
tips.add("reborncore.gui.slotconfigtip.side1");
tips.add("reborncore.gui.slotconfigtip.side2");
tips.add("reborncore.gui.slotconfigtip.side3");
tips.add("reborncore.gui.slotconfigtip.copy1");
tips.add("reborncore.gui.slotconfigtip.copy2");
})
);
builders.add(GuiTab.Builder.builder()
.name("reborncore.gui.tooltip.config_fluids")
.enabled(guiTab -> guiTab.machine().showTankConfig())
.stack(guiTab -> GuiBase.fluidCellProvider.provide(Fluids.LAVA))
.draw(FluidConfigGui::draw)
.click(FluidConfigGui::mouseClicked)
.hideGuiElements()
);
builders.add(GuiTab.Builder.builder()
.name("reborncore.gui.tooltip.config_redstone")
.stack(guiTab -> new ItemStack(Items.REDSTONE))
.draw(RedstoneConfigGui::draw)
.click(RedstoneConfigGui::mouseClicked)
);
});
public GuiBuilder builder = new GuiBuilder(); public GuiBuilder builder = new GuiBuilder();
public BlockEntity be; public BlockEntity be;
@Nullable @Nullable
@ -123,8 +61,9 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
private final int xSize = 176; private final int xSize = 176;
private final int ySize = 176; private final int ySize = 176;
private GuiTab selectedTab; @Nullable
private List<GuiTab> tabs; private GuiTab selectedTab = null;
private final List<GuiTab> tabs;
public boolean upgrades; public boolean upgrades;
@ -132,15 +71,10 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
super(screenHandler, player.getInventory(), Text.literal(I18n.translate(blockEntity.getCachedState().getBlock().getTranslationKey()))); super(screenHandler, player.getInventory(), Text.literal(I18n.translate(blockEntity.getCachedState().getBlock().getTranslationKey())));
this.be = blockEntity; this.be = blockEntity;
this.builtScreenHandler = (BuiltScreenHandler) screenHandler; this.builtScreenHandler = (BuiltScreenHandler) screenHandler;
selectedTab = null; tabs = GuiTab.TABS.stream()
populateSlots(); .map(factory -> factory.create(this))
}
private void populateSlots() {
tabs = tabBuilders.stream()
.map(builder -> builder.build(getMachine(), this))
.filter(GuiTab::enabled) .filter(GuiTab::enabled)
.collect(Collectors.toList()); .toList();
} }
public int getScreenWidth() { public int getScreenWidth() {
@ -185,11 +119,8 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
@Override @Override
public void init() { public void init() {
super.init(); super.init();
if (isConfigEnabled()) { for (GuiTab tab : tabs) {
SlotConfigGui.init(this); tab.open();
}
if (isConfigEnabled() && getMachine().getTank() != null && getMachine().showTankConfig()) {
FluidConfigGui.init(this);
} }
} }
@ -308,7 +239,7 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
} }
public GuiButtonHologram addHologramButton(int x, int y, int id, Layer layer) { public GuiButtonHologram addHologramButton(int x, int y, int id, Layer layer) {
GuiButtonHologram buttonHologram = new GuiButtonHologram(x + this.x, y + this.y, this, layer, var1 -> { GuiButtonHologram buttonHologram = new GuiButtonHologram(x + this.x, y + this.y, var1 -> {
}); });
addSelectableChild(buttonHologram); addSelectableChild(buttonHologram);
return buttonHologram; return buttonHologram;
@ -333,9 +264,8 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
if (selectedTab == tab) { if (selectedTab == tab) {
closeSelectedTab(); closeSelectedTab();
} else { } else {
selectedTab = tab; setSelectedTab(tab);
} }
SlotConfigGui.reset();
break; break;
} }
offset -= 24; offset -= 24;
@ -362,7 +292,6 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
super.close(); super.close();
} }
@Nullable
public MachineBaseBlockEntity getMachine() { public MachineBaseBlockEntity getMachine() {
return (MachineBaseBlockEntity) be; return (MachineBaseBlockEntity) be;
} }
@ -427,10 +356,24 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
return selectedTab != null && selectedTab.hideGuiElements(); return selectedTab != null && selectedTab.hideGuiElements();
} }
private void setSelectedTab(GuiTab tab) {
Objects.requireNonNull(tab);
selectedTab = tab;
selectedTab.open();
}
public void closeSelectedTab() { public void closeSelectedTab() {
if (selectedTab != null) {
selectedTab.close();
}
selectedTab = null; selectedTab = null;
} }
public GuiTab getSelectedTab() {
return selectedTab;
}
@Override @Override
protected boolean isClickOutsideBounds(double mouseX, double mouseY, int left, int top, int mouseButton) { protected boolean isClickOutsideBounds(double mouseX, double mouseY, int left, int top, int mouseButton) {
// Upgrades are normally outside the bounds, so let's pretend we are within the bounds if there is a slot here. // Upgrades are normally outside the bounds, so let's pretend we are within the bounds if there is a slot here.

View file

@ -25,23 +25,41 @@
package reborncore.client.gui.builder; package reborncore.client.gui.builder;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.item.ItemRenderer; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import reborncore.client.ClientNetworkManager; import reborncore.client.ClientNetworkManager;
import reborncore.client.gui.guibuilder.GuiBuilder; import reborncore.client.gui.builder.slot.GuiTab;
import reborncore.common.blockentity.RedstoneConfiguration; import reborncore.common.blockentity.RedstoneConfiguration;
import reborncore.common.network.IdentifiedPacket; import reborncore.common.network.IdentifiedPacket;
import reborncore.common.network.ServerBoundPackets; import reborncore.common.network.ServerBoundPackets;
import java.util.Locale; import java.util.Locale;
public class RedstoneConfigGui { public class RedstoneConfigGui extends GuiTab {
public RedstoneConfigGui(GuiBase<?> guiBase) {
super(guiBase);
}
public static void draw(DrawContext drawContext, GuiBase<?> guiBase, int mouseX, int mouseY) { @Override
public String name() {
return "reborncore.gui.tooltip.config_redstone";
}
@Override
public boolean enabled() {
return true;
}
@Override
public ItemStack stack() {
return new ItemStack(Items.REDSTONE);
}
@Override
public void draw(DrawContext drawContext, int mouseX, int mouseY) {
if (guiBase.getMachine() == null) return; if (guiBase.getMachine() == null) return;
RedstoneConfiguration configuration = guiBase.getMachine().getRedstoneConfiguration(); RedstoneConfiguration configuration = guiBase.getMachine().getRedstoneConfiguration();
GuiBuilder builder = guiBase.builder;
ItemRenderer itemRenderer = guiBase.getMinecraft().getItemRenderer();
int x = 10; int x = 10;
int y = 100; int y = 100;
@ -60,10 +78,10 @@ public class RedstoneConfigGui {
guiBase.drawCentredText(drawContext, name, y + (i * spread), -1, x + 37, GuiBase.Layer.FOREGROUND); guiBase.drawCentredText(drawContext, name, y + (i * spread), -1, x + 37, GuiBase.Layer.FOREGROUND);
i++; i++;
} }
} }
public static boolean mouseClicked(GuiBase<?> guiBase, double mouseX, double mouseY, int mouseButton) { @Override
public boolean click(double mouseX, double mouseY, int mouseButton) {
if (guiBase.getMachine() == null) return false; if (guiBase.getMachine() == null) return false;
RedstoneConfiguration configuration = guiBase.getMachine().getRedstoneConfiguration(); RedstoneConfiguration configuration = guiBase.getMachine().getRedstoneConfiguration();
@ -89,10 +107,14 @@ public class RedstoneConfigGui {
return false; return false;
} }
private static boolean withinBounds(GuiBase<?> guiBase, int mouseX, int mouseY, int x, int y, int width, int height) { @Override
public boolean hideGuiElements() {
return false;
}
private boolean withinBounds(GuiBase<?> guiBase, int mouseX, int mouseY, int x, int y, int width, int height) {
mouseX -= guiBase.getGuiLeft(); mouseX -= guiBase.getGuiLeft();
mouseY -= guiBase.getGuiTop(); mouseY -= guiBase.getGuiTop();
return (mouseX > x && mouseX < x + width) && (mouseY > y && mouseY < y + height); return (mouseX > x && mouseX < x + width) && (mouseY > y && mouseY < y + height);
} }
} }

View file

@ -24,46 +24,60 @@
package reborncore.client.gui.builder.slot; package reborncore.client.gui.builder.slot;
import com.google.common.collect.Lists;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.builder.slot.elements.ConfigFluidElement; import reborncore.client.gui.builder.slot.elements.ConfigFluidElement;
import reborncore.client.gui.builder.slot.elements.ElementBase;
import reborncore.client.gui.builder.slot.elements.SlotType; import reborncore.client.gui.builder.slot.elements.SlotType;
import java.util.Collections; import java.util.Objects;
import java.util.List;
// Why is this static? public class FluidConfigGui extends GuiTab {
public class FluidConfigGui { @Nullable
private ConfigFluidElement fluidConfigElement;
static ConfigFluidElement fluidConfigElement; public FluidConfigGui(GuiBase<?> guiBase) {
super(guiBase);
public static void init(GuiBase<?> guiBase) {
fluidConfigElement = new ConfigFluidElement(guiBase.getMachine().getTank(), SlotType.NORMAL, 35 - guiBase.getGuiLeft() + 50, 35 - guiBase.getGuiTop() - 25, guiBase);
} }
public static void draw(DrawContext drawContext, GuiBase<?> guiBase, int mouseX, int mouseY) { @Override
fluidConfigElement.draw(drawContext, guiBase, mouseX, mouseY); public String name() {
return "reborncore.gui.tooltip.config_fluids";
} }
public static List<ConfigFluidElement> getVisibleElements() { @Override
return Collections.singletonList(fluidConfigElement); public boolean enabled() {
return machine.showTankConfig();
} }
public static boolean mouseClicked(GuiBase<?> guiBase, double mouseX, double mouseY, int mouseButton) { @Override
if (mouseButton == 0) { public ItemStack stack() {
for (ConfigFluidElement configSlotElement : getVisibleElements()) { return GuiBase.fluidCellProvider.provide(Fluids.LAVA);
for (ElementBase element : Lists.reverse(configSlotElement.elements)) {
if (element.isMouseWithinRect(guiBase, mouseX, mouseY)) {
if (element.onClick(guiBase.getMachine(), guiBase, mouseX, mouseY)) {
return true;
}
}
}
}
} }
return !getVisibleElements().isEmpty(); @Override
public void open() {
fluidConfigElement = new ConfigFluidElement(SlotType.NORMAL, 35 - guiBase.getGuiLeft() + 50, 35 - guiBase.getGuiTop() - 25, guiBase);
}
@Override
public void close() {
fluidConfigElement = null;
}
@Override
public void draw(DrawContext drawContext, int x, int y) {
Objects.requireNonNull(fluidConfigElement).draw(drawContext, guiBase, x, y);
}
@Override
public boolean click(double mouseX, double mouseY, int mouseButton) {
if (mouseButton == 0 && fluidConfigElement != null) {
return fluidConfigElement.onClick(guiBase, mouseX, mouseY);
}
return false;
} }
} }

View file

@ -26,142 +26,61 @@ package reborncore.client.gui.builder.slot;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import org.apache.commons.lang3.Validate;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.builder.RedstoneConfigGui;
import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.blockentity.MachineBaseBlockEntity;
import java.util.LinkedList; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public class GuiTab { public abstract class GuiTab {
public static List<GuiTabFactory> TABS = List.of(
SlotConfigGui::new,
FluidConfigGui::new,
RedstoneConfigGui::new
);
private final Builder builder; protected final MachineBaseBlockEntity machine;
private final MachineBaseBlockEntity machineBaseBlockEntity; protected final GuiBase<?> guiBase;
private final GuiBase<?> guiBase;
private GuiTab(Builder builder, MachineBaseBlockEntity machineBaseBlockEntity, GuiBase<?> guiBase) { public GuiTab(GuiBase<?> guiBase) {
this.builder = builder; this.machine = guiBase.getMachine();
this.machineBaseBlockEntity = machineBaseBlockEntity;
this.guiBase = guiBase; this.guiBase = guiBase;
} }
public String name() { public abstract String name();
return builder.name;
}
public boolean enabled() { public abstract boolean enabled();
return builder.enabled.apply(this);
}
public ItemStack stack() { public abstract ItemStack stack();
return builder.stack.apply(this);
}
public MachineBaseBlockEntity machine() { public void open() {};
return machineBaseBlockEntity;
}
public void draw(DrawContext drawContext, int x, int y) { public void close() {};
builder.draw.draw(drawContext, guiBase, x, y);
} public abstract void draw(DrawContext drawContext, int x, int y);
public boolean click(double mouseX, double mouseY, int mouseButton) { public boolean click(double mouseX, double mouseY, int mouseButton) {
return builder.click.click(guiBase, mouseX, mouseY, mouseButton); return false;
} }
public boolean keyPress(int keyCode, int scanCode, int modifiers) { public boolean keyPress(int keyCode, int scanCode, int modifiers) {
return builder.keyPressed.keyPress(guiBase, keyCode, scanCode, modifiers); return false;
} }
public List<String> getTips() { public List<String> getTips() {
List<String> tips = new LinkedList<>(); return Collections.emptyList();
builder.tips.accept(tips);
return tips;
} }
public boolean hideGuiElements() { public boolean hideGuiElements() {
return builder.hideGuiElements; return true;
} }
public GuiBase<?> gui() { public GuiBase<?> gui() {
return guiBase; return guiBase;
} }
public static class Builder { public interface GuiTabFactory {
GuiTab create(GuiBase<?> gui);
private String name;
private Function<GuiTab, Boolean> enabled = (tab) -> true;
private Function<GuiTab, ItemStack> stack = (tab) -> ItemStack.EMPTY;
private Draw draw = (matrixStack, gui, x, y) -> {
};
private Click click = (guiBase, mouseX, mouseY, mouseButton) -> false;
private KeyPressed keyPressed = (guiBase, keyCode, scanCode, modifiers) -> false;
private Consumer<List<String>> tips = strings -> {
};
private boolean hideGuiElements = false;
public static Builder builder() {
return new Builder();
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder stack(Function<GuiTab, ItemStack> function) {
this.stack = function;
return this;
}
public Builder enabled(Function<GuiTab, Boolean> function) {
this.enabled = function;
return this;
}
public Builder draw(Draw draw) {
this.draw = draw;
return this;
}
public Builder click(Click click) {
this.click = click;
return this;
}
public Builder keyPressed(KeyPressed keyPressed) {
this.keyPressed = keyPressed;
return this;
}
public Builder tips(Consumer<List<String>> listConsumer) {
this.tips = listConsumer;
return this;
}
public Builder hideGuiElements() {
hideGuiElements = true;
return this;
}
public GuiTab build(MachineBaseBlockEntity blockEntity, GuiBase<?> guiBase) {
Validate.notBlank(name, "No name provided");
return new GuiTab(this, blockEntity, guiBase);
}
public interface Draw {
void draw(DrawContext drawContext, GuiBase<?> guiBase, int mouseX, int mouseY);
}
public interface Click {
boolean click(GuiBase<?> guiBase, double mouseX, double mouseY, int mouseButton);
}
public interface KeyPressed {
boolean keyPress(GuiBase<?> guiBase, int keyCode, int scanCode, int modifiers);
}
} }
} }

View file

@ -24,41 +24,56 @@
package reborncore.client.gui.builder.slot; package reborncore.client.gui.builder.slot;
import com.google.common.collect.Lists; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot; import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
import reborncore.client.ClientChatUtils; import reborncore.client.ClientChatUtils;
import reborncore.client.ClientNetworkManager; import reborncore.client.ClientNetworkManager;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.builder.slot.elements.ConfigSlotElement; import reborncore.client.gui.builder.slot.elements.ConfigSlotElement;
import reborncore.client.gui.builder.slot.elements.ElementBase;
import reborncore.client.gui.builder.slot.elements.SlotType; import reborncore.client.gui.builder.slot.elements.SlotType;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.network.ServerBoundPackets; import reborncore.common.network.ServerBoundPackets;
import reborncore.common.screen.BuiltScreenHandler; import reborncore.common.screen.BuiltScreenHandler;
import reborncore.common.util.Color; import reborncore.common.util.Color;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.Objects;
// Why is all this static? public class SlotConfigGui extends GuiTab {
public class SlotConfigGui { private Int2ObjectMap<ConfigSlotElement> slotElementMap = new Int2ObjectOpenHashMap<>();
public static HashMap<Integer, ConfigSlotElement> slotElementMap = new HashMap<>(); @Nullable
private ConfigSlotElement selectedSlot;
public static int selectedSlot = 0; public SlotConfigGui(GuiBase<?> guiBase) {
super(guiBase);
public static void reset() {
selectedSlot = -1;
} }
public static void init(GuiBase<?> guiBase) { @Override
reset(); public String name() {
return "reborncore.gui.tooltip.config_slots";
}
@Override
public boolean enabled() {
return machine.hasSlotConfig();
}
@Override
public ItemStack stack() {
return GuiBase.wrenchStack;
}
@Override
public void open() {
selectedSlot = null;
slotElementMap.clear(); slotElementMap.clear();
BuiltScreenHandler container = guiBase.builtScreenHandler; BuiltScreenHandler container = guiBase.builtScreenHandler;
@ -67,13 +82,27 @@ public class SlotConfigGui {
continue; continue;
} }
ConfigSlotElement slotElement = new ConfigSlotElement(guiBase.getMachine().getOptionalInventory().get(), slot.getIndex(), SlotType.NORMAL, slot.x - guiBase.getGuiLeft() + 50, slot.y - guiBase.getGuiTop() - 25, guiBase); ConfigSlotElement slotElement = new ConfigSlotElement(
guiBase.getMachine().getOptionalInventory().get(),
slot.getIndex(),
SlotType.NORMAL,
slot.x - guiBase.getGuiLeft() + 50,
slot.y - guiBase.getGuiTop() - 25,
guiBase,
this::close
);
slotElementMap.put(slot.getIndex(), slotElement); slotElementMap.put(slot.getIndex(), slotElement);
} }
} }
public static void draw(DrawContext drawContext, GuiBase<?> guiBase, int mouseX, int mouseY) { @Override
public void close() {
selectedSlot = null;
}
@Override
public void draw(DrawContext drawContext, int x, int y) {
BuiltScreenHandler container = guiBase.builtScreenHandler; BuiltScreenHandler container = guiBase.builtScreenHandler;
for (Slot slot : container.slots) { for (Slot slot : container.slots) {
if (guiBase.be != slot.inventory) { if (guiBase.be != slot.inventory) {
@ -84,35 +113,69 @@ public class SlotConfigGui {
drawContext.fill(slot.x -1, slot.y -1, slot.x + 17, slot.y + 17, color.getColor()); drawContext.fill(slot.x -1, slot.y -1, slot.x + 17, slot.y + 17, color.getColor());
} }
if (selectedSlot != -1) { if (selectedSlot != null) {
slotElementMap.get(selectedSlot).draw(drawContext, guiBase, mouseX, mouseY); selectedSlot.draw(drawContext, guiBase, x, y);
} }
} }
public static List<ConfigSlotElement> getVisibleElements() { @Override
if (selectedSlot == -1) { public boolean click(double mouseX, double mouseY, int mouseButton) {
return Collections.emptyList(); final BuiltScreenHandler screenHandler = Objects.requireNonNull(guiBase.builtScreenHandler);
if (selectedSlot != null) {
return selectedSlot.onClick(guiBase, mouseX, mouseY);
} else {
for (Slot slot : screenHandler.slots) {
if (guiBase.be != slot.inventory) {
continue;
}
if (guiBase.isPointInRect(slot.x, slot.y, 18, 18, mouseX, mouseY)) {
selectedSlot = slotElementMap.get(slot.getIndex());
return true;
}
} }
return slotElementMap.values().stream()
.filter(configSlotElement -> configSlotElement.getId() == selectedSlot)
.collect(Collectors.toList());
} }
public static void copyToClipboard() { return false;
MachineBaseBlockEntity machine = getMachine();
if (machine == null || machine.getSlotConfiguration() == null) {
return;
} }
@Override
public boolean keyPress(int keyCode, int scanCode, int modifiers) {
if (Screen.hasControlDown() && keyCode == GLFW.GLFW_KEY_C) {
copyToClipboard();
return true;
} else if (Screen.hasControlDown() && keyCode == GLFW.GLFW_KEY_V) {
pasteFromClipboard();
return true;
} else if (keyCode == GLFW.GLFW_KEY_ESCAPE && selectedSlot != null) {
selectedSlot = null;
return true;
}
return false;
}
@Override
public List<String> getTips() {
return List.of(
"reborncore.gui.slotconfigtip.slot",
"reborncore.gui.slotconfigtip.side1",
"reborncore.gui.slotconfigtip.side2",
"reborncore.gui.slotconfigtip.side3",
"reborncore.gui.slotconfigtip.copy1",
"reborncore.gui.slotconfigtip.copy2"
);
}
private void copyToClipboard() {
machine.getSlotConfiguration();
String json = machine.getSlotConfiguration().toJson(machine.getClass().getCanonicalName()); String json = machine.getSlotConfiguration().toJson(machine.getClass().getCanonicalName());
MinecraftClient.getInstance().keyboard.setClipboard(json); MinecraftClient.getInstance().keyboard.setClipboard(json);
ClientChatUtils.addHudMessage(Text.literal("Slot configuration copied to clipboard")); ClientChatUtils.addHudMessage(Text.literal("Slot configuration copied to clipboard"));
} }
public static void pasteFromClipboard() { private void pasteFromClipboard() {
MachineBaseBlockEntity machine = getMachine(); machine.getSlotConfiguration();
if (machine == null || machine.getSlotConfiguration() == null) {
return;
}
String json = MinecraftClient.getInstance().keyboard.getClipboard(); String json = MinecraftClient.getInstance().keyboard.getClipboard();
try { try {
machine.getSlotConfiguration().readJson(json, machine.getClass().getCanonicalName()); machine.getSlotConfiguration().readJson(json, machine.getClass().getCanonicalName());
@ -123,44 +186,7 @@ public class SlotConfigGui {
} }
} }
@Nullable public ConfigSlotElement getSelectedSlot() {
private static MachineBaseBlockEntity getMachine() { return selectedSlot;
if (!(MinecraftClient.getInstance().currentScreen instanceof GuiBase<?> base)) {
return null;
}
if (base.be instanceof MachineBaseBlockEntity machineBase) {
return machineBase;
}
return null;
}
public static boolean mouseClicked(GuiBase<?> guiBase, double mouseX, double mouseY, int mouseButton) {
BuiltScreenHandler screenHandler = guiBase.builtScreenHandler;
if (getVisibleElements().isEmpty()) {
for (Slot slot : screenHandler.slots) {
if (guiBase.be != slot.inventory) {
continue;
}
if (guiBase.isPointInRect(slot.x, slot.y, 18, 18, mouseX, mouseY)) {
selectedSlot = slot.getIndex();
return true;
}
}
}
if (mouseButton == 0) {
for (ConfigSlotElement configSlotElement : getVisibleElements()) {
for (ElementBase element : Lists.reverse(configSlotElement.elements)) {
if (element.isMouseWithinRect(guiBase, mouseX, mouseY)) {
if (element.onClick(guiBase.getMachine(), guiBase, mouseX, mouseY)) {
return true;
}
}
}
}
}
return !getVisibleElements().isEmpty();
} }
} }

View file

@ -75,24 +75,24 @@ public abstract class AbstractConfigPopupElement extends ElementBase {
} }
@Override @Override
public final boolean onClick(MachineBaseBlockEntity provider, GuiBase<?> gui, double mouseX, double mouseY) { public final boolean onClick(GuiBase<?> gui, double mouseX, double mouseY) {
if (isInBox(23, 4, 16, 16, mouseX, mouseY, gui)) { if (isInBox(23, 4, 16, 16, mouseX, mouseY, gui)) {
cycleConfig(MachineFacing.UP.getFacing(provider), gui); cycleConfig(MachineFacing.UP.getFacing(gui.getMachine()), gui);
return true; return true;
} else if (isInBox(23, 23, 16, 16, mouseX, mouseY, gui)) { } else if (isInBox(23, 23, 16, 16, mouseX, mouseY, gui)) {
cycleConfig(MachineFacing.FRONT.getFacing(provider), gui); cycleConfig(MachineFacing.FRONT.getFacing(gui.getMachine()), gui);
return true; return true;
} else if (isInBox(42, 23, 16, 16, mouseX, mouseY, gui)) { } else if (isInBox(42, 23, 16, 16, mouseX, mouseY, gui)) {
cycleConfig(MachineFacing.RIGHT.getFacing(provider), gui); cycleConfig(MachineFacing.RIGHT.getFacing(gui.getMachine()), gui);
return true; return true;
} else if (isInBox(4, 23, 16, 16, mouseX, mouseY, gui)) { } else if (isInBox(4, 23, 16, 16, mouseX, mouseY, gui)) {
cycleConfig(MachineFacing.LEFT.getFacing(provider), gui); cycleConfig(MachineFacing.LEFT.getFacing(gui.getMachine()), gui);
return true; return true;
} else if (isInBox(23, 42, 16, 16, mouseX, mouseY, gui)) { } else if (isInBox(23, 42, 16, 16, mouseX, mouseY, gui)) {
cycleConfig(MachineFacing.DOWN.getFacing(provider), gui); cycleConfig(MachineFacing.DOWN.getFacing(gui.getMachine()), gui);
return true; return true;
} else if (isInBox(42, 42, 16, 16, mouseX, mouseY, gui)) { } else if (isInBox(42, 42, 16, 16, mouseX, mouseY, gui)) {
cycleConfig(MachineFacing.BACK.getFacing(provider), gui); cycleConfig(MachineFacing.BACK.getFacing(gui.getMachine()), gui);
return true; return true;
} }

View file

@ -26,7 +26,6 @@ package reborncore.client.gui.builder.slot.elements;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.common.blockentity.MachineBaseBlockEntity;
public class ButtonElement extends ElementBase { public class ButtonElement extends ElementBase {
private final Sprite.Button buttonSprite; private final Sprite.Button buttonSprite;
@ -39,7 +38,7 @@ public class ButtonElement extends ElementBase {
} }
@Override @Override
public boolean onClick(MachineBaseBlockEntity provider, GuiBase<?> gui, double mouseX, double mouseY) { public boolean onClick(GuiBase<?> gui, double mouseX, double mouseY) {
onClicked.run(); onClicked.run();
return true; return true;
} }

View file

@ -27,7 +27,6 @@ package reborncore.client.gui.builder.slot.elements;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -57,7 +56,7 @@ public class CheckBoxElement extends ElementBase {
} }
@Override @Override
public boolean onClick(MachineBaseBlockEntity provider, GuiBase<?> gui, double mouseX, double mouseY) { public boolean onClick(GuiBase<?> gui, double mouseX, double mouseY) {
onChange.run(); onChange.run();
updateSprites(); updateSprites();
return true; return true;

View file

@ -27,21 +27,13 @@ package reborncore.client.gui.builder.slot.elements;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.common.util.Tank;
import java.util.ArrayList; public class ConfigFluidElement extends ParentElement {
import java.util.List; private final SlotType type;
public class ConfigFluidElement extends ElementBase { public ConfigFluidElement(SlotType type, int x, int y, GuiBase<?> gui) {
SlotType type;
Tank tank;
public List<ElementBase> elements = new ArrayList<>();
boolean filter = false;
public ConfigFluidElement(Tank tank, SlotType type, int x, int y, GuiBase<?> gui) {
super(x, y, type.getButtonSprite()); super(x, y, type.getButtonSprite());
this.type = type; this.type = type;
this.tank = tank;
FluidConfigPopupElement popupElement; FluidConfigPopupElement popupElement;
@ -63,19 +55,14 @@ public class ConfigFluidElement extends ElementBase {
@Override @Override
public int getHeight() { public int getHeight() {
return 105 + (filter ? 15 : 0); return 105;
} }
@Override @Override
public void draw(DrawContext drawContext, GuiBase<?> gui, int mouseX, int mouseY) { public void draw(DrawContext drawContext, GuiBase<?> gui, int mouseX, int mouseY) {
super.draw(drawContext, gui, mouseX, mouseY);
if (isMouseWithinRect(gui, mouseX, mouseY)) { if (isMouseWithinRect(gui, mouseX, mouseY)) {
drawSprite(drawContext, gui, type.getButtonHoverOverlay(), getX(), getY()); drawSprite(drawContext, gui, type.getButtonHoverOverlay(), getX(), getY());
} }
elements.forEach(elementBase -> elementBase.draw(drawContext, gui, mouseX, mouseY)); super.draw(drawContext, gui, mouseX, mouseY);
}
public SlotType getType() {
return type;
} }
} }

View file

@ -29,27 +29,30 @@ import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.builder.slot.SlotConfigGui;
import reborncore.common.blockentity.SlotConfiguration; import reborncore.common.blockentity.SlotConfiguration;
import reborncore.common.screen.slot.BaseSlot; import reborncore.common.screen.slot.BaseSlot;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class ConfigSlotElement extends ElementBase { public class ConfigSlotElement extends ParentElement {
SlotType type; private final SlotType type;
Inventory inventory; private final Inventory inventory;
int id; private final int id;
public List<ElementBase> elements = new ArrayList<>(); private final boolean filter;
boolean filter = false;
public ConfigSlotElement(Inventory slotInventory, int slotId, SlotType type, int x, int y, GuiBase<?> gui) { public ConfigSlotElement(Inventory slotInventory,
int slotId,
SlotType type,
int x,
int y,
GuiBase<?> gui,
Runnable closeConfig) {
super(x, y, type.getButtonSprite()); super(x, y, type.getButtonSprite());
this.type = type; this.type = type;
this.inventory = slotInventory; this.inventory = slotInventory;
this.id = slotId; this.id = slotId;
this.filter = gui.getMachine() instanceof SlotConfiguration.SlotFilter;
SlotConfigPopupElement popupElement; SlotConfigPopupElement popupElement;
@ -63,10 +66,7 @@ public class ConfigSlotElement extends ElementBase {
elements.add(popupElement = new SlotConfigPopupElement(this.id, x - 22, y - 22, inputEnabled)); elements.add(popupElement = new SlotConfigPopupElement(this.id, x - 22, y - 22, inputEnabled));
elements.add(new ButtonElement(x + 37, y - 25, Sprite.EXIT_BUTTON, () -> { elements.add(new ButtonElement(x + 37, y - 25, Sprite.EXIT_BUTTON, closeConfig));
SlotConfigGui.selectedSlot = -1;
gui.closeSelectedTab();
}));
if (inputEnabled) { if (inputEnabled) {
elements.add(new CheckBoxElement(Text.translatable("reborncore.gui.slotconfig.autoinput"), x - 26, y + 42, elements.add(new CheckBoxElement(Text.translatable("reborncore.gui.slotconfig.autoinput"), x - 26, y + 42,
@ -83,7 +83,6 @@ public class ConfigSlotElement extends ElementBase {
elements.add(new CheckBoxElement(Text.translatable("reborncore.gui.slotconfig.filter_input"), x - 26, y + 72, elements.add(new CheckBoxElement(Text.translatable("reborncore.gui.slotconfig.filter_input"), x - 26, y + 72,
checkBoxElement -> gui.getMachine().getSlotConfiguration().getSlotDetails(slotId).filter(), checkBoxElement -> gui.getMachine().getSlotConfiguration().getSlotDetails(slotId).filter(),
() -> popupElement.updateCheckBox("filter", gui))); () -> popupElement.updateCheckBox("filter", gui)));
filter = true;
popupElement.filter = true; popupElement.filter = true;
} }
} }
@ -101,7 +100,6 @@ public class ConfigSlotElement extends ElementBase {
@Override @Override
public void draw(DrawContext drawContext, GuiBase<?> gui, int mouseX, int mouseY) { public void draw(DrawContext drawContext, GuiBase<?> gui, int mouseX, int mouseY) {
super.draw(drawContext, gui, mouseX, mouseY);
ItemStack stack = inventory.getStack(id); ItemStack stack = inventory.getStack(id);
int xPos = getX() + 1 + gui.getGuiLeft(); int xPos = getX() + 1 + gui.getGuiLeft();
int yPos = getY() + 1 + gui.getGuiTop(); int yPos = getY() + 1 + gui.getGuiTop();
@ -111,12 +109,9 @@ public class ConfigSlotElement extends ElementBase {
if (isMouseWithinRect(gui, mouseX, mouseY)) { if (isMouseWithinRect(gui, mouseX, mouseY)) {
drawSprite(drawContext, gui, type.getButtonHoverOverlay(), getX(), getY()); drawSprite(drawContext, gui, type.getButtonHoverOverlay(), getX(), getY());
} }
elements.forEach(elementBase -> elementBase.draw(drawContext, gui, mouseX, mouseY)); super.draw(drawContext, gui, mouseX, mouseY);
} }
public SlotType getType() {
return type;
}
public int getId() { public int getId() {
return id; return id;

View file

@ -30,7 +30,6 @@ import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.guibuilder.GuiBuilder; import reborncore.client.gui.guibuilder.GuiBuilder;
import reborncore.common.blockentity.MachineBaseBlockEntity;
public class ElementBase { public class ElementBase {
private final int x; private final int x;
@ -69,7 +68,7 @@ public class ElementBase {
return sprite.height(); return sprite.height();
} }
public boolean onClick(MachineBaseBlockEntity provider, GuiBase<?> gui, double mouseX, double mouseY) { public boolean onClick(GuiBase<?> gui, double mouseX, double mouseY) {
return false; return false;
} }

View file

@ -0,0 +1,59 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2023 TeamReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package reborncore.client.gui.builder.slot.elements;
import com.google.common.collect.Lists;
import net.minecraft.client.gui.DrawContext;
import reborncore.client.gui.builder.GuiBase;
import java.util.ArrayList;
import java.util.List;
public abstract class ParentElement extends ElementBase {
protected final List<ElementBase> elements = new ArrayList<>();
public ParentElement(int x, int y, Sprite sprite) {
super(x, y, sprite);
}
@Override
public void draw(DrawContext drawContext, GuiBase<?> gui, int mouseX, int mouseY) {
super.draw(drawContext, gui, mouseX, mouseY);
elements.forEach(elementBase -> elementBase.draw(drawContext, gui, mouseX, mouseY));
}
@Override
public boolean onClick(GuiBase<?> gui, double mouseX, double mouseY) {
for (ElementBase element : Lists.reverse(elements)) {
if (element.isMouseWithinRect(gui, mouseX, mouseY)) {
if (element.onClick(gui, mouseX, mouseY)) {
return true;
}
}
}
return super.onClick(gui, mouseX, mouseY);
}
}

View file

@ -27,20 +27,10 @@ package reborncore.client.gui.builder.widget;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import reborncore.client.gui.builder.GuiBase;
/**
* Created by Prospector
*/
public class GuiButtonHologram extends GuiButtonExtended { public class GuiButtonHologram extends GuiButtonExtended {
public GuiButtonHologram(int x, int y, ButtonWidget.PressAction pressAction) {
GuiBase.Layer layer;
GuiBase<?> gui;
public GuiButtonHologram(int x, int y, GuiBase<?> gui, GuiBase.Layer layer, ButtonWidget.PressAction pressAction) {
super(x, y, 20, 12, Text.empty(), pressAction); super(x, y, 20, 12, Text.empty(), pressAction);
this.layer = layer;
this.gui = gui;
} }
@Override @Override

View file

@ -26,7 +26,6 @@ package reborncore.client.gui.guibuilder;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.fabricmc.fabric.api.transfer.v1.client.fluid.FluidVariantRendering; import net.fabricmc.fabric.api.transfer.v1.client.fluid.FluidVariantRendering;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;

View file

@ -38,13 +38,16 @@ public class SlotConfigExclusionZones implements ExclusionZonesProvider<GuiBase<
@Override @Override
public Collection<Rectangle> provide(GuiBase screen) { public Collection<Rectangle> provide(GuiBase screen) {
if (!screen.hideGuiElements()) { if (!(screen.getSelectedTab() instanceof SlotConfigGui slotConfigGui)){
return Collections.emptyList(); return Collections.emptyList();
} }
if (SlotConfigGui.selectedSlot == -1){
final ConfigSlotElement element = slotConfigGui.getSelectedSlot();
if (element == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
ConfigSlotElement element = SlotConfigGui.slotElementMap.get(SlotConfigGui.selectedSlot);
int slotX = element.getX() + screen.getGuiLeft() -50; int slotX = element.getX() + screen.getGuiLeft() -50;
if (element.getWidth() + slotX > screen.getScreenWidth() ) { if (element.getWidth() + slotX > screen.getScreenWidth() ) {
int slotY = element.getY() + screen.getGuiTop() + 25; int slotY = element.getY() + screen.getGuiTop() + 25;