Skip to content

Commit

Permalink
Add ability to show multiple materials in /mt mods
Browse files Browse the repository at this point in the history
  • Loading branch information
Flo56958 committed Oct 4, 2023
1 parent b374c02 commit e664b7e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
73 changes: 50 additions & 23 deletions src/main/java/de/flo56958/minetinker/api/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void show(@NotNull final Player player, final int page) {
throw new IllegalStateException("GUI (" + this.hashCode() + ") is closed.");
}

player.openInventory(windows.get(page).inventory);
windows.get(page).show(player);
}
}

Expand All @@ -143,7 +143,7 @@ public void show(@NotNull final Player player, final Window window) {
+ ") does not manage Window (" + window.hashCode() + ")!");
}

player.openInventory(window.inventory);
window.show(player);
}
}

Expand Down Expand Up @@ -183,22 +183,7 @@ public void close() {

isClosed = true;
for (Window w : windows) {
for (HumanEntity humanEntity : new ArrayList<>(w.getInventory().getViewers())) {
//new ArrayList is required as of ModificationException
humanEntity.closeInventory();
}

for (GUI.Window.Button button : w.buttonMap) {
if (button == null) continue;
for (ButtonAction action : button.actions.values()) {
if (action instanceof ButtonAction.PAGE_GOTO) {
GUI other = ((ButtonAction.PAGE_GOTO) action).window.gui;
if (!other.equals(this)) { //Close other GUIs
other.close();
}
}
}
}
w.close();
}

HandlerList.unregisterAll(this);
Expand Down Expand Up @@ -279,6 +264,9 @@ public static class Window {
private final Inventory inventory;
private final GUI gui;
private final Button[] buttonMap;
private Runnable showRunnable = null;
private int runnableRepeatTime = -1;
private int showRunnableTaskID;

/**
* Creates a new Window with the given size and the given title.
Expand Down Expand Up @@ -362,11 +350,6 @@ public Button addButton(final int slot, @NotNull final ItemStack item) {
return b;
}

@NotNull
public Inventory getInventory() {
return inventory;
}

@NotNull
public GUI getGUI() {
return gui;
Expand All @@ -377,6 +360,50 @@ public Button getButtonFromSlot(final int slot) {
return buttonMap[slot];
}

public void setShowRunnable(final Runnable runnable, final int repeatTime) {
this.showRunnable = runnable;
this.runnableRepeatTime = repeatTime;
}

public void show(final Player player) {
player.openInventory(this.inventory);

if (showRunnable != null && this.showRunnableTaskID == -1) {
this.showRunnableTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(this.gui.plugin, () -> {
if (inventory.getViewers().isEmpty()) {
Bukkit.getScheduler().cancelTask(this.showRunnableTaskID);
this.showRunnableTaskID = -1;
return;
}

this.showRunnable.run();
}, 0, runnableRepeatTime);
}
}

public void close() {
if (showRunnableTaskID != -1) {
Bukkit.getScheduler().cancelTask(this.showRunnableTaskID);
this.showRunnableTaskID = -1;
}
for (HumanEntity humanEntity : new ArrayList<>(this.inventory.getViewers())) {
//new ArrayList is required as of ModificationException
humanEntity.closeInventory();
}

for (GUI.Window.Button button : this.buttonMap) {
if (button == null) continue;
for (ButtonAction action : button.actions.values()) {
if (action instanceof ButtonAction.PAGE_GOTO) {
GUI other = ((ButtonAction.PAGE_GOTO) action).window.gui;
if (!other.equals(this.gui)) { //Close other GUIs
other.close();
}
}
}
}
}

/**
* This class is a Button for the Window. The button can be clicked by the User to trigger certain methods.
*/
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/de/flo56958/minetinker/data/GUIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
Expand Down Expand Up @@ -228,6 +229,7 @@ public static void reload() {
if (rec != null) {
GUI.Window modRecipe = modRecipes.addWindow(3, m.getColor() + m.getName());
if (rec instanceof ShapedRecipe srec) {
HashMap<GUI.Window.Button, RecipeChoice.MaterialChoice> choices = new HashMap<>();
ItemStack modItem = m.getModItem().clone();
DataHandler.setTag(modItem, "Showcase", (int) Math.round(Math.random() * 1000), PersistentDataType.INTEGER, false);
GUI.Window.Button result = modRecipe.addButton(6, 1, modItem);
Expand All @@ -251,7 +253,14 @@ public static void reload() {
ItemStack resItem = srec.getIngredientMap().get(c).clone();
DataHandler.setTag(resItem, "MT-MODSRecipeItem",
Math.round(Math.random() * 42), PersistentDataType.LONG, false);
modRecipe.addButton((slot % 3) + 2, (slot / 3), resItem);
final GUI.Window.Button recButton = modRecipe.addButton((slot % 3) + 2, (slot / 3), resItem);

// prepare runnable for multiple choices
if (srec.getChoiceMap().get(c) instanceof RecipeChoice.MaterialChoice mchoice) {
if (mchoice.getChoices().size() > 1) {
choices.put(recButton, mchoice);
}
}
} catch (NullPointerException ignored) {
}
}
Expand All @@ -260,6 +269,26 @@ public static void reload() {
slot++;
}
}

if (!choices.isEmpty()) {
final Runnable runnable = new Runnable() {
private final HashMap<GUI.Window.Button, RecipeChoice.MaterialChoice> map = choices;
private int counter = 0;
@Override
public void run() {
for (Map.Entry<GUI.Window.Button, RecipeChoice.MaterialChoice> entry : map.entrySet()) {
final List<Material> choices = entry.getValue().getChoices();
entry.getKey().getItemStack().setType(choices.get(counter % choices.size()));
}

counter++;
if (counter < 0) {
counter = 0;
}
}
};
modRecipe.setShowRunnable(runnable, 20);
}
}
modButton.addAction(ClickType.LEFT, new ButtonAction.PAGE_GOTO(modButton, modRecipe));
GUI.Window.Button returnButton = modRecipe.addButton(8, 2, backOtherMenuStack.clone());
Expand Down

0 comments on commit e664b7e

Please sign in to comment.