Skip to content

Commit

Permalink
Add new API
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 5, 2023
1 parent 5dcebea commit 9e674ee
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
12 changes: 4 additions & 8 deletions src/main/java/net/citizensnpcs/api/CitizensAPI.java
Expand Up @@ -89,10 +89,6 @@ private static ClassLoader getImplementationClassLoader() {
return getImplementation().getOwningClassLoader();
}

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

public static LocationLookup getLocationLookup() {
return getImplementation().getLocationLookup();
}
Expand All @@ -111,6 +107,10 @@ public static NPCRegistry getNamedNPCRegistry(String name) {
return getImplementation().getNamedNPCRegistry(name);
}

public static NMSHelper getNMSHelper() {
return getImplementation().getNMSHelper();
}

public static Iterable<NPCRegistry> getNPCRegistries() {
return getImplementation().getNPCRegistries();
}
Expand Down Expand Up @@ -148,10 +148,6 @@ public static File getScriptFolder() {
return getImplementation().getScriptFolder();
}

public static SkullMetaProvider getSkullMetaProvider() {
return instance.getSkullMetaProvider();
}

/**
* Gets the current implementation's {@link SpeechFactory}.
*
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/net/citizensnpcs/api/CitizensPlugin.java
Expand Up @@ -44,8 +44,6 @@ public interface CitizensPlugin extends Plugin {
*/
public NPCSelector getDefaultNPCSelector();

public InventoryHelper getInventoryHelper();

/**
* Gets the Citizens {@link LocationLookup}
*
Expand All @@ -62,6 +60,8 @@ public interface CitizensPlugin extends Plugin {
*/
public NPCRegistry getNamedNPCRegistry(String name);

public NMSHelper getNMSHelper();

/**
* Get all registered {@link NPCRegistry}s.
*/
Expand All @@ -81,11 +81,6 @@ public interface CitizensPlugin extends Plugin {
*/
public File getScriptFolder();

/**
* @return The Skull ItemMeta provider
*/
public SkullMetaProvider getSkullMetaProvider();

/**
* Gets the SpeechFactory.
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/citizensnpcs/api/NMSHelper.java
@@ -0,0 +1,20 @@
package net.citizensnpcs.api;

import org.bukkit.OfflinePlayer;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.meta.SkullMeta;

public interface NMSHelper {
public OfflinePlayer getPlayer(BlockCommandSender sender);

public String getTexture(SkullMeta meta);

InventoryView openAnvilInventory(Player player, Inventory inventory, String title);

public void setTexture(String string, SkullMeta meta);

void updateInventoryTitle(Player player, InventoryView view, String newTitle);
}
15 changes: 10 additions & 5 deletions src/main/java/net/citizensnpcs/api/gui/InventoryMenu.java
Expand Up @@ -274,7 +274,7 @@ private void handleShiftClick(InventoryClickEvent event, Inventory dest, boolean
InventoryClickEvent e = new InventoryClickEvent(event.getView(), event.getSlotType(),
toNPC ? i : event.getRawSlot(), event.getClick(),
toNPC ? InventoryAction.PLACE_ALL : InventoryAction.PICKUP_ALL);
handleClick(e);
onInventoryClick(e);
if (toNPC) {
event.getView().setCursor(null);
}
Expand All @@ -297,7 +297,7 @@ private void handleShiftClick(InventoryClickEvent event, Inventory dest, boolean
}
InventoryClickEvent e = new InventoryClickEvent(event.getView(), event.getSlotType(),
toNPC ? i : event.getRawSlot(), event.getClick(), action);
handleClick(e);
onInventoryClick(e);
if (toNPC) {
event.getView().setCursor(null);
}
Expand All @@ -323,8 +323,13 @@ private boolean isOurInventory(Inventory other) {

@EventHandler(ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
if (page == null || transitioning || closingViews)
if (page == null || transitioning || closingViews) {
if (transitioning && isOurInventory(
event.getClickedInventory() != null ? event.getClickedInventory() : event.getInventory())) {
event.setCancelled(true);
}
return;
}
delayViewerChanges = true;
try {
handleClick(event);
Expand Down Expand Up @@ -354,7 +359,7 @@ public void onInventoryDrag(InventoryDragEvent event) {

private InventoryView openInventory(HumanEntity player, Inventory inventory, String title) {
if (inventory.getType() == InventoryType.ANVIL) {
return CitizensAPI.getInventoryHelper().openAnvilInventory((Player) player, inventory, title);
return CitizensAPI.getNMSHelper().openAnvilInventory((Player) player, inventory, title);
} else {
return player.openInventory(inventory);
}
Expand Down Expand Up @@ -594,7 +599,7 @@ private void transitionViewersToInventory(Inventory inventory) {

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

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/api/util/ItemStorage.java
Expand Up @@ -216,7 +216,7 @@ private static void deserialiseMeta(DataKey root, ItemStack res) {
}

if (root.keyExists("skull.texture") && !root.getString("skull.texture").isEmpty()) {
CitizensAPI.getSkullMetaProvider().setTexture(root.getString("skull.texture", ""), meta);
CitizensAPI.getNMSHelper().setTexture(root.getString("skull.texture", ""), meta);
}

res.setItemMeta(meta);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/citizensnpcs/api/util/Placeholders.java
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand All @@ -22,8 +23,13 @@
import net.citizensnpcs.api.trait.trait.Owner;

public class Placeholders {
private static OfflinePlayer getPlayer(BlockCommandSender sender) {
return CitizensAPI.getNMSHelper().getPlayer(sender);
}

public static String replace(String text, CommandSender sender, NPC npc) {
text = replace(text, sender instanceof OfflinePlayer ? (OfflinePlayer) sender : null);
text = replace(text, sender instanceof OfflinePlayer ? (OfflinePlayer) sender
: sender instanceof BlockCommandSender ? getPlayer((BlockCommandSender) sender) : null);
if (npc == null || text == null) {
return text;
}
Expand Down

0 comments on commit 9e674ee

Please sign in to comment.