Skip to content

Commit

Permalink
* Re-added deprecated old Menu stuff (Just so VanillaShop won't break)
Browse files Browse the repository at this point in the history
* Added customizable cancellation feature to Synchronous & Asynchronous task builders.
  • Loading branch information
Hempfest committed Mar 27, 2021
1 parent 19e7aab commit 50a3385
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.sanctum.Labyrinth</groupId>
<artifactId>Labyrinth</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
<packaging>jar</packaging>

<name>Labyrinth</name>
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/github/sanctum/labyrinth/Labyrinth.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.github.sanctum.labyrinth.data.AdvancedHook;
import com.github.sanctum.labyrinth.data.DefaultProvision;
import com.github.sanctum.labyrinth.data.EconomyProvision;
import com.github.sanctum.labyrinth.data.FileList;
import com.github.sanctum.labyrinth.data.FileManager;
import com.github.sanctum.labyrinth.data.VaultHook;
import com.github.sanctum.labyrinth.data.container.DataContainer;
import com.github.sanctum.labyrinth.library.Applicable;
Expand All @@ -13,7 +11,6 @@
import com.github.sanctum.labyrinth.library.SkullItem;
import com.github.sanctum.labyrinth.task.Schedule;
import com.github.sanctum.labyrinth.task.Synchronous;
import java.io.InputStream;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -74,7 +71,7 @@ public void onEnable() {
item.setItemMeta(meta);
new SkullItem(p.getUniqueId().toString(), item);
});

run(() -> new VaultHook(this)).applyAfter(() -> new AdvancedHook(this)).wait(2);
}

@Override
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/gui/GuiLibrary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.sanctum.labyrinth.gui;

import org.bukkit.entity.Player;

import java.util.UUID;

/**
* @deprecated Fully replaced by new functional menu building tools.
* Companion class to all menus. This is needed to pass information across the entire
* menu system no matter how many inventories are opened or closed.
*
* Each player has one of these objects, and only one.
*/
@Deprecated
public class GuiLibrary {
private final Player viewer;
private final UUID viewerID;
private String data;
private String data2;

public GuiLibrary(Player p) {
this.viewer = p;
this.viewerID = p.getUniqueId();
}

public Player getViewer() { return viewer; }

public UUID getUUID() {
return viewerID;
}

public void setData(String data) {
this.data = data;
}

public String getData() {
return data;
}

public void setData2(String data2) {
this.data2 = data2;
}

public String getData2() {
return data2;
}
}
133 changes: 133 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/gui/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.github.sanctum.labyrinth.gui;

import com.github.sanctum.labyrinth.Labyrinth;
import com.github.sanctum.labyrinth.formatting.string.ColoredString;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

import java.util.ArrayList;
import java.util.List;

