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.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
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.SlotConfigGui;
import reborncore.client.gui.builder.widget.GuiButtonHologram;
import reborncore.client.gui.guibuilder.GuiBuilder;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.common.screen.slot.PlayerInventorySlot;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Created by Prospector
*/
import java.util.*;
public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
public static FluidCellProvider fluidCellProvider = fluid -> 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 BlockEntity be;
@Nullable
@ -123,8 +61,9 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
private final int xSize = 176;
private final int ySize = 176;
private GuiTab selectedTab;
private List<GuiTab> tabs;
@Nullable
private GuiTab selectedTab = null;
private final List<GuiTab> tabs;
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())));
this.be = blockEntity;
this.builtScreenHandler = (BuiltScreenHandler) screenHandler;
selectedTab = null;
populateSlots();
}
private void populateSlots() {
tabs = tabBuilders.stream()
.map(builder -> builder.build(getMachine(), this))
.filter(GuiTab::enabled)
.collect(Collectors.toList());
tabs = GuiTab.TABS.stream()
.map(factory -> factory.create(this))
.filter(GuiTab::enabled)
.toList();
}
public int getScreenWidth() {
@ -185,11 +119,8 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
@Override
public void init() {
super.init();
if (isConfigEnabled()) {
SlotConfigGui.init(this);
}
if (isConfigEnabled() && getMachine().getTank() != null && getMachine().showTankConfig()) {
FluidConfigGui.init(this);
for (GuiTab tab : tabs) {
tab.open();
}
}
@ -308,7 +239,7 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
}
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);
return buttonHologram;
@ -333,9 +264,8 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
if (selectedTab == tab) {
closeSelectedTab();
} else {
selectedTab = tab;
setSelectedTab(tab);
}
SlotConfigGui.reset();
break;
}
offset -= 24;
@ -362,7 +292,6 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
super.close();
}
@Nullable
public MachineBaseBlockEntity getMachine() {
return (MachineBaseBlockEntity) be;
}
@ -427,10 +356,24 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
return selectedTab != null && selectedTab.hideGuiElements();
}
private void setSelectedTab(GuiTab tab) {
Objects.requireNonNull(tab);
selectedTab = tab;
selectedTab.open();
}
public void closeSelectedTab() {
if (selectedTab != null) {
selectedTab.close();
}
selectedTab = null;
}
public GuiTab getSelectedTab() {
return selectedTab;
}
@Override
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.

View file

@ -25,23 +25,41 @@
package reborncore.client.gui.builder;
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 reborncore.client.ClientNetworkManager;
import reborncore.client.gui.guibuilder.GuiBuilder;
import reborncore.client.gui.builder.slot.GuiTab;
import reborncore.common.blockentity.RedstoneConfiguration;
import reborncore.common.network.IdentifiedPacket;
import reborncore.common.network.ServerBoundPackets;
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;
RedstoneConfiguration configuration = guiBase.getMachine().getRedstoneConfiguration();
GuiBuilder builder = guiBase.builder;
ItemRenderer itemRenderer = guiBase.getMinecraft().getItemRenderer();
int x = 10;
int y = 100;
@ -60,10 +78,10 @@ public class RedstoneConfigGui {
guiBase.drawCentredText(drawContext, name, y + (i * spread), -1, x + 37, GuiBase.Layer.FOREGROUND);
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;
RedstoneConfiguration configuration = guiBase.getMachine().getRedstoneConfiguration();
@ -89,10 +107,14 @@ public class RedstoneConfigGui {
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();
mouseY -= guiBase.getGuiTop();
return (mouseX > x && mouseX < x + width) && (mouseY > y && mouseY < y + height);
}
}

View file

