Small Fix

Added better Vault Error Description
Reverted Player Caching Change
This commit is contained in:
LikeWhat 2023-10-13 22:26:50 +02:00
parent 3faa8e26ff
commit fe5306e1d4
8 changed files with 58 additions and 41 deletions

View File

@ -249,7 +249,9 @@ public class CustomHeads extends JavaPlugin {
instance = this; instance = this;
pluginLogger = instance.getLogger(); pluginLogger = instance.getLogger();
if(DEV_BUILD) { if(DEV_BUILD) {
getServer().getConsoleSender().sendMessage("[CustomHeads]\n§e=============================================================================================\nThis is a Dev Version of the Plugin! Please update update as soon as a new Version gets released\n============================================================================================="); String txt = " This is a Dev Version of the Plugin! Please update update as soon as a public Version gets released ";
String line = Utils.repeatString("=", txt.length());
getServer().getConsoleSender().sendMessage("[CustomHeads]\n§e"+ line + "\n" + txt + "\n" + line);
} }
bukkitVersion = Bukkit.getVersion().substring(Bukkit.getVersion().lastIndexOf("(")); bukkitVersion = Bukkit.getVersion().substring(Bukkit.getVersion().lastIndexOf("("));

View File

@ -95,13 +95,15 @@ public class CategoryManager {
Permission categoryAllHeadsPermission = new Permission(category.getPermission() + ".all_heads"); Permission categoryAllHeadsPermission = new Permission(category.getPermission() + ".all_heads");
// Add Permissions from the Categories dynamically // Add Permissions from the Categories dynamically
pluginManager.addPermission(categoryBasePermission); if (pluginManager.getPermission(categoryBasePermission.getName()) == null) {
pluginManager.addPermission(categoryAllHeadsPermission); pluginManager.addPermission(categoryBasePermission);
categoryBasePermission.addParent(viewCategoryWildcard, true);
// Add the Category Permissions as Parent from the View Category Permission categoryBasePermission.addParent(categoryAllHeadsPermission, true);
categoryBasePermission.addParent(viewCategoryWildcard, true); }
categoryBasePermission.addParent(categoryAllHeadsPermission, true); if (pluginManager.getPermission(categoryAllHeadsPermission.getName()) == null) {
categoryAllHeadsPermission.addParent(viewCategoryWildcard, true); pluginManager.addPermission(categoryAllHeadsPermission);
categoryAllHeadsPermission.addParent(viewCategoryWildcard, true);
}
categoriesLoaded++; categoriesLoaded++;
if (category.hasSubCategories()) { if (category.hasSubCategories()) {

View File

@ -14,7 +14,7 @@ public interface EconomyHandler {
/** /**
* Whether the Plugin/Handler is currently able to handle incoming Economy Requests * Whether the Plugin/Handler is currently able to handle incoming Economy Requests
* Example: {@link de.likewhat.customheads.economy.handlers.Vault} * Example: {@link de.likewhat.customheads.economy.handlers.VaultHandler}
* @return true/false * @return true/false
*/ */
boolean isValid(); boolean isValid();

View File

@ -5,7 +5,7 @@ import de.likewhat.customheads.api.CustomHeadsPlayer;
import de.likewhat.customheads.category.Category; import de.likewhat.customheads.category.Category;
import de.likewhat.customheads.category.CustomHead; import de.likewhat.customheads.category.CustomHead;
import de.likewhat.customheads.economy.errors.InvalidEconomyHandlerException; import de.likewhat.customheads.economy.errors.InvalidEconomyHandlerException;
import de.likewhat.customheads.economy.handlers.Vault; import de.likewhat.customheads.economy.handlers.VaultHandler;
import lombok.Getter; import lombok.Getter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -28,7 +28,7 @@ public class EconomyManager {
public EconomyManager() throws InvalidEconomyHandlerException { public EconomyManager() throws InvalidEconomyHandlerException {
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) { if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
registerAndSetActiveHandler(new Vault()); registerAndSetActiveHandler(new VaultHandler());
} }
} }

View File

@ -1,5 +1,6 @@
package de.likewhat.customheads.economy.handlers; package de.likewhat.customheads.economy.handlers;
import de.likewhat.customheads.CustomHeads;
import de.likewhat.customheads.economy.EconomyCallback; import de.likewhat.customheads.economy.EconomyCallback;
import de.likewhat.customheads.economy.EconomyHandler; import de.likewhat.customheads.economy.EconomyHandler;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
@ -8,13 +9,15 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
public class Vault implements EconomyHandler { public class VaultHandler implements EconomyHandler {
private Economy economyPlugin; private Economy economyPlugin;
public Vault() { public VaultHandler() {
RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager().getRegistration(Economy.class); RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
if (economyProvider != null) { if (economyProvider == null) {
CustomHeads.getPluginLogger().warning("Vault Economy Service not found. Do you have a supported Economy Plugin installed?");
} else {
economyPlugin = economyProvider.getProvider(); economyPlugin = economyProvider.getProvider();
} }
} }

View File

@ -44,8 +44,8 @@ public class PlayerWrapper implements CustomHeadsPlayer {
this.player = player; this.player = player;
try { try {
JsonObject dataRoot = CustomHeads.getPlayerDataFile().getJson().getAsJsonObject(); JsonObject dataRoot = CustomHeads.getPlayerDataFile().getJson().getAsJsonObject();
searchHistory = new SearchHistory(player); this.searchHistory = new SearchHistory(player);
getHistory = new GetHistory(player); this.getHistory = new GetHistory(player);
if (dataRoot.has(player.getUniqueId().toString())) { if (dataRoot.has(player.getUniqueId().toString())) {
JsonObject uuidObject = dataRoot.getAsJsonObject(player.getUniqueId().toString()); JsonObject uuidObject = dataRoot.getAsJsonObject(player.getUniqueId().toString());
@ -57,13 +57,13 @@ public class PlayerWrapper implements CustomHeadsPlayer {
} else { } else {
editor.setOwner(textureValue); editor.setOwner(textureValue);
} }
savedHeads.add(editor.getItem()); this.savedHeads.add(editor.getItem());
}); });
if (uuidObject.has("unlockedCategories")) { if (uuidObject.has("unlockedCategories")) {
List<Category> categories = new ArrayList<>(); List<Category> categories = new ArrayList<>();
uuidObject.getAsJsonArray("unlockedCategories").forEach(categoryId -> categories.add(CustomHeads.getCategoryManager().getCategory(categoryId.getAsString()))); uuidObject.getAsJsonArray("unlockedCategories").forEach(categoryId -> categories.add(CustomHeads.getCategoryManager().getCategory(categoryId.getAsString())));
unlockedCategories = categories; this.unlockedCategories = categories;
} }
if (uuidObject.has("unlockedHeads")) { if (uuidObject.has("unlockedHeads")) {
@ -72,7 +72,7 @@ public class PlayerWrapper implements CustomHeadsPlayer {
String[] idParts = headId.getAsString().split(":"); String[] idParts = headId.getAsString().split(":");
heads.add(CustomHeads.getApi().getHead(CustomHeads.getCategoryManager().getCategory(idParts[0]), Integer.parseInt(idParts[1]))); heads.add(CustomHeads.getApi().getHead(CustomHeads.getCategoryManager().getCategory(idParts[0]), Integer.parseInt(idParts[1])));
}); });
unlockedHeads = heads; this.unlockedHeads = heads;
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -82,7 +82,11 @@ public class PlayerWrapper implements CustomHeadsPlayer {
} }
static CustomHeadsPlayer wrapPlayer(Player player) { static CustomHeadsPlayer wrapPlayer(Player player) {
return WRAPPED_PLAYERS_CACHE.getOrDefault(player.getUniqueId(), new PlayerWrapper(player)); if (WRAPPED_PLAYERS_CACHE.containsKey(player.getUniqueId())) {
return WRAPPED_PLAYERS_CACHE.get(player.getUniqueId());
} else {
return new PlayerWrapper(player);
}
} }
public static void clearCache() { public static void clearCache() {
@ -139,7 +143,7 @@ public class PlayerWrapper implements CustomHeadsPlayer {
} }
public List<Category> getUnlockedCategories(boolean ignorePermission) { public List<Category> getUnlockedCategories(boolean ignorePermission) {
return CustomHeads.getCategoryManager().getCategoryList().stream().filter(category -> (!ignorePermission && (Utils.hasPermission(player, category.getPermission()) || Utils.hasPermission(player, category.getPermission() + ".allheads"))) || unlockedCategories.contains(category)).collect(Collectors.toList()); return CustomHeads.getCategoryManager().getCategoryList().stream().filter(category -> (!ignorePermission && (Utils.hasPermission(this.player, category.getPermission()) || Utils.hasPermission(this.player, category.getPermission() + ".allheads"))) || this.unlockedCategories.contains(category)).collect(Collectors.toList());
} }
public Player unwrap() { public Player unwrap() {
@ -147,39 +151,29 @@ public class PlayerWrapper implements CustomHeadsPlayer {
} }
public boolean unlockCategory(Category category) { public boolean unlockCategory(Category category) {
if (unlockedCategories.contains(category)) { if (this.unlockedCategories.contains(category)) {
return false; return false;
} else { } else {
unlockedCategories.add(category); this.unlockedCategories.add(category);
return true; return true;
} }
} }
public boolean lockCategory(Category category) { public boolean lockCategory(Category category) {
if (unlockedCategories.contains(category)) { return unlockedCategories.remove(category);
unlockedCategories.remove(category);
return true;
} else {
return false;
}
} }
public boolean unlockHead(Category category, int id) { public boolean unlockHead(Category category, int id) {
if (unlockedHeads.contains(CustomHeads.getApi().getHead(category, id))) { if (this.unlockedHeads.contains(CustomHeads.getApi().getHead(category, id))) {
return false; return false;
} else { } else {
unlockedHeads.add(CustomHeads.getApi().getHead(category, id)); this.unlockedHeads.add(CustomHeads.getApi().getHead(category, id));
return true; return true;
} }
} }
public boolean lockHead(Category category, int id) { public boolean lockHead(Category category, int id) {
if (unlockedHeads.contains(CustomHeads.getApi().getHead(category, id))) { return this.unlockedHeads.remove(CustomHeads.getApi().getHead(category, id));
unlockedHeads.remove(CustomHeads.getApi().getHead(category, id));
return true;
} else {
return false;
}
} }
public boolean saveHead(String name, String texture) { public boolean saveHead(String name, String texture) {
@ -192,7 +186,7 @@ public class PlayerWrapper implements CustomHeadsPlayer {
} else { } else {
editor.setOwner(texture); editor.setOwner(texture);
} }
savedHeads.add(editor.getItem()); this.savedHeads.add(editor.getItem());
return true; return true;
} }
} }
@ -201,17 +195,17 @@ public class PlayerWrapper implements CustomHeadsPlayer {
if (!hasHead(name)) { if (!hasHead(name)) {
return false; return false;
} else { } else {
savedHeads.remove(getHead(name)); this.savedHeads.remove(getHead(name));
return true; return true;
} }
} }
public ItemStack getHead(String name) { public ItemStack getHead(String name) {
return savedHeads.stream().filter(itemStack -> Utils.toConfigString(itemStack.getItemMeta().getDisplayName()).equals(name)).iterator().next(); return this.savedHeads.stream().filter(itemStack -> Utils.toConfigString(itemStack.getItemMeta().getDisplayName()).equals(name)).iterator().next();
} }
public boolean hasHead(String name) { public boolean hasHead(String name) {
return savedHeads.stream().filter(itemStack -> Utils.toConfigString(itemStack.getItemMeta().getDisplayName()).equals(name)).iterator().hasNext(); return this.savedHeads.stream().filter(itemStack -> Utils.toConfigString(itemStack.getItemMeta().getDisplayName()).equals(name)).iterator().hasNext();
} }
} }

