Skip to content

Commit

Permalink
Check for items from supported ItemHooks (added MythicLib dependency …
Browse files Browse the repository at this point in the history
…for MMOItems)
  • Loading branch information
Tanguygab committed Nov 17, 2024
1 parent b8cefce commit a0ffd8d
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 9 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
compileOnly(libs.headdb)
compileOnly(libs.itemsadder)
compileOnly(libs.oraxen)
compileOnly(libs.mythiclib)
compileOnly(libs.mmoitems)
compileOnly(libs.score)

Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ authlib = "1.5.25"
headdb = "1.3.1"
itemsadder = "3.2.5"
oraxen = "1.159.0"
mmoitems = "6.9.4-SNAPSHOT"
mythiclib = "1.6.2-SNAPSHOT"
mmoitems = "6.9.5-SNAPSHOT"
papi = "2.11.6"
score = "4.23.10.8"

Expand All @@ -23,6 +24,7 @@ authlib = { module = "com.mojang:authlib", version.ref = "authlib" }
headdb = { module = "com.arcaniax:HeadDatabase-API", version.ref = "headdb" }
itemsadder = { module = "com.github.LoneDev6:api-itemsadder", version.ref = "itemsadder" }
oraxen = { module = "com.github.oraxen:oraxen", version.ref = "oraxen" }
mythiclib = { module = "io.lumine:MythicLib-dist", version.ref = "mythiclib"}
mmoitems = { module = "net.Indyuce:MMOItems-API", version.ref = "mmoitems" }
papi = { module = "me.clip:placeholderapi", version.ref = "papi" }
score = { module = "com.github.Ssomar-Developement:SCore", version.ref = "score" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,9 +1056,14 @@ private RequirementList getRequirements(FileConfiguration c, String path) {
case DOES_NOT_HAVE_ITEM:
ItemWrapper wrapper = new ItemWrapper();
if (c.contains(rPath + ".material")) {
String materialName = c.getString(rPath + ".material");
try {
if (!containsPlaceholders(c.getString(rPath + ".material")))
Material.valueOf(c.getString(rPath + ".material").toUpperCase());
if (!containsPlaceholders(materialName) && plugin.getItemHooks().values()
.stream()
.filter(x -> materialName.startsWith(x.getPrefix()))
.findFirst()
.orElse(null) == null)
Material.valueOf(materialName.toUpperCase());
} catch (Exception ex) {
DeluxeMenus.debug(
DebugLevel.HIGHEST,
Expand All @@ -1067,7 +1072,7 @@ private RequirementList getRequirements(FileConfiguration c, String path) {
);
break;
}
wrapper.setMaterial(c.getString(rPath + ".material"));
wrapper.setMaterial(materialName);
} else {
DeluxeMenus.debug(
DebugLevel.HIGHEST,
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/hooks/BaseHeadHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand All @@ -31,6 +32,15 @@ public ItemStack getItem(@NotNull final String... arguments) {
return DeluxeMenus.getInstance().getHead().clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
ItemStack skull = SkullUtils.getSkullByBase64EncodedTextureUrl(arguments[0]);
return Bukkit.getItemFactory().equals(item.getItemMeta(), skull.getItemMeta());
}

@Override
public String getPrefix() {
return "basehead-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public ItemStack getItem(@NotNull String... arguments) {
return (item == null) ? new ItemStack(Material.STONE) : item.clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
return false;
}

@Override
public String getPrefix() {
return "executableblocks-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import com.ssomar.score.api.executableitems.config.ExecutableItemInterface;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand All @@ -31,6 +33,16 @@ public ItemStack getItem(@NotNull String... arguments) {
return (item == null) ? new ItemStack(Material.STONE) : item.clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
ExecutableItemInterface fromId = ExecutableItemsAPI.getExecutableItemsManager().getExecutableItem(arguments[0]).orElse(null);
ExecutableItemInterface fromItem = ExecutableItemsAPI.getExecutableItemsManager().getExecutableItem(item).orElse(null);
return fromItem != null && fromItem.equals(fromId);
}

@Override
public String getPrefix() {
return "executableitems-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public ItemStack getItem(@NotNull final String... arguments) {
return DeluxeMenus.getInstance().getHead().clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
return arguments[0].equalsIgnoreCase(api.getItemID(item));
}

@Override
public String getPrefix() {
return "hdb-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public interface ItemHook {

ItemStack getItem(@NotNull final String... arguments);

boolean isItem(@NotNull ItemStack item, @NotNull final String... arguments);

String getPrefix();

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public ItemStack getItem(@NotNull final String... arguments) {
return item.clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
CustomStack stack = CustomStack.byItemStack(item);
return stack != null && stack.getId().equalsIgnoreCase(arguments[0]);
}

@Override
public String getPrefix() {
return "itemsadder-";
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/hooks/MMOItemsHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public ItemStack getItem(@NotNull final String... arguments) {
return mmoItem == null ? new ItemStack(Material.STONE, 1) : mmoItem;
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
String[] splitArgs = arguments[0].split(":");
if (splitArgs.length != 2) return false;
return splitArgs[0].equalsIgnoreCase(MMOItems.getTypeName(item)) && splitArgs[1].equalsIgnoreCase(MMOItems.getID(item));
}

@Override
public String getPrefix() {
return "mmoitems-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.extendedclip.deluxemenus.utils.VersionHelper;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.jetbrains.annotations.NotNull;

public class NamedHeadHook implements ItemHook, Listener, SimpleCache {
Expand Down Expand Up @@ -39,6 +43,25 @@ public ItemStack getItem(@NotNull final String... arguments) {
return DeluxeMenus.getInstance().getHead().clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
if (!(item.getItemMeta() instanceof SkullMeta)) return false;
final SkullMeta headMeta = (SkullMeta) item.getItemMeta();

String owner;
if (!VersionHelper.IS_SKULL_OWNER_LEGACY) {
if (headMeta.getOwningPlayer() == null) return false;
owner = headMeta.getOwningPlayer().getName();
} else {
owner = headMeta.getOwner();
if (owner == null) return false;
}
return arguments[0].equalsIgnoreCase(owner);
}

@EventHandler(ignoreCancelled = true)
public void onPlayerQuit(final PlayerQuitEvent event) {
cache.remove(event.getPlayer().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public ItemStack getItem(@NotNull String... arguments) {
return (item == null) ? new ItemStack(Material.STONE) : item.clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
return arguments[0].equalsIgnoreCase(OraxenItems.getIdByItem(item));
}

@Override
public String getPrefix() {
return "oraxen-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand All @@ -31,6 +32,15 @@ public ItemStack getItem(@NotNull final String... arguments) {
return DeluxeMenus.getInstance().getHead().clone();
}

@Override
public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
ItemStack skull = SkullUtils.getSkullByBase64EncodedTextureUrl(arguments[0]);
return Bukkit.getItemFactory().equals(item.getItemMeta(), skull.getItemMeta());
}

@Override
public String getPrefix() {
return "texture-";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.extendedclip.deluxemenus.requirement;

import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.hooks.ItemHook;
import com.extendedclip.deluxemenus.menu.MenuHolder;
import com.extendedclip.deluxemenus.requirement.wrappers.ItemWrapper;
import com.extendedclip.deluxemenus.utils.StringUtils;
Expand All @@ -25,8 +26,14 @@ public HasItemRequirement(ItemWrapper wrapper, boolean invert) {
public boolean evaluate(MenuHolder holder) {
String materialName = holder.setPlaceholdersAndArguments(wrapper.getMaterial()).toUpperCase();
Material material = DeluxeMenus.MATERIALS.get(materialName);
ItemHook pluginHook = null;
if (material == null) {
return invert;
pluginHook = DeluxeMenus.getInstance().getItemHooks().values()
.stream()
.filter(x -> materialName.startsWith(x.getPrefix()))
.findFirst()
.orElse(null);
if (pluginHook == null) return invert;
}

if (material == Material.AIR) return invert == (holder.getViewer().getInventory().firstEmpty() == -1);
Expand All @@ -37,29 +44,31 @@ public boolean evaluate(MenuHolder holder) {

int total = 0;
for (ItemStack itemToCheck: inventory) {
if (!isRequiredItem(itemToCheck, holder, material)) continue;
if (!isRequiredItem(itemToCheck, holder, material, pluginHook)) continue;
total += itemToCheck.getAmount();
}

if (offHand != null) {
for (ItemStack itemToCheck: offHand) {
if (!isRequiredItem(itemToCheck, holder, material)) continue;
if (!isRequiredItem(itemToCheck, holder, material, pluginHook)) continue;
total += itemToCheck.getAmount();
}
}

if (armor != null) {
for (ItemStack itemToCheck: armor) {
if (!isRequiredItem(itemToCheck, holder, material)) continue;
if (!isRequiredItem(itemToCheck, holder, material, pluginHook)) continue;
total += itemToCheck.getAmount();
}
}

return invert == (total < wrapper.getAmount());
}

private boolean isRequiredItem(ItemStack itemToCheck, MenuHolder holder, Material material) {
private boolean isRequiredItem(ItemStack itemToCheck, MenuHolder holder, Material material, ItemHook pluginHook) {
if (itemToCheck == null || itemToCheck.getType() == Material.AIR) return false;

if (pluginHook != null && !pluginHook.isItem(itemToCheck, holder.setPlaceholdersAndArguments(wrapper.getMaterial().substring(pluginHook.getPrefix().length())))) return false;
if (wrapper.getMaterial() != null && itemToCheck.getType() != material) return false;
if (wrapper.hasData() && itemToCheck.getDurability() != wrapper.getData()) return false;

Expand Down

0 comments on commit a0ffd8d

Please sign in to comment.