Skip to content

Commit

Permalink
Add copy methods
Browse files Browse the repository at this point in the history
Gui, all panes and GuiItem now have a method for deep copying.
  • Loading branch information
stefvanschie committed Aug 3, 2020
1 parent fd37540 commit 5acf0b9
Show file tree
Hide file tree
Showing 18 changed files with 434 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/main/java/com/github/stefvanschie/inventoryframework/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ public void show(@NotNull HumanEntity humanEntity) {
humanEntity.openInventory(inventory);
}

/**
* Makes a copy of this gui and returns it. This makes a deep copy of the gui. This entails that the underlying
* panes will be copied as per their {@link Pane#copy} and miscellaneous data will be copied. The copy of this gui,
* will however have no viewers even if this gui currently has viewers. With this, cache data for viewers will also
* be non-existent for the copied gui. The returned gui will never be reference equal to the current gui.
*
* @return a copy of the gui
* @since 0.6.2
*/
@NotNull
@Contract(pure = true)
public Gui copy() {
Gui gui = new Gui(getRows(), getTitle());

for (Pane pane : panes) {
gui.addPane(pane.copy());
}

gui.onTopClick = onTopClick;
gui.onBottomClick = onBottomClick;
gui.onGlobalClick = onGlobalClick;
gui.onOutsideClick = onOutsideClick;
gui.onClose = onClose;

return gui;
}

/**
* Sets the amount of rows for this inventory.
* This will (unlike most other methods) directly update itself in order to ensure all viewers will still be viewing the new inventory as well.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ public GuiItem(@NotNull ItemStack item) {
this(item, null);
}

/**
* Makes a copy of this gui item and returns it. This makes a deep copy of the gui item. This entails that the
* underlying item will be copied as per their {@link ItemStack#clone()} and miscellaneous data will be copied in
* such a way that they are identical with exception to the underlying unique identifier. The returned gui item will
* never be reference equal to the current gui item.
*
* @return a copy of the gui item
* @since 0.6.2
*/
@NotNull
@Contract(pure = true)
public GuiItem copy() {
GuiItem guiItem = new GuiItem(item.clone(), action);

guiItem.visible = visible;

return guiItem;
}

/**
* Calls the handler of the {@link InventoryClickEvent}
* if such a handler was specified in the constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -188,6 +189,23 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
return success;
}

@NotNull
@Contract(pure = true)
@Override
public MasonryPane copy() {
MasonryPane masonryPane = new MasonryPane(x, y, length, height, getPriority());

for (Pane pane : panes) {
masonryPane.addPane(pane.copy());
}

masonryPane.setVisible(isVisible());
masonryPane.onClick = onClick;
masonryPane.orientation = orientation;

return masonryPane;
}

/**
* Adds a pane to this masonry pane
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,30 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
return true;
}

@NotNull
@Contract(pure = true)
@Override
public OutlinePane copy() {
OutlinePane outlinePane = new OutlinePane(x, y, length, height, getPriority());

for (GuiItem item : items) {
outlinePane.addItem(item.copy());
}

outlinePane.setVisible(isVisible());
outlinePane.onClick = onClick;

outlinePane.orientation = orientation;
outlinePane.rotation = rotation;
outlinePane.gap = gap;
outlinePane.repeat = repeat;
outlinePane.flipHorizontally = flipHorizontally;
outlinePane.flipVertically = flipVertically;
outlinePane.mask = mask;

return outlinePane;
}

@Override
public void setRotation(int rotation) {
if (length != height) {
Expand Down Expand Up @@ -375,6 +399,18 @@ public List<GuiItem> getItems() {
return items;
}

/**
* Gets the mask applied to this pane.
*
* @return the mask
* @since 0.6.2
*/
@NotNull
@Contract(pure = true)
public Mask getMask() {
return mask;
}

/**
* Gets the orientation of this outline pane
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
return success;
}

@NotNull
@Contract(pure = true)
@Override
public PaginatedPane copy() {
PaginatedPane paginatedPane = new PaginatedPane(x, y, length, height, getPriority());

for (Map.Entry<Integer, List<Pane>> entry : panes.entrySet()) {
for (Pane pane : entry.getValue()) {
paginatedPane.addPane(entry.getKey(), pane.copy());
}
}

paginatedPane.setVisible(isVisible());
paginatedPane.onClick = onClick;

paginatedPane.page = page;

return paginatedPane;
}

@NotNull
@Contract(pure = true)
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.lang.UnsupportedOperationException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
Expand Down Expand Up @@ -65,7 +66,7 @@ public abstract class Pane {
* The consumer that will be called once a players clicks in this pane
*/
@Nullable
private Consumer<InventoryClickEvent> onClick;
protected Consumer<InventoryClickEvent> onClick;