/**
* @deprecated Fully replaced by {@link com.github.sanctum.labyrinth.gui.menuman.Menu}
*/
@Deprecated
public abstract class Menu implements InventoryHolder {

/*
* Defines the behavior and attributes of all menus in our plugin
*/

// Protected values that can be accessed in the menus
protected GuiLibrary guiLibrary;
protected Inventory inventory;
protected boolean isNew = Bukkit.getVersion().contains("1.16") || Bukkit.getVersion().contains("1.15") || Bukkit.getVersion().contains("1.14") || Bukkit.getVersion().contains("1.13");
protected ItemStack FILLER_GLASS = makeItem(isNew ? Material.getMaterial("BLACK_STAINED_GLASS_PANE") : Material.getMaterial("STAINED_GLASS_PANE"), " ");
protected ItemStack FILLER_GLASS_LIGHT = makeItem(isNew ? Material.LIGHT_BLUE_STAINED_GLASS_PANE : Material.getMaterial("STAINED_GLASS_PANE"),
new ColoredString("&7&oOther items will appear here.", ColoredString.ColorType.MC).toString());

// Constructor for Menu. Pass in a gui so that
// we have information on who's menu this is and
// what info is to be transfered
public Menu(GuiLibrary guiLibrary) {
this.guiLibrary = guiLibrary;
}

// let each menu decide their name
public abstract String getMenuName();

// let each menu decide their slot amount
public abstract int getSlots();

// let each menu decide how the items in the menu will be handled when clicked
public abstract void handleMenu(InventoryClickEvent e);

// let each menu decide what items are to be placed in the inventory menu
public abstract void setMenuItems();

// When called, an inventory is created and opened for the player
public void open() {
// The owner of the inventory created is the Menu itself,
// so we are able to reverse engineer the Menu object from the
// inventoryHolder in the MenuListener class when handling clicks
inventory = Bukkit.createInventory(this, getSlots(), getMenuName());

// grab all the items specified to be used for this menu and add to inventory
this.setMenuItems();

// open the inventory for the player
guiLibrary.getViewer().openInventory(inventory);
}

public void start() {
// The owner of the inventory created is the Menu itself,
// so we are able to reverse engineer the Menu object from the
// inventoryHolder in the MenuListener class when handling clicks
inventory = Bukkit.createInventory(this, InventoryType.ANVIL, getMenuName());

// grab all the items specified to be used for this menu and add to inventory
this.setMenuItems();

// open the inventory for the player
guiLibrary.getViewer().openInventory(inventory);
}


// Overridden method from the InventoryHolder interface
@Override
public Inventory getInventory() {
return inventory;
}

// Helpful utility method to fill all remaining slots with "filler glass"
public void setFillerGlass() {
for (int i = 0; i < getSlots(); i++) {
if (inventory.getItem(i) == null) {
inventory.setItem(i, FILLER_GLASS);
}
}
}

protected List<String> color(String... text) {
ArrayList<String> convert = new ArrayList<>();
for (String t : text) {
convert.add(new ColoredString(t, ColoredString.ColorType.MC).toString());
}
return convert;
}

public ItemStack makeItem(Material material, String displayName, String... lore) {

ItemStack item = new ItemStack(material);
ItemMeta itemMeta = item.getItemMeta();
assert itemMeta != null;
itemMeta.setDisplayName(new ColoredString(displayName, ColoredString.ColorType.MC).toString());

itemMeta.setLore(color(lore));
item.setItemMeta(itemMeta);

return item;
}

public ItemStack makePersistentItem(Material material, String displayName, String key, String data, String... lore) {

ItemStack item = new ItemStack(material);
ItemMeta itemMeta = item.getItemMeta();
assert itemMeta != null;
itemMeta.setDisplayName(new ColoredString(displayName, ColoredString.ColorType.MC).toString());
itemMeta.getPersistentDataContainer().set(new NamespacedKey(Labyrinth.getInstance(), key), PersistentDataType.STRING, data);
itemMeta.setLore(color(lore));
item.setItemMeta(itemMeta);

return item;
}

}
72 changes: 72 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/gui/Pagination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.sanctum.labyrinth.gui;

import org.bukkit.ChatColor;
import org.bukkit.Material;

