diff --git a/.gitignore b/.gitignore index b1b60b1..711e4c6 100644 --- a/.gitignore +++ b/.gitignore @@ -55,6 +55,6 @@ target/ .libs/ # Testing -src/test/ -src/main/java/de/studiocode/invui/InvUIPlugin.java -src/main/resources/plugin.yml +InvUI/src/test/ +InvUI/src/main/java/de/studiocode/invui/InvUIPlugin.java +InvUI/src/main/resources/plugin.yml diff --git a/InvUI/pom.xml b/InvUI/pom.xml new file mode 100644 index 0000000..c16f958 --- /dev/null +++ b/InvUI/pom.xml @@ -0,0 +1,131 @@ + + + 4.0.0 + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../pom.xml + + + InvUI + + + 16 + 16 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + minecraft-repo + https://libraries.minecraft.net/ + + + + + + org.jetbrains + annotations + 16.0.1 + compile + + + org.spigotmc + spigot + 1.17-R0.1-SNAPSHOT + provided + + + com.mojang + authlib + 1.5.21 + provided + + + * + * + + + + + de.studiocode.invui + api + 0.1-SNAPSHOT + + + de.studiocode.invui + 1_14_R1 + 0.1-SNAPSHOT + + + de.studiocode.invui + 1_15_R1 + 0.1-SNAPSHOT + + + de.studiocode.invui + 1_16_R1 + 0.1-SNAPSHOT + + + de.studiocode.invui + 1_16_R2 + 0.1-SNAPSHOT + + + de.studiocode.invui + 1_16_R3 + 0.1-SNAPSHOT + + + de.studiocode.invui + 1_17_R1 + 0.1-SNAPSHOT + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 16 + 16 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + ${dir}/${project.artifactId}-${project.version}-shaded.jar + + + + package + + shade + + + + + * + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/de/studiocode/invui/InvUI.java b/InvUI/src/main/java/de/studiocode/invui/InvUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/InvUI.java rename to InvUI/src/main/java/de/studiocode/invui/InvUI.java diff --git a/src/main/java/de/studiocode/invui/animation/Animation.java b/InvUI/src/main/java/de/studiocode/invui/animation/Animation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/Animation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/Animation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/BaseAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/BaseAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/BaseAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/BaseAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/ColumnAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/ColumnAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/ColumnAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/ColumnAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/HorizontalSnakeAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/HorizontalSnakeAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/HorizontalSnakeAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/HorizontalSnakeAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/RandomAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/RandomAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/RandomAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/RandomAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/RowAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/RowAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/RowAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/RowAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/SequentialAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/SequentialAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/SequentialAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/SequentialAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/SoundAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/SoundAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/SoundAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/SoundAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/SplitSequentialAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/SplitSequentialAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/SplitSequentialAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/SplitSequentialAnimation.java diff --git a/src/main/java/de/studiocode/invui/animation/impl/VerticalSnakeAnimation.java b/InvUI/src/main/java/de/studiocode/invui/animation/impl/VerticalSnakeAnimation.java similarity index 100% rename from src/main/java/de/studiocode/invui/animation/impl/VerticalSnakeAnimation.java rename to InvUI/src/main/java/de/studiocode/invui/animation/impl/VerticalSnakeAnimation.java diff --git a/src/main/java/de/studiocode/invui/gui/Controllable.java b/InvUI/src/main/java/de/studiocode/invui/gui/Controllable.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/Controllable.java rename to InvUI/src/main/java/de/studiocode/invui/gui/Controllable.java diff --git a/src/main/java/de/studiocode/invui/gui/GUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/GUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/GUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/GUI.java diff --git a/src/main/java/de/studiocode/invui/gui/GUIParent.java b/InvUI/src/main/java/de/studiocode/invui/gui/GUIParent.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/GUIParent.java rename to InvUI/src/main/java/de/studiocode/invui/gui/GUIParent.java diff --git a/src/main/java/de/studiocode/invui/gui/SlotElement.java b/InvUI/src/main/java/de/studiocode/invui/gui/SlotElement.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/SlotElement.java rename to InvUI/src/main/java/de/studiocode/invui/gui/SlotElement.java diff --git a/src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java rename to InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java diff --git a/src/main/java/de/studiocode/invui/gui/builder/GUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIType.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/builder/GUIType.java rename to InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIType.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/SimpleGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/SimpleGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/SimpleGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/SimpleGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/SimplePagedItemsGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/SimplePagedItemsGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/SimplePagedItemsGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/SimplePagedItemsGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/SimplePagedNestedGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/SimplePagedNestedGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/SimplePagedNestedGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/SimplePagedNestedGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/SimpleScrollGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/SimpleScrollGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/SimpleScrollGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/SimpleScrollGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/SimpleTabGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/SimpleTabGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/SimpleTabGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/SimpleTabGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/impl/TabGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/TabGUI.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/impl/TabGUI.java rename to InvUI/src/main/java/de/studiocode/invui/gui/impl/TabGUI.java diff --git a/src/main/java/de/studiocode/invui/gui/structure/Ingredient.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Ingredient.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/structure/Ingredient.java rename to InvUI/src/main/java/de/studiocode/invui/gui/structure/Ingredient.java diff --git a/src/main/java/de/studiocode/invui/gui/structure/IngredientList.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/IngredientList.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/structure/IngredientList.java rename to InvUI/src/main/java/de/studiocode/invui/gui/structure/IngredientList.java diff --git a/src/main/java/de/studiocode/invui/gui/structure/Marker.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Marker.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/structure/Marker.java rename to InvUI/src/main/java/de/studiocode/invui/gui/structure/Marker.java diff --git a/src/main/java/de/studiocode/invui/gui/structure/Structure.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Structure.java similarity index 100% rename from src/main/java/de/studiocode/invui/gui/structure/Structure.java rename to InvUI/src/main/java/de/studiocode/invui/gui/structure/Structure.java diff --git a/src/main/java/de/studiocode/invui/item/Click.java b/InvUI/src/main/java/de/studiocode/invui/item/Click.java similarity index 100% rename from src/main/java/de/studiocode/invui/item/Click.java rename to InvUI/src/main/java/de/studiocode/invui/item/Click.java diff --git a/src/main/java/de/studiocode/invui/item/Item.java b/InvUI/src/main/java/de/studiocode/invui/item/Item.java similarity index 100% rename from src/main/java/de/studiocode/invui/item/Item.java rename to InvUI/src/main/java/de/studiocode/invui/item/Item.java diff --git a/src/main/java/de/studiocode/invui/item/ItemBuilder.java b/InvUI/src/main/java/de/studiocode/invui/item/ItemBuilder.java similarity index 100% rename from src/main/java/de/studiocode/invui/item/ItemBuilder.java rename to InvUI/src/main/java/de/studiocode/invui/item/ItemBuilder.java diff --git a/src/main/java/de/studiocode/invui/item/impl/AsyncItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/AsyncItem.java similarity index 100% rename from src/main/java/de/studiocode/invui/item/impl/AsyncItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/AsyncItem.java diff --git a/src/main/java/de/studiocode/invui/item/impl/AutoCycleItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/AutoCycleItem.java similarity index 100% rename from src/main/java/de/studiocode/invui/item/impl/AutoCycleItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/AutoCycleItem.java diff --git a/src/main/java/de/studiocode/invui/item/impl/AutoUpdateItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/AutoUpdateItem.java similarity index 88% rename from src/main/java/de/studiocode/invui/item/impl/AutoUpdateItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/AutoUpdateItem.java index 2bf33f0..bc251f6 100644 --- a/src/main/java/de/studiocode/invui/item/impl/AutoUpdateItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/AutoUpdateItem.java @@ -12,12 +12,12 @@ import java.util.function.Supplier; * An {@link Item} that updates it's {@link ItemBuilder} every specified amount * of ticks. */ -public class AutoUpdateItem extends SupplierItem { +public class AutoUpdateItem extends SuppliedItem { private final BukkitTask task; public AutoUpdateItem(int period, Supplier builderSupplier) { - super(builderSupplier); + super(builderSupplier, null); task = Bukkit.getScheduler().runTaskTimer(InvUI.getInstance().getPlugin(), this::notifyWindows, 0, period); } diff --git a/src/main/java/de/studiocode/invui/item/impl/BaseItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/BaseItem.java similarity index 100% rename from src/main/java/de/studiocode/invui/item/impl/BaseItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/BaseItem.java diff --git a/src/main/java/de/studiocode/invui/item/impl/CommandItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/CommandItem.java similarity index 100% rename from src/main/java/de/studiocode/invui/item/impl/CommandItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/CommandItem.java diff --git a/src/main/java/de/studiocode/invui/item/impl/CycleItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/CycleItem.java similarity index 86% rename from src/main/java/de/studiocode/invui/item/impl/CycleItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/CycleItem.java index 51e7ed1..b239136 100644 --- a/src/main/java/de/studiocode/invui/item/impl/CycleItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/CycleItem.java @@ -32,13 +32,17 @@ public class CycleItem extends BaseItem { @Override public void handleClick(ClickType clickType, Player player, InventoryClickEvent event) { - if (clickType.isLeftClick()) { + if (clickType.isLeftClick()) cycle(true); + else if (clickType.isRightClick()) cycle(false); + } + + public void cycle(boolean forward) { + if (forward) { if (++state == states.length) state = 0; - handleStateChange(); - } else if (clickType.isRightClick()) { + } else { if (--state < 0) state = states.length - 1; - handleStateChange(); } + handleStateChange(); } private void handleStateChange() { diff --git a/src/main/java/de/studiocode/invui/item/impl/SimpleItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/SimpleItem.java similarity index 69% rename from src/main/java/de/studiocode/invui/item/impl/SimpleItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/SimpleItem.java index c5f9318..a2b3427 100644 --- a/src/main/java/de/studiocode/invui/item/impl/SimpleItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/SimpleItem.java @@ -7,6 +7,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.function.Consumer; @@ -16,18 +17,16 @@ import java.util.function.Consumer; public class SimpleItem extends BaseItem { private final ItemBuilder itemBuilder; + private final Consumer clickHandler; public SimpleItem(@NotNull ItemBuilder itemBuilder) { this.itemBuilder = itemBuilder; + this.clickHandler = null; } - public static SimpleItem of(ItemBuilder itemBuilder, Consumer clickHandler) { - return new SimpleItem(itemBuilder) { - @Override - public void handleClick(ClickType clickType, Player player, InventoryClickEvent event) { - clickHandler.accept(new Click(event)); - } - }; + public SimpleItem(@NotNull ItemBuilder itemBuilder, @Nullable Consumer clickHandler) { + this.itemBuilder = itemBuilder; + this.clickHandler = clickHandler; } public ItemBuilder getItemBuilder() { @@ -36,7 +35,7 @@ public class SimpleItem extends BaseItem { @Override public void handleClick(ClickType clickType, Player player, InventoryClickEvent event) { - // empty + if (clickHandler != null) clickHandler.accept(new Click(event)); } } diff --git a/src/main/java/de/studiocode/invui/item/impl/SupplierItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/SuppliedItem.java similarity index 53% rename from src/main/java/de/studiocode/invui/item/impl/SupplierItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/SuppliedItem.java index 603651d..9a8e9a8 100644 --- a/src/main/java/de/studiocode/invui/item/impl/SupplierItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/SuppliedItem.java @@ -1,18 +1,24 @@ package de.studiocode.invui.item.impl; +import de.studiocode.invui.item.Click; import de.studiocode.invui.item.ItemBuilder; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.function.Function; import java.util.function.Supplier; -public class SupplierItem extends BaseItem { +public class SuppliedItem extends BaseItem { private final Supplier builderSupplier; + private final Function clickHandler; - public SupplierItem(Supplier builderSupplier) { + public SuppliedItem(@NotNull Supplier builderSupplier, @Nullable Function clickHandler) { this.builderSupplier = builderSupplier; + this.clickHandler = clickHandler; } @Override @@ -22,7 +28,7 @@ public class SupplierItem extends BaseItem { @Override public void handleClick(ClickType clickType, Player player, InventoryClickEvent event) { - // empty + if (clickHandler != null && clickHandler.apply(new Click(event))) notifyWindows(); } } diff --git a/src/main/java/de/studiocode/invui/item/impl/controlitem/ControlItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/ControlItem.java similarity index 66% rename from src/main/java/de/studiocode/invui/item/impl/controlitem/ControlItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/ControlItem.java index 0566a88..fccdc98 100644 --- a/src/main/java/de/studiocode/invui/item/impl/controlitem/ControlItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/ControlItem.java @@ -1,23 +1,18 @@ package de.studiocode.invui.item.impl.controlitem; import de.studiocode.invui.gui.GUI; -import de.studiocode.invui.item.impl.BaseItem; import de.studiocode.invui.item.ItemBuilder; - -import java.util.function.Function; +import de.studiocode.invui.item.impl.BaseItem; public abstract class ControlItem extends BaseItem { private G gui; - private final Function builderFunction; - public ControlItem(Function builderFunction) { - this.builderFunction = builderFunction; - } + public abstract ItemBuilder getItemBuilder(G gui); @Override - public ItemBuilder getItemBuilder() { - return builderFunction.apply(gui); + public final ItemBuilder getItemBuilder() { + return getItemBuilder(gui); } public G getGui() { diff --git a/src/main/java/de/studiocode/invui/item/impl/controlitem/PageItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/PageItem.java similarity index 69% rename from src/main/java/de/studiocode/invui/item/impl/controlitem/PageItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/PageItem.java index 7770ef9..88368f9 100644 --- a/src/main/java/de/studiocode/invui/item/impl/controlitem/PageItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/PageItem.java @@ -1,22 +1,18 @@ package de.studiocode.invui.item.impl.controlitem; import de.studiocode.invui.gui.impl.PagedGUI; -import de.studiocode.invui.item.ItemBuilder; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; -import java.util.function.Function; - /** * Switches between pages in a {@link PagedGUI} */ -public class PageItem extends ControlItem { +public abstract class PageItem extends ControlItem { private final boolean forward; - public PageItem(boolean forward, Function builderFunction) { - super(builderFunction); + public PageItem(boolean forward) { this.forward = forward; } @@ -25,7 +21,7 @@ public class PageItem extends ControlItem { if (clickType == ClickType.LEFT) { if (forward) getGui().goForward(); else getGui().goBack(); - } + } } } diff --git a/src/main/java/de/studiocode/invui/item/impl/controlitem/ScrollItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/ScrollItem.java similarity index 63% rename from src/main/java/de/studiocode/invui/item/impl/controlitem/ScrollItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/ScrollItem.java index 93a132f..db34a56 100644 --- a/src/main/java/de/studiocode/invui/item/impl/controlitem/ScrollItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/ScrollItem.java @@ -1,29 +1,25 @@ package de.studiocode.invui.item.impl.controlitem; import de.studiocode.invui.gui.impl.ScrollGUI; -import de.studiocode.invui.item.ItemBuilder; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; import java.util.HashMap; -import java.util.function.Function; /** * Scrolls in a {@link ScrollGUI} */ -public class ScrollItem extends ControlItem { +public abstract class ScrollItem extends ControlItem { private final HashMap scroll; - public ScrollItem(int scrollLeftClick, Function builderFunction) { - super(builderFunction); + public ScrollItem(int scrollLeftClick) { scroll = new HashMap<>(); scroll.put(ClickType.LEFT, scrollLeftClick); } - public ScrollItem(HashMap scroll, Function builderFunction) { - super(builderFunction); + public ScrollItem(HashMap scroll) { this.scroll = scroll; } diff --git a/src/main/java/de/studiocode/invui/item/impl/controlitem/TabItem.java b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/TabItem.java similarity index 69% rename from src/main/java/de/studiocode/invui/item/impl/controlitem/TabItem.java rename to InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/TabItem.java index 2e7e6e5..0a85ac2 100644 --- a/src/main/java/de/studiocode/invui/item/impl/controlitem/TabItem.java +++ b/InvUI/src/main/java/de/studiocode/invui/item/impl/controlitem/TabItem.java @@ -1,22 +1,18 @@ package de.studiocode.invui.item.impl.controlitem; import de.studiocode.invui.gui.impl.TabGUI; -import de.studiocode.invui.item.ItemBuilder; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; -import java.util.function.Function; - /** * Switches between tabs in a {@link TabGUI} */ -public class TabItem extends ControlItem { +public abstract class TabItem extends ControlItem { private final int tab; - public TabItem(int tab, Function builderFunction) { - super(builderFunction); + public TabItem(int tab) { this.tab = tab; } diff --git a/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java b/InvUI/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java similarity index 95% rename from src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java rename to InvUI/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java index 39c4fbe..d7953c4 100644 --- a/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java +++ b/InvUI/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java @@ -42,7 +42,6 @@ public class ForceResourcePack implements Listener { private ForceResourcePack() { Bukkit.getPluginManager().registerEvents(this, InvUI.getInstance().getPlugin()); - Bukkit.getOnlinePlayers().forEach(this::sendResourcePack); } public static ForceResourcePack getInstance() { @@ -51,12 +50,13 @@ public class ForceResourcePack implements Listener { /** * Sets the URL String for the custom ResourcePack every {@link Player} is required to download. - * Can be set to null to stop the forcing of a ResourcePack + * Can be set to null to stop forcing the Resource Pack. * * @param resourcePackUrl The ResourcePack URL String */ public void setResourcePackUrl(@Nullable String resourcePackUrl) { this.resourcePackUrl = resourcePackUrl; + if (resourcePackUrl != null) Bukkit.getOnlinePlayers().forEach(this::sendResourcePack); } public String getResourcePackUrl() { diff --git a/src/main/java/de/studiocode/invui/resourcepack/Icon.java b/InvUI/src/main/java/de/studiocode/invui/resourcepack/Icon.java similarity index 100% rename from src/main/java/de/studiocode/invui/resourcepack/Icon.java rename to InvUI/src/main/java/de/studiocode/invui/resourcepack/Icon.java diff --git a/src/main/java/de/studiocode/invui/util/ArrayUtils.java b/InvUI/src/main/java/de/studiocode/invui/util/ArrayUtils.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/ArrayUtils.java rename to InvUI/src/main/java/de/studiocode/invui/util/ArrayUtils.java diff --git a/src/main/java/de/studiocode/invui/util/InventoryUtils.java b/InvUI/src/main/java/de/studiocode/invui/util/InventoryUtils.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/InventoryUtils.java rename to InvUI/src/main/java/de/studiocode/invui/util/InventoryUtils.java diff --git a/src/main/java/de/studiocode/invui/util/MojangApiUtils.java b/InvUI/src/main/java/de/studiocode/invui/util/MojangApiUtils.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/MojangApiUtils.java rename to InvUI/src/main/java/de/studiocode/invui/util/MojangApiUtils.java diff --git a/src/main/java/de/studiocode/invui/util/Pair.java b/InvUI/src/main/java/de/studiocode/invui/util/Pair.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/Pair.java rename to InvUI/src/main/java/de/studiocode/invui/util/Pair.java diff --git a/src/main/java/de/studiocode/invui/util/Point2D.java b/InvUI/src/main/java/de/studiocode/invui/util/Point2D.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/Point2D.java rename to InvUI/src/main/java/de/studiocode/invui/util/Point2D.java diff --git a/src/main/java/de/studiocode/invui/util/SlotUtils.java b/InvUI/src/main/java/de/studiocode/invui/util/SlotUtils.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/SlotUtils.java rename to InvUI/src/main/java/de/studiocode/invui/util/SlotUtils.java diff --git a/src/main/java/de/studiocode/invui/util/WebUtils.java b/InvUI/src/main/java/de/studiocode/invui/util/WebUtils.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/WebUtils.java rename to InvUI/src/main/java/de/studiocode/invui/util/WebUtils.java diff --git a/src/main/java/de/studiocode/invui/util/reflection/ReflectionRegistry.java b/InvUI/src/main/java/de/studiocode/invui/util/reflection/ReflectionRegistry.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/reflection/ReflectionRegistry.java rename to InvUI/src/main/java/de/studiocode/invui/util/reflection/ReflectionRegistry.java diff --git a/src/main/java/de/studiocode/invui/util/reflection/ReflectionUtils.java b/InvUI/src/main/java/de/studiocode/invui/util/reflection/ReflectionUtils.java similarity index 100% rename from src/main/java/de/studiocode/invui/util/reflection/ReflectionUtils.java rename to InvUI/src/main/java/de/studiocode/invui/util/reflection/ReflectionUtils.java diff --git a/src/main/java/de/studiocode/invui/virtualinventory/VirtualInventory.java b/InvUI/src/main/java/de/studiocode/invui/virtualinventory/VirtualInventory.java similarity index 100% rename from src/main/java/de/studiocode/invui/virtualinventory/VirtualInventory.java rename to InvUI/src/main/java/de/studiocode/invui/virtualinventory/VirtualInventory.java diff --git a/src/main/java/de/studiocode/invui/virtualinventory/VirtualInventoryManager.java b/InvUI/src/main/java/de/studiocode/invui/virtualinventory/VirtualInventoryManager.java similarity index 100% rename from src/main/java/de/studiocode/invui/virtualinventory/VirtualInventoryManager.java rename to InvUI/src/main/java/de/studiocode/invui/virtualinventory/VirtualInventoryManager.java diff --git a/src/main/java/de/studiocode/invui/virtualinventory/event/ItemUpdateEvent.java b/InvUI/src/main/java/de/studiocode/invui/virtualinventory/event/ItemUpdateEvent.java similarity index 100% rename from src/main/java/de/studiocode/invui/virtualinventory/event/ItemUpdateEvent.java rename to InvUI/src/main/java/de/studiocode/invui/virtualinventory/event/ItemUpdateEvent.java diff --git a/src/main/java/de/studiocode/invui/virtualinventory/event/PlayerUpdateReason.java b/InvUI/src/main/java/de/studiocode/invui/virtualinventory/event/PlayerUpdateReason.java similarity index 100% rename from src/main/java/de/studiocode/invui/virtualinventory/event/PlayerUpdateReason.java rename to InvUI/src/main/java/de/studiocode/invui/virtualinventory/event/PlayerUpdateReason.java diff --git a/src/main/java/de/studiocode/invui/virtualinventory/event/UpdateReason.java b/InvUI/src/main/java/de/studiocode/invui/virtualinventory/event/UpdateReason.java similarity index 100% rename from src/main/java/de/studiocode/invui/virtualinventory/event/UpdateReason.java rename to InvUI/src/main/java/de/studiocode/invui/virtualinventory/event/UpdateReason.java diff --git a/src/main/java/de/studiocode/invui/window/Window.java b/InvUI/src/main/java/de/studiocode/invui/window/Window.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/Window.java rename to InvUI/src/main/java/de/studiocode/invui/window/Window.java diff --git a/src/main/java/de/studiocode/invui/window/WindowManager.java b/InvUI/src/main/java/de/studiocode/invui/window/WindowManager.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/WindowManager.java rename to InvUI/src/main/java/de/studiocode/invui/window/WindowManager.java diff --git a/src/main/java/de/studiocode/invui/window/impl/BaseWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/BaseWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/BaseWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/BaseWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/merged/MergedWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/merged/MergedWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/merged/MergedWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/merged/MergedWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/merged/combined/CombinedWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/merged/combined/CombinedWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/merged/combined/CombinedWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/merged/combined/CombinedWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/merged/combined/SimpleCombinedWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/merged/combined/SimpleCombinedWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/merged/combined/SimpleCombinedWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/merged/combined/SimpleCombinedWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/merged/split/AnvilSplitWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/merged/split/AnvilSplitWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/merged/split/AnvilSplitWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/merged/split/AnvilSplitWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/merged/split/SimpleSplitWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/merged/split/SimpleSplitWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/merged/split/SimpleSplitWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/merged/split/SimpleSplitWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/merged/split/SplitWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/merged/split/SplitWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/merged/split/SplitWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/merged/split/SplitWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/single/AnvilWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/single/AnvilWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/single/AnvilWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/single/AnvilWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/single/SimpleWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/single/SimpleWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/single/SimpleWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/single/SimpleWindow.java diff --git a/src/main/java/de/studiocode/invui/window/impl/single/SingleWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/single/SingleWindow.java similarity index 100% rename from src/main/java/de/studiocode/invui/window/impl/single/SingleWindow.java rename to InvUI/src/main/java/de/studiocode/invui/window/impl/single/SingleWindow.java diff --git a/InventoryAccess/1_14_R1/pom.xml b/InventoryAccess/1_14_R1/pom.xml new file mode 100644 index 0000000..ef11e60 --- /dev/null +++ b/InventoryAccess/1_14_R1/pom.xml @@ -0,0 +1,34 @@ + + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + 1_14_R1 + + + 8 + 8 + + + + + org.spigotmc + spigot + 1.14.4-R0.1-SNAPSHOT + provided + + + de.studiocode.invui + api + ${project.parent.version} + + + + \ No newline at end of file diff --git a/InventoryAccess/1_14_R1/src/main/java/de/studiocode/inventoryaccess/v1_14_R1/inventory/AnvilInventoryImpl.java b/InventoryAccess/1_14_R1/src/main/java/de/studiocode/inventoryaccess/v1_14_R1/inventory/AnvilInventoryImpl.java new file mode 100644 index 0000000..695141a --- /dev/null +++ b/InventoryAccess/1_14_R1/src/main/java/de/studiocode/inventoryaccess/v1_14_R1/inventory/AnvilInventoryImpl.java @@ -0,0 +1,172 @@ +package de.studiocode.inventoryaccess.v1_14_R1.inventory; + +import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.v1_14_R1.*; +import org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftInventoryAnvil; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Field; +import java.util.function.Consumer; + +public class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { + + private static final Field CONTAINER_ACCESS_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "containerAccess"); + private static final Field REPAIR_INVENTORY_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "repairInventory"); + private static final Field RESULT_INVENTORY_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "resultInventory"); + + private final String title; + private final Consumer renameHandler; + private final CraftInventoryView view; + private final EntityPlayer player; + + private final IInventory repairInventory; + private final IInventory resultInventory; + + private String text; + private boolean open; + + public AnvilInventoryImpl(Player player, String title, Consumer renameHandler) { + this(((CraftPlayer) player).getHandle(), title, renameHandler); + } + + public AnvilInventoryImpl(EntityPlayer player, String title, Consumer renameHandler) { + super(player.nextContainerCounter(), player.inventory, + ContainerAccess.at(player.getWorld(), new BlockPosition(Integer.MAX_VALUE, 0, 0))); + + this.title = title; + this.renameHandler = renameHandler; + this.player = player; + + repairInventory = ReflectionUtils.getValueOfField(REPAIR_INVENTORY_FIELD, this); + resultInventory = ReflectionUtils.getValueOfField(RESULT_INVENTORY_FIELD, this); + ContainerAccess containerAccess = ReflectionUtils.getValueOfField(CONTAINER_ACCESS_FIELD, this); + + CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), + repairInventory, resultInventory, this); + this.view = new CraftInventoryView(player.getBukkitEntity(), inventory, this); + } + + public void open() { + open = true; + + // call the InventoryOpenEvent + CraftEventFactory.callInventoryOpenEvent(player, this); + + // set active container + player.activeContainer = this; + + // send open packet + player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, Containers.ANVIL, new ChatMessage(title))); + + // send initial items + NonNullList itemsList = NonNullList.a(ItemStack.a, getItem(0), getItem(1), getItem(2)); + player.playerConnection.sendPacket(new PacketPlayOutWindowItems(getActiveWindowId(player), itemsList)); + } + + public void sendItem(int slot) { + player.playerConnection.sendPacket(new PacketPlayOutSetSlot(getActiveWindowId(player), slot, getItem(slot))); + } + + public void setItem(int slot, ItemStack item) { + if (slot < 2) repairInventory.setItem(slot, item); + else resultInventory.setItem(0, item); + + if (open) sendItem(slot); + } + + private ItemStack getItem(int slot) { + if (slot < 2) return repairInventory.getItem(slot); + else return resultInventory.getItem(0); + } + + private int getActiveWindowId(EntityPlayer player) { + Container container = player.activeContainer; + return container == null ? -1 : container.windowId; + } + + @Override + public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { + setItem(slot, CraftItemStack.asNMSCopy(itemStack)); + } + + @Override + public Inventory getBukkitInventory() { + return view.getTopInventory(); + } + + @Override + public String getRenameText() { + return text; + } + + @Override + public boolean isOpen() { + return open; + } + + // --- ContainerAnvil --- + + @Override + public CraftInventoryView getBukkitView() { + return view; + } + + /** + * Called every tick to see if the {@link EntityHuman} can still use that container. + * (Used to for checking the distance between the {@link EntityHuman} and the container + * and closing the window when the distance gets too big.) + * + * @param entityhuman The {@link EntityHuman} + * @return If the {@link EntityHuman} can still use that container + */ + @Override + public boolean canUse(EntityHuman entityhuman) { + return true; + } + + /** + * Called when the rename text gets changed. + * + * @param s The new rename text + */ + @Override + public void a(String s) { + // save rename text + text = s; + + // call the rename handler + if (renameHandler != null) renameHandler.accept(s); + + // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again + sendItem(2); + } + + /** + * Called when the container is closed to give the items back. + * + * @param entityhuman The {@link EntityHuman} that closed this container + */ + @Override + public void b(EntityHuman entityhuman) { + open = false; + // don't give them the items, they don't own them + } + + /** + * Called when both items in the {@link AnvilInventoryImpl#repairInventory} were set to create + * the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. + */ + @Override + public void e() { + // no + } + + +} diff --git a/InventoryAccess/1_14_R1/src/main/java/de/studiocode/inventoryaccess/v1_14_R1/util/InventoryUtilsImpl.java b/InventoryAccess/1_14_R1/src/main/java/de/studiocode/inventoryaccess/v1_14_R1/util/InventoryUtilsImpl.java new file mode 100644 index 0000000..927c478 --- /dev/null +++ b/InventoryAccess/1_14_R1/src/main/java/de/studiocode/inventoryaccess/v1_14_R1/util/InventoryUtilsImpl.java @@ -0,0 +1,27 @@ +package de.studiocode.inventoryaccess.v1_14_R1.util; + +import de.studiocode.inventoryaccess.api.abstraction.util.InventoryUtils; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.v1_14_R1.Containers; +import net.minecraft.server.v1_14_R1.EntityPlayer; +import org.bukkit.craftbukkit.v1_14_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftContainer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Method; + +public class InventoryUtilsImpl implements InventoryUtils { + + private static final Method OPEN_CUSTOM_INVENTORY_METHOD = ReflectionUtils.getMethod(CraftHumanEntity.class, + true, "openCustomInventory", Inventory.class, EntityPlayer.class, Containers.class); + + @Override + public void openCustomInventory(Player player, Inventory inventory) { + EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle(); + Containers windowType = CraftContainer.getNotchInventoryType(inventory.getType()); + ReflectionUtils.invokeMethod(OPEN_CUSTOM_INVENTORY_METHOD, player, inventory, entityPlayer, windowType); + } + +} diff --git a/InventoryAccess/1_15_R1/pom.xml b/InventoryAccess/1_15_R1/pom.xml new file mode 100644 index 0000000..021634f --- /dev/null +++ b/InventoryAccess/1_15_R1/pom.xml @@ -0,0 +1,34 @@ + + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + 1_15_R1 + + + 8 + 8 + + + + + org.spigotmc + spigot + 1.15.2-R0.1-SNAPSHOT + provided + + + de.studiocode.invui + api + ${project.parent.version} + + + + \ No newline at end of file diff --git a/InventoryAccess/1_15_R1/src/main/java/de/studiocode/inventoryaccess/v1_15_R1/inventory/AnvilInventoryImpl.java b/InventoryAccess/1_15_R1/src/main/java/de/studiocode/inventoryaccess/v1_15_R1/inventory/AnvilInventoryImpl.java new file mode 100644 index 0000000..7fe2dc2 --- /dev/null +++ b/InventoryAccess/1_15_R1/src/main/java/de/studiocode/inventoryaccess/v1_15_R1/inventory/AnvilInventoryImpl.java @@ -0,0 +1,172 @@ +package de.studiocode.inventoryaccess.v1_15_R1.inventory; + +import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.v1_15_R1.*; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftInventoryAnvil; +import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Field; +import java.util.function.Consumer; + +public class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { + + private static final Field CONTAINER_ACCESS_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "containerAccess"); + private static final Field REPAIR_INVENTORY_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "repairInventory"); + private static final Field RESULT_INVENTORY_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "resultInventory"); + + private final String title; + private final Consumer renameHandler; + private final CraftInventoryView view; + private final EntityPlayer player; + + private final IInventory repairInventory; + private final IInventory resultInventory; + + private String text; + private boolean open; + + public AnvilInventoryImpl(Player player, String title, Consumer renameHandler) { + this(((CraftPlayer) player).getHandle(), title, renameHandler); + } + + public AnvilInventoryImpl(EntityPlayer player, String title, Consumer renameHandler) { + super(player.nextContainerCounter(), player.inventory, + ContainerAccess.at(player.getWorld(), new BlockPosition(Integer.MAX_VALUE, 0, 0))); + + this.title = title; + this.renameHandler = renameHandler; + this.player = player; + + repairInventory = ReflectionUtils.getValueOfField(REPAIR_INVENTORY_FIELD, this); + resultInventory = ReflectionUtils.getValueOfField(RESULT_INVENTORY_FIELD, this); + ContainerAccess containerAccess = ReflectionUtils.getValueOfField(CONTAINER_ACCESS_FIELD, this); + + CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), + repairInventory, resultInventory, this); + this.view = new CraftInventoryView(player.getBukkitEntity(), inventory, this); + } + + public void open() { + open = true; + + // call the InventoryOpenEvent + CraftEventFactory.callInventoryOpenEvent(player, this); + + // set active container + player.activeContainer = this; + + // send open packet + player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, Containers.ANVIL, new ChatMessage(title))); + + // send initial items + NonNullList itemsList = NonNullList.a(ItemStack.a, getItem(0), getItem(1), getItem(2)); + player.playerConnection.sendPacket(new PacketPlayOutWindowItems(getActiveWindowId(player), itemsList)); + } + + public void sendItem(int slot) { + player.playerConnection.sendPacket(new PacketPlayOutSetSlot(getActiveWindowId(player), slot, getItem(slot))); + } + + public void setItem(int slot, ItemStack item) { + if (slot < 2) repairInventory.setItem(slot, item); + else resultInventory.setItem(0, item); + + if (open) sendItem(slot); + } + + private ItemStack getItem(int slot) { + if (slot < 2) return repairInventory.getItem(slot); + else return resultInventory.getItem(0); + } + + private int getActiveWindowId(EntityPlayer player) { + Container container = player.activeContainer; + return container == null ? -1 : container.windowId; + } + + @Override + public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { + setItem(slot, CraftItemStack.asNMSCopy(itemStack)); + } + + @Override + public Inventory getBukkitInventory() { + return view.getTopInventory(); + } + + @Override + public String getRenameText() { + return text; + } + + @Override + public boolean isOpen() { + return open; + } + + // --- ContainerAnvil --- + + @Override + public CraftInventoryView getBukkitView() { + return view; + } + + /** + * Called every tick to see if the {@link EntityHuman} can still use that container. + * (Used to for checking the distance between the {@link EntityHuman} and the container + * and closing the window when the distance gets too big.) + * + * @param entityhuman The {@link EntityHuman} + * @return If the {@link EntityHuman} can still use that container + */ + @Override + public boolean canUse(EntityHuman entityhuman) { + return true; + } + + /** + * Called when the rename text gets changed. + * + * @param s The new rename text + */ + @Override + public void a(String s) { + // save rename text + text = s; + + // call the rename handler + if (renameHandler != null) renameHandler.accept(s); + + // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again + sendItem(2); + } + + /** + * Called when the container is closed to give the items back. + * + * @param entityhuman The {@link EntityHuman} that closed this container + */ + @Override + public void b(EntityHuman entityhuman) { + open = false; + // don't give them the items, they don't own them + } + + /** + * Called when both items in the {@link de.studiocode.inventoryaccess.v1_14_R1.inventory.AnvilInventoryImpl#repairInventory} were set to create + * the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. + */ + @Override + public void e() { + // no + } + + +} diff --git a/InventoryAccess/1_15_R1/src/main/java/de/studiocode/inventoryaccess/v1_15_R1/util/InventoryUtilsImpl.java b/InventoryAccess/1_15_R1/src/main/java/de/studiocode/inventoryaccess/v1_15_R1/util/InventoryUtilsImpl.java new file mode 100644 index 0000000..0aa658f --- /dev/null +++ b/InventoryAccess/1_15_R1/src/main/java/de/studiocode/inventoryaccess/v1_15_R1/util/InventoryUtilsImpl.java @@ -0,0 +1,27 @@ +package de.studiocode.inventoryaccess.v1_15_R1.util; + +import de.studiocode.inventoryaccess.api.abstraction.util.InventoryUtils; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.v1_15_R1.Containers; +import net.minecraft.server.v1_15_R1.EntityPlayer; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftContainer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Method; + +public class InventoryUtilsImpl implements InventoryUtils { + + private static final Method OPEN_CUSTOM_INVENTORY_METHOD = ReflectionUtils.getMethod(CraftHumanEntity.class, + true, "openCustomInventory", Inventory.class, EntityPlayer.class, Containers.class); + + @Override + public void openCustomInventory(Player player, Inventory inventory) { + EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle(); + Containers windowType = CraftContainer.getNotchInventoryType(inventory); + ReflectionUtils.invokeMethod(OPEN_CUSTOM_INVENTORY_METHOD, player, inventory, entityPlayer, windowType); + } + +} diff --git a/InventoryAccess/1_16_R1/pom.xml b/InventoryAccess/1_16_R1/pom.xml new file mode 100644 index 0000000..b894d7f --- /dev/null +++ b/InventoryAccess/1_16_R1/pom.xml @@ -0,0 +1,34 @@ + + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + 1_16_R1 + + + 8 + 8 + + + + + org.spigotmc + spigot + 1.16.1-R0.1-SNAPSHOT + provided + + + de.studiocode.invui + api + ${project.parent.version} + + + + \ No newline at end of file diff --git a/InventoryAccess/1_16_R1/src/main/java/de/studiocode/inventoryaccess/v1_16_R1/inventory/AnvilInventoryImpl.java b/InventoryAccess/1_16_R1/src/main/java/de/studiocode/inventoryaccess/v1_16_R1/inventory/AnvilInventoryImpl.java new file mode 100644 index 0000000..33a5eb4 --- /dev/null +++ b/InventoryAccess/1_16_R1/src/main/java/de/studiocode/inventoryaccess/v1_16_R1/inventory/AnvilInventoryImpl.java @@ -0,0 +1,158 @@ +package de.studiocode.inventoryaccess.v1_16_R1.inventory; + +import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; +import net.minecraft.server.v1_16_R1.*; +import org.bukkit.craftbukkit.v1_16_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventoryAnvil; +import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.util.function.Consumer; + +public class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { + + private final String title; + private final Consumer renameHandler; + private final CraftInventoryView view; + private final EntityPlayer player; + + private String text; + private boolean open; + + public AnvilInventoryImpl(Player player, String title, Consumer renameHandler) { + this(((CraftPlayer) player).getHandle(), title, renameHandler); + } + + public AnvilInventoryImpl(EntityPlayer player, String title, Consumer renameHandler) { + super(player.nextContainerCounter(), player.inventory, + ContainerAccess.at(player.getWorld(), new BlockPosition(Integer.MAX_VALUE, 0, 0))); + + this.title = title; + this.renameHandler = renameHandler; + this.player = player; + + CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), + repairInventory, resultInventory, this); + this.view = new CraftInventoryView(player.getBukkitEntity(), inventory, this); + } + + public void open() { + open = true; + + // call the InventoryOpenEvent + CraftEventFactory.callInventoryOpenEvent(player, this); + + // set active container + player.activeContainer = this; + + // send open packet + player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, Containers.ANVIL, new ChatMessage(title))); + + // send initial items + NonNullList itemsList = NonNullList.a(ItemStack.b, getItem(0), getItem(1), getItem(2)); + player.playerConnection.sendPacket(new PacketPlayOutWindowItems(getActiveWindowId(player), itemsList)); + } + + public void sendItem(int slot) { + player.playerConnection.sendPacket(new PacketPlayOutSetSlot(getActiveWindowId(player), slot, getItem(slot))); + } + + public void setItem(int slot, ItemStack item) { + if (slot < 2) repairInventory.setItem(slot, item); + else resultInventory.setItem(0, item); + + if (open) sendItem(slot); + } + + private ItemStack getItem(int slot) { + if (slot < 2) return repairInventory.getItem(slot); + else return resultInventory.getItem(0); + } + + private int getActiveWindowId(EntityPlayer player) { + Container container = player.activeContainer; + return container == null ? -1 : container.windowId; + } + + @Override + public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { + setItem(slot, CraftItemStack.asNMSCopy(itemStack)); + } + + @Override + public Inventory getBukkitInventory() { + return view.getTopInventory(); + } + + @Override + public String getRenameText() { + return text; + } + + @Override + public boolean isOpen() { + return open; + } + + // --- ContainerAnvil --- + + @Override + public CraftInventoryView getBukkitView() { + return view; + } + + /** + * Called every tick to see if the {@link EntityHuman} can still use that container. + * (Used to for checking the distance between the {@link EntityHuman} and the container + * and closing the window when the distance gets too big.) + * + * @param entityhuman The {@link EntityHuman} + * @return If the {@link EntityHuman} can still use that container + */ + @Override + public boolean canUse(EntityHuman entityhuman) { + return true; + } + + /** + * Called when the rename text gets changed. + * + * @param s The new rename text + */ + @Override + public void a(String s) { + // save rename text + text = s; + + // call the rename handler + if (renameHandler != null) renameHandler.accept(s); + + // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again + sendItem(2); + } + + /** + * Called when the container is closed to give the items back. + * + * @param entityhuman The {@link EntityHuman} that closed this container + */ + @Override + public void b(EntityHuman entityhuman) { + open = false; + // don't give them the items, they don't own them + } + + /** + * Called when both items in the {@link ContainerAnvil#repairInventory} were set to create + * the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. + */ + @Override + public void e() { + // no + } + + +} diff --git a/InventoryAccess/1_16_R1/src/main/java/de/studiocode/inventoryaccess/v1_16_R1/util/InventoryUtilsImpl.java b/InventoryAccess/1_16_R1/src/main/java/de/studiocode/inventoryaccess/v1_16_R1/util/InventoryUtilsImpl.java new file mode 100644 index 0000000..8c9ab73 --- /dev/null +++ b/InventoryAccess/1_16_R1/src/main/java/de/studiocode/inventoryaccess/v1_16_R1/util/InventoryUtilsImpl.java @@ -0,0 +1,27 @@ +package de.studiocode.inventoryaccess.v1_16_R1.util; + +import de.studiocode.inventoryaccess.api.abstraction.util.InventoryUtils; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.v1_16_R1.Containers; +import net.minecraft.server.v1_16_R1.EntityPlayer; +import org.bukkit.craftbukkit.v1_16_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_16_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftContainer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Method; + +public class InventoryUtilsImpl implements InventoryUtils { + + private static final Method OPEN_CUSTOM_INVENTORY_METHOD = ReflectionUtils.getMethod(CraftHumanEntity.class, + true, "openCustomInventory", Inventory.class, EntityPlayer.class, Containers.class); + + @Override + public void openCustomInventory(Player player, Inventory inventory) { + EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle(); + Containers windowType = CraftContainer.getNotchInventoryType(inventory); + ReflectionUtils.invokeMethod(OPEN_CUSTOM_INVENTORY_METHOD, player, inventory, entityPlayer, windowType); + } + +} diff --git a/InventoryAccess/1_16_R2/pom.xml b/InventoryAccess/1_16_R2/pom.xml new file mode 100644 index 0000000..7505d5e --- /dev/null +++ b/InventoryAccess/1_16_R2/pom.xml @@ -0,0 +1,34 @@ + + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + 1_16_R2 + + + 8 + 8 + + + + + org.spigotmc + spigot + 1.16.2-R0.1-SNAPSHOT + provided + + + de.studiocode.invui + api + ${project.parent.version} + + + + \ No newline at end of file diff --git a/InventoryAccess/1_16_R2/src/main/java/de/studiocode/inventoryaccess/v1_16_R2/inventory/AnvilInventoryImpl.java b/InventoryAccess/1_16_R2/src/main/java/de/studiocode/inventoryaccess/v1_16_R2/inventory/AnvilInventoryImpl.java new file mode 100644 index 0000000..3ba46a6 --- /dev/null +++ b/InventoryAccess/1_16_R2/src/main/java/de/studiocode/inventoryaccess/v1_16_R2/inventory/AnvilInventoryImpl.java @@ -0,0 +1,159 @@ +package de.studiocode.inventoryaccess.v1_16_R2.inventory; + +import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; +import net.minecraft.server.v1_16_R2.*; +import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R2.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftInventoryAnvil; +import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.inventory.Inventory; + +import java.util.function.Consumer; + +public class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { + + private final String title; + private final Consumer renameHandler; + private final CraftInventoryView view; + private final EntityPlayer player; + + private String text; + private boolean open; + + public AnvilInventoryImpl(Player player, String title, Consumer renameHandler) { + this(((CraftPlayer) player).getHandle(), title, renameHandler); + } + + public AnvilInventoryImpl(EntityPlayer player, String title, Consumer renameHandler) { + super(player.nextContainerCounter(), player.inventory, + ContainerAccess.at(player.getWorld(), new BlockPosition(Integer.MAX_VALUE, 0, 0))); + + this.title = title; + this.renameHandler = renameHandler; + this.player = player; + + CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), + repairInventory, resultInventory, this); + this.view = new CraftInventoryView(player.getBukkitEntity(), inventory, this); + } + + public void open() { + open = true; + + // call the InventoryOpenEvent + CraftEventFactory.callInventoryOpenEvent(player, this); + + // set active container + player.activeContainer = this; + + // send open packet + player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, Containers.ANVIL, new ChatMessage(title))); + + // send initial items + NonNullList itemsList = NonNullList.a(ItemStack.b, getItem(0), getItem(1), getItem(2)); + player.playerConnection.sendPacket(new PacketPlayOutWindowItems(getActiveWindowId(player), itemsList)); + } + + public void sendItem(int slot) { + player.playerConnection.sendPacket(new PacketPlayOutSetSlot(getActiveWindowId(player), slot, getItem(slot))); + } + + public void setItem(int slot, ItemStack item) { + if (slot < 2) repairInventory.setItem(slot, item); + else resultInventory.setItem(0, item); + + if (open) sendItem(slot); + } + + private ItemStack getItem(int slot) { + if (slot < 2) return repairInventory.getItem(slot); + else return resultInventory.getItem(0); + } + + private int getActiveWindowId(EntityPlayer player) { + Container container = player.activeContainer; + return container == null ? -1 : container.windowId; + } + + @Override + public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { + setItem(slot, CraftItemStack.asNMSCopy(itemStack)); + } + + @Override + public Inventory getBukkitInventory() { + return view.getTopInventory(); + } + + @Override + public String getRenameText() { + return text; + } + + @Override + public boolean isOpen() { + return open; + } + + // --- ContainerAnvil --- + + @Override + public CraftInventoryView getBukkitView() { + return view; + } + + /** + * Called every tick to see if the {@link EntityHuman} can still use that container. + * (Used to for checking the distance between the {@link EntityHuman} and the container + * and closing the window when the distance gets too big.) + * + * @param entityhuman The {@link EntityHuman} + * @return If the {@link EntityHuman} can still use that container + */ + @Override + public boolean canUse(EntityHuman entityhuman) { + return true; + } + + /** + * Called when the rename text gets changed. + * + * @param s The new rename text + */ + @Override + public void a(String s) { + // save rename text + text = s; + + // call the rename handler + if (renameHandler != null) renameHandler.accept(s); + + // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again + sendItem(2); + } + + /** + * Called when the container is closed to give the items back. + * + * @param entityhuman The {@link EntityHuman} that closed this container + */ + @Override + public void b(EntityHuman entityhuman) { + open = false; + // don't give them the items, they don't own them + } + + /** + * Called when both items in the {@link ContainerAnvil#repairInventory} were set to create + * the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. + */ + @Override + public void e() { + // no + } + + +} diff --git a/InventoryAccess/1_16_R2/src/main/java/de/studiocode/inventoryaccess/v1_16_R2/util/InventoryUtilsImpl.java b/InventoryAccess/1_16_R2/src/main/java/de/studiocode/inventoryaccess/v1_16_R2/util/InventoryUtilsImpl.java new file mode 100644 index 0000000..98ce1a9 --- /dev/null +++ b/InventoryAccess/1_16_R2/src/main/java/de/studiocode/inventoryaccess/v1_16_R2/util/InventoryUtilsImpl.java @@ -0,0 +1,27 @@ +package de.studiocode.inventoryaccess.v1_16_R2.util; + +import de.studiocode.inventoryaccess.api.abstraction.util.InventoryUtils; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.v1_16_R2.Containers; +import net.minecraft.server.v1_16_R2.EntityPlayer; +import org.bukkit.craftbukkit.v1_16_R2.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftContainer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Method; + +public class InventoryUtilsImpl implements InventoryUtils { + + private static final Method OPEN_CUSTOM_INVENTORY_METHOD = ReflectionUtils.getMethod(CraftHumanEntity.class, + true, "openCustomInventory", Inventory.class, EntityPlayer.class, Containers.class); + + @Override + public void openCustomInventory(Player player, Inventory inventory) { + EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle(); + Containers windowType = CraftContainer.getNotchInventoryType(inventory); + ReflectionUtils.invokeMethod(OPEN_CUSTOM_INVENTORY_METHOD, player, inventory, entityPlayer, windowType); + } + +} diff --git a/InventoryAccess/1_16_R3/pom.xml b/InventoryAccess/1_16_R3/pom.xml new file mode 100644 index 0000000..e700327 --- /dev/null +++ b/InventoryAccess/1_16_R3/pom.xml @@ -0,0 +1,34 @@ + + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + 1_16_R3 + + + 8 + 8 + + + + + org.spigotmc + spigot + 1.16.4-R0.1-SNAPSHOT + provided + + + de.studiocode.invui + api + ${project.parent.version} + + + + \ No newline at end of file diff --git a/InventoryAccess/1_16_R3/src/main/java/de/studiocode/inventoryaccess/v1_16_R3/inventory/AnvilInventoryImpl.java b/InventoryAccess/1_16_R3/src/main/java/de/studiocode/inventoryaccess/v1_16_R3/inventory/AnvilInventoryImpl.java new file mode 100644 index 0000000..7191ff3 --- /dev/null +++ b/InventoryAccess/1_16_R3/src/main/java/de/studiocode/inventoryaccess/v1_16_R3/inventory/AnvilInventoryImpl.java @@ -0,0 +1,159 @@ +package de.studiocode.inventoryaccess.v1_16_R3.inventory; + +import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; +import net.minecraft.server.v1_16_R3.*; +import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryAnvil; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.inventory.Inventory; + +import java.util.function.Consumer; + +public class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { + + private final String title; + private final Consumer renameHandler; + private final CraftInventoryView view; + private final EntityPlayer player; + + private String text; + private boolean open; + + public AnvilInventoryImpl(Player player, String title, Consumer renameHandler) { + this(((CraftPlayer) player).getHandle(), title, renameHandler); + } + + public AnvilInventoryImpl(EntityPlayer player, String title, Consumer renameHandler) { + super(player.nextContainerCounter(), player.inventory, + ContainerAccess.at(player.getWorld(), new BlockPosition(Integer.MAX_VALUE, 0, 0))); + + this.title = title; + this.renameHandler = renameHandler; + this.player = player; + + CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), + repairInventory, resultInventory, this); + this.view = new CraftInventoryView(player.getBukkitEntity(), inventory, this); + } + + public void open() { + open = true; + + // call the InventoryOpenEvent + CraftEventFactory.callInventoryOpenEvent(player, this); + + // set active container + player.activeContainer = this; + + // send open packet + player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, Containers.ANVIL, new ChatMessage(title))); + + // send initial items + NonNullList itemsList = NonNullList.a(ItemStack.b, getItem(0), getItem(1), getItem(2)); + player.playerConnection.sendPacket(new PacketPlayOutWindowItems(getActiveWindowId(player), itemsList)); + } + + public void sendItem(int slot) { + player.playerConnection.sendPacket(new PacketPlayOutSetSlot(getActiveWindowId(player), slot, getItem(slot))); + } + + public void setItem(int slot, ItemStack item) { + if (slot < 2) repairInventory.setItem(slot, item); + else resultInventory.setItem(0, item); + + if (open) sendItem(slot); + } + + private ItemStack getItem(int slot) { + if (slot < 2) return repairInventory.getItem(slot); + else return resultInventory.getItem(0); + } + + private int getActiveWindowId(EntityPlayer player) { + Container container = player.activeContainer; + return container == null ? -1 : container.windowId; + } + + @Override + public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { + setItem(slot, CraftItemStack.asNMSCopy(itemStack)); + } + + @Override + public Inventory getBukkitInventory() { + return view.getTopInventory(); + } + + @Override + public String getRenameText() { + return text; + } + + @Override + public boolean isOpen() { + return open; + } + + // --- ContainerAnvil --- + + @Override + public CraftInventoryView getBukkitView() { + return view; + } + + /** + * Called every tick to see if the {@link EntityHuman} can still use that container. + * (Used to for checking the distance between the {@link EntityHuman} and the container + * and closing the window when the distance gets too big.) + * + * @param entityhuman The {@link EntityHuman} + * @return If the {@link EntityHuman} can still use that container + */ + @Override + public boolean canUse(EntityHuman entityhuman) { + return true; + } + + /** + * Called when the rename text gets changed. + * + * @param s The new rename text + */ + @Override + public void a(String s) { + // save rename text + text = s; + + // call the rename handler + if (renameHandler != null) renameHandler.accept(s); + + // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again + sendItem(2); + } + + /** + * Called when the container is closed to give the items back. + * + * @param entityhuman The {@link EntityHuman} that closed this container + */ + @Override + public void b(EntityHuman entityhuman) { + open = false; + // don't give them the items, they don't own them + } + + /** + * Called when both items in the {@link ContainerAnvil#repairInventory} were set to create + * the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. + */ + @Override + public void e() { + // no + } + + +} diff --git a/InventoryAccess/1_16_R3/src/main/java/de/studiocode/inventoryaccess/v1_16_R3/util/InventoryUtilsImpl.java b/InventoryAccess/1_16_R3/src/main/java/de/studiocode/inventoryaccess/v1_16_R3/util/InventoryUtilsImpl.java new file mode 100644 index 0000000..793fc6f --- /dev/null +++ b/InventoryAccess/1_16_R3/src/main/java/de/studiocode/inventoryaccess/v1_16_R3/util/InventoryUtilsImpl.java @@ -0,0 +1,27 @@ +package de.studiocode.inventoryaccess.v1_16_R3.util; + +import de.studiocode.inventoryaccess.api.abstraction.util.InventoryUtils; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.v1_16_R3.Containers; +import net.minecraft.server.v1_16_R3.EntityPlayer; +import org.bukkit.craftbukkit.v1_16_R3.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftContainer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Method; + +public class InventoryUtilsImpl implements InventoryUtils { + + private static final Method OPEN_CUSTOM_INVENTORY_METHOD = ReflectionUtils.getMethod(CraftHumanEntity.class, + true, "openCustomInventory", Inventory.class, EntityPlayer.class, Containers.class); + + @Override + public void openCustomInventory(Player player, Inventory inventory) { + EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle(); + Containers windowType = CraftContainer.getNotchInventoryType(inventory); + ReflectionUtils.invokeMethod(OPEN_CUSTOM_INVENTORY_METHOD, player, inventory, entityPlayer, windowType); + } + +} diff --git a/InventoryAccess/1_17_R1/pom.xml b/InventoryAccess/1_17_R1/pom.xml new file mode 100644 index 0000000..4b6f6dc --- /dev/null +++ b/InventoryAccess/1_17_R1/pom.xml @@ -0,0 +1,80 @@ + + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + 1_17_R1 + + + 16 + 16 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + org.spigotmc + spigot + 1.17-R0.1-SNAPSHOT + remapped-mojang + provided + + + de.studiocode.invui + api + ${project.parent.version} + + + + + + + net.md-5 + specialsource-maven-plugin + 1.2.2 + + + package + + remap + + remap-obf + + org.spigotmc:minecraft-server:1.17-R0.1-SNAPSHOT:txt:maps-mojang + true + org.spigotmc:spigot:1.17-R0.1-SNAPSHOT:jar:remapped-mojang + true + remapped-obf + + + + package + + remap + + remap-spigot + + ${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar + org.spigotmc:minecraft-server:1.17-R0.1-SNAPSHOT:csrg:maps-spigot + org.spigotmc:spigot:1.17-R0.1-SNAPSHOT:jar:remapped-obf + + + + + + + + \ No newline at end of file diff --git a/InventoryAccess/1_17_R1/src/main/java/de/studiocode/inventoryaccess/v1_17_R1/inventory/AnvilInventoryImpl.java b/InventoryAccess/1_17_R1/src/main/java/de/studiocode/inventoryaccess/v1_17_R1/inventory/AnvilInventoryImpl.java new file mode 100644 index 0000000..aeb1800 --- /dev/null +++ b/InventoryAccess/1_17_R1/src/main/java/de/studiocode/inventoryaccess/v1_17_R1/inventory/AnvilInventoryImpl.java @@ -0,0 +1,186 @@ +package de.studiocode.inventoryaccess.v1_17_R1.inventory; + +import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; +import net.minecraft.core.BlockPos; +import net.minecraft.core.NonNullList; +import net.minecraft.network.chat.TextComponent; +import net.minecraft.network.protocol.game.ClientboundContainerSetContentPacket; +import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket; +import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.Container; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.AnvilMenu; +import net.minecraft.world.inventory.ContainerLevelAccess; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.item.ItemStack; +import org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftInventoryAnvil; +import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; +import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.inventory.Inventory; + +import java.util.function.Consumer; + +public class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { + + private final String title; + private final Consumer renameHandler; + private final CraftInventoryView view; + private final ServerPlayer player; + + private String text; + private boolean open; + + public AnvilInventoryImpl(org.bukkit.entity.Player player, String title, Consumer renameHandler) { + this(((CraftPlayer) player).getHandle(), title, renameHandler); + } + + public AnvilInventoryImpl(ServerPlayer player, String title, Consumer renameHandler) { + super(player.nextContainerCounter(), player.getInventory(), + ContainerLevelAccess.create(player.level, new BlockPos(Integer.MAX_VALUE, 0, 0))); + + this.title = title; + this.renameHandler = renameHandler; + this.player = player; + + CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), + inputSlots, resultSlots, this); + this.view = new CraftInventoryView(player.getBukkitEntity(), inventory, this); + } + + public void open() { + open = true; + + // call the InventoryOpenEvent + CraftEventFactory.callInventoryOpenEvent(player, this); + + // set active container + player.containerMenu = this; + + // send open packet + player.connection.send(new ClientboundOpenScreenPacket(containerId, MenuType.ANVIL, new TextComponent(title))); + + // send initial items + NonNullList itemsList = NonNullList.of(ItemStack.EMPTY, getItem(0), getItem(1), getItem(2)); + player.connection.send(new ClientboundContainerSetContentPacket(getActiveWindowId(player), itemsList)); + + // init menu + player.initMenu(this); + } + + public void sendItem(int slot) { + player.connection.send(new ClientboundContainerSetSlotPacket(getActiveWindowId(player), slot, getItem(slot))); + } + + public void setItem(int slot, ItemStack item) { + if (slot < 2) inputSlots.setItem(slot, item); + else resultSlots.setItem(0, item); + + if (open) sendItem(slot); + } + + private ItemStack getItem(int slot) { + if (slot < 2) return inputSlots.getItem(slot); + else return resultSlots.getItem(0); + } + + private int getActiveWindowId(ServerPlayer player) { + AbstractContainerMenu container = player.containerMenu; + return container == null ? -1 : container.containerId; + } + + @Override + public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { + setItem(slot, CraftItemStack.asNMSCopy(itemStack)); + } + + @Override + public Inventory getBukkitInventory() { + return view.getTopInventory(); + } + + @Override + public String getRenameText() { + return text; + } + + @Override + public boolean isOpen() { + return open; + } + + // --- ContainerAnvil --- + + @Override + public CraftInventoryView getBukkitView() { + return view; + } + + /** + * Called every tick to see if the {@link Player} can still use that container. + * (Used to for checking the distance between the {@link Player} and the container + * and closing the window when the distance gets too big.) + * + * @param player The {@link Player} + * @return If the {@link Player} can still use that container + */ + @Override + public boolean stillValid(Player player) { + return true; + } + + /** + * Called when the rename text gets changed. + * + * @param s The new rename text + */ + @Override + public void setItemName(String s) { + // save rename text + text = s; + + // call the rename handler + if (renameHandler != null) renameHandler.accept(s); + + // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again + sendItem(2); + } + + /** + * Called when the container is closed to give the items back. + * + * @param player The {@link Player} that closed this container + */ + @Override + public void removed(Player player) { + open = false; + } + + + /** + * Called when the container gets closed to put items back into a players + * inventory or drop them in the world. + * + * @param player The {@link Player} that closed this container + * @param container The container + */ + @Override + protected void clearContainer(Player player, Container container) { + open = false; + } + + /** + * Called when both items in the {@link AnvilMenu#inputSlots} were set to create + * the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. + */ + @Override + public void createResult() { + // empty + } + + +} diff --git a/InventoryAccess/1_17_R1/src/main/java/de/studiocode/inventoryaccess/v1_17_R1/util/InventoryUtilsImpl.java b/InventoryAccess/1_17_R1/src/main/java/de/studiocode/inventoryaccess/v1_17_R1/util/InventoryUtilsImpl.java new file mode 100644 index 0000000..fd969ed --- /dev/null +++ b/InventoryAccess/1_17_R1/src/main/java/de/studiocode/inventoryaccess/v1_17_R1/util/InventoryUtilsImpl.java @@ -0,0 +1,28 @@ +package de.studiocode.inventoryaccess.v1_17_R1.util; + +import de.studiocode.inventoryaccess.api.abstraction.util.InventoryUtils; +import de.studiocode.inventoryaccess.api.version.ReflectionUtils; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.inventory.MenuType; +import org.bukkit.craftbukkit.v1_17_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftContainer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Method; + +public class InventoryUtilsImpl implements InventoryUtils { + + private static final Method OPEN_CUSTOM_INVENTORY_METHOD = ReflectionUtils.getMethod(CraftHumanEntity.class, + true, "openCustomInventory", Inventory.class, ServerPlayer.class, MenuType.class); + + @Override + public void openCustomInventory(Player player, Inventory inventory) { + ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle(); + MenuType menuType = CraftContainer.getNotchInventoryType(inventory); + ReflectionUtils.invokeMethod(OPEN_CUSTOM_INVENTORY_METHOD, null, inventory, serverPlayer, menuType); + serverPlayer.containerMenu.checkReachable = false; + } + +} diff --git a/InventoryAccess/api/pom.xml b/InventoryAccess/api/pom.xml new file mode 100644 index 0000000..07c61cd --- /dev/null +++ b/InventoryAccess/api/pom.xml @@ -0,0 +1,36 @@ + + + + de.studiocode.invui + InvUI-Parent + 0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + api + + + 8 + 8 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + org.spigotmc + spigot-api + 1.16.4-R0.1-SNAPSHOT + provided + + + + \ No newline at end of file diff --git a/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/abstraction/inventory/AnvilInventory.java b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/abstraction/inventory/AnvilInventory.java new file mode 100644 index 0000000..82914e2 --- /dev/null +++ b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/abstraction/inventory/AnvilInventory.java @@ -0,0 +1,42 @@ +package de.studiocode.inventoryaccess.api.abstraction.inventory; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public interface AnvilInventory { + + /** + * Gets Bukkit's {@link Inventory} associated to this {@link AnvilInventory} + * + * @return The {@link Inventory} + */ + Inventory getBukkitInventory(); + + /** + * Opens the inventory. + */ + void open(); + + /** + * Sets an {@link ItemStack} on one of the three slots. + * + * @param slot The slot + * @param itemStack The {@link ItemStack} + */ + void setItem(int slot, ItemStack itemStack); + + /** + * Gets the rename text the user has typed in the renaming section of the anvil. + * + * @return The rename text + */ + String getRenameText(); + + /** + * Gets if this inventory is currently open. + * + * @return If the inventory is currently open + */ + boolean isOpen(); + +} diff --git a/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/abstraction/util/InventoryUtils.java b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/abstraction/util/InventoryUtils.java new file mode 100644 index 0000000..4ec6a8f --- /dev/null +++ b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/abstraction/util/InventoryUtils.java @@ -0,0 +1,20 @@ +package de.studiocode.inventoryaccess.api.abstraction.util; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; + +public interface InventoryUtils { + + /** + * Opens an {@link Inventory} as a custom inventory. + * Internally, this creates a CraftContainer which can save the {@link InventoryView}, + * unlike when using nms Containers, which is the default way for opening Inventories of + * TileEntities. + * + * @param player The {@link Player} to open the {@link Inventory} for + * @param inventory The {@link Inventory} + */ + void openCustomInventory(Player player, Inventory inventory); + +} diff --git a/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/version/InventoryAccess.java b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/version/InventoryAccess.java new file mode 100644 index 0000000..b50b9c3 --- /dev/null +++ b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/version/InventoryAccess.java @@ -0,0 +1,42 @@ +package de.studiocode.inventoryaccess.api.version; + +import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; +import de.studiocode.inventoryaccess.api.abstraction.util.InventoryUtils; +import org.bukkit.entity.Player; + +import java.lang.reflect.Constructor; +import java.util.function.Consumer; + +public class InventoryAccess { + + private static final Class INVENTORY_UTILS_CLASS = ReflectionUtils.getClass("util.InventoryUtilsImpl"); + private static final Class ANVIL_INVENTORY_CLASS = ReflectionUtils.getClass("inventory.AnvilInventoryImpl"); + + private static final Constructor ANVIL_INVENTORY_CONSTRUCTOR + = ReflectionUtils.getConstructor(ANVIL_INVENTORY_CLASS, Player.class, String.class, Consumer.class); + + private static final InventoryUtils INVENTORY_UTILS = ReflectionUtils.constructEmpty(INVENTORY_UTILS_CLASS); + + /** + * Gets the {@link InventoryUtils} + * + * @return The {@link InventoryUtils} + */ + public static InventoryUtils getInventoryUtils() { + return INVENTORY_UTILS; + } + + /** + * Creates a new {@link AnvilInventory}. + * + * @param player The {@link Player} that should see this {@link AnvilInventory} + * @param title The title {@link String} of the {@link AnvilInventory} + * @param renameHandler A {@link Consumer} that is called whenever the {@link Player} + * types something in the renaming section of the anvil + * @return The {@link AnvilInventory} + */ + public static AnvilInventory createAnvilInventory(Player player, String title, Consumer renameHandler) { + return ReflectionUtils.construct(ANVIL_INVENTORY_CONSTRUCTOR, player, title, renameHandler); + } + +} diff --git a/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/version/ReflectionUtils.java b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/version/ReflectionUtils.java new file mode 100644 index 0000000..db36c22 --- /dev/null +++ b/InventoryAccess/api/src/main/java/de/studiocode/inventoryaccess/api/version/ReflectionUtils.java @@ -0,0 +1,105 @@ +package de.studiocode.inventoryaccess.api.version; + +import org.bukkit.Bukkit; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class ReflectionUtils { + + private static final String VERSION = getVersion(); + + private static String getVersion() { + String path = Bukkit.getServer().getClass().getPackage().getName(); + return path.substring(path.lastIndexOf(".") + 1); + } + + @SuppressWarnings("unchecked") + public static Class getClass(String path) { + try { + return (Class) Class.forName("de.studiocode.inventoryaccess." + VERSION + "." + path); + } catch (ClassNotFoundException e) { + throw new UnsupportedOperationException("Your version (" + VERSION + ") is not supported by InventoryAccess"); + } + } + + public static Constructor getConstructor(Class clazz, Class... parameterTypes) { + try { + return clazz.getConstructor(parameterTypes); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return null; + } + + @SuppressWarnings("unchecked") + public static T constructEmpty(Class clazz) { + try { + return (T) clazz.getConstructor().newInstance(); + } catch (ReflectiveOperationException e) { + e.printStackTrace(); + } + + return null; + } + + public static T construct(Constructor constructor, Object... args) { + try { + return constructor.newInstance(args); + } catch (ReflectiveOperationException e) { + e.printStackTrace(); + } + + return null; + } + + public static Field getField(Class clazz, boolean declared, String name) { + try { + Field field = declared ? clazz.getDeclaredField(name) : clazz.getField(name); + if (declared) field.setAccessible(true); + return field; + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + return null; + } + + @SuppressWarnings("unchecked") + public static T getValueOfField(Field field, Object obj) { + try { + return (T) field.get(obj); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + return null; + } + + public static Method getMethod(Class clazz, boolean declared, String name, Class... parameterTypes) { + try { + Method method = declared ? clazz.getDeclaredMethod(name, parameterTypes) : clazz.getMethod(name, parameterTypes); + if (declared) method.setAccessible(true); + return method; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return null; + } + + @SuppressWarnings("unchecked") + public static T invokeMethod(Method method, Object obj, Object... args) { + try { + return (T) method.invoke(obj, args); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/pom.xml b/pom.xml index d557bf0..8995866 100644 --- a/pom.xml +++ b/pom.xml @@ -4,69 +4,20 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - de.studiocode - InvUI + de.studiocode.invui + InvUI-Parent 0.1-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - 8 - 8 - - - - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - minecraft-repo - https://libraries.minecraft.net/ - - - jitpack.io - https://jitpack.io - - - - - - org.jetbrains - annotations - 16.0.1 - compile - - - org.spigotmc - spigot-api - 1.16.4-R0.1-SNAPSHOT - provided - - - com.mojang - authlib - 1.5.21 - provided - - - * - * - - - - - com.github.NichtStudioCode - InventoryAccess - 0.3 - compile - - + pom + + + InventoryAccess/1_14_R1 + InventoryAccess/1_15_R1 + InventoryAccess/1_16_R1 + InventoryAccess/1_16_R2 + InventoryAccess/1_16_R3 + InventoryAccess/1_17_R1 + InventoryAccess/api + InvUI + \ No newline at end of file