Change window-related methods to use AbstractWindow

This commit is contained in:
NichtStudioCode 2023-03-03 17:45:10 +01:00
parent 1f12b58a1f
commit 8406efdb66
10 changed files with 37 additions and 56 deletions

@ -6,6 +6,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.xenondevs.invui.window.AbstractWindow;
import xyz.xenondevs.invui.window.Window; import xyz.xenondevs.invui.window.Window;
import java.util.Set; import java.util.Set;
@ -21,20 +22,20 @@ public interface Item {
ItemProvider getItemProvider(); ItemProvider getItemProvider();
/** /**
* Adds a {@link Window} to the window set, telling the {@link Item} that it is * Adds an {@link AbstractWindow} to the window set, telling the {@link Item} that it is
* currently being displayed in that {@link Window}. * currently being displayed in that {@link AbstractWindow}.
* *
* @param window The {@link Window} the {@link Item} is currently displayed in. * @param window The {@link AbstractWindow} the {@link Item} is currently displayed in.
*/ */
void addWindow(Window window); void addWindow(AbstractWindow window);
/** /**
* Removes a {@link Window} from the window set, telling the {@link Item} that it * Removes an {@link AbstractWindow} from the window set, telling the {@link Item} that it
* is no longer being displayed in that {@link Window}. * is no longer being displayed in that {@link AbstractWindow}.
* *
* @param window The {@link Window} the {@link Item} is no longer displayed in. * @param window The {@link AbstractWindow} the {@link Item} is no longer displayed in.
*/ */
void removeWindow(Window window); void removeWindow(AbstractWindow window);
/** /**
* Gets an immutable {@link Set} of all the {@link Window}s where this * Gets an immutable {@link Set} of all the {@link Window}s where this

@ -9,25 +9,19 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
* The base for all {@link Item}s. * An abstract implementation of the {@link Item} interface.
*/ */
public abstract class BaseItem implements Item { public abstract class AbstractItem implements Item {
private final Set<AbstractWindow> windows = new HashSet<>(); private final Set<AbstractWindow> windows = new HashSet<>();
@Override @Override
public void addWindow(Window window) { public void addWindow(AbstractWindow window) {
if (!(window instanceof AbstractWindow)) windows.add(window);
throw new IllegalArgumentException("Illegal window implementation");
windows.add((AbstractWindow) window);
} }
@Override @Override
public void removeWindow(Window window) { public void removeWindow(AbstractWindow window) {
if (!(window instanceof AbstractWindow))
throw new IllegalArgumentException("Illegal window implementation");
windows.remove(window); windows.remove(window);
} }

@ -19,7 +19,7 @@ import java.util.function.Supplier;
* An {@link Item} that creates it's {@link ItemProvider} asynchronously and displays * An {@link Item} that creates it's {@link ItemProvider} asynchronously and displays
* a placeholder {@link ItemProvider} until the actual {@link ItemProvider} has been created. * a placeholder {@link ItemProvider} until the actual {@link ItemProvider} has been created.
*/ */
public class AsyncItem extends BaseItem { public class AsyncItem extends AbstractItem {
private volatile ItemProvider itemProvider; private volatile ItemProvider itemProvider;

@ -15,7 +15,7 @@ import xyz.xenondevs.invui.window.Window;
* An {@link Item} that automatically cycles through a predefined array of * An {@link Item} that automatically cycles through a predefined array of
* {@link ItemProvider} at a predefined speed. * {@link ItemProvider} at a predefined speed.
*/ */
public class AutoCycleItem extends BaseItem { public class AutoCycleItem extends AbstractItem {
private final ItemProvider[] itemProviders; private final ItemProvider[] itemProviders;
private final int period; private final int period;

@ -13,7 +13,7 @@ import java.util.function.BiConsumer;
/** /**
* An {@link Item} that cycles through a predefined array of {@link ItemProvider}s when clicked. * An {@link Item} that cycles through a predefined array of {@link ItemProvider}s when clicked.
*/ */
public class CycleItem extends BaseItem { public class CycleItem extends AbstractItem {
private final ItemProvider[] states; private final ItemProvider[] states;

@ -14,7 +14,7 @@ import java.util.function.Consumer;
/** /**
* A simple {@link Item} that does nothing. * A simple {@link Item} that does nothing.
*/ */
public class SimpleItem extends BaseItem { public class SimpleItem extends AbstractItem {
private final ItemProvider itemProvider; private final ItemProvider itemProvider;
private final Consumer<Click> clickHandler; private final Consumer<Click> clickHandler;

@ -11,7 +11,7 @@ import xyz.xenondevs.invui.item.ItemProvider;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
public class SuppliedItem extends BaseItem { public class SuppliedItem extends AbstractItem {
private final Supplier<? extends ItemProvider> builderSupplier; private final Supplier<? extends ItemProvider> builderSupplier;
private final Function<Click, Boolean> clickHandler; private final Function<Click, Boolean> clickHandler;

@ -3,14 +3,14 @@ package xyz.xenondevs.invui.item.impl.controlitem;
import xyz.xenondevs.invui.gui.Gui; import xyz.xenondevs.invui.gui.Gui;
import xyz.xenondevs.invui.item.Item; import xyz.xenondevs.invui.item.Item;
import xyz.xenondevs.invui.item.ItemProvider; import xyz.xenondevs.invui.item.ItemProvider;
import xyz.xenondevs.invui.item.impl.BaseItem; import xyz.xenondevs.invui.item.impl.AbstractItem;
/** /**
* A special type of {@link Item} that stores the {@link Gui} in which it is displayed. * A special type of {@link Item} that stores the {@link Gui} in which it is displayed.
* *
* @param <G> The Gui Type this {@link ControlItem} will control. Not checked when adding it to a Gui. * @param <G> The Gui Type this {@link ControlItem} will control. Not checked when adding it to a Gui.
*/ */
public abstract class ControlItem<G extends Gui> extends BaseItem { public abstract class ControlItem<G extends Gui> extends AbstractItem {
private G gui; private G gui;

@ -77,28 +77,22 @@ public class VirtualInventory {
} }
/** /**
* Adds a {@link Window} to the set of {@link Window}s, telling the {@link VirtualInventory} that * Adds an {@link AbstractWindow} to the set of {@link AbstractWindow AbstractWindows}, telling the {@link VirtualInventory} that
* its contents are now being displayed in that {@link Window}. * its contents are now being displayed in that {@link AbstractWindow}.
* *
* @param window The {@link Window} to be added. * @param window The {@link Window} to be added.
*/ */
public void addWindow(Window window) { public void addWindow(AbstractWindow window) {
if (!(window instanceof AbstractWindow)) windows.add(window);
throw new IllegalArgumentException("Illegal window implementation");
windows.add((AbstractWindow) window);
} }
/** /**
* Removes a {@link Window} from the set of {@link Window}s, telling the {@link VirtualInventory} that * Removes an {@link AbstractWindow} from the set of {@link AbstractWindow AbstractWindows}, telling the {@link VirtualInventory} that
* its contents are no longer being displayed in that {@link Window}. * its contents are no longer being displayed in that {@link AbstractWindow}.
* *
* @param window The {@link Window} to be removed. * @param window The {@link AbstractWindow} to be removed.
*/ */
public void removeWindow(Window window) { public void removeWindow(AbstractWindow window) {
if (!(window instanceof AbstractWindow))
throw new IllegalArgumentException("Illegal window implementation");
windows.remove(window); windows.remove(window);
} }

@ -44,31 +44,23 @@ public class WindowManager implements Listener {
} }
/** /**
* Adds a {@link Window} to the list of windows. * Adds an {@link AbstractWindow} to the list of windows.
* This method is usually called by the {@link Window} itself. * This method is usually called by the {@link Window} itself.
* *
* @param window The {@link Window} to add * @param window The {@link AbstractWindow} to add
*/ */
public void addWindow(Window window) { public void addWindow(AbstractWindow window) {
if (!(window instanceof AbstractWindow)) windows.put(window.getInventories()[0], window);
throw new IllegalArgumentException("Illegal window implementation");
AbstractWindow abstractWindow = (AbstractWindow) window;
windows.put(abstractWindow.getInventories()[0], abstractWindow);
} }
/** /**
* Removes a {@link Window} from the list of windows. * Removes an {@link AbstractWindow} from the list of windows.
* This method is usually called by the {@link Window} itself. * This method is usually called by the {@link Window} itself.
* *
* @param window The {@link Window} to remove * @param window The {@link AbstractWindow} to remove
*/ */
public void removeWindow(Window window) { public void removeWindow(AbstractWindow window) {
if (!(window instanceof AbstractWindow)) windows.remove(window.getInventories()[0]);
throw new IllegalArgumentException("Illegal window implementation");
AbstractWindow abstractWindow = (AbstractWindow) window;
windows.remove(abstractWindow.getInventories()[0]);
} }
/** /**