Skip to content

Commit

Permalink
Merge pull request #149 from Tanguygab/improved-has-item
Browse files Browse the repository at this point in the history
  • Loading branch information
BlitzOffline authored Nov 21, 2024
2 parents 8027cbe + e47da1c commit b18a43b
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 210 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.toLowerCase().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
13 changes: 13 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/hooks/BaseHeadHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public ItemStack getItem(@NotNull final String... arguments) {
return DeluxeMenus.getInstance().getHead().clone();
}

@Override
public boolean itemMatchesIdentifiers(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
String itemTexture = SkullUtils.getTextureFromSkull(item);
String texture = SkullUtils.decodeSkinUrl(arguments[0]);
if (itemTexture == null || texture == null) return false;

texture = texture.substring("https://textures.minecraft.net/texture/".length()-1);
return texture.equals(itemTexture);
}

@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 itemMatchesIdentifiers(@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 itemMatchesIdentifiers(@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 itemMatchesIdentifiers(@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 itemMatchesIdentifiers(@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 itemMatchesIdentifiers(@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 itemMatchesIdentifiers(@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
124 changes: 68 additions & 56 deletions src/main/java/com/extendedclip/deluxemenus/hooks/NamedHeadHook.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,68 @@
package com.extendedclip.deluxemenus.hooks;

import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.cache.SimpleCache;
import com.extendedclip.deluxemenus.utils.SkullUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public class NamedHeadHook implements ItemHook, Listener, SimpleCache {

private final Map<String, ItemStack> cache = new ConcurrentHashMap<>();

public NamedHeadHook(@NotNull final DeluxeMenus plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@Override
public ItemStack getItem(@NotNull final String... arguments) {
if (arguments.length == 0) {
return DeluxeMenus.getInstance().getHead().clone();
}

try {
return cache.computeIfAbsent(arguments[0], SkullUtils::getSkullByName).clone();
} catch (Exception exception) {
DeluxeMenus.printStacktrace(
"Something went wrong while trying to get a head by name" +
": " + arguments[0],
exception
);
}

return DeluxeMenus.getInstance().getHead().clone();
}

@EventHandler(ignoreCancelled = true)
public void onPlayerQuit(final PlayerQuitEvent event) {
cache.remove(event.getPlayer().getName());
}

@Override
public String getPrefix() {
return "head-";
}

@Override
public void clearCache() {
cache.clear();
}
}
package com.extendedclip.deluxemenus.hooks;

import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.cache.SimpleCache;
import com.extendedclip.deluxemenus.utils.SkullUtils;
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 {

private final Map<String, ItemStack> cache = new ConcurrentHashMap<>();

public NamedHeadHook(@NotNull final DeluxeMenus plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@Override
public ItemStack getItem(@NotNull final String... arguments) {
if (arguments.length == 0) {
return DeluxeMenus.getInstance().getHead().clone();
}

try {
return cache.computeIfAbsent(arguments[0], SkullUtils::getSkullByName).clone();
} catch (Exception exception) {
DeluxeMenus.printStacktrace(
"Something went wrong while trying to get a head by name" +
": " + arguments[0],
exception
);
}

return DeluxeMenus.getInstance().getHead().clone();
}

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

@EventHandler(ignoreCancelled = true)
public void onPlayerQuit(final PlayerQuitEvent event) {
cache.remove(event.getPlayer().getName());
}

@Override
public String getPrefix() {
return "head-";
}

@Override
public void clearCache() {
cache.clear();
}
}
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 itemMatchesIdentifiers(@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 @@ -31,6 +31,14 @@ public ItemStack getItem(@NotNull final String... arguments) {
return DeluxeMenus.getInstance().getHead().clone();
}

@Override
public boolean itemMatchesIdentifiers(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
return arguments[0].equals(SkullUtils.getTextureFromSkull(item));
}

@Override
public String getPrefix() {
return "texture-";
Expand Down
Loading

0 comments on commit b18a43b

Please sign in to comment.