Minor changes

-Added some @NotNull and -@Nullable
-Some refactoring
-Added some new constructors for windows that don't require the closeable and closeOnEvent boolean
This commit is contained in:
NichtStudioCode 2021-02-08 19:00:09 +01:00
parent b5af62d64d
commit 951178f161
12 changed files with 64 additions and 38 deletions

@ -68,7 +68,7 @@ public interface GUI extends GUIParent {
* @param y The y coordinate * @param y The y coordinate
* @param slotElement The {@link SlotElement} to be placed there. * @param slotElement The {@link SlotElement} to be placed there.
*/ */
void setSlotElement(int x, int y, SlotElement slotElement); void setSlotElement(int x, int y, @Nullable SlotElement slotElement);
/** /**
* Sets the {@link SlotElement} on these coordinates. * Sets the {@link SlotElement} on these coordinates.
@ -77,7 +77,7 @@ public interface GUI extends GUIParent {
* @param index The slot index * @param index The slot index
* @param slotElement The {@link SlotElement} to be placed there. * @param slotElement The {@link SlotElement} to be placed there.
*/ */
void setSlotElement(int index, SlotElement slotElement); void setSlotElement(int index, @Nullable SlotElement slotElement);
/** /**
* Gets the {@link SlotElement} on these coordinates. * Gets the {@link SlotElement} on these coordinates.
@ -271,7 +271,7 @@ public interface GUI extends GUIParent {
* @param item The {@link Item} that should be used or null to remove an existing item. * @param item The {@link Item} that should be used or null to remove an existing item.
* @param replaceExisting If existing {@link Item}s should be replaced. * @param replaceExisting If existing {@link Item}s should be replaced.
*/ */
void fill(int start, int end, Item item, boolean replaceExisting); void fill(int start, int end, @Nullable Item item, boolean replaceExisting);
/** /**
* Fills the entire {@link GUI} with {@link Item}s. * Fills the entire {@link GUI} with {@link Item}s.
@ -279,7 +279,7 @@ public interface GUI extends GUIParent {
* @param item The {@link Item} that should be used or null to remove an existing item. * @param item The {@link Item} that should be used or null to remove an existing item.
* @param replaceExisting If existing {@link Item}s should be replaced. * @param replaceExisting If existing {@link Item}s should be replaced.
*/ */
void fill(Item item, boolean replaceExisting); void fill(@Nullable Item item, boolean replaceExisting);
/** /**
* Fills one row with an specific {@link Item} * Fills one row with an specific {@link Item}
@ -288,7 +288,7 @@ public interface GUI extends GUIParent {
* @param item The {@link Item} that should be used or null to remove an existing item. * @param item The {@link Item} that should be used or null to remove an existing item.
* @param replaceExisting If existing {@link Item}s should be replaced. * @param replaceExisting If existing {@link Item}s should be replaced.
*/ */
void fillRow(int row, Item item, boolean replaceExisting); void fillRow(int row, @Nullable Item item, boolean replaceExisting);
/** /**
* Fills one column with an specific {@link Item} * Fills one column with an specific {@link Item}
@ -297,7 +297,7 @@ public interface GUI extends GUIParent {
* @param item The {@link Item} that should be used or null to remove an existing item. * @param item The {@link Item} that should be used or null to remove an existing item.
* @param replaceExisting If existing {@link Item}s should be replaced. * @param replaceExisting If existing {@link Item}s should be replaced.
*/ */
void fillColumn(int column, Item item, boolean replaceExisting); void fillColumn(int column, @Nullable Item item, boolean replaceExisting);
/** /**
* Fills the borders of this {@link GUI} with an specific {@link Item} * Fills the borders of this {@link GUI} with an specific {@link Item}
@ -305,7 +305,7 @@ public interface GUI extends GUIParent {
* @param item The {@link Item} that should be used or null to remove an existing item. * @param item The {@link Item} that should be used or null to remove an existing item.
* @param replaceExisting If existing {@link Item}s should be replaced. * @param replaceExisting If existing {@link Item}s should be replaced.
*/ */
void fillBorders(Item item, boolean replaceExisting); void fillBorders(@Nullable Item item, boolean replaceExisting);
/** /**
* Fills a rectangle in this {@link GUI} with an specific {@link Item} * Fills a rectangle in this {@link GUI} with an specific {@link Item}
@ -317,7 +317,7 @@ public interface GUI extends GUIParent {
* @param item The {@link Item} that should be used or null to remove an existing item. * @param item The {@link Item} that should be used or null to remove an existing item.
* @param replaceExisting If existing {@link Item}s should be replaced. * @param replaceExisting If existing {@link Item}s should be replaced.
*/ */
void fillRectangle(int x, int y, int width, int height, Item item, boolean replaceExisting); void fillRectangle(int x, int y, int width, int height, @Nullable Item item, boolean replaceExisting);
/** /**
* Fills a rectangle of another {@link GUI} in this {@link GUI}. * Fills a rectangle of another {@link GUI} in this {@link GUI}.
@ -327,7 +327,7 @@ public interface GUI extends GUIParent {
* @param gui The {@link GUI} to be put into this {@link GUI} * @param gui The {@link GUI} to be put into this {@link GUI}
* @param replaceExisting If existing {@link SlotElement}s should be replaced. * @param replaceExisting If existing {@link SlotElement}s should be replaced.
*/ */
void fillRectangle(int x, int y, GUI gui, boolean replaceExisting); void fillRectangle(int x, int y, @NotNull GUI gui, boolean replaceExisting);
/** /**
* Fills a rectangle of a {@link VirtualInventory} in this {@link GUI}. * Fills a rectangle of a {@link VirtualInventory} in this {@link GUI}.
@ -338,6 +338,6 @@ public interface GUI extends GUIParent {
* @param virtualInventory The {@link VirtualInventory} to be put into this {@link GUI}. * @param virtualInventory The {@link VirtualInventory} to be put into this {@link GUI}.
* @param replaceExisting If existing {@link SlotElement}s should be replaced. * @param replaceExisting If existing {@link SlotElement}s should be replaced.
*/ */
void fillRectangle(int x, int y, int width, VirtualInventory virtualInventory, boolean replaceExisting); void fillRectangle(int x, int y, int width, @NotNull VirtualInventory virtualInventory, boolean replaceExisting);
} }

