Refactored utils

This commit is contained in:
Auxilor 2020-11-29 12:32:55 +00:00
parent f0e722124a
commit d748127893
29 changed files with 120 additions and 123 deletions

View File

@ -16,7 +16,7 @@ import com.willfp.ecoenchants.mmo.enchants.stamina.Endurance;
import com.willfp.ecoenchants.mmo.enchants.stamina.Fortitude;
import com.willfp.ecoenchants.mmo.enchants.stamina.Motivate;
import com.willfp.ecoenchants.mmo.structure.MMOEnchant;
import com.willfp.ecoenchants.util.Logger;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
public class MMOMain extends Extension {

View File

@ -3,7 +3,7 @@ package com.willfp.ecoenchants;
import com.comphenix.protocol.ProtocolManager;
import com.willfp.ecoenchants.extensions.loader.EcoExtensionLoader;
import com.willfp.ecoenchants.extensions.loader.ExtensionLoader;
import com.willfp.ecoenchants.util.Loader;
import com.willfp.ecoenchants.util.internal.Loader;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

View File

@ -5,7 +5,7 @@ import com.willfp.ecoenchants.command.AbstractCommand;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.util.Logger;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;

View File

@ -2,7 +2,7 @@ package com.willfp.ecoenchants.command.commands;
import com.willfp.ecoenchants.command.AbstractCommand;
import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.util.Loader;
import com.willfp.ecoenchants.util.internal.Loader;
import org.bukkit.command.CommandSender;
import java.util.List;

View File

@ -4,6 +4,7 @@ import com.willfp.ecoenchants.command.AbstractCommand;
import com.willfp.ecoenchants.command.AbstractTabCompleter;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.util.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.util.StringUtil;
@ -25,13 +26,6 @@ public class TabCompleterEnchantinfo extends AbstractTabCompleter {
enchantsNames.addAll(EcoEnchants.getAll().stream().filter(EcoEnchant::isEnabled).map(EcoEnchant::getName).collect(Collectors.toList()));
}
private static String removePrefix(String s, String prefix) {
if (s != null && prefix != null && s.startsWith(prefix)) {
return s.substring(prefix.length());
}
return s;
}
@Override
public List<String> onTab(CommandSender sender, List<String> args) {
List<String> completions = new ArrayList<>();
@ -48,7 +42,7 @@ public class TabCompleterEnchantinfo extends AbstractTabCompleter {
finishedArgs.remove(args.size() - 1);
String prefix = String.join(" ", finishedArgs);
completions = completions.stream().map(enchantName -> removePrefix(enchantName, prefix).trim()).collect(Collectors.toList());
completions = completions.stream().map(enchantName -> StringUtils.removePrefix(enchantName, prefix).trim()).collect(Collectors.toList());
}
Collections.sort(completions);

View File

@ -1,7 +1,7 @@
package com.willfp.ecoenchants.config;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.util.Logger;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

View File

@ -1,7 +1,7 @@
package com.willfp.ecoenchants.config;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.util.Logger;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

View File

@ -6,7 +6,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
import com.willfp.ecoenchants.util.Logger;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;

View File

@ -84,7 +84,7 @@ public class EnchantDisplay {
* @param item The item to revert
* @return The item, updated
*/
public static ItemStack revertDisplay(ItemStack item) {
public static ItemStack revertDisplay(final ItemStack item) {
if(item == null || !EnchantmentTarget.ALL.getMaterials().contains(item.getType()) || item.getItemMeta() == null) return item;
ItemMeta meta = item.getItemMeta();

View File

@ -4,7 +4,7 @@ import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.util.Logger;
import com.willfp.ecoenchants.util.internal.Logger;
import org.apache.commons.lang.WordUtils;
import org.bukkit.enchantments.Enchantment;

View File

@ -3,7 +3,7 @@ package com.willfp.ecoenchants.enchantments.ecoenchants.normal;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;

View File

@ -2,7 +2,7 @@ package com.willfp.ecoenchants.enchantments.ecoenchants.normal;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;

View File

@ -5,7 +5,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager;
import com.willfp.ecoenchants.util.DurabilityUtils;
import com.willfp.ecoenchants.util.VectorShapes;
import com.willfp.ecoenchants.util.VectorUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -53,9 +53,9 @@ public class Farmhand extends EcoEnchant {
Vector[] vecs;
if (this.getConfig().getBool(EcoEnchants.CONFIG_LOCATION + "use-cube")) {
vecs = VectorShapes.getCube(radius);
vecs = VectorUtils.getCube(radius);
} else {
vecs = VectorShapes.getSquare(radius);
vecs = VectorUtils.getSquare(radius);
}
if (!this.getConfig().getBool(EcoEnchants.CONFIG_LOCATION + "per-block-damage")) {

View File

@ -4,7 +4,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.events.entitydeathbyentity.EntityDeathByEntityEvent;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;

View File

@ -3,7 +3,7 @@ package com.willfp.ecoenchants.enchantments.ecoenchants.normal;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import com.willfp.ecoenchants.util.tuplets.Pair;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;

View File

@ -6,7 +6,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.integrations.anticheat.AnticheatManager;
import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager;
import com.willfp.ecoenchants.util.VectorShapes;
import com.willfp.ecoenchants.util.VectorUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@ -36,7 +36,7 @@ public class MagmaWalker extends EcoEnchant {
if (!EnchantChecks.boots(player, this)) return;
Vector[] circle = VectorShapes.getCircle(this.getConfig().getInt(EcoEnchants.CONFIG_LOCATION + "initial-radius")
Vector[] circle = VectorUtils.getCircle(this.getConfig().getInt(EcoEnchants.CONFIG_LOCATION + "initial-radius")
+ (this.getConfig().getInt(EcoEnchants.CONFIG_LOCATION + "per-level-radius") * EnchantChecks.getBootsLevel(player, this) - 1));
AnticheatManager.exemptPlayer(player);

View File

@ -3,7 +3,7 @@ package com.willfp.ecoenchants.enchantments.ecoenchants.normal;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.WitherSkeleton;

View File

@ -5,7 +5,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.nms.TridentStack;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import com.willfp.ecoenchants.util.NumberUtils;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;

View File

@ -3,7 +3,7 @@ package com.willfp.ecoenchants.enchantments.ecoenchants.normal;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import com.willfp.ecoenchants.util.NumberUtils;
import org.bukkit.GameMode;
import org.bukkit.Material;

View File

@ -7,7 +7,7 @@ import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.events.entitydeathbyentity.EntityDeathByEntityEvent;
import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager;
import com.willfp.ecoenchants.nms.TridentStack;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;

View File

@ -3,7 +3,7 @@ package com.willfp.ecoenchants.enchantments.ecoenchants.normal;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.util.DropQueue;
import com.willfp.ecoenchants.util.internal.DropQueue;
import com.willfp.ecoenchants.util.NumberUtils;
import org.bukkit.GameMode;
import org.bukkit.Material;

View File

@ -2,7 +2,7 @@ package com.willfp.ecoenchants.listeners;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.util.UpdateChecker;
import com.willfp.ecoenchants.util.internal.UpdateChecker;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

View File

@ -55,4 +55,11 @@ public class StringUtils {
return c.stream().map(String::valueOf).collect(Collectors.joining(", "));
} else return String.valueOf(object);
}
public static String removePrefix(String s, String prefix) {
if (s != null && prefix != null && s.startsWith(prefix)) {
return s.substring(prefix.length());
}
return s;
}
}

View File

@ -1,87 +0,0 @@
package com.willfp.ecoenchants.util;
import org.bukkit.util.Vector;
import java.util.ArrayList;
public class VectorShapes {
/**
* Get circle as relative vectors
* @param radius The radius
* @return An array of {@link Vector}s
*/
public static Vector[] getCircle(int radius) {
ArrayList<Vector> circleVecs = new ArrayList<>();
int xoffset = -radius;
int zoffset = -radius;
while (zoffset <= radius) {
while (xoffset <= radius) {
if (Math.round(Math.sqrt((xoffset * xoffset) + (zoffset * zoffset))) <= radius) {
circleVecs.add(new Vector(xoffset, 0, zoffset));
} else {
xoffset++;
continue;
}
xoffset++;
}
xoffset = -radius;
zoffset++;
}
return circleVecs.toArray(new Vector[0]);
}
/**
* Get square as relative vectors
* @param radius The radius of the square
* @return An array of {@link Vector}s
*/
public static Vector[] getSquare(int radius) {
ArrayList<Vector> circleVecs = new ArrayList<>();
int xoffset = -radius;
int zoffset = -radius;
while (zoffset <= radius) {
while (xoffset <= radius) {
circleVecs.add(new Vector(xoffset, 0, zoffset));
xoffset++;
}
xoffset = -radius;
zoffset++;
}
return circleVecs.toArray(new Vector[0]);
}
/**
* Get cube as relative vectors
* @param radius The radius of the cube
* @return An array of {@link Vector}s
*/
public static Vector[] getCube(int radius) {
ArrayList<Vector> cubeVecs = new ArrayList<>();
int xoffset = -radius;
int zoffset = -radius;
int yoffset = -radius;
while (yoffset <= radius) {
while (zoffset <= radius) {
while (xoffset <= radius) {
cubeVecs.add(new Vector(xoffset, yoffset, zoffset));
xoffset++;
}
xoffset = -radius;
zoffset++;
}
zoffset = -radius;
yoffset++;
}
return cubeVecs.toArray(new Vector[0]);
}
}

View File

@ -3,6 +3,8 @@ package com.willfp.ecoenchants.util;
import org.bukkit.util.NumberConversions;
import org.bukkit.util.Vector;
import java.util.ArrayList;
public class VectorUtils {
public static boolean isFinite(Vector vector) {
try {
@ -48,4 +50,83 @@ public class VectorUtils {
return new Vector(0, 0, 1);
}
}
/**
* Get circle as relative vectors
* @param radius The radius
* @return An array of {@link Vector}s
*/
public static Vector[] getCircle(int radius) {
ArrayList<Vector> circleVecs = new ArrayList<>();
int xoffset = -radius;
int zoffset = -radius;
while (zoffset <= radius) {
while (xoffset <= radius) {
if (Math.round(Math.sqrt((xoffset * xoffset) + (zoffset * zoffset))) <= radius) {
circleVecs.add(new Vector(xoffset, 0, zoffset));
} else {
xoffset++;
continue;
}
xoffset++;
}
xoffset = -radius;
zoffset++;
}
return circleVecs.toArray(new Vector[0]);
}
/**
* Get square as relative vectors
* @param radius The radius of the square
* @return An array of {@link Vector}s
*/
public static Vector[] getSquare(int radius) {
ArrayList<Vector> circleVecs = new ArrayList<>();
int xoffset = -radius;
int zoffset = -radius;
while (zoffset <= radius) {
while (xoffset <= radius) {
circleVecs.add(new Vector(xoffset, 0, zoffset));
xoffset++;
}
xoffset = -radius;
zoffset++;
}
return circleVecs.toArray(new Vector[0]);
}
/**
* Get cube as relative vectors
* @param radius The radius of the cube
* @return An array of {@link Vector}s
*/
public static Vector[] getCube(int radius) {
ArrayList<Vector> cubeVecs = new ArrayList<>();
int xoffset = -radius;
int zoffset = -radius;
int yoffset = -radius;
while (yoffset <= radius) {
while (zoffset <= radius) {
while (xoffset <= radius) {
cubeVecs.add(new Vector(xoffset, yoffset, zoffset));
xoffset++;
}
xoffset = -radius;
zoffset++;
}
zoffset = -radius;
yoffset++;
}
return cubeVecs.toArray(new Vector[0]);
}
}

View File

@ -1,7 +1,8 @@
package com.willfp.ecoenchants.util;
package com.willfp.ecoenchants.util.internal;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.util.NumberUtils;
import org.bukkit.*;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;

View File

@ -1,4 +1,4 @@
package com.willfp.ecoenchants.util;
package com.willfp.ecoenchants.util.internal;
import com.comphenix.protocol.ProtocolLibrary;
import com.willfp.ecoenchants.EcoEnchantsPlugin;

View File

@ -1,6 +1,7 @@
package com.willfp.ecoenchants.util;
package com.willfp.ecoenchants.util.internal;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.util.StringUtils;
public class Logger {
private static final EcoEnchantsPlugin INSTANCE = EcoEnchantsPlugin.getInstance();

View File

@ -1,4 +1,4 @@
package com.willfp.ecoenchants.util;
package com.willfp.ecoenchants.util.internal;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;