@ -24,46 +24,60 @@
package reborncore.client.gui.builder.slot;
import com.google.common.collect.Lists;
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.slot.elements.ConfigFluidElement;
import reborncore.client.gui.builder.slot.elements.ElementBase;
import reborncore.client.gui.builder.slot.elements.SlotType;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
// Why is this static?
public class FluidConfigGui {
public class FluidConfigGui extends GuiTab {
@Nullable
private ConfigFluidElement fluidConfigElement;
static ConfigFluidElement fluidConfigElement;
public static void init(GuiBase<?> guiBase) {
fluidConfigElement = new ConfigFluidElement(guiBase.getMachine().getTank(), SlotType.NORMAL, 35 - guiBase.getGuiLeft() + 50, 35 - guiBase.getGuiTop() - 25, guiBase);
public FluidConfigGui(GuiBase<?> guiBase) {
super(guiBase);
}
public static void draw(DrawContext drawContext, GuiBase<?> guiBase, int mouseX, int mouseY) {
fluidConfigElement.draw(drawContext, guiBase, mouseX, mouseY);
@Override
public String name() {
return "reborncore.gui.tooltip.config_fluids";
}
public static List<ConfigFluidElement> getVisibleElements() {
return Collections.singletonList(fluidConfigElement);
@Override
public boolean enabled() {
return machine.showTankConfig();
}
public static boolean mouseClicked(GuiBase<?> guiBase, double mouseX, double mouseY, int mouseButton) {
if (mouseButton == 0) {
for (ConfigFluidElement 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;
}
}
}
}
@Override
public ItemStack stack() {
return GuiBase.fluidCellProvider.provide(Fluids.LAVA);
}
@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 !getVisibleElements().isEmpty();
return false;
}
}

View file

@ -26,142 +26,61 @@ package reborncore.client.gui.builder.slot;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack;
import org.apache.commons.lang3.Validate;
import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.builder.RedstoneConfigGui;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import java.util.LinkedList;
import java.util.Collections;
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;
private final MachineBaseBlockEntity machineBaseBlockEntity;
private final GuiBase<?> guiBase;
protected final MachineBaseBlockEntity machine;
protected final GuiBase<?> guiBase;
private GuiTab(Builder builder, MachineBaseBlockEntity machineBaseBlockEntity, GuiBase<?> guiBase) {
this.builder = builder;
this.machineBaseBlockEntity = machineBaseBlockEntity;
public GuiTab(GuiBase<?> guiBase) {
this.machine = guiBase.getMachine();
this.guiBase = guiBase;
}
public String name() {
return builder.name;
}
public abstract String name();
public boolean enabled() {
return builder.enabled.apply(this);
}
public abstract boolean enabled();
public ItemStack stack() {
return builder.stack.apply(this);
}
public abstract ItemStack stack();
public MachineBaseBlockEntity machine() {
return machineBaseBlockEntity;
}
public void open() {};
public void draw(DrawContext drawContext, int x, int y) {
builder.draw.draw(drawContext, guiBase, x, y);
}
public void close() {};
public abstract void draw(DrawContext drawContext, int x, int y);
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) {
return builder.keyPressed.keyPress(guiBase, keyCode, scanCode, modifiers);
return false;
}
public List<String> getTips() {
List<String> tips = new LinkedList<>();
builder.tips.accept(tips);
return tips;
return Collections.emptyList();
}
public boolean hideGuiElements() {
return builder.hideGuiElements;
return true;
}
public GuiBase<?> gui() {
return guiBase;
}
public static class Builder {
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);
}
public interface GuiTabFactory {
GuiTab create(GuiBase<?> gui);
}
}

View file