/**
* A map containing the mappings for properties for items
Expand Down Expand Up @@ -119,6 +120,19 @@ protected Pane(int x, int y, int length, int height) {
this(x, y, length, height, Priority.NORMAL);
}

/**
* Makes a copy of this pane and returns it. This makes a deep copy of the pane. This entails that the underlying
* panes and/or items will be copied as well. The returned pane will never be reference equal to the current pane.
*
* @return a copy of this pane
* @since 0.6.2
*/
@NotNull
@Contract(pure = true)
public Pane copy() {
throw new UnsupportedOperationException("The implementing pane hasn't overridden the copy method");
}

/**
* Set the length of this pane
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,29 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
return true;
}

@NotNull
@Contract(pure = true)
@Override
public StaticPane copy() {
StaticPane staticPane = new StaticPane(x, y, length, height, getPriority());

for (Map.Entry<Map.Entry<Integer, Integer>, GuiItem> entry : items.entrySet()) {
Map.Entry<Integer, Integer> coordinates = entry.getKey();

staticPane.addItem(entry.getValue().copy(), coordinates.getKey(), coordinates.getValue());
}

staticPane.setVisible(isVisible());
staticPane.onClick = onClick;

staticPane.rotation = rotation;
staticPane.flipHorizontally = flipHorizontally;
staticPane.flipVertically = flipVertically;

return staticPane;
}

@Override
public void setRotation(int rotation) {
if (length != height) {
throw new UnsupportedOperationException("length and height are different");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -101,6 +102,24 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
panes.get(position).display(gui, inventory, playerInventory, newX, newY, newMaxLength, newMaxHeight);
}

@NotNull
@Contract(pure = true)
@Override
public CycleButton copy() {
CycleButton cycleButton = new CycleButton(x, y, length, height, getPriority());

for (Pane pane : panes) {
cycleButton.addPane(pane);
}

cycleButton.setVisible(isVisible());
cycleButton.onClick = onClick;

cycleButton.position = position;

return cycleButton;
}

@NotNull
@Override
public Collection<GuiItem> getItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ public void setText(@NotNull String text) {
}
}

@NotNull
@Contract(pure = true)
@Override
public Label copy() {
Label label = new Label(x, y, length, height, getPriority(), font);

for (GuiItem item : getItems()) {
label.addItem(item.copy());
}

label.setVisible(isVisible());
label.onClick = onClick;

label.setOrientation(getOrientation());
label.setRotation(getRotation());
label.setGap(getGap());
label.setRepeat(doesRepeat());
label.flipHorizontally(isFlippedHorizontally());
label.flipVertically(isFlippedVertically());
label.applyMask(getMask());

label.text = text;

return label;
}

@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
int maxLength, int maxHeight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ public void setPercentage(float percentage) {
}
}

@NotNull
@Contract(pure = true)
@Override
public PercentageBar copy() {
PercentageBar percentageBar = new PercentageBar(x, y, length, height, getPriority());

applyContents(percentageBar);

return percentageBar;
}

/**
* Gets the percentage as a float in between (0,1) this bar is currently set at.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public void setValue(float value) {
}
}

@NotNull
@Contract(pure = true)
@Override
public Slider copy() {
Slider slider = new Slider(x, y, length, height, getPriority());

applyContents(slider);

return slider;
}

/**
* Gets the value as a float in between (0,1) this bar is currently set at.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
return true;
}

@NotNull
@Contract(pure = true)
@Override
public ToggleButton copy() {
ToggleButton toggleButton = new ToggleButton(x, y, length, height, getPriority());

toggleButton.setVisible(isVisible());
toggleButton.onClick = onClick;

toggleButton.setEnabledItem(enabledPane.getItems().get(0).copy());
toggleButton.setDisabledItem(disabledPane.getItems().get(0).copy());

toggleButton.enabled = enabled;

return toggleButton;
}

/**
* Sets the item to use when the button is set to disabled
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,33 @@ public void setHeight(int height) {
this.backgroundPane.setHeight(height);
}

/**
* Applies the contents of this variable bar onto the provided copy of this variable bar. This variable bar will not
* be modified.
*
* @param copy the copy of the variable bar
* @since 0.6.2
*/
protected void applyContents(@NotNull VariableBar copy) {
copy.x = x;
copy.y = y;
copy.length = length;
copy.height = height;
copy.setPriority(getPriority());

copy.setVisible(isVisible());
copy.onClick = onClick;

copy.setFillItem(fillPane.getItems().get(0).copy());
copy.setBackgroundItem(backgroundPane.getItems().get(0).copy());

copy.value = value;
copy.orientation = orientation;

copy.flipHorizontally = flipHorizontally;
copy.flipVertically = flipVertically;
}

@Override
public void setOrientation(@NotNull Orientation orientation) {
this.orientation = orientation;
Expand Down
Loading

0 comments on commit 5acf0b9

Please sign in to comment.