From ee6bd1379cff67b6d36fa941f12cbf46ec6bd018 Mon Sep 17 00:00:00 2001 From: Michael Hillcox Date: Sat, 24 Sep 2022 09:42:45 +0100 Subject: [PATCH] feat: cleaned up some legacy code and fixed sliders --- .../client/renders/BaseRenderer.java | 5 +- .../client/renders/BuildRender.java | 22 +-- .../client/screen/CopyGUI.java | 4 +- .../client/screen/DestructionGUI.java | 89 +++++------- .../buildinggadgets/client/screen/GuiMod.java | 16 +-- .../client/screen/ModeRadialMenu.java | 11 +- .../client/screen/PasteGUI.java | 4 +- .../client/screen/ScrollingMaterialList.java | 4 +- .../screen/components/GuiSliderInt.java | 136 ------------------ .../{components => widgets}/DireButton.java | 2 +- .../{components => widgets}/EntryList.java | 2 +- .../GuiIconActionable.java | 2 +- .../GuiIncrementer.java | 2 +- .../GuiTextFieldBase.java | 4 +- .../widgets/IncrementalSliderWidget.java | 125 ++++++++++++++++ .../common/blocks/TemplateManager.java | 4 +- .../capability/CapabilityProviderEnergy.java | 6 +- .../PasteContainerCapabilityProvider.java | 6 +- .../containers/TemplateManagerContainer.java | 12 +- .../common/items/AbstractGadget.java | 18 +-- .../common/items/GadgetCopyPaste.java | 9 +- .../PacketTemplateManagerTemplateCreated.java | 4 +- .../tainted/inventory/InventoryHelper.java | 6 +- .../tainted/inventory/InventoryLinker.java | 6 +- .../inventory/handle/ItemHandlerProvider.java | 4 +- .../TemplateManagerTileEntity.java | 4 +- .../common/util/GadgetUtils.java | 4 +- 27 files changed, 234 insertions(+), 277 deletions(-) delete mode 100644 src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiSliderInt.java rename src/main/java/com/direwolf20/buildinggadgets/client/screen/{components => widgets}/DireButton.java (97%) rename src/main/java/com/direwolf20/buildinggadgets/client/screen/{components => widgets}/EntryList.java (98%) rename src/main/java/com/direwolf20/buildinggadgets/client/screen/{components => widgets}/GuiIconActionable.java (98%) rename src/main/java/com/direwolf20/buildinggadgets/client/screen/{components => widgets}/GuiIncrementer.java (98%) rename src/main/java/com/direwolf20/buildinggadgets/client/screen/{components => widgets}/GuiTextFieldBase.java (96%) create mode 100644 src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/IncrementalSliderWidget.java diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/renders/BaseRenderer.java b/src/main/java/com/direwolf20/buildinggadgets/client/renders/BaseRenderer.java index 2eeef440..c535a431 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/renders/BaseRenderer.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/renders/BaseRenderer.java @@ -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; @@ -90,7 +89,7 @@ private static void renderLinkedInventoryOutline(RenderLevelStageEvent evt, Item int getEnergy(Player player, ItemStack heldItem) { - LazyOptional energy = heldItem.getCapability(CapabilityEnergy.ENERGY); + LazyOptional energy = heldItem.getCapability(ForgeCapabilities.ENERGY); if (player.isCreative() || !energy.isPresent()) return Integer.MAX_VALUE; diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/renders/BuildRender.java b/src/main/java/com/direwolf20/buildinggadgets/client/renders/BuildRender.java index 1c6613b8..13fdb95c 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/renders/BuildRender.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/renders/BuildRender.java @@ -2,6 +2,10 @@ 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; @@ -9,30 +13,26 @@ 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; @@ -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 energyCap = heldItem.getCapability(CapabilityEnergy.ENERGY); + LazyOptional 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; diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/CopyGUI.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/CopyGUI.java index 2f8c6cc2..9011b6b4 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/CopyGUI.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/CopyGUI.java @@ -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; @@ -169,4 +169,4 @@ static void centerButtonList(List buttons, int startX) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/DestructionGUI.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/DestructionGUI.java index d86bc9c7..320f243a 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/DestructionGUI.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/DestructionGUI.java @@ -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; @@ -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 sliders = new HashSet<>(); - private GuiDestructionSlider left; - private GuiDestructionSlider right; - private GuiDestructionSlider up; - private GuiDestructionSlider down; - private GuiDestructionSlider depth; + private final Set 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 @@ -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(); @@ -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(); @@ -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() { @@ -123,10 +129,7 @@ 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 @@ -134,30 +137,4 @@ 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(); -// } - } -} \ No newline at end of file +} diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/GuiMod.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/GuiMod.java index 8598da72..302ea0b8 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/GuiMod.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/GuiMod.java @@ -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; @@ -19,19 +18,14 @@ public enum GuiMod { DESTRUCTION(GadgetDestruction::getGadget, stack -> () -> new DestructionGUI(stack)), MATERIAL_LIST(TemplateItem::getTemplateItem, stack -> () -> new MaterialListGUI(stack)); - private Function stackReader; - private Function> clientScreenProvider; + private final Function stackReader; + private final Function> clientScreenProvider; GuiMod(Function stackReader, Function> 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; @@ -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); - } } diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/ModeRadialMenu.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/ModeRadialMenu.java index a468bcf6..3b93ce58 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/ModeRadialMenu.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/ModeRadialMenu.java @@ -8,7 +8,8 @@ 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; @@ -16,6 +17,7 @@ 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; @@ -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()); @@ -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 diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/PasteGUI.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/PasteGUI.java index c9f4ee52..98a6243d 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/PasteGUI.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/PasteGUI.java @@ -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; @@ -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); } -} \ No newline at end of file +} diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/ScrollingMaterialList.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/ScrollingMaterialList.java index 73a045e8..699062c5 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/ScrollingMaterialList.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/ScrollingMaterialList.java @@ -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; @@ -302,4 +302,4 @@ public SortingModes next() { public static final SortingModes[] VALUES = SortingModes.values(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiSliderInt.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiSliderInt.java deleted file mode 100644 index bdcc0d3d..00000000 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiSliderInt.java +++ /dev/null @@ -1,136 +0,0 @@ -package com.direwolf20.buildinggadgets.client.screen.components; - -import com.direwolf20.buildinggadgets.client.ClientProxy; -import com.direwolf20.buildinggadgets.client.screen.GuiMod; -import com.direwolf20.buildinggadgets.common.network.PacketHandler; -import com.direwolf20.buildinggadgets.common.network.packets.PacketChangeRange; -import com.google.common.collect.ImmutableSet; -import com.mojang.blaze3d.vertex.PoseStack; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.components.AbstractWidget; -import net.minecraft.client.gui.components.Button; -import net.minecraft.client.sounds.SoundManager; -import net.minecraft.network.chat.Component; -import net.minecraft.sounds.SoundEvents; -import net.minecraftforge.client.gui.widget.ForgeSlider; - -import java.awt.*; -import java.util.Collection; -import java.util.function.BiConsumer; - -public class GuiSliderInt extends ForgeSlider { - private final int colorBackground; - private final int colorSliderBackground; - private final int colorSlider; - private final BiConsumer increment; - private int value; - - private Button.OnPress pressAction; - - public GuiSliderInt(int xPos, int yPos, int width, int height, Component prefix, Component suf, double minVal, double maxVal, - double currentVal, Color color, Button.OnPress par, - BiConsumer increment) { - - super(xPos, yPos, width, height, prefix, suf, minVal, maxVal, currentVal, true); - - this.pressAction = par; - this.colorBackground = GuiMod.getColor(color, 200).getRGB(); - this.colorSliderBackground = GuiMod.getColor(color.darker(), 200).getRGB(); - this.colorSlider = GuiMod.getColor(color.brighter().brighter(), 200).getRGB(); - - this.increment = increment; - } - - @Override - protected void applyValue() { - super.applyValue(); - int valueInt = this.getValueInt(); - if (this.value != valueInt) { - this.value = valueInt; - this.playSound(); - PacketHandler.sendToServer(new PacketChangeRange(this.getValueInt())); - } - } - - private void playSound() { - ClientProxy.playSound(SoundEvents.DISPENSER_FAIL, 2F); - } - - @Override - public void render(PoseStack matrices, int mouseX, int mouseY, float partial) { - if (!visible) - return; - - Minecraft mc = Minecraft.getInstance(); - this.isHovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height; - fill(matrices, this.x, this.y, this.x + this.width, this.y + this.height, this.colorBackground); - this.renderBg(matrices, mc, mouseX, mouseY); - this.renderText(matrices, mc, this); - } - - private void renderText(PoseStack matrices, Minecraft mc, AbstractWidget component) { - int color = !active ? 10526880 : (isHovered ? 16777120 : -1); - String buttonText = component.getMessage().getString(); - int strWidth = mc.font.width(buttonText); - int ellipsisWidth = mc.font.width("..."); - if (strWidth > component.getWidth() - 6 && strWidth > ellipsisWidth) - buttonText = mc.font.plainSubstrByWidth(buttonText, component.getWidth() - 6 - ellipsisWidth).trim() + "..."; - - drawCenteredString(matrices, mc.font, buttonText, component.x + component.getWidth() / 2, component.y + (component.getHeight() - 8) / 2, color); - } - - @Override - public void playDownSound(SoundManager p_playDownSound_1_) { - } - - @Override - protected void renderBg(PoseStack matrices, Minecraft mc, int mouseX, int mouseY) { - if (!visible) - return; - -// if (this.dragging) { -// this.sliderValue = (mouseX - (this.x + 4)) / (float) (this.width - 8); -// this.updateSlider(); -// } - - this.drawBorderedRect(matrices, this.x + (int) (this.getValueInt() * (this.width - 8)), this.y, 8, this.height); - } - - private void drawBorderedRect(PoseStack matrices, int x, int y, int width, int height) { - fill(matrices, x, y, x + width, y + height, colorSliderBackground); - fill(matrices, ++x, ++y, x + width - 2, y + height - 2, colorSlider); - } - - public Collection getComponents() { - return ImmutableSet.of( - this, - new GuiButtonIncrement(this, x - height, y, height, height, Component.literal("-"), b -> increment.accept(this, -1)), - new GuiButtonIncrement(this, x + width, y, height, height, Component.literal("+"), b -> increment.accept(this, 1) - )); - } - - private static class GuiButtonIncrement extends Button { - private GuiSliderInt parent; - - public GuiButtonIncrement(GuiSliderInt parent, int x, int y, int width, int height, Component buttonText, OnPress action) { - super(x, y, width, height, buttonText, action); - this.parent = parent; - } - - @Override - public void render(PoseStack matrices, int mouseX, int mouseY, float partial) { - if (!visible) - return; - - Minecraft mc = Minecraft.getInstance(); - this.isHovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height; - fill(matrices, this.x, this.y, this.x + this.width, this.y + this.height, this.parent.colorBackground); - this.parent.drawBorderedRect(matrices, this.x, this.y, this.width, this.height); - this.parent.renderText(matrices, mc, this); - } - - @Override - public void playDownSound(SoundManager p_playDownSound_1_) { - } - } -} diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/DireButton.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/DireButton.java similarity index 97% rename from src/main/java/com/direwolf20/buildinggadgets/client/screen/components/DireButton.java rename to src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/DireButton.java index 6ef5630d..e68cf861 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/DireButton.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/DireButton.java @@ -1,4 +1,4 @@ -package com.direwolf20.buildinggadgets.client.screen.components; +package com.direwolf20.buildinggadgets.client.screen.widgets; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/EntryList.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/EntryList.java similarity index 98% rename from src/main/java/com/direwolf20/buildinggadgets/client/screen/components/EntryList.java rename to src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/EntryList.java index 023864dc..be049656 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/EntryList.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/EntryList.java @@ -1,4 +1,4 @@ -package com.direwolf20.buildinggadgets.client.screen.components; +package com.direwolf20.buildinggadgets.client.screen.widgets; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.*; diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiIconActionable.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiIconActionable.java similarity index 98% rename from src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiIconActionable.java rename to src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiIconActionable.java index b66767e2..45c517cc 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiIconActionable.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiIconActionable.java @@ -1,4 +1,4 @@ -package com.direwolf20.buildinggadgets.client.screen.components; +package com.direwolf20.buildinggadgets.client.screen.widgets; import com.direwolf20.buildinggadgets.client.OurSounds; import com.direwolf20.buildinggadgets.common.util.ref.Reference; diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiIncrementer.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiIncrementer.java similarity index 98% rename from src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiIncrementer.java rename to src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiIncrementer.java index 4bbcc5b1..7c539f4e 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiIncrementer.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiIncrementer.java @@ -1,4 +1,4 @@ -package com.direwolf20.buildinggadgets.client.screen.components; +package com.direwolf20.buildinggadgets.client.screen.widgets; import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.client.Minecraft; diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiTextFieldBase.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiTextFieldBase.java similarity index 96% rename from src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiTextFieldBase.java rename to src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiTextFieldBase.java index ef52c5a6..117af59b 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/client/screen/components/GuiTextFieldBase.java +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/GuiTextFieldBase.java @@ -1,4 +1,4 @@ -package com.direwolf20.buildinggadgets.client.screen.components; +package com.direwolf20.buildinggadgets.client.screen.widgets; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.components.EditBox; @@ -70,4 +70,4 @@ public GuiTextFieldBase setDefaultValue(String defaultValue) { this.valueDefault = defaultValue; return this; } -} \ No newline at end of file +} diff --git a/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/IncrementalSliderWidget.java b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/IncrementalSliderWidget.java new file mode 100644 index 00000000..6aa601c4 --- /dev/null +++ b/src/main/java/com/direwolf20/buildinggadgets/client/screen/widgets/IncrementalSliderWidget.java @@ -0,0 +1,125 @@ +package com.direwolf20.buildinggadgets.client.screen.widgets; + +import com.direwolf20.buildinggadgets.client.OurSounds; +import com.google.common.collect.ImmutableSet; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.components.AbstractWidget; +import net.minecraft.client.gui.components.Button; +import net.minecraft.client.sounds.SoundManager; +import net.minecraft.network.chat.Component; +import net.minecraft.util.Mth; +import net.minecraftforge.client.gui.widget.ForgeSlider; + +import java.awt.*; +import java.util.Collection; +import java.util.function.Consumer; + +/** + * A flat colored, incremental (+ and - buttons) slider widget + */ +public class IncrementalSliderWidget extends ForgeSlider { + private static final int BACKGROUND = createAlphaColor(Color.DARK_GRAY, 200).getRGB(); + private static final int SLIDER_BACKGROUND = createAlphaColor(Color.DARK_GRAY.darker(), 200).getRGB(); + private static final int SLIDER_COLOR = createAlphaColor(Color.DARK_GRAY.brighter().brighter(), 200).getRGB(); + + public final Consumer onUpdate; + + public IncrementalSliderWidget(int x, int y, int width, int height, double min, double max, Component prefix, double current, Consumer onUpdate) { + super(x, y, width, height, prefix, Component.empty(), min, max, current, 1D, 1, true); + this.onUpdate = onUpdate; + } + + @Override + public void renderButton(PoseStack poseStack, int mouseX, int mouseY, float partialTick) { + fill(poseStack, this.x, this.y, this.x + this.width, this.y + this.height, BACKGROUND); + this.drawBorderedRect(poseStack, (this.x + (int)(this.value * (double)(this.width - 8)) + 4) - 4, this.y, 8, this.height); + this.renderText(poseStack); + } + + private void renderText(PoseStack matrices) { + int color = !active ? 10526880 : (isHovered ? 16777120 : -1); + + Minecraft minecraft = Minecraft.getInstance(); + drawCenteredString(matrices, minecraft.font, this.prefix.copy().append(this.getValueString()), x + getWidth() / 2, y + (getHeight() - 8) / 2, color); + } + + private void drawBorderedRect(PoseStack matrices, int x, int y, int width, int height) { + fill(matrices, x, y, x + width, y + height, SLIDER_BACKGROUND); + fill(matrices, ++x, ++y, x + width - 2, y + height - 2, SLIDER_COLOR); + } + + @Override + protected void applyValue() { + this.onUpdate.accept(this); + } + + @Override + public void onRelease(double p_93609_, double p_93610_) { + } + + @Override + public void playDownSound(SoundManager p_93605_) { + } + + @Override + public boolean mouseReleased(double p_93684_, double p_93685_, int p_93686_) { + var result = super.mouseReleased(p_93684_, p_93685_, p_93686_); + + // Prevents spam of sounds due to the ForgeSlider + if (result) { + OurSounds.playSound(OurSounds.BEEP.get()); + } + + return result; + } + + private static Color createAlphaColor(Color color, int alpha) { + return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); + } + + // This is lazy, I should really just build it into a single widget render + public Collection getComponents() { + return ImmutableSet.of( + this, + new GuiButtonIncrement(x - height, y, height, height, Component.literal("-"), b -> { + this.setValue(this.getValueInt() - 1); + IncrementalSliderWidget.this.applyValue(); + }), + new GuiButtonIncrement(x + width, y, height, height, Component.literal("+"), b -> { + this.setValue(this.getValueInt() + 1); + IncrementalSliderWidget.this.applyValue(); + }) + ); + } + + private class GuiButtonIncrement extends Button { + public GuiButtonIncrement(int x, int y, int width, int height, Component buttonText, OnPress action) { + super(x, y, width, height, buttonText, action); + } + + @Override + public void render(PoseStack matrices, int mouseX, int mouseY, float partial) { + if (!visible) + return; + + Minecraft minecraft = Minecraft.getInstance(); + Font font = minecraft.font; + + this.isHovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height; + fill(matrices, this.x, this.y, this.x + this.width, this.y + this.height, IncrementalSliderWidget.BACKGROUND); + IncrementalSliderWidget.this.drawBorderedRect(matrices, this.x, this.y, this.width, this.height); + drawCenteredString(matrices, font, this.getMessage(), this.x + this.width / 2, this.y + (this.height - 8) / 2, getFGColor() | Mth.ceil(this.alpha * 255.0F) << 24); + } + + @Override + public void onRelease(double p_93609_, double p_93610_) { + } + + @Override + public void playDownSound(SoundManager soundManager) { + OurSounds.playSound(OurSounds.BEEP.get()); + } + } +} diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/blocks/TemplateManager.java b/src/main/java/com/direwolf20/buildinggadgets/common/blocks/TemplateManager.java index 810cbc9e..405c486d 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/blocks/TemplateManager.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/blocks/TemplateManager.java @@ -22,7 +22,7 @@ import net.minecraft.world.level.block.state.properties.DirectionProperty; import net.minecraft.world.level.material.Material; import net.minecraft.world.phys.BlockHitResult; -import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.PacketDistributor; @@ -71,7 +71,7 @@ public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Play if (! (te instanceof TemplateManagerTileEntity)) return InteractionResult.FAIL; - te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> { + te.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(handler -> { worldIn.getCapability(CapabilityTemplate.TEMPLATE_PROVIDER_CAPABILITY).ifPresent(provider -> { for (int i = 0; i < handler.getSlots(); i++) { ItemStack itemStack = handler.getStackInSlot(i); diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/capability/CapabilityProviderEnergy.java b/src/main/java/com/direwolf20/buildinggadgets/common/capability/CapabilityProviderEnergy.java index 1efd2df4..f44ec25f 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/capability/CapabilityProviderEnergy.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/capability/CapabilityProviderEnergy.java @@ -1,11 +1,11 @@ package com.direwolf20.buildinggadgets.common.capability; -import net.minecraft.world.item.ItemStack; import net.minecraft.core.Direction; +import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.energy.CapabilityEnergy; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -23,7 +23,7 @@ public CapabilityProviderEnergy(ItemStack stack, IntSupplier energyCapacity) { @Nonnull @Override public LazyOptional getCapability(@Nonnull Capability cap, @Nullable Direction side) { - return cap == CapabilityEnergy.ENERGY ? energyCapability.cast() : LazyOptional.empty(); + return cap == ForgeCapabilities.ENERGY ? energyCapability.cast() : LazyOptional.empty(); } } diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/capability/PasteContainerCapabilityProvider.java b/src/main/java/com/direwolf20/buildinggadgets/common/capability/PasteContainerCapabilityProvider.java index ad4cb62c..528f28f1 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/capability/PasteContainerCapabilityProvider.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/capability/PasteContainerCapabilityProvider.java @@ -1,11 +1,11 @@ package com.direwolf20.buildinggadgets.common.capability; -import net.minecraft.world.item.ItemStack; import net.minecraft.core.Direction; +import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.items.CapabilityItemHandler; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -20,7 +20,7 @@ public PasteContainerCapabilityProvider(ItemStack container) { @Nonnull @Override public LazyOptional getCapability(@Nonnull Capability cap, @Nullable Direction side) { - if (CapabilityItemHandler.ITEM_HANDLER_CAPABILITY == cap) + if (ForgeCapabilities.ITEM_HANDLER == cap) return itemHandler.cast(); return LazyOptional.empty(); } diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/containers/TemplateManagerContainer.java b/src/main/java/com/direwolf20/buildinggadgets/common/containers/TemplateManagerContainer.java index e55b799f..7d410794 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/containers/TemplateManagerContainer.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/containers/TemplateManagerContainer.java @@ -3,14 +3,14 @@ import com.direwolf20.buildinggadgets.common.tileentities.TemplateManagerTileEntity; import com.direwolf20.buildinggadgets.common.util.exceptions.CapabilityNotPresentException; import com.direwolf20.buildinggadgets.common.util.ref.Reference; -import net.minecraft.world.entity.player.Player; +import net.minecraft.core.BlockPos; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.core.BlockPos; -import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.SlotItemHandler; @@ -46,7 +46,7 @@ public boolean stillValid(Player playerIn) { } private void addOwnSlots() { - IItemHandler itemHandler = this.getTe().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseThrow(CapabilityNotPresentException::new); + IItemHandler itemHandler = this.getTe().getCapability(ForgeCapabilities.ITEM_HANDLER).orElseThrow(CapabilityNotPresentException::new); int x = 132; addSlot(new SlotTemplateManager(itemHandler, 0, x, 18, TEXTURE_LOC_SLOT_TOOL)); addSlot(new SlotTemplateManager(itemHandler, 1, x, 63, TEXTURE_LOC_SLOT_TEMPLATE)); diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/items/AbstractGadget.java b/src/main/java/com/direwolf20/buildinggadgets/common/items/AbstractGadget.java index ad22c087..f3c9be72 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/items/AbstractGadget.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/items/AbstractGadget.java @@ -46,9 +46,9 @@ import net.minecraft.world.phys.BlockHitResult; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.fml.DistExecutor; import org.apache.commons.lang3.tuple.Pair; @@ -125,7 +125,7 @@ public void fillItemCategory(CreativeModeTab group, NonNullList items @Override public int getBarWidth(ItemStack stack) { - LazyOptional cap = stack.getCapability(CapabilityEnergy.ENERGY); + LazyOptional cap = stack.getCapability(ForgeCapabilities.ENERGY); if (!cap.isPresent()) return super.getBarWidth(stack); @@ -135,7 +135,7 @@ public int getBarWidth(ItemStack stack) { @Override public int getBarColor(ItemStack stack) { - LazyOptional cap = stack.getCapability(CapabilityEnergy.ENERGY); + LazyOptional cap = stack.getCapability(ForgeCapabilities.ENERGY); if (!cap.isPresent()) return super.getBarColor(stack); @@ -145,7 +145,7 @@ public int getBarColor(ItemStack stack) { @Override public boolean isDamaged(ItemStack stack) { - LazyOptional cap = stack.getCapability(CapabilityEnergy.ENERGY); + LazyOptional cap = stack.getCapability(ForgeCapabilities.ENERGY); if (!cap.isPresent()) return super.isDamaged(stack); @@ -159,12 +159,12 @@ public boolean isBarVisible(ItemStack stack) { if (stack.hasTag() && stack.getTag().contains(NBTKeys.CREATIVE_MARKER)) return false; - return stack.getCapability(CapabilityEnergy.ENERGY).map(e -> e.getEnergyStored() != e.getMaxEnergyStored()).orElse(super.isBarVisible(stack)); + return stack.getCapability(ForgeCapabilities.ENERGY).map(e -> e.getEnergyStored() != e.getMaxEnergyStored()).orElse(super.isBarVisible(stack)); } @Override public boolean isValidRepairItem(ItemStack toRepair, ItemStack repair) { - return !toRepair.getCapability(CapabilityEnergy.ENERGY).isPresent() && repair.getItem() == Items.DIAMOND; + return !toRepair.getCapability(ForgeCapabilities.ENERGY).isPresent() && repair.getItem() == Items.DIAMOND; } public boolean isAllowedBlock(BlockState block) { @@ -190,21 +190,21 @@ public boolean canUse(ItemStack tool, Player player) { if (player.isCreative() || getEnergyMax() == 0) return true; - return getEnergyCost(tool) <= tool.getCapability(CapabilityEnergy.ENERGY).map(IEnergyStorage::getEnergyStored).orElse(0); + return getEnergyCost(tool) <= tool.getCapability(ForgeCapabilities.ENERGY).map(IEnergyStorage::getEnergyStored).orElse(0); } public void applyDamage(ItemStack tool, ServerPlayer player) { if (player.isCreative() || getEnergyMax() == 0) return; - tool.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> ((IPrivateEnergy) e).extractPower(getEnergyCost(tool), false)); + tool.getCapability(ForgeCapabilities.ENERGY).ifPresent(e -> ((IPrivateEnergy) e).extractPower(getEnergyCost(tool), false)); } protected void addEnergyInformation(List tooltip, ItemStack stack) { if (getEnergyMax() == 0) return; - stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(energy -> { + stack.getCapability(ForgeCapabilities.ENERGY).ifPresent(energy -> { tooltip.add(TooltipTranslation.GADGET_ENERGY .componentTranslation(withSuffix(energy.getEnergyStored()), withSuffix(energy.getMaxEnergyStored())) .setStyle(Styles.GRAY)); diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/items/GadgetCopyPaste.java b/src/main/java/com/direwolf20/buildinggadgets/common/items/GadgetCopyPaste.java index 4bd77221..f14633a7 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/items/GadgetCopyPaste.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/items/GadgetCopyPaste.java @@ -63,9 +63,8 @@ import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult.Type; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.capabilities.ICapabilityProvider; -import net.minecraftforge.energy.CapabilityEnergy; -import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.network.PacketDistributor; import javax.annotation.Nullable; @@ -319,7 +318,7 @@ public InteractionResultHolder use(Level world, Player player, Intera BlockHitResult posLookingAt = VectorHelper.getLookingAt(player, stack); BlockEntity tileEntity = world.getBlockEntity(posLookingAt.getBlockPos()); - boolean lookingAtInventory = tileEntity != null && tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent(); + boolean lookingAtInventory = tileEntity != null && tileEntity.getCapability(ForgeCapabilities.ITEM_HANDLER).isPresent(); if (!world.isClientSide()) { if (player.isShiftKeyDown() && lookingAtInventory) { @@ -469,7 +468,7 @@ private void schedulePlacement(ItemStack stack, IBuildView view, Player player) boolean overwrite = Config.GENERAL.allowOverwriteBlocks.get(); BlockPlaceContext useContext = new BlockPlaceContext(new UseOnContext(player, InteractionHand.MAIN_HAND, VectorHelper.getLookingAt(player, stack))); PlacementChecker checker = new PlacementChecker( - stack.getCapability(CapabilityEnergy.ENERGY), + stack.getCapability(ForgeCapabilities.ENERGY), t -> energyCost, index, (c, t) -> overwrite ? c.getWorld().getBlockState(t.getPos()).canBeReplaced(useContext) : c.getWorld().isEmptyBlock(t.getPos()), @@ -489,4 +488,4 @@ private void onBuildFinished(ItemStack stack, Player player, Region bounds) { private void sendMessage(ItemStack stack, Player player, ITranslationProvider messageSource, Style style) { player.displayClientMessage(messageSource.componentTranslation().setStyle(style), true); } -} \ No newline at end of file +} diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/network/packets/PacketTemplateManagerTemplateCreated.java b/src/main/java/com/direwolf20/buildinggadgets/common/network/packets/PacketTemplateManagerTemplateCreated.java index 49225334..91e83368 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/network/packets/PacketTemplateManagerTemplateCreated.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/network/packets/PacketTemplateManagerTemplateCreated.java @@ -10,8 +10,8 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.fml.LogicalSide; -import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.PacketDistributor; @@ -46,7 +46,7 @@ public void handle(Supplier contextSupplier) { if (world.hasChunkAt(pos)) { BlockEntity tileEntity = world.getBlockEntity(pos); if (tileEntity != null) { - tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> { + tileEntity.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(handler -> { ItemStack stack = new ItemStack(OurItems.TEMPLATE_ITEM.get()); ITemplateKey key = stack.getCapability(CapabilityTemplate.TEMPLATE_KEY_CAPABILITY).orElseThrow(CapabilityNotPresentException::new); UUID id = key.getTemplateId(this::getId); diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryHelper.java b/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryHelper.java index 0f9c93ef..27b96cdb 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryHelper.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryHelper.java @@ -33,8 +33,8 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.DoubleBlockHalf; import net.minecraft.world.level.block.state.properties.Property; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.fml.InterModComms; -import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.wrapper.EmptyHandler; @@ -95,7 +95,7 @@ static List getHandlers(ItemStack stack, Player player) { List handlers = new ArrayList<>(); InventoryLinker.getLinkedInventory(player.level, stack).ifPresent(handlers::add); - player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handlers::add); + player.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(handlers::add); return handlers; } @@ -218,7 +218,7 @@ private static List findInvContainers(Inventory inv) { for (int i = 0; i < 36; ++i) { ItemStack stack = inv.getItem(i); - stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + stack.getCapability(ForgeCapabilities.ITEM_HANDLER) .ifPresent(containers::add); } diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryLinker.java b/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryLinker.java index 6f38dfad..d4c9d89b 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryLinker.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/InventoryLinker.java @@ -12,8 +12,8 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.phys.BlockHitResult; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import org.apache.commons.lang3.tuple.Pair; @@ -25,7 +25,7 @@ public class InventoryLinker { */ public static Result linkInventory(Level world, ItemStack stack, BlockHitResult trace) { BlockEntity tileEntity = world.getBlockEntity(trace.getBlockPos()); - if (tileEntity == null || !tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent()) { + if (tileEntity == null || !tileEntity.getCapability(ForgeCapabilities.ITEM_HANDLER).isPresent()) { return Result.fail(MessageTranslation.INVALID_BOUND_TILE); } @@ -64,7 +64,7 @@ public static LazyOptional getLinkedInventory(Level world, BlockPo return LazyOptional.empty(); } - return tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY); + return tileEntity.getCapability(ForgeCapabilities.ITEM_HANDLER); } public static LazyOptional getLinkedInventory(Level world, ItemStack stack) { diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/handle/ItemHandlerProvider.java b/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/handle/ItemHandlerProvider.java index 6451d052..de0c31fa 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/handle/ItemHandlerProvider.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/tainted/inventory/handle/ItemHandlerProvider.java @@ -4,9 +4,9 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import java.util.*; @@ -44,7 +44,7 @@ public static void index(IItemHandler handler, Map, Map, Map>>> indexMap, Set> indexedClasses) { if (indexedClasses.contains(Item.class)) return false; - LazyOptional cap = capProvider.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY); + LazyOptional cap = capProvider.getCapability(ForgeCapabilities.ITEM_HANDLER); if (! cap.isPresent()) return false; IItemHandler handler = cap.orElseThrow(RuntimeException::new); diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/tileentities/TemplateManagerTileEntity.java b/src/main/java/com/direwolf20/buildinggadgets/common/tileentities/TemplateManagerTileEntity.java index dd26a973..5e6a1cef 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/tileentities/TemplateManagerTileEntity.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/tileentities/TemplateManagerTileEntity.java @@ -21,8 +21,8 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemStackHandler; @@ -106,7 +106,7 @@ public boolean canInteractWith(Player playerIn) { @Nonnull @Override public LazyOptional getCapability(@Nonnull Capability cap, final @Nullable Direction side) { - if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && handlerOpt != null) + if (cap == ForgeCapabilities.ITEM_HANDLER && handlerOpt != null) return handlerOpt.cast(); return super.getCapability(cap, side); } diff --git a/src/main/java/com/direwolf20/buildinggadgets/common/util/GadgetUtils.java b/src/main/java/com/direwolf20/buildinggadgets/common/util/GadgetUtils.java index 72ced5cb..b50fa8d7 100644 --- a/src/main/java/com/direwolf20/buildinggadgets/common/util/GadgetUtils.java +++ b/src/main/java/com/direwolf20/buildinggadgets/common/util/GadgetUtils.java @@ -42,8 +42,8 @@ import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.BlockHitResult; +import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; @@ -305,7 +305,7 @@ public static ResourceLocation getDIMFromNBT(ItemStack stack, String tagName) { public static void dropTileEntityInventory(Level world, BlockPos pos) { BlockEntity tileEntity = world.getBlockEntity(pos); if (tileEntity != null) { - LazyOptional cap = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY); + LazyOptional cap = tileEntity.getCapability(ForgeCapabilities.ITEM_HANDLER); cap.ifPresent(handler -> { for (int i = 0; i < handler.getSlots(); i++) { net.minecraft.world.Containers.dropItemStack(world, pos.getX(), pos.getY(), pos.getZ(), handler.getStackInSlot(i));