@ -24,41 +24,56 @@
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.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
import reborncore.client.ClientChatUtils;
import reborncore.client.ClientNetworkManager;
import reborncore.client.gui.builder.GuiBase;
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.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.network.ServerBoundPackets;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.common.util.Color;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Objects;
// Why is all this static?
public class SlotConfigGui {
public class SlotConfigGui extends GuiTab {
private Int2ObjectMap<ConfigSlotElement> slotElementMap = new Int2ObjectOpenHashMap<>();
public static HashMap<Integer, ConfigSlotElement> slotElementMap = new HashMap<>();
@Nullable
private ConfigSlotElement selectedSlot;
public static int selectedSlot = 0;
public static void reset() {
selectedSlot = -1;
public SlotConfigGui(GuiBase<?> guiBase) {
super(guiBase);
}
public static void init(GuiBase<?> guiBase) {
reset();
@Override
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();
BuiltScreenHandler container = guiBase.builtScreenHandler;
@ -67,13 +82,27 @@ public class SlotConfigGui {
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);
}
}
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;
for (Slot slot : container.slots) {
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());
}
if (selectedSlot != -1) {
slotElementMap.get(selectedSlot).draw(drawContext, guiBase, mouseX, mouseY);
if (selectedSlot != null) {
selectedSlot.draw(drawContext, guiBase, x, y);
}
}
public static List<ConfigSlotElement> getVisibleElements() {
if (selectedSlot == -1) {
return Collections.emptyList();
@Override
public boolean click(double mouseX, double mouseY, int mouseButton) {
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());
return false;
}
public static void copyToClipboard() {
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());
MinecraftClient.getInstance().keyboard.setClipboard(json);
ClientChatUtils.addHudMessage(Text.literal("Slot configuration copied to clipboard"));
}
public static void pasteFromClipboard() {
MachineBaseBlockEntity machine = getMachine();
if (machine == null || machine.getSlotConfiguration() == null) {
return;
}
private void pasteFromClipboard() {
machine.getSlotConfiguration();
String json = MinecraftClient.getInstance().keyboard.getClipboard();
try {
machine.getSlotConfiguration().readJson(json, machine.getClass().getCanonicalName());
@ -123,44 +186,7 @@ public class SlotConfigGui {
}
}
@Nullable
private static MachineBaseBlockEntity getMachine() {
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();
public ConfigSlotElement getSelectedSlot() {
return selectedSlot;
}
}

View file

@ -75,24 +75,24 @@ public abstract class AbstractConfigPopupElement extends ElementBase {
}
@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)) {
cycleConfig(MachineFacing.UP.getFacing(provider), gui);
cycleConfig(MachineFacing.UP.getFacing(gui.getMachine()), gui);
return true;
} 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;
} 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;
} 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;
} 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;
} 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;
}

View file

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

View file

@ -27,7 +27,6 @@ package reborncore.client.gui.builder.slot.elements;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import reborncore.client.gui.builder.GuiBase;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import java.util.function.Predicate;
@ -38,8 +37,8 @@ public class CheckBoxElement extends ElementBase {
private final Sprite.CheckBox checkBoxSprite;
public CheckBoxElement(Text label, int x, int y,
Predicate<CheckBoxElement> ticked,
Runnable onChange) {
Predicate<CheckBoxElement> ticked,
Runnable onChange) {
super(x, y, Sprite.LIGHT_CHECK_BOX.normal());
this.checkBoxSprite = Sprite.LIGHT_CHECK_BOX;
this.label = label;
@ -57,7 +56,7 @@ public class CheckBoxElement extends ElementBase {
}
@Override
public boolean onClick(MachineBaseBlockEntity provider, GuiBase<?> gui, double mouseX, double mouseY) {
public boolean onClick(GuiBase<?> gui, double mouseX, double mouseY) {
onChange.run();
updateSprites();
return true;

View file

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

View file

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

View file

@ -30,7 +30,6 @@ import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.guibuilder.GuiBuilder;
import reborncore.common.blockentity.MachineBaseBlockEntity;
public class ElementBase {
private final int x;
@ -69,7 +68,7 @@ public class ElementBase {
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;
}

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

View file

@ -26,7 +26,6 @@ package reborncore.client.gui.guibuilder;
import com.google.common.collect.Lists;
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.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;

View file

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