View File

@ -380,12 +380,14 @@ public class Utils {
if (baseCategory.isSubCategory()) { if (baseCategory.isSubCategory()) {
SubCategory subCategory = baseCategory.getAsSubCategory(); SubCategory subCategory = baseCategory.getAsSubCategory();
if (!customHeadsPlayer.getUnlockedCategories(CustomHeads.hasEconomy() && !CustomHeads.keepCategoryPermissions()).contains(subCategory.getOriginCategory())) { if (!customHeadsPlayer.getUnlockedCategories(CustomHeads.hasEconomy() && !CustomHeads.keepCategoryPermissions()).contains(subCategory.getOriginCategory())) {
CustomHeads.getPluginLogger().info("Player doesn't have this Sub-Category unlocked ("+subCategory.getName()+") Permissions ignored? " + (CustomHeads.hasEconomy() && !CustomHeads.keepCategoryPermissions()));
return; return;
} }
categoryHeads = subCategory.getHeads(); categoryHeads = subCategory.getHeads();
} else { } else {
Category category = baseCategory.getAsCategory(); Category category = baseCategory.getAsCategory();
if (!customHeadsPlayer.getUnlockedCategories(CustomHeads.hasEconomy() && !CustomHeads.keepCategoryPermissions()).contains(category)) { if (!customHeadsPlayer.getUnlockedCategories(CustomHeads.hasEconomy() && !CustomHeads.keepCategoryPermissions()).contains(category)) {
CustomHeads.getPluginLogger().info("Player doesn't have this Category unlocked ("+category.getName()+") Permissions ignored? " + (CustomHeads.hasEconomy() && !CustomHeads.keepCategoryPermissions()));
return; return;
} }
if (category.hasSubCategories()) { if (category.hasSubCategories()) {
@ -745,6 +747,14 @@ public class Utils {
return ""; return "";
} }
public static String repeatString(String string, int n) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < n; i++) {
result.append(string);
}
return result.toString();
}
public static int clamp(int min, int value, int max) { public static int clamp(int min, int value, int max) {
return Math.max(Math.min(value, min), max); return Math.max(Math.min(value, min), max);
} }

View File

@ -45,6 +45,11 @@
# Categories to the Cursor otherwise it will be added # # Categories to the Cursor otherwise it will be added #
# to your Inventory like /give # # to your Inventory like /give #
# --------------------------------------------------- # # --------------------------------------------------- #
# replaceHelmet: Whether or not to replace the Helmet #
# When Shift-Right clicking a Head #
# When false the Helmet will be returned into the #
# Inventory #
# --------------------------------------------------- #
# disabledCategories: A List of Categories which will # # disabledCategories: A List of Categories which will #
# be ignored by the Plugin # # be ignored by the Plugin #
# --------------------------------------------------- # # --------------------------------------------------- #
@ -82,6 +87,7 @@ update-notifications:
onJoin: true onJoin: true
console: true console: true
grabHeadToCursor: true grabHeadToCursor: true
replaceHelmet: false
disabledCategories: [] disabledCategories: []
langFile: none langFile: none
reducedDebug: true reducedDebug: true