/**
* @deprecated Fully replaced by functional paginaton builder. {@link com.github.sanctum.labyrinth.gui.builder.PaginatedBuilder}
*/
@Deprecated
public abstract class Pagination extends Menu {

// Keep track of what page the menu is on
protected int page = 0;
// 28 is max items because with the border set below,
// 28 empty slots are remaining.
protected int maxItemsPerPage = 28;
// the index represents the index of the slot
// that the loop is on
protected int index = 0;

public Pagination(GuiLibrary guiLibrary) {
super(guiLibrary);
}

// Set the border and menu buttons for the menu
public void addMenuBorder() {
inventory.setItem(48, makeItem(isNew ? Material.DARK_OAK_BUTTON : Material.getMaterial("WOODEN_BUTTON"), ChatColor.GREEN + "Left"));

inventory.setItem(49, makeItem(Material.BARRIER, ChatColor.DARK_RED + "Close"));

inventory.setItem(50, makeItem(isNew ? Material.DARK_OAK_BUTTON : Material.getMaterial("WOODEN_BUTTON"), ChatColor.GREEN + "Right"));

for (int i = 0; i < 10; i++) {
if (inventory.getItem(i) == null) {
inventory.setItem(i, super.FILLER_GLASS);
}
}

inventory.setItem(17, super.FILLER_GLASS);
inventory.setItem(18, super.FILLER_GLASS);
inventory.setItem(26, super.FILLER_GLASS);
inventory.setItem(27, super.FILLER_GLASS);
inventory.setItem(35, super.FILLER_GLASS);
inventory.setItem(36, super.FILLER_GLASS);

for (int i = 44; i < 54; i++) {
if (inventory.getItem(i) == null) {
inventory.setItem(i, super.FILLER_GLASS);
}
}
}

public void setFillerGlass() {
for (int i = 0; i < getSlots(); i++) {
if (inventory.getItem(i) == null) {
inventory.setItem(i, super.FILLER_GLASS);
}
}
}

public void setFillerGlassLight() {
for (int i = 0; i < getSlots(); i++) {
if (inventory.getItem(i) == null) {
inventory.setItem(i, super.FILLER_GLASS_LIGHT);
}
}
}

public int getMaxItemsPerPage() {
return maxItemsPerPage;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/task/Asynchronous.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ public class Asynchronous {
private Player p = null;
private Map<?, ?> map = null;
private Object o = null;
private TaskCancellation cancellation = null;

protected Asynchronous(Applicable applicable) {
this.runnable = new BukkitRunnable() {
@Override
public void run() {
try {
if (cancellation != null) {
cancellation.execute(new ScheduledTask(this));
return;
}
if (cancel == null) {
if (fallback) {
if (Bukkit.getOnlinePlayers().size() == 0) {
Expand Down Expand Up @@ -147,6 +152,17 @@ public Asynchronous cancelAfter(int count) {
return this;
}

/**
* Define your own running logic for cancellations.
*
* @param cancellation The cancellation objective.
* @return The same asynchronous task builder.
*/
public Asynchronous cancelAfter(TaskCancellation cancellation) {
this.cancellation = cancellation;
return this;
}

/**
* Automatically cancel the task if a target object goes missing from a map.
*
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/task/ScheduledTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.sanctum.labyrinth.task;

import org.bukkit.scheduler.BukkitRunnable;

public class ScheduledTask {

protected final BukkitRunnable runnable;

protected ScheduledTask(BukkitRunnable runnable) {
this.runnable = runnable;
}

public synchronized void cancel() {
this.runnable.cancel();
}

public synchronized boolean isCancelled() {
return this.runnable.isCancelled();
}

public synchronized int getId() {
return this.runnable.getTaskId();
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/task/Synchronous.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ public class Synchronous {
private boolean fallback;
private String cancel = null;
private Player p = null;
private TaskCancellation cancellation = null;

protected Synchronous(Applicable applicable) {
this.initial = applicable;
this.runnable = new BukkitRunnable() {
@Override
public void run() {
try {
if (cancellation != null) {
cancellation.execute(new ScheduledTask(this));
return;
}
if (cancel == null) {
if (fallback) {
if (Bukkit.getOnlinePlayers().size() == 0) {
Expand Down Expand Up @@ -154,6 +159,17 @@ public Synchronous cancelAfter(int count) {
return this;
}

/**
* Define your own running logic for cancellations.
*
* @param cancellation The cancellation objective.
* @return The same synchronous task builder.
*/
public Synchronous cancelAfter(TaskCancellation cancellation) {
this.cancellation = cancellation;
return this;
}

/**
* Automatically cancel the task if a target object goes missing from a map.
*
Expand Down

0 comments on commit 50a3385

Please sign in to comment.