diff --git a/Example/pom.xml b/Example/pom.xml index dd09a18..129b149 100644 --- a/Example/pom.xml +++ b/Example/pom.xml @@ -5,7 +5,7 @@ PowerLibAPI it.mycraft - 1.2.0-TEST-12 + 1.2.0 4.0.0 PowerLibExample @@ -60,7 +60,7 @@ it.mycraft PowerLib - 1.2.0-TEST-12 + 1.2.0 provided diff --git a/Java/pom.xml b/Java/pom.xml index 0fdc32e..6b4520a 100644 --- a/Java/pom.xml +++ b/Java/pom.xml @@ -5,7 +5,7 @@ PowerLibAPI it.mycraft - 1.2.0-TEST-12 + 1.2.0 4.0.0 PowerLib diff --git a/Java/src/main/java/it/mycraft/powerlib/bukkit/PowerLib.java b/Java/src/main/java/it/mycraft/powerlib/bukkit/PowerLib.java index 23bc4b7..1b9594d 100644 --- a/Java/src/main/java/it/mycraft/powerlib/bukkit/PowerLib.java +++ b/Java/src/main/java/it/mycraft/powerlib/bukkit/PowerLib.java @@ -1,5 +1,6 @@ package it.mycraft.powerlib.bukkit; +import it.mycraft.powerlib.bukkit.config.ConfigManager; import it.mycraft.powerlib.bukkit.updater.PluginUpdater; import it.mycraft.powerlib.common.chat.Message; import lombok.Getter; @@ -7,6 +8,7 @@ import net.md_5.bungee.api.chat.TextComponent; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -24,10 +26,15 @@ public class PowerLib extends JavaPlugin implements Listener { @Getter private static PowerLib instance; + @Getter + private ConfigManager configManager; + private PluginUpdater updater; public void onEnable() { instance = this; + this.configManager = new ConfigManager(this); + this.configManager.create("config.yml"); this.updater = new PluginUpdater(this).setGitHubURL("AlbeMiglio", "PowerLib"); Bukkit.getPluginManager().registerEvents(this, this); Metrics metrics = new Metrics(this, 11161); @@ -35,6 +42,9 @@ public void onEnable() { @EventHandler public void onJoin(PlayerJoinEvent event) { + if(!getConfig().getBoolean("check-for-updates")) { + return; + } Bukkit.getScheduler().runTaskAsynchronously(this, () -> { Player player = event.getPlayer(); if (!player.isOp() && !player.hasPermission("powerlib.update")) { @@ -52,4 +62,9 @@ public void onJoin(PlayerJoinEvent event) { } }); } + + @Override + public FileConfiguration getConfig() { + return this.configManager.get("config.yml"); + } } diff --git a/Java/src/main/java/it/mycraft/powerlib/bukkit/item/ItemBuilder.java b/Java/src/main/java/it/mycraft/powerlib/bukkit/item/ItemBuilder.java index fde37b2..aa0ae49 100644 --- a/Java/src/main/java/it/mycraft/powerlib/bukkit/item/ItemBuilder.java +++ b/Java/src/main/java/it/mycraft/powerlib/bukkit/item/ItemBuilder.java @@ -7,6 +7,7 @@ import it.mycraft.powerlib.bukkit.PowerLib; import it.mycraft.powerlib.common.utils.ColorAPI; import it.mycraft.powerlib.reflection.ReflectionAPI; +import lombok.Getter; import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.configuration.file.FileConfiguration; @@ -23,11 +24,11 @@ import java.lang.reflect.Field; import java.util.*; import java.util.logging.Level; +import java.util.stream.Collectors; +@Getter public class ItemBuilder { - //TODO: Add addEnchant method - private Material material; private String name; @@ -35,13 +36,15 @@ public class ItemBuilder { private int amount; private short metadata; private boolean glowing; - private Map enchantments; - private Map potion; + private HashMap enchantments; + private HashMap potions; + private HashMap placeholders; public ItemBuilder() { lore = new ArrayList<>(); enchantments = new HashMap<>(); - potion = new HashMap<>(); + potions = new HashMap<>(); + placeholders = new HashMap<>(); amount = 1; metadata = 0; } @@ -74,7 +77,7 @@ public ItemBuilder setMaterial(String material) { * Sets the item's material getting it from an int ID. 1.13+ * * @param id The ID - * @return The ItemBuilder + * @return The ItemBuilder */ public ItemBuilder setMaterial(int id) { this.material = LegacyItemAPI.getMaterial(id); @@ -86,7 +89,7 @@ public ItemBuilder setMaterial(int id) { * * @param id The ID * @param data The Data - * @return The ItemBuilder + * @return The ItemBuilder */ public ItemBuilder setMaterial(int id, int data) { this.material = LegacyItemAPI.getMaterial(id, data); @@ -190,7 +193,7 @@ public ItemBuilder setPotionEffect(PotionEffectType type, int duration, int leve * @return The ItemBuilder */ public ItemBuilder setPotionEffect(PotionEffectType type, int duration, int level, boolean overwrite, boolean ambient, boolean particles) { - potion.put(new PotionEffect(type, duration, (level - 1), overwrite, ambient), particles); + potions.put(new PotionEffect(type, duration, (level - 1), overwrite, ambient), particles); return this; } @@ -239,8 +242,9 @@ public ItemBuilder clone(ItemStack itemStack) { amount = itemStack.getAmount(); metadata = itemStack.getDurability(); - if (itemMeta.getDisplayName() != null) + if (itemMeta != null) { name = itemMeta.getDisplayName(); + } if (itemMeta.hasLore()) lore = itemMeta.getLore(); @@ -342,21 +346,23 @@ private ItemStack setColorToArmor(int red, int green, int blue, ItemBuilder item * @return The ItemBuilder */ public ItemBuilder addPlaceHolder(String placeholder, Object value) { - name = name.replaceAll(placeholder, value.toString()); - - List newLore = new ArrayList<>(); - lore.forEach((s) -> newLore.add(s.replaceAll(placeholder, value.toString()))); - - lore = newLore; + placeholders.put(placeholder, value); return this; } /** - * Builds an Itemstack with the data provided previously + * Builds an ItemStack with the data provided previously * * @return The ItemStack */ public ItemStack build() { + for(String placeholder : placeholders.keySet()) { + Object value = placeholders.get(placeholder); + setName(name.replace(placeholder, value.toString())); + + setLore(lore.stream().map((s) -> s.replace(placeholder, value.toString())) + .collect(Collectors.toList())); + } ItemStack itemStack = new ItemStack(material, amount, metadata); ItemMeta itemMeta = itemStack.getItemMeta(); @@ -380,15 +386,13 @@ public ItemStack build() { itemStack.setItemMeta(itemMeta); - if (material == Material.POTION && !potion.isEmpty()) { + if (material == Material.POTION && !potions.isEmpty()) { PotionMeta potionMeta = (PotionMeta) itemStack.getItemMeta(); - for (PotionEffect potionEffect : potion.keySet()) { - potionMeta.addCustomEffect(potionEffect, potion.get(potionEffect)); + for (PotionEffect potionEffect : potions.keySet()) { + potionMeta.addCustomEffect(potionEffect, potions.get(potionEffect)); } itemStack.setItemMeta(potionMeta); } - - reset(); return itemStack; } @@ -445,9 +449,9 @@ public ItemStack coloredArmorBuild(int red, int green, int blue) { * * @param fileConfiguration The file configuration to get the item's info from * @param path The section where the item's info are stored - * @return The built ItemStack + * @return The related ItemBuilder */ - public ItemStack fromConfig(FileConfiguration fileConfiguration, String path) { + public ItemBuilder fromConfig(FileConfiguration fileConfiguration, String path) { boolean legacy = false, glowing = false; String newPath, material = "STONE", name = null; List lore = null; @@ -489,30 +493,30 @@ public ItemStack fromConfig(FileConfiguration fileConfiguration, String path) { break; } } - return new ItemBuilder() - .setMaterial(material) - .setName(name) - .setLore(lore) - .setAmount(amount) - .setMetaData(metadata) - .setGlowing(glowing) - .build(); + return this.setMaterial(material) + .setName(name).setLore(lore) + .setAmount(amount).setMetaData(metadata) + .setGlowing(glowing); + } + + public ItemBuilder hex() { + setName(ColorAPI.hex(getName())); + setLore(ColorAPI.hex(getLore())); + return this; } /** * Just puts in the ItemBuilder object its default values */ private void reset() { - material = null; - name = null; - - lore = null; - enchantments = null; - potion = null; - + material = Material.STONE; + name = ""; + glowing = false; + lore = new ArrayList<>(); + enchantments = new HashMap<>(); + potions = new HashMap<>(); + placeholders = new HashMap<>(); amount = 1; metadata = 0; - - glowing = false; } } diff --git a/Java/src/main/java/it/mycraft/powerlib/bungee/PowerLib.java b/Java/src/main/java/it/mycraft/powerlib/bungee/PowerLib.java index c58804c..129ca32 100644 --- a/Java/src/main/java/it/mycraft/powerlib/bungee/PowerLib.java +++ b/Java/src/main/java/it/mycraft/powerlib/bungee/PowerLib.java @@ -1,7 +1,9 @@ package it.mycraft.powerlib.bungee; +import it.mycraft.powerlib.bungee.config.ConfigManager; import it.mycraft.powerlib.bungee.updater.PluginUpdater; import it.mycraft.powerlib.common.chat.Message; +import it.mycraft.powerlib.configuration.Configuration; import lombok.Getter; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.ClickEvent; @@ -24,10 +26,15 @@ public class PowerLib extends Plugin implements Listener { @Getter private static PowerLib instance; + @Getter + private ConfigManager configManager; + private PluginUpdater updater; public void onEnable() { instance = this; + this.configManager = new ConfigManager(this); + this.configManager.create("config.yml"); this.updater = new PluginUpdater(this).setGitHubURL("AlbeMiglio", "PowerLib"); getProxy().getPluginManager().registerListener(this, this); Metrics metrics = new Metrics(this, 11162); @@ -35,6 +42,9 @@ public void onEnable() { @EventHandler public void onJoin(ServerConnectedEvent event) { + if(!getConfig().getBoolean("check-for-updates")) { + return; + } ProxyServer.getInstance().getScheduler().runAsync(this, () -> { ProxiedPlayer player = event.getPlayer(); if (!player.hasPermission("powerlib.update")) { @@ -52,4 +62,8 @@ public void onJoin(ServerConnectedEvent event) { } }); } + + public Configuration getConfig() { + return this.configManager.get("config.yml"); + } } diff --git a/Java/src/main/java/it/mycraft/powerlib/bungee/config/ConfigManager.java b/Java/src/main/java/it/mycraft/powerlib/bungee/config/ConfigManager.java index 7f45c02..2952170 100644 --- a/Java/src/main/java/it/mycraft/powerlib/bungee/config/ConfigManager.java +++ b/Java/src/main/java/it/mycraft/powerlib/bungee/config/ConfigManager.java @@ -7,63 +7,71 @@ import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.HashMap; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; /** * @author AlbeMiglio - * @version 1.2.0-TEST-12 + * @version 1.2.0 */ public class ConfigManager { private HashMap configs; private Plugin plugin; + private File folder; + private File serverJar; + private File pluginJar; public ConfigManager(Plugin plugin) { this.plugin = plugin; this.configs = new HashMap<>(); - if (!this.plugin.getDataFolder().exists()) { - this.plugin.getDataFolder().mkdir(); + try { + serverJar = new File(Plugin.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + pluginJar = this.plugin.getFile(); + folder = this.plugin.getDataFolder(); + if (!folder.exists()) { + folder.mkdir(); + } + } + catch(URISyntaxException ex) { + ex.printStackTrace(); } } /** - * Gets a config file from the local Map and creates it if it doesn't exist + * Gets a config file from the local Map + * Returns NULL if the config file isn't inside the map! * * @param file The config file name * @return The related FileConfiguration */ public Configuration get(String file) { - if (this.configs.containsKey(file)) { - return this.configs.get(file); - } else { - return this.create(file); - } + return this.configs.getOrDefault(file, null); } /** - * Creates a file if it doesn't exist and then puts it into the local Map + * Same as #create(String,String) but source name equals to the new one * * @param file The config file name * @return The new file */ public Configuration create(String file, String source) { - File resourcePath = new File(this.plugin.getDataFolder() + "/" + file); + File resourcePath = new File(folder + "/" + file); if (!resourcePath.exists()) { - createYAML(resourcePath.getName(), source, false); - } else this.reload(file); + createYAML(file, source, false); + } + reload(file); return this.configs.get(file); } - /** - * Same as #create(String,String) but source name equals to the new one - * - * @param file The config file name - * @return The new file - */ public Configuration create(String file) { - return this.create(file, file); + return create(file, file); } /** @@ -78,7 +86,7 @@ public void save(String file) { } try { ConfigurationProvider.getProvider(YamlConfiguration.class) - .save(config, new File(this.plugin.getDataFolder() + "/" + file)); + .save(config, new File(folder + "/" + file)); } catch (IOException ex) { ex.printStackTrace(); } @@ -115,7 +123,7 @@ public void reloadAll() { private Configuration load(String file) { try { return ConfigurationProvider.getProvider(YamlConfiguration.class) - .load(new File(this.plugin.getDataFolder() + "/" + file)); + .load(new File(folder + "/" + file)); } catch (IOException ex) { ex.printStackTrace(); return null; @@ -141,14 +149,22 @@ private void put(String file, Configuration config) { */ private void createYAML(String resourcePath, String source, boolean replace) { try { - File file = new File(this.plugin.getDataFolder(), resourcePath); - if (!file.exists()) { - if (replace) { - Files.copy(this.plugin.getResourceAsStream(source), + File file = new File(folder + "/" + resourcePath); + if (!file.getParentFile().exists() || !file.exists()) { + file.getParentFile().mkdir(); + if (!file.exists()) { + file.createNewFile(); + } + boolean forcereplace = replace; + if(file.length() == 0) { + forcereplace = true; + } + if (forcereplace) { + Files.copy(getResourceAsStream(source), file.toPath(), StandardCopyOption.REPLACE_EXISTING); - } else Files.copy(this.plugin.getResourceAsStream(source), file.toPath()); + } else Files.copy(getResourceAsStream(source), file.toPath()); } - } catch (IOException e) { + } catch (IOException | ClassNotFoundException | URISyntaxException e) { e.printStackTrace(); } } @@ -157,5 +173,20 @@ private void createYAML(String resourcePath, boolean replace) { this.createYAML(resourcePath, resourcePath, replace); } -} + private InputStream getResourceAsStream(String name) throws ClassNotFoundException, URISyntaxException, IOException { + ZipFile file = new ZipFile(pluginJar); + ZipInputStream zip = new ZipInputStream(pluginJar.toURL().openStream()); + boolean stop = false; + while(!stop) { + ZipEntry e = zip.getNextEntry(); + if(e == null) { + stop = true; + } + else if(e.getName().equals(name)) { + return file.getInputStream(e); + } + } + return null; + } +} \ No newline at end of file diff --git a/Java/src/main/java/it/mycraft/powerlib/common/chat/Message.java b/Java/src/main/java/it/mycraft/powerlib/common/chat/Message.java index f732bd9..239d8e2 100644 --- a/Java/src/main/java/it/mycraft/powerlib/common/chat/Message.java +++ b/Java/src/main/java/it/mycraft/powerlib/common/chat/Message.java @@ -19,10 +19,13 @@ public Message() { this.message = ""; this.messages = new ArrayList<>(); } + public Message(String message, boolean color) { + this.message = color ? ColorAPI.color(message) : message; + this.messages = new ArrayList<>(); + } public Message(String message) { - this.message = ColorAPI.color(message); - this.messages = new ArrayList<>(); + this(message, true); } public Message(String... messages) { @@ -30,19 +33,21 @@ public Message(String... messages) { this.messages = new ArrayList<>(ColorAPI.color(Arrays.asList(messages))); } - public Message(List messages) { + public Message(List messages, boolean color) { this.message = ""; - this.messages = new ArrayList<>(ColorAPI.color(messages)); + this.messages = color ? new ArrayList<>(ColorAPI.color(messages)) : messages; + } + + public Message(List messages) { + this(messages, true); } public Message addPlaceHolder(String placeholder, Object value) { message = message.replace(placeholder, value.toString()); - - List newMessages = new ArrayList<>(); - messages.forEach((s) -> newMessages.add(s.replace(placeholder, value.toString()))); - - messages = newMessages; - + for(int i = 0; i < messages.size(); i++) { + String current = messages.get(i); + messages.set(i, current.replace(placeholder, value.toString())); + } return this; } diff --git a/Java/src/main/java/it/mycraft/powerlib/velocity/PowerLib.java b/Java/src/main/java/it/mycraft/powerlib/velocity/PowerLib.java index b479f9d..a1262bf 100644 --- a/Java/src/main/java/it/mycraft/powerlib/velocity/PowerLib.java +++ b/Java/src/main/java/it/mycraft/powerlib/velocity/PowerLib.java @@ -10,6 +10,8 @@ import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import it.mycraft.powerlib.common.chat.Message; +import it.mycraft.powerlib.configuration.Configuration; +import it.mycraft.powerlib.velocity.config.ConfigManager; import it.mycraft.powerlib.velocity.updater.PluginUpdater; import lombok.Getter; import net.kyori.adventure.text.Component; @@ -20,7 +22,7 @@ import java.nio.file.Path; @Getter -@Plugin(id = "powerlib", name = "PowerLib", version = "1.2.0-TEST-12", authors = {"AlbeMiglio", "pompiere1"}) +@Plugin(id = "powerlib", name = "PowerLib", version = "1.2.0", authors = {"AlbeMiglio", "pompiere1"}) public class PowerLib { @Getter @@ -32,6 +34,7 @@ public class PowerLib { @Getter private static PowerLib instance; private PluginUpdater updater; + private ConfigManager configManager; @Inject public void init(ProxyServer proxy, PluginDescription description, Logger logger, Metrics.Factory metricsFactory, @@ -46,12 +49,17 @@ public void init(ProxyServer proxy, PluginDescription description, Logger logger @Subscribe public void onEnable(ProxyInitializeEvent event) { + this.configManager = new ConfigManager(description); + this.configManager.create("config.yml"); this.updater = new PluginUpdater(description.getVersion().get()).setGitHubURL("AlbeMiglio", "PowerLib"); Metrics metrics = metricsFactory.make(this, 11190); } @Subscribe public void onServerConnected(ServerPostConnectEvent ev) { + if(!getConfig().getBoolean("check-for-updates")) { + return; + } getProxy().getScheduler().buildTask(this, () -> { Player player = ev.getPlayer(); if (!player.hasPermission("powerlib.update")) { @@ -68,4 +76,8 @@ public void onServerConnected(ServerPostConnectEvent ev) { } }).schedule(); } + + public Configuration getConfig() { + return this.configManager.get("config.yml"); + } } diff --git a/Java/src/main/java/it/mycraft/powerlib/velocity/config/ConfigManager.java b/Java/src/main/java/it/mycraft/powerlib/velocity/config/ConfigManager.java index 16570ee..23da4da 100644 --- a/Java/src/main/java/it/mycraft/powerlib/velocity/config/ConfigManager.java +++ b/Java/src/main/java/it/mycraft/powerlib/velocity/config/ConfigManager.java @@ -66,7 +66,8 @@ public Configuration create(String file, String source) { File resourcePath = new File(folder + "/" + file); if (!resourcePath.exists()) { createYAML(file, source, false); - } else this.reload(file); + } + reload(file); return this.configs.get(file); } diff --git a/Java/src/main/resources/bungee.yml b/Java/src/main/resources/bungee.yml index d47d4d7..7627845 100644 --- a/Java/src/main/resources/bungee.yml +++ b/Java/src/main/resources/bungee.yml @@ -1,4 +1,5 @@ name: PowerLib +author: AlbeMiglio authors: [AlbeMiglio, pompiere1] -main: it.mycraft.powerlib.bukkit.PowerLib -version: 1.2.0-TEST-12 \ No newline at end of file +main: it.mycraft.powerlib.bungee.PowerLib +version: 1.2.0 \ No newline at end of file diff --git a/Java/src/main/resources/config.yml b/Java/src/main/resources/config.yml new file mode 100644 index 0000000..0f27c3e --- /dev/null +++ b/Java/src/main/resources/config.yml @@ -0,0 +1,4 @@ +# +# PowerLib v1.2.0 +# +check-for-updates: true \ No newline at end of file diff --git a/Java/src/main/resources/plugin.yml b/Java/src/main/resources/plugin.yml index d47d4d7..3334739 100644 --- a/Java/src/main/resources/plugin.yml +++ b/Java/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ name: PowerLib authors: [AlbeMiglio, pompiere1] main: it.mycraft.powerlib.bukkit.PowerLib -version: 1.2.0-TEST-12 \ No newline at end of file +version: 1.2.0 \ No newline at end of file diff --git a/README.md b/README.md index 49783b1..040b8db 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ it.mycraft PowerLib - 1.2.0-TEST-12 + 1.2.0 ``` diff --git a/pom.xml b/pom.xml index 60c43b1..4569b13 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ it.mycraft PowerLibAPI pom - 1.2.0-TEST-12 + 1.2.0 Example Java