Updated to 2.0.3 (some manual patches).

This commit is contained in:
CoderMarido 2018-08-23 20:51:59 +02:00
parent 1caf138d56
commit 02be1447fc
13 changed files with 501 additions and 471 deletions

View File

@ -8,8 +8,8 @@ Quality, performance, and support are my priorities for this resource. Purchase
Here is an example with built-in methods for developers that want to use the developers API to code other resources. Here is an example with built-in methods for developers that want to use the developers API to code other resources.
```ruby ```ruby
# Check if Heads is installed and enabled. # Check if Heads is installed and enabled.
if(Bukkit.getPluginManager().getPlugin("Heads") != null) { if (HeadsAPI.isEnabled()) {
Bukkit.broadcastMessage("Hooray!"); Hooray();
} }
# How you would get the heads in any way. # How you would get the heads in any way.

View File

@ -45,15 +45,17 @@ public class HeadNamer implements Listener {
return Heads.getMainConfig().shouldUseBlockStore() && Heads.isBlockStoreAvailable(); return Heads.getMainConfig().shouldUseBlockStore() && Heads.isBlockStoreAvailable();
} }
@SuppressWarnings("deprecation")
private boolean isHeadsHead(ItemStack item) { private boolean isHeadsHead(ItemStack item) {
if (!Items.isSkull(item)) if (!Items.isSkull(item))
return false; return false;
SkullMeta meta = (SkullMeta) item.getItemMeta(); SkullMeta meta = (SkullMeta) item.getItemMeta();
// This needs to be kept too since it will not work on 1.8 if changed.
return meta.hasOwner() && meta.getOwner().equals("SpigotHeadPlugin"); return meta.hasOwner() && meta.getOwner().equals("SpigotHeadPlugin");
} }
@SuppressWarnings("deprecation")
private boolean isHeadsHead(Block block) { private boolean isHeadsHead(Block block) {
BlockState state = block.getState(); BlockState state = block.getState();
if (!(state instanceof Skull)) if (!(state instanceof Skull))
@ -61,6 +63,7 @@ public class HeadNamer implements Listener {
Skull skull = (Skull) state; Skull skull = (Skull) state;
// This needs to be kept too since it will not work on 1.8 if changed.
return skull.getOwner() != null && skull.getOwner().equals("SpigotHeadPlugin"); return skull.getOwner() != null && skull.getOwner().equals("SpigotHeadPlugin");
} }

View File

@ -45,10 +45,12 @@ public class LegacyIDs {
} }
} }
@SuppressWarnings("deprecation")
public static LegacyIDs create() { public static LegacyIDs create() {
Map<Integer, String> idToType = new HashMap<>(); Map<Integer, String> idToType = new HashMap<>();
for (Material type : Material.values()) { for (Material type : Material.values()) {
// This need to be kept for the legacy IDS for 1.13.
idToType.put(type.getId(), type.name()); idToType.put(type.getId(), type.name());
} }

View File

@ -1,7 +1,28 @@
package net.sothatsit.heads; package net.sothatsit.heads;
import java.util.List;
import org.bukkit.Location;
public class LiveHead { public class LiveHead {
// TODO: Add animation heads with live (cached) texures in the next updates. private int frames;
private List<Heads> fases;
private Location location;
public LiveHead(int frames, List<Heads> fases, Location location /*.more.*/) {
// Safety first, experimental features should not crash servers.
if (frames > 60)
frames = 60;
this.frames = frames;
this.fases = fases;
this.location = location;
}
public void renderTexure() {
int interval = frames / 20;
// Render (but I am too tired for now).
// TODO: External classes from the animation packages.
}
} }

View File

@ -1,114 +1,120 @@
package net.sothatsit.heads.api; package net.sothatsit.heads.api;
import com.google.common.collect.ImmutableList;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.util.Checks;
import net.sothatsit.heads.volatilecode.TextureGetter;
import org.bukkit.inventory.ItemStack;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.bukkit.inventory.ItemStack;
import com.google.common.collect.ImmutableList;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.util.Checks;
import net.sothatsit.heads.volatilecode.TextureGetter;
public class HeadsAPI { public class HeadsAPI {
public static class Head { public static class Head {
private final CacheHead head; private final CacheHead head;
private Head(CacheHead head) { private Head(CacheHead head) {
Checks.ensureNonNull(head, "head"); Checks.ensureNonNull(head, "head");
this.head = head; this.head = head;
} }
public int getId() { public boolean isEnabled() {
return head.getId(); return Heads.getInstance() != null;
} }
public String getName() { public int getId() {
return head.getName(); return head.getId();
} }
public String getCategory() { public String getName() {
return head.getCategory(); return head.getName();
} }
public double getCost() { public String getCategory() {
return head.getCost(); return head.getCategory();
} }
public ItemStack getItem() { public double getCost() {
return head.getItemStack(); return head.getCost();
} }
public ItemStack getItem(String displayName) { public ItemStack getItem() {
return head.getItemStack(displayName); return head.getItemStack();
} }
private static Head fromCacheHead(CacheHead head) { public ItemStack getItem(String displayName) {
return (head == null ? null : new Head(head)); return head.getItemStack(displayName);
} }
private static Head fromNameAndTexture(String name, String texture) { private static Head fromCacheHead(CacheHead head) {
return (texture == null ? null : fromCacheHead(new CacheHead(name, "HeadsAPI", texture))); return (head == null ? null : new Head(head));
} }
private static List<Head> fromCacheHeads(List<CacheHead> heads) { private static Head fromNameAndTexture(String name, String texture) {
ImmutableList.Builder<Head> converted = ImmutableList.builder(); return (texture == null ? null : fromCacheHead(new CacheHead(name, "HeadsAPI", texture)));
}
for(CacheHead head : heads) { private static List<Head> fromCacheHeads(List<CacheHead> heads) {
converted.add(Head.fromCacheHead(head)); ImmutableList.Builder<Head> converted = ImmutableList.builder();
}
return converted.build(); for (CacheHead head : heads) {
} converted.add(Head.fromCacheHead(head));
}
} return converted.build();
}
public static Head getHead(int id) { }
CacheHead head = Heads.getCache().findHead(id);
if(head == null) public static Head getHead(int id) {
return null; CacheHead head = Heads.getCache().findHead(id);
return new Head(head); if (head == null)
} return null;
@Deprecated return new Head(head);
public static List<Head> searchHeads(String query) { }
List<CacheHead> search = Heads.getCache().searchHeads(query);
return Head.fromCacheHeads(search); @Deprecated
} public static List<Head> searchHeads(String query) {
List<CacheHead> search = Heads.getCache().searchHeads(query);
public static void searchHeads(String query, Consumer<List<Head>> onResult) { return Head.fromCacheHeads(search);
Heads.getCache().searchHeadsAsync(query, heads -> { }
onResult.accept(Head.fromCacheHeads(heads));
});
}
public static Set<String> getCategories() { public static void searchHeads(String query, Consumer<List<Head>> onResult) {
return Heads.getCache().getCategories(); Heads.getCache().searchHeadsAsync(query, heads -> {
} onResult.accept(Head.fromCacheHeads(heads));
});
}
public static List<Head> getCategoryHeads(String category) { public static Set<String> getCategories() {
List<CacheHead> categoryHeads = Heads.getCache().getCategoryHeads(category); return Heads.getCache().getCategories();
}
return Head.fromCacheHeads(categoryHeads); public static List<Head> getCategoryHeads(String category) {
} List<CacheHead> categoryHeads = Heads.getCache().getCategoryHeads(category);
public static List<Head> getAllHeads() { return Head.fromCacheHeads(categoryHeads);
List<CacheHead> heads = Heads.getCache().getHeads(); }
return Head.fromCacheHeads(heads); public static List<Head> getAllHeads() {
} List<CacheHead> heads = Heads.getCache().getHeads();
public static void downloadHead(String playerName, Consumer<Head> consumer) { return Head.fromCacheHeads(heads);
TextureGetter.getTexture(playerName, (texture) -> { }
consumer.accept(Head.fromNameAndTexture(playerName, texture));
}); public static void downloadHead(String playerName, Consumer<Head> consumer) {
} TextureGetter.getTexture(playerName, (texture) -> {
consumer.accept(Head.fromNameAndTexture(playerName, texture));
});
}
} }

View File

@ -1,5 +1,11 @@
package net.sothatsit.heads.command.admin; package net.sothatsit.heads.command.admin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import net.sothatsit.heads.Heads; import net.sothatsit.heads.Heads;
import net.sothatsit.heads.cache.CacheHead; import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.command.AbstractCommand; import net.sothatsit.heads.command.AbstractCommand;
@ -10,111 +16,105 @@ import net.sothatsit.heads.volatilecode.Items;
import net.sothatsit.heads.volatilecode.TextureGetter; import net.sothatsit.heads.volatilecode.TextureGetter;
import net.sothatsit.heads.volatilecode.reflection.Version; import net.sothatsit.heads.volatilecode.reflection.Version;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
public class HandCommand extends AbstractCommand { public class HandCommand extends AbstractCommand {
@Override @Override
public String getCommandLabel(MainConfig config) { public String getCommandLabel(MainConfig config) {
return config.getHandCommand(); return config.getHandCommand();
} }
@Override @Override
public String getPermission() { public String getPermission() {
return "heads.hand"; return "heads.hand";
} }
@Override @Override
public Lang.HelpSection getHelp() { public Lang.HelpSection getHelp() {
return Lang.Command.Hand.help(); return Lang.Command.Hand.help();
} }
@Override @Override
public boolean onCommand(final CommandSender sender, Command command, String label, final String[] args) { public boolean onCommand(final CommandSender sender, Command command, String label, final String[] args) {
if (!(sender instanceof Player)) { if (!(sender instanceof Player)) {
Lang.Command.Errors.mustBePlayer().send(sender); Lang.Command.Errors.mustBePlayer().send(sender);
return true; return true;
} }
if (args.length < 3) { if (args.length < 3) {
sendInvalidArgs(sender); sendInvalidArgs(sender);
return true; return true;
} }
StringBuilder nameBuilder = new StringBuilder(); StringBuilder nameBuilder = new StringBuilder();
for (int i = 2; i < args.length; i++) { for (int i = 2; i < args.length; i++) {
nameBuilder.append(' '); nameBuilder.append(' ');
nameBuilder.append(args[i]); nameBuilder.append(args[i]);
} }
String name = nameBuilder.toString().substring(1); String name = nameBuilder.toString().substring(1);
String category = args[1]; String category = args[1];
if (category.length() > 32) { if (category.length() > 32) {
Lang.Command.Hand.categoryLength(category).send(sender); Lang.Command.Hand.categoryLength(category).send(sender);
return true; return true;
} }
Player player = (Player) sender; Player player = (Player) sender;
ItemStack hand = player.getInventory().getItemInMainHand(); ItemStack hand = player.getInventory().getItemInMainHand();
if(!Items.isSkull(hand)) { if (!Items.isSkull(hand)) {
Lang.Command.Hand.notSkull().send(sender); Lang.Command.Hand.notSkull().send(sender);
return true; return true;
} }
String texture = ItemNBT.getTextureProperty(hand); String texture = ItemNBT.getTextureProperty(hand);
if (texture == null || texture.isEmpty()) { if (texture == null || texture.isEmpty()) {
Lang.Command.Hand.noTextureProperty().send(sender); Lang.Command.Hand.noTextureProperty().send(sender);
if (Version.v1_8.higherThan(Version.getVersion())) { if (Version.v1_8.higherThan(Version.getVersion())) {
Lang.Command.Hand.notSupported().send(sender); Lang.Command.Hand.notSupported().send(sender);
return true; return true;
} }
SkullMeta meta = (SkullMeta) hand.getItemMeta(); SkullMeta meta = (SkullMeta) hand.getItemMeta();
final String owner = meta.getOwner(); @SuppressWarnings("deprecation")
final String owner = meta.getOwner();
if (owner == null || owner.isEmpty()) { if (owner == null || owner.isEmpty()) {
Lang.Command.Hand.noNameProperty().send(sender); Lang.Command.Hand.noNameProperty().send(sender);
return true; return true;
} }
texture = TextureGetter.getCachedTexture(owner); texture = TextureGetter.getCachedTexture(owner);
if (texture == null || texture.isEmpty()) { if (texture == null || texture.isEmpty()) {
Lang.Command.Hand.fetching().send(sender); Lang.Command.Hand.fetching().send(sender);
TextureGetter.getTexture(owner, (resolvedTexture) -> { TextureGetter.getTexture(owner, (resolvedTexture) -> {
if (resolvedTexture == null || resolvedTexture.isEmpty()) { if (resolvedTexture == null || resolvedTexture.isEmpty()) {
Lang.Command.Hand.cantFind(owner).send(sender); Lang.Command.Hand.cantFind(owner).send(sender);
return; return;
} }
add(sender, category, name, resolvedTexture); add(sender, category, name, resolvedTexture);
}); });
return true; return true;
} }
} }
add(sender, category, name, texture); add(sender, category, name, texture);
return true; return true;
} }
public void add(CommandSender sender, String category, String name, String texture) {
CacheHead head = new CacheHead(name, category, texture);
Heads.getCache().addHead(head);
Heads.getInstance().saveCache();
Lang.Command.Hand.adding(name, category).send(sender);
}
public void add(CommandSender sender, String category, String name, String texture) {
CacheHead head = new CacheHead(name, category, texture);
Heads.getCache().addHead(head);
Heads.getInstance().saveCache();
Lang.Command.Hand.adding(name, category).send(sender);
}
} }

View File

@ -2,6 +2,13 @@ package net.sothatsit.heads.command.user;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import net.sothatsit.heads.cache.CacheHead; import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.command.AbstractCommand; import net.sothatsit.heads.command.AbstractCommand;
import net.sothatsit.heads.config.MainConfig; import net.sothatsit.heads.config.MainConfig;
@ -10,89 +17,82 @@ import net.sothatsit.heads.volatilecode.Items;
import net.sothatsit.heads.volatilecode.TextureGetter; import net.sothatsit.heads.volatilecode.TextureGetter;
import net.sothatsit.heads.volatilecode.reflection.Version; import net.sothatsit.heads.volatilecode.reflection.Version;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
public class GetCommand extends AbstractCommand { public class GetCommand extends AbstractCommand {
@Override @Override
public String getCommandLabel(MainConfig config) { public String getCommandLabel(MainConfig config) {
return config.getGetCommand(); return config.getGetCommand();
} }
@Override @Override
public String getPermission() { public String getPermission() {
return "heads.get"; return "heads.get";
} }
@Override @Override
public Lang.HelpSection getHelp() { public Lang.HelpSection getHelp() {
return Lang.Command.Get.help(); return Lang.Command.Get.help();
} }
@Override @SuppressWarnings("deprecation")
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { @Override
if (!(sender instanceof Player)) { public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
Lang.Command.Errors.mustBePlayer().send(sender); if (!(sender instanceof Player)) {
return true; Lang.Command.Errors.mustBePlayer().send(sender);
} return true;
}
if (args.length != 2) {
sendInvalidArgs(sender);
return true;
}
if (Version.v1_8.higherThan(Version.getVersion())) {
Lang.Command.Get.oldMethod().send(sender);
ItemStack head = Items.createSkull().build(); if (args.length != 2) {
SkullMeta meta = (SkullMeta) head.getItemMeta(); sendInvalidArgs(sender);
return true;
meta.setOwner(args[1]); }
meta.setDisplayName(Lang.Command.Get.headName(args[1]).getSingle());
head.setItemMeta(meta);
Lang.Command.Get.adding(args[1]).send(sender);
((Player) sender).getInventory().addItem(head);
return true;
}
String texture = TextureGetter.getCachedTexture(args[1]); if (Version.v1_8.higherThan(Version.getVersion())) {
Lang.Command.Get.oldMethod().send(sender);
if (texture != null) { ItemStack head = Items.createSkull().build();
giveHead((Player) sender, args[1], texture); SkullMeta meta = (SkullMeta) head.getItemMeta();
return true;
}
Lang.Command.Get.fetching().send(sender); meta.setOwner(args[1]);
meta.setDisplayName(Lang.Command.Get.headName(args[1]).getSingle());
final UUID uuid = ((Player) sender).getUniqueId(); head.setItemMeta(meta);
final String name = args[1];
TextureGetter.getTexture(name, (resolvedTexture) -> { Lang.Command.Get.adding(args[1]).send(sender);
giveHead(Bukkit.getPlayer(uuid), name, resolvedTexture); ((Player) sender).getInventory().addItem(head);
}); return true;
return true; }
}
public void giveHead(Player player, String name, String texture) { String texture = TextureGetter.getCachedTexture(args[1]);
if (player != null) {
if (texture == null || texture.isEmpty()) {
Lang.Command.Get.cantFind(name).send(player);
return;
}
CacheHead head = new CacheHead(name, "getcommand", texture); if (texture != null) {
giveHead((Player) sender, args[1], texture);
return true;
}
Lang.Command.Get.adding(name).send(player); Lang.Command.Get.fetching().send(sender);
player.getInventory().addItem(head.getItemStack()); final UUID uuid = ((Player) sender).getUniqueId();
} final String name = args[1];
}
TextureGetter.getTexture(name, (resolvedTexture) -> {
giveHead(Bukkit.getPlayer(uuid), name, resolvedTexture);
});
return true;
}
public void giveHead(Player player, String name, String texture) {
if (player != null) {
if (texture == null || texture.isEmpty()) {
Lang.Command.Get.cantFind(name).send(player);
return;
}
CacheHead head = new CacheHead(name, "getcommand", texture);
Lang.Command.Get.adding(name).send(player);
player.getInventory().addItem(head.getItemStack());
}
}
} }

View File

@ -1,95 +1,94 @@
package net.sothatsit.heads.config.menu; package net.sothatsit.heads.config.menu;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.config.ConfigFile;
import net.sothatsit.heads.config.FileConfigFile;
import net.sothatsit.heads.menu.CacheHeadsMenu;
import net.sothatsit.heads.menu.CategoriesMenu;
import net.sothatsit.heads.menu.HeadsMenu;
import net.sothatsit.heads.menu.ui.item.Item;
import net.sothatsit.heads.menu.ui.element.Scrollbar;
import net.sothatsit.heads.menu.ui.element.PagedBox;
import net.sothatsit.heads.util.Checks;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.config.ConfigFile;
import net.sothatsit.heads.menu.CacheHeadsMenu;
import net.sothatsit.heads.menu.CategoriesMenu;
import net.sothatsit.heads.menu.HeadsMenu;
import net.sothatsit.heads.menu.ui.element.PagedBox;
import net.sothatsit.heads.menu.ui.element.Scrollbar;
import net.sothatsit.heads.menu.ui.item.Item;
import net.sothatsit.heads.util.Checks;
public class MenuConfig { public class MenuConfig {
private final ConfigFile config; private final ConfigFile config;
private final AtomicBoolean requiresSave; private final AtomicBoolean requiresSave;
public MenuConfig(String fileName) { public MenuConfig(String fileName) {
this(Heads.getVersionedConfig(fileName)); this(Heads.getVersionedConfig(fileName));
} }
public MenuConfig(ConfigFile config) { public MenuConfig(ConfigFile config) {
Checks.ensureNonNull(config, "configFile"); Checks.ensureNonNull(config, "configFile");
this.config = config; this.config = config;
this.requiresSave = new AtomicBoolean(false); this.requiresSave = new AtomicBoolean(false);
} }
public void load() { public void load() {
config.copyDefaults(); config.copyDefaults();
config.reload(); config.reload();
requiresSave.set(false); requiresSave.set(false);
} }
public void saveIfChanged() { public void saveIfChanged() {
if(!requiresSave.get()) if (!requiresSave.get())
return; return;
config.save(); config.save();
} }
public Scrollbar.Template loadScrollbar(String key) { public Scrollbar.Template loadScrollbar(String key) {
Item left = config.getOrCopyDefault(key + ".left", Scrollbar.defaultLeft, requiresSave); Item left = config.getOrCopyDefault(key + ".left", Scrollbar.defaultLeft, requiresSave);
Item right = config.getOrCopyDefault(key + ".right", Scrollbar.defaultRight, requiresSave); Item right = config.getOrCopyDefault(key + ".right", Scrollbar.defaultRight, requiresSave);
Item noLeft = config.getOrCopyDefault(key + ".no-left", Scrollbar.defaultNoLeft, requiresSave); Item noLeft = config.getOrCopyDefault(key + ".no-left", Scrollbar.defaultNoLeft, requiresSave);
Item noRight = config.getOrCopyDefault(key + ".no-right", Scrollbar.defaultNoRight, requiresSave); Item noRight = config.getOrCopyDefault(key + ".no-right", Scrollbar.defaultNoRight, requiresSave);
Item filler = config.getOrCopyDefault(key + ".filler", Scrollbar.defaultFiller, requiresSave); Item filler = config.getOrCopyDefault(key + ".filler", Scrollbar.defaultFiller, requiresSave);
return new Scrollbar.Template(left, right, noLeft, noRight, filler); return new Scrollbar.Template(left, right, noLeft, noRight, filler);
} }
public PagedBox.Template loadPagedBox(String key) { public PagedBox.Template loadPagedBox(String key) {
Item unselected = config.getOrCopyDefault(key + ".unselected-page", PagedBox.defaultUnselected, requiresSave); Item unselected = config.getOrCopyDefault(key + ".unselected-page", PagedBox.defaultUnselected, requiresSave);
Item selected = config.getOrCopyDefault(key + ".selected-page", PagedBox.defaultSelected, requiresSave); Item selected = config.getOrCopyDefault(key + ".selected-page", PagedBox.defaultSelected, requiresSave);
Scrollbar.Template scrollbar = loadScrollbar(key + ".scrollbar"); Scrollbar.Template scrollbar = loadScrollbar(key + ".scrollbar");
return new PagedBox.Template(scrollbar, unselected, selected); return new PagedBox.Template(scrollbar, unselected, selected);
} }
public CategoriesMenu.Template loadCategoriesMenu(String key) { public CategoriesMenu.Template loadCategoriesMenu(String key) {
Item category = config.getOrCopyDefault(key + ".category", CategoriesMenu.defaultCategoryItem, requiresSave); Item category = config.getOrCopyDefault(key + ".category", CategoriesMenu.defaultCategoryItem, requiresSave);
PagedBox.Template pagedBoxTemplate = loadPagedBox(key); PagedBox.Template pagedBoxTemplate = loadPagedBox(key);
return new CategoriesMenu.Template(pagedBoxTemplate, category); return new CategoriesMenu.Template(pagedBoxTemplate, category);
} }
public HeadsMenu.Template loadHeadsMenu(String key) { public HeadsMenu.Template loadHeadsMenu(String key) {
Item head = config.getOrCopyDefault(key + ".head", HeadsMenu.defaultHead, requiresSave); Item head = config.getOrCopyDefault(key + ".head", HeadsMenu.defaultHead, requiresSave);
PagedBox.Template pagedBoxTemplate = loadPagedBox(key); PagedBox.Template pagedBoxTemplate = loadPagedBox(key);
return new HeadsMenu.Template(pagedBoxTemplate, head); return new HeadsMenu.Template(pagedBoxTemplate, head);
} }
public CacheHeadsMenu.Template loadCacheHeadsMenu(String key) { public CacheHeadsMenu.Template loadCacheHeadsMenu(String key) {
String categoriesTitle = config.getOrCopyDefault(key + ".categories-title", CacheHeadsMenu.defaultCategoriesTitle, requiresSave); String categoriesTitle = config.getOrCopyDefault(key + ".categories-title", CacheHeadsMenu.defaultCategoriesTitle, requiresSave);
String categoryTitle = config.getOrCopyDefault(key + ".category-title", CacheHeadsMenu.defaultCategoryTitle, requiresSave); String categoryTitle = config.getOrCopyDefault(key + ".category-title", CacheHeadsMenu.defaultCategoryTitle, requiresSave);
Item close = config.getOrCopyDefault(key + ".close", CacheHeadsMenu.defaultClose, requiresSave); Item close = config.getOrCopyDefault(key + ".close", CacheHeadsMenu.defaultClose, requiresSave);
Item back = config.getOrCopyDefault(key + ".back", CacheHeadsMenu.defaultBack, requiresSave); Item back = config.getOrCopyDefault(key + ".back", CacheHeadsMenu.defaultBack, requiresSave);
Item search = config.getOrCopyDefault(key + ".search", CacheHeadsMenu.defaultSearch, requiresSave); Item search = config.getOrCopyDefault(key + ".search", CacheHeadsMenu.defaultSearch, requiresSave);
CategoriesMenu.Template categoriesTemplate = loadCategoriesMenu(key + ".categories"); CategoriesMenu.Template categoriesTemplate = loadCategoriesMenu(key + ".categories");
HeadsMenu.Template headsTemplate = loadHeadsMenu(key + ".heads"); HeadsMenu.Template headsTemplate = loadHeadsMenu(key + ".heads");
return new CacheHeadsMenu.Template(categoriesTemplate, headsTemplate, close, back, search, categoriesTitle, categoryTitle); return new CacheHeadsMenu.Template(categoriesTemplate, headsTemplate, close, back, search, categoriesTitle, categoryTitle);
} }
} }

View File

@ -1,101 +1,98 @@
package net.sothatsit.heads.config.oldmenu; package net.sothatsit.heads.config.oldmenu;
import java.io.File;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.bukkit.configuration.ConfigurationSection;
import net.sothatsit.heads.Heads; import net.sothatsit.heads.Heads;
import net.sothatsit.heads.config.ConfigFile; import net.sothatsit.heads.config.ConfigFile;
import net.sothatsit.heads.util.Clock; import net.sothatsit.heads.util.Clock;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
public class MenuConfig { public class MenuConfig {
private final ConfigurationSection defaults;
private final ConfigFile configFile;
private final Map<String, Menu> menus;
private final Map<String, Menu> defaultMenus;
public MenuConfig(ConfigFile configFile) {
this.menus = new HashMap<>();
this.defaultMenus = new HashMap<>();
this.configFile = configFile; private final ConfigurationSection defaults;
this.defaults = loadDefaults(); private final ConfigFile configFile;
private final Map<String, Menu> menus;
private final Map<String, Menu> defaultMenus;
reload(); public MenuConfig(ConfigFile configFile) {
} this.menus = new HashMap<>();
this.defaultMenus = new HashMap<>();
public Menu getMenu(String name) { this.configFile = configFile;
Menu menu = menus.get(name.toLowerCase()); this.defaults = loadDefaults();
return (menu != null ? menu : defaultMenus.get(name.toLowerCase())); reload();
} }
public void reload() {
Clock timer = Clock.start();
configFile.copyDefaults();
configFile.reload();
String filename = configFile.getName(); public Menu getMenu(String name) {
ConfigurationSection config = configFile.getConfig(); Menu menu = menus.get(name.toLowerCase());
AtomicBoolean shouldSave = new AtomicBoolean(false);
menus.clear(); return (menu != null ? menu : defaultMenus.get(name.toLowerCase()));
}
for (String key : config.getKeys(false)) { public void reload() {
if (!config.isConfigurationSection(key)) { Clock timer = Clock.start();
Heads.warning("Unknown use of value " + key + " in " + filename);
continue;
}
ConfigurationSection menuSection = config.getConfigurationSection(key); configFile.copyDefaults();
configFile.reload();
Menu defaultMenu = defaultMenus.get(key.toLowerCase()); String filename = configFile.getName();
Menu menu = Menu.loadMenu(filename, menuSection, shouldSave, defaultMenu); ConfigurationSection config = configFile.getConfig();
AtomicBoolean shouldSave = new AtomicBoolean(false);
menus.put(key.toLowerCase(), menu);
}
for (String key : defaultMenus.keySet()) {
if(menus.containsKey(key))
continue;
config.set(key, defaults.getConfigurationSection(key)); menus.clear();
Heads.warning(key + " was missing in " + filename + ", creating it"); for (String key : config.getKeys(false)) {
shouldSave.set(true); if (!config.isConfigurationSection(key)) {
} Heads.warning("Unknown use of value " + key + " in " + filename);
continue;
if (shouldSave.get()) { }
configFile.save();
}
Heads.info("Loaded Menu Config with " + menus.size() + " Menus " + timer);
}
private ConfigurationSection loadDefaults() { ConfigurationSection menuSection = config.getConfigurationSection(key);
String filename = configFile.getName();
ConfigurationSection config = configFile.getDefaults();
AtomicBoolean shouldSave = new AtomicBoolean(false);
defaultMenus.clear(); Menu defaultMenu = defaultMenus.get(key.toLowerCase());
Menu menu = Menu.loadMenu(filename, menuSection, shouldSave, defaultMenu);
for (String key : config.getKeys(false)) { menus.put(key.toLowerCase(), menu);
if (!config.isConfigurationSection(key)) }
continue;
ConfigurationSection menuSection = config.getConfigurationSection(key); for (String key : defaultMenus.keySet()) {
if (menus.containsKey(key))
continue;
defaultMenus.put(key.toLowerCase(), Menu.loadMenu(filename, menuSection, shouldSave)); config.set(key, defaults.getConfigurationSection(key));
}
return config; Heads.warning(key + " was missing in " + filename + ", creating it");
} shouldSave.set(true);
}
if (shouldSave.get()) {
configFile.save();
}
Heads.info("Loaded Menu Config with " + menus.size() + " Menus " + timer);
}
private ConfigurationSection loadDefaults() {
String filename = configFile.getName();
ConfigurationSection config = configFile.getDefaults();
AtomicBoolean shouldSave = new AtomicBoolean(false);
defaultMenus.clear();
for (String key : config.getKeys(false)) {
if (!config.isConfigurationSection(key))
continue;
ConfigurationSection menuSection = config.getConfigurationSection(key);
defaultMenus.put(key.toLowerCase(), Menu.loadMenu(filename, menuSection, shouldSave));
}
return config;
}
} }

View File

@ -23,6 +23,7 @@ import net.sothatsit.heads.util.Stringify;
import net.sothatsit.heads.volatilecode.ItemNBT; import net.sothatsit.heads.volatilecode.ItemNBT;
import net.sothatsit.heads.volatilecode.reflection.Version; import net.sothatsit.heads.volatilecode.reflection.Version;
@SuppressWarnings("deprecation")
public final class Item { public final class Item {
private final Material type; private final Material type;
@ -205,6 +206,7 @@ public final class Item {
return new Item(type, 1, data, null, null, false); return new Item(type, 1, data, null, null, false);
} }
return new Item(type, 1, (short) 0, null, null, false); return new Item(type, 1, (short) 0, null, null, false);
// TODO: Needs manual testing on 1.13.
} }
public static Item create(ItemStack itemStack) { public static Item create(ItemStack itemStack) {
@ -361,6 +363,7 @@ public final class Item {
return Material.matchMaterial(typeName, true); return Material.matchMaterial(typeName, true);
} }
// Need to be kept, will look for an alternative in the future.
public static Material fromLegacyType(Material legacyType, byte data) { public static Material fromLegacyType(Material legacyType, byte data) {
return Bukkit.getUnsafe().fromLegacy(new MaterialData(legacyType, data)); return Bukkit.getUnsafe().fromLegacy(new MaterialData(legacyType, data));
} }

View File

@ -1,56 +1,55 @@
package net.sothatsit.heads.oldmenu.mode; package net.sothatsit.heads.oldmenu.mode;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.config.oldmenu.Menus;
import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.config.oldmenu.Menu;
import net.sothatsit.heads.config.lang.Lang;
import net.sothatsit.heads.economy.Economy;
import net.sothatsit.heads.oldmenu.ConfirmMenu;
import net.sothatsit.heads.oldmenu.HeadMenu;
import net.sothatsit.heads.oldmenu.InventoryType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.config.lang.Lang;
import net.sothatsit.heads.config.oldmenu.Menu;
import net.sothatsit.heads.config.oldmenu.Menus;
import net.sothatsit.heads.oldmenu.ConfirmMenu;
import net.sothatsit.heads.oldmenu.HeadMenu;
import net.sothatsit.heads.oldmenu.InventoryType;
public class GetMode extends BaseMode { public class GetMode extends BaseMode {
public GetMode(Player player) {
super(player);
Lang.Menu.Get.open().send(player);
}
@Override
public Menu getMenu(InventoryType type) {
return Menus.GET.fromType(type);
}
@Override
public void onHeadSelect(InventoryClickEvent e, HeadMenu menu, CacheHead head) {
Player player = getPlayer();
if(!Heads.getInstance().chargeForHead(player, head)) public GetMode(Player player) {
return; super(player);
Lang.Menu.Get.added(head.getName()).send(player); Lang.Menu.Get.open().send(player);
}
@Override
public Menu getMenu(InventoryType type) {
return Menus.GET.fromType(type);
}
@Override
public void onHeadSelect(InventoryClickEvent e, HeadMenu menu, CacheHead head) {
Player player = getPlayer();
if (!Heads.getInstance().chargeForHead(player, head))
return;
Lang.Menu.Get.added(head.getName()).send(player);
player.getInventory().addItem(head.getItemStack());
}
@Override
public void onConfirm(InventoryClickEvent e, ConfirmMenu menu, CacheHead head) {
// should not be reached
}
@Override
public boolean canOpenCategory(String category) {
if (getPlayer().hasPermission("heads.category." + category.toLowerCase().replace(' ', '_'))) {
return true;
} else {
Lang.Menu.Get.categoryPermission(category).send(getPlayer());
return false;
}
}
player.getInventory().addItem(head.getItemStack());
}
@Override
public void onConfirm(InventoryClickEvent e, ConfirmMenu menu, CacheHead head) {
// should not be reached
}
@Override
public boolean canOpenCategory(String category) {
if (getPlayer().hasPermission("heads.category." + category.toLowerCase().replace(' ', '_'))) {
return true;
} else {
Lang.Menu.Get.categoryPermission(category).send(getPlayer());
return false;
}
}
} }

View File

@ -1,63 +1,63 @@
package net.sothatsit.heads.oldmenu.mode; package net.sothatsit.heads.oldmenu.mode;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.config.oldmenu.Menus;
import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.config.oldmenu.Menu;
import net.sothatsit.heads.config.lang.Lang;
import net.sothatsit.heads.economy.Economy;
import net.sothatsit.heads.oldmenu.ConfirmMenu;
import net.sothatsit.heads.oldmenu.HeadMenu;
import net.sothatsit.heads.oldmenu.InventoryType;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import java.util.List; import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import net.sothatsit.heads.Heads;
import net.sothatsit.heads.cache.CacheHead;
import net.sothatsit.heads.config.lang.Lang;
import net.sothatsit.heads.config.oldmenu.Menu;
import net.sothatsit.heads.config.oldmenu.Menus;
import net.sothatsit.heads.oldmenu.ConfirmMenu;
import net.sothatsit.heads.oldmenu.HeadMenu;
import net.sothatsit.heads.oldmenu.InventoryType;
public class SearchMode extends BaseMode { public class SearchMode extends BaseMode {
public SearchMode(Player player, List<CacheHead> heads) { public SearchMode(Player player, List<CacheHead> heads) {
super(player, InventoryType.HEADS, "Search", heads); super(player, InventoryType.HEADS, "Search", heads);
} }
@Override
public Menu getMenu(InventoryType type) {
return Menus.SEARCH.heads();
}
public String getHeadId(CacheHead head) { @Override
if(!getPlayer().hasPermission("heads.category." + head.getCategory().toLowerCase().replace(' ', '_'))) { public Menu getMenu(InventoryType type) {
return "head-no-perms"; return Menus.SEARCH.heads();
} else { }
return (head.hasCost() && Heads.getMainConfig().isEconomyEnabled() ? "head-cost" : "head");
}
}
@Override
public void onHeadSelect(InventoryClickEvent e, HeadMenu menu, CacheHead head) {
Player player = getPlayer();
if (!player.hasPermission("heads.category." + head.getCategory().toLowerCase().replace(' ', '_'))) { public String getHeadId(CacheHead head) {
Lang.Menu.Search.categoryPermission(head.getCategory()).send(getPlayer()); if (!getPlayer().hasPermission("heads.category." + head.getCategory().toLowerCase().replace(' ', '_'))) {
return; return "head-no-perms";
} } else {
return (head.hasCost() && Heads.getMainConfig().isEconomyEnabled() ? "head-cost" : "head");
}
}
if(!Heads.getInstance().chargeForHead(player, head)) @Override
return; public void onHeadSelect(InventoryClickEvent e, HeadMenu menu, CacheHead head) {
Player player = getPlayer();
Lang.Menu.Search.added(head.getName()).send(player);
if (!player.hasPermission("heads.category." + head.getCategory().toLowerCase().replace(' ', '_'))) {
Lang.Menu.Search.categoryPermission(head.getCategory()).send(getPlayer());
return;
}
if (!Heads.getInstance().chargeForHead(player, head))
return;
Lang.Menu.Search.added(head.getName()).send(player);
player.getInventory().addItem(head.getItemStack());
}
@Override
public void onConfirm(InventoryClickEvent e, ConfirmMenu menu, CacheHead head) {
// should not be reached
}
@Override
public boolean canOpenCategory(String category) {
return true;
}
player.getInventory().addItem(head.getItemStack());
}
@Override
public void onConfirm(InventoryClickEvent e, ConfirmMenu menu, CacheHead head) {
// should not be reached
}
@Override
public boolean canOpenCategory(String category) {
return true;
}
} }

View File

@ -1,7 +1,7 @@
main: net.sothatsit.heads.Heads main: net.sothatsit.heads.Heads
author: Marido author: Marido
name: Heads name: Heads
version: 2.0.2 version: 2.0.3
api-version: 1.13 api-version: 1.13
softdepend: [Vault, PlayerPoints, BlockStore] softdepend: [Vault, PlayerPoints, BlockStore]
loadbefore: [DeluxeMenus] loadbefore: [DeluxeMenus]