Skip to content

Commit

Permalink
Add new API; disable shift click in inventory GUIs
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jun 11, 2022
1 parent 4a98b52 commit b1ae6ed
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/main/java/net/citizensnpcs/api/CitizensAPI.java
Expand Up @@ -84,6 +84,10 @@ private static ClassLoader getImplementationClassLoader() {
return getImplementation().getOwningClassLoader();
}

public static InventoryHelper getInventoryHelper() {
return getImplementation().getInventoryHelper();
}

/**
* Retrieves the {@link NPCRegistry} previously created via {@link #createNamedNPCRegistry(String, NPCDataStore)}
* with the given name, or null if not found.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/citizensnpcs/api/CitizensPlugin.java
Expand Up @@ -41,6 +41,8 @@ public interface CitizensPlugin extends Plugin {
*/
public NPCSelector getDefaultNPCSelector();

public InventoryHelper getInventoryHelper();

/**
*
* @param name
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/citizensnpcs/api/InventoryHelper.java
@@ -0,0 +1,8 @@
package net.citizensnpcs.api;

import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;

public interface InventoryHelper {
void updateInventoryTitle(Player player, InventoryView view, String newTitle);
}
38 changes: 37 additions & 1 deletion src/main/java/net/citizensnpcs/api/gui/InputMenu.java
@@ -1,21 +1,57 @@
package net.citizensnpcs.api.gui;

import java.util.function.Consumer;
import java.util.function.Supplier;

import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.google.common.base.Function;

@Menu(type = InventoryType.ANVIL)
public class InputMenu extends InventoryMenuPage {
private final Function<String, Boolean> callback;
private MenuContext ctx;
private final Supplier<String> initialValue;
@MenuSlot(slot = { 0, 0 }, material = Material.PAPER, amount = 1)
private InventoryMenuSlot slot;

public InputMenu(Supplier<String> initialValue, Function<String, Boolean> callback) {
this.initialValue = initialValue;
this.callback = callback;
}

@ClickHandler(slot = { 0, 0 })
public void cancel(InventoryMenuSlot slot, CitizensInventoryClickEvent evt) {
evt.setCancelled(true);
ctx.getMenu().transitionBack();
}

@Override
public void initialise(MenuContext ctx) {
this.ctx = ctx;
ItemStack item = slot.getCurrentItem();
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(initialValue.get());
item.setItemMeta(meta);
}

@ClickHandler(slot = { 0, 1 })
@ClickHandler(slot = { 0, 2 })
public void save(InventoryMenuSlot slot, CitizensInventoryClickEvent evt) {
evt.setCancelled(true);
if (callback.apply(slot.getCurrentItem().getItemMeta().getDisplayName())) {
ctx.getMenu().transitionBack();
} else {
evt.setCancelled(true);
}
}

public static InputMenu setter(Supplier<String> initialValue, Consumer<String> callback) {
return new InputMenu(initialValue, (s) -> {
callback.accept(s);
return true;
});
}
}
20 changes: 17 additions & 3 deletions src/main/java/net/citizensnpcs/api/gui/InventoryMenu.java
Expand Up @@ -197,6 +197,8 @@ private int getInventorySize(InventoryType type, int[] dim) {
}

private void handleShiftClick(InventoryClickEvent event, Inventory dest, boolean toNPC) {
if (event.getCurrentItem() == null)
return;
int amount = event.getCurrentItem().getAmount();
ItemStack merging = new ItemStack(event.getCurrentItem().clone());
ItemStack[] contents = dest.getContents();
Expand Down Expand Up @@ -259,10 +261,11 @@ public void onInventoryClick(InventoryClickEvent event) {
Inventory dest = event.getInventory() == event.getClickedInventory() ? event.getWhoClicked().getInventory()
: page.ctx.getInventory();
boolean toNPC = dest == page.ctx.getInventory();
if ((event.getCursor() == null || event.getCursor().getType() == Material.AIR)) {
if (false) {
// TODO
handleShiftClick(event, dest, toNPC);
return;
}
return;
}
if (!clicked.equals(page.ctx.getInventory()))
return;
Expand Down Expand Up @@ -374,6 +377,12 @@ public void run() {
page.page.run();
}

public void setTitle(String newTitle) {
for (InventoryView view : views) {
CitizensAPI.getInventoryHelper().updateInventoryTitle((Player) view.getPlayer(), view, newTitle);
}
}

/**
* Transition to another page. Adds the previous page to a stack which will be returned to when the current page is
* closed.
Expand Down Expand Up @@ -516,7 +525,9 @@ private void transitionViewersToInventory(Inventory inventory) {
view.close();
if (!view.getPlayer().isValid() || inventory == null)
continue;
views.add(view.getPlayer().openInventory(inventory));
InventoryView newview = view.getPlayer().openInventory(inventory);
// TODO: fix anvil inventory
views.add(newview);
}
}

Expand Down Expand Up @@ -558,6 +569,8 @@ public InventoryMenuInfo(Class<?> clazz) {
}

public InventoryMenuPage createInstance() {
if (constructor == null)
throw new RuntimeException("no constructor provided");
try {
return constructor.newInstance();
} catch (Exception e) {
Expand Down Expand Up @@ -675,6 +688,7 @@ private static void cacheInfo(Class<? extends InventoryMenuPage> clazz) {
Constructor<? extends InventoryMenuPage> found = clazz.getDeclaredConstructor();
found.setAccessible(true);
info.constructor = found;
} catch (NoSuchMethodException e2) {
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
38 changes: 0 additions & 38 deletions src/main/java/net/citizensnpcs/api/gui/ModalMenuInput.java

This file was deleted.

0 comments on commit b1ae6ed

Please sign in to comment.