@ -9,6 +9,7 @@ import de.studiocode.invui.item.Item;
import de.studiocode.invui.util.SlotUtils; import de.studiocode.invui.util.SlotUtils;
import de.studiocode.invui.virtualinventory.VirtualInventory; import de.studiocode.invui.virtualinventory.VirtualInventory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set; import java.util.Set;
@ -79,7 +80,7 @@ public abstract class BaseGUI extends IndexedGUI {
// filling methods // filling methods
public void fill(@NotNull Set<Integer> slots, Item item, boolean replaceExisting) { public void fill(@NotNull Set<Integer> slots, @Nullable Item item, boolean replaceExisting) {
for (int slot : slots) { for (int slot : slots) {
if (!replaceExisting && hasSlotElement(slot)) continue; if (!replaceExisting && hasSlotElement(slot)) continue;
setItem(slot, item); setItem(slot, item);
@ -87,7 +88,7 @@ public abstract class BaseGUI extends IndexedGUI {
} }
@Override @Override
public void fill(int start, int end, Item item, boolean replaceExisting) { public void fill(int start, int end, @Nullable Item item, boolean replaceExisting) {
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
if (!replaceExisting && hasSlotElement(i)) continue; if (!replaceExisting && hasSlotElement(i)) continue;
setItem(i, item); setItem(i, item);
@ -95,34 +96,34 @@ public abstract class BaseGUI extends IndexedGUI {
} }
@Override @Override
public void fill(Item item, boolean replaceExisting) { public void fill(@Nullable Item item, boolean replaceExisting) {
fill(0, getSize(), item, replaceExisting); fill(0, getSize(), item, replaceExisting);
} }
@Override @Override
public void fillRow(int row, Item item, boolean replaceExisting) { public void fillRow(int row, @Nullable Item item, boolean replaceExisting) {
if (row >= height) throw new IllegalArgumentException("Row out of bounds"); if (row >= height) throw new IllegalArgumentException("Row out of bounds");
fill(SlotUtils.getSlotsRow(row, width), item, replaceExisting); fill(SlotUtils.getSlotsRow(row, width), item, replaceExisting);
} }
@Override @Override
public void fillColumn(int column, Item item, boolean replaceExisting) { public void fillColumn(int column, @Nullable Item item, boolean replaceExisting) {
if (column >= width) throw new IllegalArgumentException("Column out of bounds"); if (column >= width) throw new IllegalArgumentException("Column out of bounds");
fill(SlotUtils.getSlotsColumn(column, width, height), item, replaceExisting); fill(SlotUtils.getSlotsColumn(column, width, height), item, replaceExisting);
} }
@Override @Override
public void fillBorders(Item item, boolean replaceExisting) { public void fillBorders(@Nullable Item item, boolean replaceExisting) {
fill(SlotUtils.getSlotsBorders(width, height), item, replaceExisting); fill(SlotUtils.getSlotsBorders(width, height), item, replaceExisting);
} }
@Override @Override
public void fillRectangle(int x, int y, int width, int height, Item item, boolean replaceExisting) { public void fillRectangle(int x, int y, int width, int height, @Nullable Item item, boolean replaceExisting) {
fill(SlotUtils.getSlotsRect(x, y, width, height, this.width), item, replaceExisting); fill(SlotUtils.getSlotsRect(x, y, width, height, this.width), item, replaceExisting);
} }
@Override @Override
public void fillRectangle(int x, int y, GUI gui, boolean replaceExisting) { public void fillRectangle(int x, int y, @NotNull GUI gui, boolean replaceExisting) {
int slotIndex = 0; int slotIndex = 0;
for (int slot : SlotUtils.getSlotsRect(x, y, gui.getWidth(), gui.getHeight(), this.width)) { for (int slot : SlotUtils.getSlotsRect(x, y, gui.getWidth(), gui.getHeight(), this.width)) {
if (hasSlotElement(slot) && !replaceExisting) continue; if (hasSlotElement(slot) && !replaceExisting) continue;
@ -132,7 +133,7 @@ public abstract class BaseGUI extends IndexedGUI {
} }
@Override @Override
public void fillRectangle(int x, int y, int width, VirtualInventory virtualInventory, boolean replaceExisting) { public void fillRectangle(int x, int y, int width, @NotNull VirtualInventory virtualInventory, boolean replaceExisting) {
int height = (int) Math.ceil((double) virtualInventory.getSize() / (double) width); int height = (int) Math.ceil((double) virtualInventory.getSize() / (double) width);
int slotIndex = 0; int slotIndex = 0;

@ -1,5 +1,7 @@
package de.studiocode.invui.util; package de.studiocode.invui.util;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -7,18 +9,21 @@ import java.util.stream.Collectors;
public class SlotUtils { public class SlotUtils {
@NotNull
public static Set<Integer> getSlotsRow(int row, int width) { public static Set<Integer> getSlotsRow(int row, int width) {
Set<Integer> slots = new LinkedHashSet<>(); Set<Integer> slots = new LinkedHashSet<>();
for (int x = 0; x < width; x++) slots.add(convertToIndex(x, row, width)); for (int x = 0; x < width; x++) slots.add(convertToIndex(x, row, width));
return slots; return slots;
} }
@NotNull
public static Set<Integer> getSlotsColumn(int column, int width, int height) { public static Set<Integer> getSlotsColumn(int column, int width, int height) {
Set<Integer> slots = new LinkedHashSet<>(); Set<Integer> slots = new LinkedHashSet<>();
for (int y = 0; y < height; y++) slots.add(convertToIndex(column, y, width)); for (int y = 0; y < height; y++) slots.add(convertToIndex(column, y, width));
return slots; return slots;
} }
@NotNull
public static Set<Integer> getSlotsBorders(int width, int height) { public static Set<Integer> getSlotsBorders(int width, int height) {
Set<Integer> slots = new LinkedHashSet<>(); Set<Integer> slots = new LinkedHashSet<>();
if (height > 0) slots.addAll(getSlotsRow(0, width)); if (height > 0) slots.addAll(getSlotsRow(0, width));
@ -28,11 +33,13 @@ public class SlotUtils {
return slots; return slots;
} }
@NotNull
public static Set<Integer> getSlotsRect(int x, int y, int width, int height, int frameWidth) { public static Set<Integer> getSlotsRect(int x, int y, int width, int height, int frameWidth) {
return getSlotsRect(Order.HORIZONTAL, x, y, width, height, frameWidth); return getSlotsRect(Order.HORIZONTAL, x, y, width, height, frameWidth);
} }
public static Set<Integer> getSlotsRect(Order order, int x, int y, int width, int height, int frameWidth) { @NotNull
public static Set<Integer> getSlotsRect(@NotNull Order order, int x, int y, int width, int height, int frameWidth) {
Set<Integer> slots = new LinkedHashSet<>(); Set<Integer> slots = new LinkedHashSet<>();
switch (order) { switch (order) {
@ -50,12 +57,13 @@ public class SlotUtils {
slots.add(convertToIndex(x1, y1, frameWidth)); slots.add(convertToIndex(x1, y1, frameWidth));
} }
} }
break;
} }
return slots; return slots;
} }
public static int convertToIndex(Point2D point, int width) { public static int convertToIndex(@NotNull Point2D point, int width) {
return convertToIndex(point.getX(), point.getY(), width); return convertToIndex(point.getX(), point.getY(), width);
} }
@ -63,6 +71,7 @@ public class SlotUtils {
return y * width + x; return y * width + x;
} }
@NotNull
public static Point2D convertFromIndex(int index, int width) { public static Point2D convertFromIndex(int index, int width) {
return new Point2D(index % width, index / width); return new Point2D(index % width, index / width);
} }
@ -97,7 +106,7 @@ public class SlotUtils {
return longestLength; return longestLength;
} }
public static boolean isNeighbor(Point2D point1, Point2D point2) { public static boolean isNeighbor(@NotNull Point2D point1, @NotNull Point2D point2) {
return Math.abs(point1.getX() - point2.getX()) + Math.abs(point1.getY() - point2.getY()) == 1; return Math.abs(point1.getX() - point2.getX()) + Math.abs(point1.getY() - point2.getY()) == 1;
} }

@ -5,8 +5,8 @@ import de.studiocode.invui.gui.GUIParent;
import de.studiocode.invui.item.Item; import de.studiocode.invui.item.Item;
import de.studiocode.invui.item.itembuilder.ItemBuilder; import de.studiocode.invui.item.itembuilder.ItemBuilder;
import de.studiocode.invui.virtualinventory.VirtualInventory; import de.studiocode.invui.virtualinventory.VirtualInventory;
import de.studiocode.invui.window.impl.combined.combinedgui.SimpleCombinedWindow; import de.studiocode.invui.window.impl.merged.combined.SimpleCombinedWindow;
import de.studiocode.invui.window.impl.combined.splitgui.SimpleSplitWindow; import de.studiocode.invui.window.impl.merged.split.SimpleSplitWindow;
import de.studiocode.invui.window.impl.single.SimpleWindow; import de.studiocode.invui.window.impl.single.SimpleWindow;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;

@ -1,7 +1,7 @@
package de.studiocode.invui.window; package de.studiocode.invui.window;
import de.studiocode.invui.InvUI; import de.studiocode.invui.InvUI;
import de.studiocode.invui.window.impl.combined.BaseCombinedWindow; import de.studiocode.invui.window.impl.merged.MergedWindow;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -153,7 +153,7 @@ public class WindowManager implements Listener {
Entity entity = event.getEntity(); Entity entity = event.getEntity();
if (entity instanceof Player) { if (entity instanceof Player) {
Optional<Window> window = findOpenWindow(((Player) entity)); Optional<Window> window = findOpenWindow(((Player) entity));
if (window.isPresent() && window.get() instanceof BaseCombinedWindow) if (window.isPresent() && window.get() instanceof MergedWindow)
event.setCancelled(true); event.setCancelled(true);
} }
} }

@ -1,4 +1,4 @@
package de.studiocode.invui.window.impl.combined; package de.studiocode.invui.window.impl.merged;
import de.studiocode.invui.gui.SlotElement.ItemStackHolder; import de.studiocode.invui.gui.SlotElement.ItemStackHolder;
import de.studiocode.invui.gui.SlotElement.VISlotElement; import de.studiocode.invui.gui.SlotElement.VISlotElement;
@ -18,14 +18,14 @@ import java.util.Objects;
/** /**
* A {@link Window} that uses both top and player {@link Inventory}. * A {@link Window} that uses both top and player {@link Inventory}.
*/ */
public abstract class BaseCombinedWindow extends BaseWindow { public abstract class MergedWindow extends BaseWindow {
private final Inventory playerInventory; private final Inventory playerInventory;
private final ItemStack[] playerItems = new ItemStack[36]; private final ItemStack[] playerItems = new ItemStack[36];
protected Inventory upperInventory; protected Inventory upperInventory;
private boolean isCurrentlyOpened; private boolean isCurrentlyOpened;
public BaseCombinedWindow(Player player, int size, Inventory upperInventory, boolean closeable, boolean closeOnEvent) { public MergedWindow(Player player, int size, Inventory upperInventory, boolean closeable, boolean closeOnEvent) {
super(player.getUniqueId(), size, closeable, closeOnEvent); super(player.getUniqueId(), size, closeable, closeOnEvent);
this.upperInventory = upperInventory; this.upperInventory = upperInventory;
this.playerInventory = player.getInventory(); this.playerInventory = player.getInventory();

@ -1,10 +1,10 @@
package de.studiocode.invui.window.impl.combined.combinedgui; package de.studiocode.invui.window.impl.merged.combined;
import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.GUI;
import de.studiocode.invui.gui.SlotElement.ItemStackHolder; import de.studiocode.invui.gui.SlotElement.ItemStackHolder;
import de.studiocode.invui.util.SlotUtils; import de.studiocode.invui.util.SlotUtils;
import de.studiocode.invui.window.Window; import de.studiocode.invui.window.Window;
import de.studiocode.invui.window.impl.combined.BaseCombinedWindow; import de.studiocode.invui.window.impl.merged.MergedWindow;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -12,7 +12,7 @@ import org.bukkit.inventory.Inventory;
/** /**
* A {@link Window} where top and player {@link Inventory} are affected by the same {@link GUI}. * A {@link Window} where top and player {@link Inventory} are affected by the same {@link GUI}.
*/ */
public abstract class CombinedWindow extends BaseCombinedWindow { public abstract class CombinedWindow extends MergedWindow {
private final GUI gui; private final GUI gui;

@ -1,4 +1,4 @@
package de.studiocode.invui.window.impl.combined.combinedgui; package de.studiocode.invui.window.impl.merged.combined;
import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.GUI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -11,6 +11,10 @@ public class SimpleCombinedWindow extends CombinedWindow {
super(player, gui, createInventory(gui, title), closeable, closeOnEvent); super(player, gui, createInventory(gui, title), closeable, closeOnEvent);
} }
public SimpleCombinedWindow(Player player, String title, GUI gui) {
this(player, title, gui, true, true);
}
private static Inventory createInventory(GUI gui, String title) { private static Inventory createInventory(GUI gui, String title) {
if (gui.getWidth() != 9) if (gui.getWidth() != 9)
throw new IllegalArgumentException("GUI width has to be 9"); throw new IllegalArgumentException("GUI width has to be 9");

@ -1,4 +1,4 @@
package de.studiocode.invui.window.impl.combined.splitgui; package de.studiocode.invui.window.impl.merged.split;
import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory; import de.studiocode.inventoryaccess.api.abstraction.inventory.AnvilInventory;
import de.studiocode.inventoryaccess.api.version.InventoryAccess; import de.studiocode.inventoryaccess.api.version.InventoryAccess;
@ -23,6 +23,10 @@ public class AnvilSplitWindow extends SplitWindow {
initUpperItems(); initUpperItems();
} }
public AnvilSplitWindow(Player player, String title, GUI upperGui, GUI lowerGui, Consumer<String> renameHandler) {
this(player, title, upperGui, lowerGui, true, renameHandler);
}
@Override @Override
protected void setUpperInvItem(int slot, ItemStack itemStack) { protected void setUpperInvItem(int slot, ItemStack itemStack) {
anvilInventory.setItem(slot, itemStack); anvilInventory.setItem(slot, itemStack);

@ -1,4 +1,4 @@
package de.studiocode.invui.window.impl.combined.splitgui; package de.studiocode.invui.window.impl.merged.split;
import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.GUI;
import de.studiocode.invui.util.InventoryUtils; import de.studiocode.invui.util.InventoryUtils;
@ -10,4 +10,8 @@ public class SimpleSplitWindow extends SplitWindow {
super(player, upperGui, lowerGui, InventoryUtils.createMatchingInventory(upperGui, title), true, closeable, closeOnEvent); super(player, upperGui, lowerGui, InventoryUtils.createMatchingInventory(upperGui, title), true, closeable, closeOnEvent);
} }
public SimpleSplitWindow(Player player, String title, GUI upperGui, GUI lowerGui) {
this(player, title, upperGui, lowerGui, true, true);
}
} }

@ -1,10 +1,10 @@
package de.studiocode.invui.window.impl.combined.splitgui; package de.studiocode.invui.window.impl.merged.split;
import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.GUI;
import de.studiocode.invui.gui.SlotElement.ItemStackHolder; import de.studiocode.invui.gui.SlotElement.ItemStackHolder;
import de.studiocode.invui.util.SlotUtils; import de.studiocode.invui.util.SlotUtils;
import de.studiocode.invui.window.Window; import de.studiocode.invui.window.Window;
import de.studiocode.invui.window.impl.combined.BaseCombinedWindow; import de.studiocode.invui.window.impl.merged.MergedWindow;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -12,7 +12,7 @@ import org.bukkit.inventory.Inventory;
/** /**
* A {@link Window} where top and player {@link Inventory} are affected by different {@link GUI}s. * A {@link Window} where top and player {@link Inventory} are affected by different {@link GUI}s.
*/ */
public abstract class SplitWindow extends BaseCombinedWindow { public abstract class SplitWindow extends MergedWindow {
private final GUI upperGui; private final GUI upperGui;
private final GUI lowerGui; private final GUI lowerGui;

@ -20,6 +20,10 @@ public class AnvilWindow extends SingleWindow {
initItems(); initItems();
} }
public AnvilWindow(Player player, String title, GUI gui, Consumer<String> renameHandler) {
this(player, title, gui, true, renameHandler);
}
@Override @Override
protected void setInvItem(int slot, ItemStack itemStack) { protected void setInvItem(int slot, ItemStack itemStack) {
anvilInventory.setItem(slot, itemStack); anvilInventory.setItem(slot, itemStack);