code made more readability

This commit is contained in:
NK 2022-11-16 22:35:49 +00:00
parent 1163229410
commit bcdf49e996
3 changed files with 98 additions and 94 deletions

View file

@ -21,20 +21,20 @@ public class WeaponCommand extends Command {
@Override @Override
public void onCommand(CommandSender sender, String[] args) { public void onCommand(CommandSender sender, String[] args) {
Player player = (Player) sender; Player player = (Player) sender;
StringBuilder s = new StringBuilder(); StringBuilder builder = new StringBuilder();
if (args.length == 0) { if (args.length == 0) {
player.sendMessage(ChatColor.RED + "This item doesn't exist"); player.sendMessage(ChatColor.RED + "This item doesn't exist");
} else { } else {
List<String> b = Arrays.asList(args); List<String> arglist = Arrays.asList(args);
for (String a : args) { for (String arg : args) {
if (a.equals(b.get(b.size() - 1))) { if (arg.equals(arglist.get(arglist.size() - 1))) {
s.append(a); builder.append(arg);
} else { } else {
s.append(a); builder.append(arg);
s.append(" "); builder.append(" ");
} }
} }
ItemStack item = CustomItemManager.getItem(s.toString()); ItemStack item = CustomItemManager.getItem(builder.toString());
if (item.hasItemMeta()) { if (item.hasItemMeta()) {
player.getInventory().addItem(item); player.getInventory().addItem(item);
} else { } else {

View file

@ -22,7 +22,7 @@ import java.util.*;
public class CustomItemManager { public class CustomItemManager {
private static HashMap<String, ItemStack> a = new HashMap<>(); private static final HashMap<String, ItemStack> weapons = new HashMap<>();
public static HashMap<String, NamespacedKey> keys = new HashMap<>(); public static HashMap<String, NamespacedKey> keys = new HashMap<>();
private static Main main; private static Main main;
public CustomItemManager(Main main) { public CustomItemManager(Main main) {
@ -38,99 +38,97 @@ public class CustomItemManager {
register(); register();
} }
public void register() { public void register() {
List<String> hh = getAllFilesFromDirectory("ItemData"); List<String> files = getAllFilesFromDirectory("ItemData");
for(int kk = 0; kk < hh.size(); kk++) { for (String file : files) {
FileConfiguration c = loadConfig("ItemData/" + hh.get(kk)); FileConfiguration fileConfig = loadConfig("ItemData/" + file);
ItemStack i = new ItemStack(Material.matchMaterial(c.getString("material"))); ItemStack item = new ItemStack(Material.matchMaterial(fileConfig.getString("material")));
List<String> pr = new ArrayList<>(); List<String> properties = new ArrayList<>();
List<String> ia = new ArrayList<>(); List<String> itemAbility = new ArrayList<>();
HashMap<String, Double> aa = new HashMap<>(); HashMap<String, Double> attributes = new HashMap<>();
HashMap<String, String> pdc = new HashMap<>(); for (String key : fileConfig.getKeys(true)) {
for (String key : c.getKeys(true)) {
if (key.startsWith("enchants.")) { if (key.startsWith("enchants.")) {
i.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(Arrays.asList(key.split("\\.")).get(1))), c.getInt(key)); item.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(Arrays.asList(key.split("\\.")).get(1))), fileConfig.getInt(key));
} else if (key.startsWith("lore.properties.")) { } else if (key.startsWith("lore.properties.")) {
String property = Arrays.asList(key.split("\\.")).get(2); String property = Arrays.asList(key.split("\\.")).get(2);
if (property.equals("damage") && c.getInt(key) > 0) { if (property.equals("damage") && fileConfig.getInt(key) > 0) {
pr.add(ChatColor.GRAY + "Damage: " + ChatColor.RED + "+" + c.getInt(key)); properties.add(ChatColor.GRAY + "Damage: " + ChatColor.RED + "+" + fileConfig.getInt(key));
} else if (property.equals("speed") && c.getInt(key) > 0) { } else if (property.equals("speed") && fileConfig.getInt(key) > 0) {
pr.add(ChatColor.GRAY + "Speed: " + ChatColor.RED + "+" + c.getInt(key)); properties.add(ChatColor.GRAY + "Speed: " + ChatColor.RED + "+" + fileConfig.getInt(key));
} }
} else if (key.startsWith("lore.ability.")) { } else if (key.startsWith("lore.ability.")) {
String a = Arrays.asList(key.split("\\.")).get(2); String paths = Arrays.asList(key.split("\\.")).get(2);
if (a.equals("name")) { if (paths.equals("name")) {
ia.add(ChatColor.GOLD + "Item Ability: " + c.getString(key)); itemAbility.add(ChatColor.GOLD + "Item Ability: " + fileConfig.getString(key));
} else if (a.equals("details")) { } else if (paths.equals("details")) {
for (Object line : c.getList(key)) { for (Object line : fileConfig.getList(key)) {
ia.add(ChatColor.GRAY.toString() + line); itemAbility.add(ChatColor.GRAY.toString() + line);
} }
} }
} else if (key.startsWith("attributes.")) { } else if (key.startsWith("attributes.")) {
if (c.getDouble(key) > 0.0) { if (fileConfig.getDouble(key) > 0.0) {
aa.put(Arrays.asList(key.split("\\.")).get(1), c.getDouble(key)); attributes.put(Arrays.asList(key.split("\\.")).get(1), fileConfig.getDouble(key));
} }
} else if (key.startsWith("recipe.")) { } else if (key.startsWith("recipe.")) {
} }
} }
ItemMeta im = i.getItemMeta(); ItemMeta itemMeta = item.getItemMeta();
im.setDisplayName(Rarity.getRarity(c.getString("rarity")).getColor() + c.getString("name")); itemMeta.setDisplayName(Rarity.getRarity(fileConfig.getString("rarity")).getColor() + fileConfig.getString("name"));
im.setUnbreakable(true); itemMeta.setUnbreakable(true);
ArrayList<String> l = new ArrayList<>(); ArrayList<String> lore = new ArrayList<>();
l.addAll(pr); lore.addAll(properties);
l.add(""); lore.add("");
ArrayList<String> e = new ArrayList<>(); ArrayList<String> enchantmentList = new ArrayList<>();
for (Enchantment ee : i.getEnchantments().keySet()) { for (Enchantment enchantment : item.getEnchantments().keySet()) {
List<String> splitted = Arrays.asList(Arrays.asList(ee.getKey().toString().split(":")).get(1).split("_")); List<String> splitted = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_"));
StringBuilder n = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (String ii : splitted) { for (String strings : splitted) {
String s = ii.substring(0, 1).toUpperCase() + ii.substring(1); String formatted = strings.substring(0, 1).toUpperCase() + strings.substring(1);
if (splitted.size() > 1) { if (splitted.size() > 1) {
if (ii.equals(splitted.get(splitted.size() - 1))) { if (strings.equals(splitted.get(splitted.size() - 1))) {
n.append(s); builder.append(formatted);
} else { } else {
n.append(s); builder.append(formatted);
n.append(" "); builder.append(" ");
} }
} else n.append(s); } else builder.append(formatted);
} }
e.add(n + " " + i.getEnchantmentLevel(ee)); enchantmentList.add(builder + " " + item.getEnchantmentLevel(enchantment));
} }
l.add(ChatColor.BLUE + String.join(", ", e)); lore.add(ChatColor.BLUE + String.join(", ", enchantmentList));
l.add(""); lore.add("");
l.addAll(ia); lore.addAll(itemAbility);
l.add(""); lore.add("");
l.add(Rarity.getRarity(c.getString("rarity")).getDisplay()); lore.add(Rarity.getRarity(fileConfig.getString("rarity")).getDisplay());
im.setLore(l); itemMeta.setLore(lore);
for (String q : aa.keySet()) { for (String attribute : attributes.keySet()) {
if (q.equals("damage")) { if (attribute.equals("damage")) {
AttributeModifier p = new AttributeModifier(UUID.randomUUID(), "generic.attackDamage", aa.get(q), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND); AttributeModifier p = new AttributeModifier(UUID.randomUUID(), "generic.attackDamage", attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
im.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, p); itemMeta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, p);
} else if (q.equals("moveSpeed")) { } else if (attribute.equals("moveSpeed")) {
AttributeModifier s = new AttributeModifier(UUID.randomUUID(), "generic.movementSpeed", aa.get(q), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND); AttributeModifier s = new AttributeModifier(UUID.randomUUID(), "generic.movementSpeed", attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
im.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED, s); itemMeta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED, s);
} }
} }
im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS); itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS);
for (String key : c.getKeys(true)) { for (String key : fileConfig.getKeys(true)) {
if (key.startsWith("pdc.")) { if (key.startsWith("pdc.")) {
String property = Arrays.asList(key.split("\\.")).get(1); String property = Arrays.asList(key.split("\\.")).get(1);
if (property.equals("ammo")) { if (property.equals("ammo")) {
PersistentDataContainer container = im.getPersistentDataContainer(); PersistentDataContainer container = itemMeta.getPersistentDataContainer();
NamespacedKey key1 = new NamespacedKey(main, "ammo"); NamespacedKey key1 = new NamespacedKey(main, "ammo");
keys.put(c.getString("name") + "." + property, key1); keys.put(fileConfig.getString("name") + "." + property, key1);
container.set(key1, PersistentDataType.INTEGER, c.getInt(key)); container.set(key1, PersistentDataType.INTEGER, fileConfig.getInt(key));
} } else if (property.equals("maxload")) {
else if (property.equals("maxload")) { PersistentDataContainer container = itemMeta.getPersistentDataContainer();
PersistentDataContainer container = im.getPersistentDataContainer();
NamespacedKey key2 = new NamespacedKey(main, "maxload"); NamespacedKey key2 = new NamespacedKey(main, "maxload");
keys.put(c.getString("name") + "." + property, key2); keys.put(fileConfig.getString("name") + "." + property, key2);
container.set(key2, PersistentDataType.INTEGER, c.getInt(key)); container.set(key2, PersistentDataType.INTEGER, fileConfig.getInt(key));
} }
} }
} }
i.setItemMeta(im); item.setItemMeta(itemMeta);
a.put(c.getString("name"), i); weapons.put(fileConfig.getString("name"), item);
} }
} }
public static void setItemRecipe(NamespacedKey key, ItemStack i, int ingredient, String shape1, String shape2, String shape3, List<Material> ingredients) { public static void setItemRecipe(NamespacedKey key, ItemStack i, int ingredient, String shape1, String shape2, String shape3, List<Material> ingredients) {
@ -186,8 +184,8 @@ public class CustomItemManager {
try { try {
File f = new File(main.getDataFolder(), path); File f = new File(main.getDataFolder(), path);
File[] files = f.listFiles(); File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) { for (File file : files) {
ns.add(files[i].getName()); ns.add(file.getName());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -195,20 +193,20 @@ public class CustomItemManager {
return ns; return ns;
} }
public void createFilesFromConfig(FileConfiguration config) { public void createFilesFromConfig(FileConfiguration config) {
for(String a : config.getKeys(false)) { for(String filename : config.getKeys(false)) {
FileConfiguration c = loadConfig("ItemData/" + a + ".yml"); FileConfiguration fileConfig = loadConfig("ItemData/" + filename + ".yml");
for(String b : config.getKeys(true)) { for(String key : config.getKeys(true)) {
if(b.startsWith(a)) { if(key.startsWith(filename)) {
List<String> d = new ArrayList<>(Arrays.asList(b.split("\\."))); List<String> paths = new ArrayList<>(Arrays.asList(key.split("\\.")));
if(d.size() != 1) { if(paths.size() != 1) {
d.remove(0); paths.remove(0);
if(d.size() == 1) { if(paths.size() == 1) {
c.set(d.get(0), config.get(b)); fileConfig.set(paths.get(0), config.get(key));
} else { } else {
c.set(String.join(".", d), config.get(b)); fileConfig.set(String.join(".", paths), config.get(key));
} }
try { try {
c.save(loadFile("ItemData/" + a + ".yml")); fileConfig.save(loadFile("ItemData/" + filename + ".yml"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -219,6 +217,6 @@ public class CustomItemManager {
} }
} }
public static ItemStack getItem(String name){ public static ItemStack getItem(String name){
return a.get(name); return weapons.get(name);
} }
} }

View file

@ -6,6 +6,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -21,7 +22,7 @@ public class CryptoMiner {
protected static int generated; protected static int generated;
protected long lastclaim; protected long lastclaim;
public CryptoMiner(Main main, String name, Material type, int level, double rate, long lastclaim) { public CryptoMiner(Main main, String name, Material type, int level, double rate, long lastclaim) {
this.main = main; CryptoMiner.main = main;
this.name = name; // Name of the miner this.name = name; // Name of the miner
this.type = type; // Material to mine this.type = type; // Material to mine
this.level = level; this.level = level;
@ -52,16 +53,15 @@ public class CryptoMiner {
public int getLevel() { public int getLevel() {
return level; return level;
} }
public void setLevel(int level) { this.level = level; }; public void setLevel(int level) { this.level = level; }
public long getLastclaim() { public long getLastclaim() {
return lastclaim; return lastclaim;
} }
public void setLastClaim(long lastclaim) { public void setLastClaim(long lastclaim) {
this.lastclaim = lastclaim; this.lastclaim = lastclaim;
} }
private int aa;
public static void generate(int pp, int times) { public static void generate(int pp, int times) {
for (int ia = 0; ia < times; ia++) { for (int counter = 0; counter < times; counter++) {
int count = ThreadLocalRandom.current().nextInt(100); int count = ThreadLocalRandom.current().nextInt(100);
if(count > pp) generated++; if(count > pp) generated++;
} }
@ -75,9 +75,15 @@ public class CryptoMiner {
} }
return arr; return arr;
} }
public static CryptoMiner getMiner(String index) { public static HashMap<String, Object> getMiner(String index) {
FileConfiguration file = loadConfig("miners.yml"); FileConfiguration file = loadConfig("miners.yml");
return new CryptoMiner(main, file.getString(index + ".name"), Material.matchMaterial(file.getString(index + ".name")), file.getInt(index + ".level"), file.getDouble(index + ".rate"), file.getLong(index + ".last-claim")); HashMap<String, Object> details = new HashMap<>();
details.put("name", file.getString(index + ".name"));
details.put("material", Material.matchMaterial(file.getString(index + ".material")));
details.put("level", file.getInt(index + ".level"));
details.put("rate", file.getDouble(index + ".rate"));
details.put("lastclaim", file.getLong(index + ".last-claim"));
return details;
} }
public void setMiner(String index, String name, String material, int level, double rate, long time) { public void setMiner(String index, String name, String material, int level, double rate, long time) {
FileConfiguration file = loadConfig("miners.yml"); FileConfiguration file = loadConfig("miners.yml");