Skip to content

Commit

Permalink
feat: cleaned up some legacy code and fixed sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Sep 24, 2022
1 parent 5e68270 commit ee6bd13
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelLastEvent;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import org.apache.commons.lang3.tuple.Pair;

Expand Down Expand Up @@ -90,7 +89,7 @@ private static void renderLinkedInventoryOutline(RenderLevelStageEvent evt, Item


int getEnergy(Player player, ItemStack heldItem) {
LazyOptional<IEnergyStorage> energy = heldItem.getCapability(CapabilityEnergy.ENERGY);
LazyOptional<IEnergyStorage> energy = heldItem.getCapability(ForgeCapabilities.ENERGY);
if (player.isCreative() || !energy.isPresent())
return Integer.MAX_VALUE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@

import com.direwolf20.buildinggadgets.client.renderer.OurRenderTypes;
import com.direwolf20.buildinggadgets.common.blocks.OurBlocks;
import com.direwolf20.buildinggadgets.common.items.AbstractGadget;
import com.direwolf20.buildinggadgets.common.items.GadgetBuilding;
import com.direwolf20.buildinggadgets.common.items.GadgetExchanger;
import com.direwolf20.buildinggadgets.common.items.modes.AbstractMode;
import com.direwolf20.buildinggadgets.common.tainted.building.BlockData;
import com.direwolf20.buildinggadgets.common.tainted.building.view.BuildContext;
import com.direwolf20.buildinggadgets.common.tainted.inventory.IItemIndex;
import com.direwolf20.buildinggadgets.common.tainted.inventory.InventoryHelper;
import com.direwolf20.buildinggadgets.common.tainted.inventory.MatchResult;
import com.direwolf20.buildinggadgets.common.tainted.inventory.RecordingItemIndex;
import com.direwolf20.buildinggadgets.common.tainted.inventory.materials.MaterialList;
import com.direwolf20.buildinggadgets.common.items.AbstractGadget;
import com.direwolf20.buildinggadgets.common.items.GadgetBuilding;
import com.direwolf20.buildinggadgets.common.items.GadgetExchanger;
import com.direwolf20.buildinggadgets.common.items.modes.AbstractMode;
import com.direwolf20.buildinggadgets.common.tainted.inventory.materials.objects.UniqueItem;
import com.direwolf20.buildinggadgets.common.util.helpers.VectorHelper;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.client.model.data.ModelData;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;

import java.util.List;
Expand Down Expand Up @@ -134,7 +134,7 @@ public void render(RenderLevelStageEvent evt, Player player, ItemStack heldItem)
MaterialList materials = data.getRequiredItems(context, null, null);
int hasEnergy = getEnergy(player, heldItem);

LazyOptional<IEnergyStorage> energyCap = heldItem.getCapability(CapabilityEnergy.ENERGY);
LazyOptional<IEnergyStorage> energyCap = heldItem.getCapability(ForgeCapabilities.ENERGY);

for (BlockPos coordinate : coordinates) { //Now run through the UNSORTED list of coords, to show which blocks won't place if you don't have enough of them.
boolean renderFree = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.direwolf20.buildinggadgets.client.screen;

import com.direwolf20.buildinggadgets.client.screen.components.GuiIncrementer;
import com.direwolf20.buildinggadgets.client.screen.widgets.GuiIncrementer;
import com.direwolf20.buildinggadgets.common.config.Config;
import com.direwolf20.buildinggadgets.common.items.GadgetCopyPaste;
import com.direwolf20.buildinggadgets.common.network.PacketHandler;
Expand Down Expand Up @@ -169,4 +169,4 @@ static void centerButtonList(List<AbstractButton> buttons, int startX) {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.direwolf20.buildinggadgets.client.screen;

import com.direwolf20.buildinggadgets.client.screen.components.GuiSliderInt;
import com.direwolf20.buildinggadgets.client.screen.widgets.IncrementalSliderWidget;
import com.direwolf20.buildinggadgets.common.config.Config;
import com.direwolf20.buildinggadgets.common.items.GadgetDestruction;
import com.direwolf20.buildinggadgets.common.network.PacketHandler;
Expand All @@ -15,27 +15,28 @@
import net.minecraft.world.item.ItemStack;

import javax.annotation.Nonnull;
import java.awt.*;
import java.util.HashSet;
import java.util.Set;

public class DestructionGUI extends Screen {
private final Set<GuiDestructionSlider> sliders = new HashSet<>();
private GuiDestructionSlider left;
private GuiDestructionSlider right;
private GuiDestructionSlider up;
private GuiDestructionSlider down;
private GuiDestructionSlider depth;
private final Set<IncrementalSliderWidget> sliders = new HashSet<>();

private IncrementalSliderWidget left;
private IncrementalSliderWidget right;
private IncrementalSliderWidget up;
private IncrementalSliderWidget down;
private IncrementalSliderWidget depth;

private Button confirm;

private String sizeString = "";
private boolean isValidSize = true;

private final ItemStack destructionTool;
private final ItemStack destructionGadget;

public DestructionGUI(ItemStack tool) {
super(Component.literal("Destruction Gui?!?"));
this.destructionTool = tool;
super(Component.empty());
this.destructionGadget = tool;
}

@Override
Expand All @@ -60,11 +61,11 @@ public void init() {
this.addRenderableWidget(new Button((x - 30) - 32, y + 65, 60, 20, Component.translatable(GuiMod.getLangKeySingle("cancel")), b -> onClose()));

sliders.clear();
sliders.add(depth = new GuiDestructionSlider(x - (GuiDestructionSlider.width / 2), y - (GuiDestructionSlider.height / 2), GuiTranslation.SINGLE_DEPTH.format() + ":", GadgetDestruction.getToolValue(destructionTool, "depth")));
sliders.add(right = new GuiDestructionSlider(x + (GuiDestructionSlider.width + 5), y - (GuiDestructionSlider.height / 2), GuiTranslation.SINGLE_RIGHT.format() + ":", GadgetDestruction.getToolValue(destructionTool, "right")));
sliders.add(left = new GuiDestructionSlider(x - (GuiDestructionSlider.width * 2) - 5, y - (GuiDestructionSlider.height / 2), GuiTranslation.SINGLE_LEFT.format() + ":", GadgetDestruction.getToolValue(destructionTool, "left")));
sliders.add(up = new GuiDestructionSlider(x - (GuiDestructionSlider.width / 2), y - 35, GuiTranslation.SINGLE_UP.format() + ":", GadgetDestruction.getToolValue(destructionTool, "up")));
sliders.add(down = new GuiDestructionSlider(x - (GuiDestructionSlider.width / 2), y + 20, GuiTranslation.SINGLE_DOWN.format() + ":", GadgetDestruction.getToolValue(destructionTool, "down")));
sliders.add(depth = this.createSlider(x - (70 / 2), y - (14 / 2), GuiTranslation.SINGLE_DEPTH, GadgetDestruction.getToolValue(destructionGadget, "depth")));
sliders.add(right = this.createSlider(x + (70 + 5), y - (14 / 2), GuiTranslation.SINGLE_RIGHT, GadgetDestruction.getToolValue(destructionGadget, "right")));
sliders.add(left = this.createSlider(x - (70 * 2) - 5, y - (14 / 2), GuiTranslation.SINGLE_LEFT, GadgetDestruction.getToolValue(destructionGadget, "left")));
sliders.add(up = this.createSlider(x - (70 / 2), y - 35, GuiTranslation.SINGLE_UP, GadgetDestruction.getToolValue(destructionGadget, "up")));
sliders.add(down = this.createSlider(x - (70 / 2), y + 20, GuiTranslation.SINGLE_DOWN, GadgetDestruction.getToolValue(destructionGadget, "down")));

updateSizeString();
updateIsValid();
Expand All @@ -73,6 +74,15 @@ public void init() {
sliders.forEach(gui -> gui.getComponents().forEach(this::addRenderableWidget));
}

public IncrementalSliderWidget createSlider(int x, int y, GuiTranslation prefix, int value) {
return new IncrementalSliderWidget(x, y, 70, 14, 0D, 16D, prefix.componentTranslation().append(": "), value, this::onSliderUpdate);
}

public void onSliderUpdate(IncrementalSliderWidget widget) {
this.updateSizeString();
this.updateIsValid();
}

private boolean isWithinBounds() {
int x = 1 + left.getValueInt() + right.getValueInt();
int y = 1 + up.getValueInt() + down.getValueInt();
Expand All @@ -83,15 +93,11 @@ private boolean isWithinBounds() {
}

private String getSizeString() {
int x = 1 + left.getValueInt() + right.getValueInt(),
y = 1 + up.getValueInt() + down.getValueInt(),
z = depth.getValueInt();

return String.format("%d x %d x %d",
x,
y,
z
);
int x = 1 + left.getValueInt() + right.getValueInt();
int y = 1 + up.getValueInt() + down.getValueInt();
int z = depth.getValueInt();

return String.format("%d x %d x %d", x, y, z);
}

private void updateIsValid() {
Expand Down Expand Up @@ -123,41 +129,12 @@ public void render(@Nonnull PoseStack matrices, int mouseX, int mouseY, float pa

@Override
public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) {
for (GuiDestructionSlider slider : sliders) {
slider.onRelease(mouseX, mouseY);
}
return true;
return super.mouseReleased(mouseX, mouseY, mouseButton);
}

@Override
public boolean isPauseScreen() {
return false;
}

// This is only done to reduce code dupe in this class.
private class GuiDestructionSlider extends GuiSliderInt {
public static final int width = 70;
public static final int height = 14;

private static final int min = 0;
private static final int max = 16;


GuiDestructionSlider(int x, int y, String prefix, int current) {
super(
x, y, width, height, Component.literal(String.format("%s ", prefix)), Component.literal(""), min, max, current, Color.DARK_GRAY, null,
(slider, amount) -> {
// slider.setValue(Mth.clamp(slider.getValueInt() + amount, min, max));
// slider.updateSlider();
}
);
}

// @Override
// public void updateSlider() {
// super.updateSlider();
// DestructionGUI.this.updateSizeString();
// DestructionGUI.this.updateIsValid();
// }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.direwolf20.buildinggadgets.client.screen;

import com.direwolf20.buildinggadgets.common.items.TemplateItem;
import com.direwolf20.buildinggadgets.common.items.GadgetCopyPaste;
import com.direwolf20.buildinggadgets.common.items.GadgetDestruction;
import com.direwolf20.buildinggadgets.common.items.TemplateItem;
import com.direwolf20.buildinggadgets.common.util.lang.LangUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

import java.awt.*;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -19,19 +18,14 @@ public enum GuiMod {
DESTRUCTION(GadgetDestruction::getGadget, stack -> () -> new DestructionGUI(stack)),
MATERIAL_LIST(TemplateItem::getTemplateItem, stack -> () -> new MaterialListGUI(stack));

private Function<Player, ItemStack> stackReader;
private Function<ItemStack, Supplier<? extends Screen>> clientScreenProvider;
private final Function<Player, ItemStack> stackReader;
private final Function<ItemStack, Supplier<? extends Screen>> clientScreenProvider;

GuiMod(Function<Player, ItemStack> stackReader, Function<ItemStack, Supplier<? extends Screen>> clientScreenProvider) {
this.stackReader = stackReader;
this.clientScreenProvider = clientScreenProvider;
}

// fixme: 1.14 requires this but I'm not sure on how to implement it.
public static Screen openScreen(Minecraft minecraft, Screen screen) {
return screen;
}

public boolean openScreen(Player player) {
if (clientScreenProvider == null)
return false;
Expand All @@ -48,8 +42,4 @@ public boolean openScreen(Player player) {
public static String getLangKeySingle(String name) {
return LangUtil.getLangKey("gui", "single", name);
}

public static Color getColor(Color color, int alpha) {
return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import com.direwolf20.buildinggadgets.client.KeyBindings;
import com.direwolf20.buildinggadgets.client.OurSounds;
import com.direwolf20.buildinggadgets.client.renderer.OurRenderTypes;
import com.direwolf20.buildinggadgets.client.screen.components.GuiIconActionable;
import com.direwolf20.buildinggadgets.client.screen.widgets.GuiIconActionable;
import com.direwolf20.buildinggadgets.client.screen.widgets.IncrementalSliderWidget;
import com.direwolf20.buildinggadgets.common.config.Config;
import com.direwolf20.buildinggadgets.common.items.*;
import com.direwolf20.buildinggadgets.common.items.modes.BuildingModes;
import com.direwolf20.buildinggadgets.common.items.modes.ExchangingModes;
import com.direwolf20.buildinggadgets.common.network.PacketHandler;
import com.direwolf20.buildinggadgets.common.network.packets.*;
import com.direwolf20.buildinggadgets.common.util.GadgetUtils;
import com.direwolf20.buildinggadgets.common.util.lang.GuiTranslation;
import com.direwolf20.buildinggadgets.common.util.lang.MessageTranslation;
import com.direwolf20.buildinggadgets.common.util.lang.RadialTranslation;
import com.direwolf20.buildinggadgets.common.util.lang.Styles;
Expand Down Expand Up @@ -151,7 +153,9 @@ public void init() {
}
if (!isDestruction) {
int widthSlider = 82;
// GuiSliderInt sliderRange = new GuiSliderInt(width / 2 - widthSlider / 2, height / 2 + 72, widthSlider, 14, GuiTranslation.SINGLE_RANGE.componentTranslation().append(Component.literal(": ")), Component.literal(""), 1, Config.GADGETS.maxRange.get(),
IncrementalSliderWidget sliderRange = new IncrementalSliderWidget(width / 2 - widthSlider / 2, height / 2 + 72, widthSlider, 14, 1, Config.GADGETS.maxRange.get(), GuiTranslation.SINGLE_RANGE.componentTranslation().append(": "), GadgetUtils.getToolRange(tool), slider -> {
sendRangeUpdate(slider.getValueInt());
});
// GadgetUtils.getToolRange(tool), false, true, Color.DARK_GRAY, slider -> {
// GuiSliderInt sliderI = (GuiSliderInt) slider;
// this.sendRangeUpdate(sliderI.getValueInt());
Expand All @@ -163,8 +167,7 @@ public void init() {
// slider.updateSlider();
// }
// );
// sliderRange.precision = 1;
// sliderRange.getComponents().forEach(this::addRenderableWidget);
sliderRange.getComponents().forEach(this::addRenderableWidget);
}
} else {
// Copy Paste specific
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package com.direwolf20.buildinggadgets.client.screen;

import com.direwolf20.buildinggadgets.client.screen.components.GuiIncrementer;
import com.direwolf20.buildinggadgets.client.screen.widgets.GuiIncrementer;
import com.direwolf20.buildinggadgets.common.items.GadgetCopyPaste;
import com.direwolf20.buildinggadgets.common.network.PacketHandler;
import com.direwolf20.buildinggadgets.common.network.packets.PacketPasteGUI;
Expand Down Expand Up @@ -107,4 +107,4 @@ public void render(PoseStack matrices, int mouseX, int mouseY, float partialTick
private void drawLabel(PoseStack matrices, String name, int x) {
font.drawShadow(matrices, name, (width / 2f) + x, (height / 2f) - 30, 0xFFFFFF);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.direwolf20.buildinggadgets.client.screen;

import com.direwolf20.buildinggadgets.client.screen.components.EntryList;
import com.direwolf20.buildinggadgets.client.screen.widgets.EntryList;
import com.direwolf20.buildinggadgets.common.tainted.inventory.IItemIndex;
import com.direwolf20.buildinggadgets.common.tainted.inventory.InventoryHelper;
import com.direwolf20.buildinggadgets.common.tainted.inventory.MatchResult;
Expand Down Expand Up @@ -302,4 +302,4 @@ public SortingModes next() {
public static final SortingModes[] VALUES = SortingModes.values();

}
}
}
Loading

0 comments on commit ee6bd13

Please sign in to comment.