Dateien nach "src/main/java/pb/ajneb97/utils" hochladen
This commit is contained in:
60
src/main/java/pb/ajneb97/utils/ServerVersion.java
Normal file
60
src/main/java/pb/ajneb97/utils/ServerVersion.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package pb.ajneb97.utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public enum ServerVersion {
|
||||
v1_8_R1,
|
||||
v1_8_R2,
|
||||
v1_8_R3,
|
||||
v1_9_R1,
|
||||
v1_9_R2,
|
||||
v1_10_R1,
|
||||
v1_11_R1,
|
||||
v1_12_R1,
|
||||
v1_13_R1,
|
||||
v1_13_R2,
|
||||
v1_14_R1,
|
||||
v1_15_R1,
|
||||
v1_16_R1,
|
||||
v1_16_R2,
|
||||
v1_16_R3,
|
||||
v1_17_R1,
|
||||
v1_18_R1,
|
||||
v1_18_R2,
|
||||
v1_19_R1,
|
||||
v1_19_R2,
|
||||
v1_19_R3,
|
||||
v1_20_R1,
|
||||
v1_20_R2,
|
||||
v1_20_R3,
|
||||
v1_20_R4,
|
||||
v1_21_R1,
|
||||
v1_21_R2,
|
||||
v1_21_R3,
|
||||
v1_21_R4; // Added for Paper 1.21.8
|
||||
|
||||
public boolean serverVersionGreaterEqualThan(ServerVersion version1, ServerVersion version2) {
|
||||
return version1.ordinal() >= version2.ordinal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect the server version safely.
|
||||
* This method will try to map the CraftBukkit package version to an enum constant.
|
||||
* If it cannot find one, it falls back to the latest known version.
|
||||
*/
|
||||
public static ServerVersion detect() {
|
||||
String pkg = Bukkit.getServer().getClass().getPackage().getName();
|
||||
// Bukkit.getLogger().info(pkg);
|
||||
// Typically ends with v1_x_Ry
|
||||
String detected = pkg.substring(pkg.lastIndexOf('.') + 1);
|
||||
|
||||
try {
|
||||
ServerVersion version = ServerVersion.valueOf(detected);
|
||||
Bukkit.getLogger().info("[PaintballBattle] Detected server version: " + version.name());
|
||||
return version;
|
||||
} catch (IllegalArgumentException ex) {
|
||||
Bukkit.getLogger().warning("[PaintballBattle] Unknown server version '" + detected + "', defaulting to v1_21_R4");
|
||||
return ServerVersion.v1_21_R4;
|
||||
}
|
||||
}
|
||||
}
|
133
src/main/java/pb/ajneb97/utils/UtilidadesHologramas.java
Normal file
133
src/main/java/pb/ajneb97/utils/UtilidadesHologramas.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package pb.ajneb97.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import pb.ajneb97.PaintballBattle;
|
||||
import pb.ajneb97.database.JugadorDatos;
|
||||
import pb.ajneb97.database.MySQL;
|
||||
import pb.ajneb97.database.MySQLCallback;
|
||||
|
||||
public class UtilidadesHologramas {
|
||||
|
||||
public static int getCantidadLineasHolograma(PaintballBattle plugin) {
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
FileConfiguration messages = plugin.getMessages();
|
||||
int lineas = messages.getStringList("topHologramFormat").size();
|
||||
lineas = lineas+Integer.valueOf(config.getString("top_hologram_number_of_players"));
|
||||
return lineas;
|
||||
}
|
||||
|
||||
public static double determinarY(Location location, int cantidadLineasHolograma) {
|
||||
double cantidad = cantidadLineasHolograma*0.15;
|
||||
return cantidad;
|
||||
}
|
||||
|
||||
//Este metodo se usa solo para monthly o weekly
|
||||
public static void getTopPlayersSQL(final PaintballBattle plugin,final String tipo,final String periodo,final MySQLCallback callback){
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final ArrayList<String> playersList = new ArrayList<String>();
|
||||
|
||||
ArrayList<JugadorDatos> jugadores = new ArrayList<JugadorDatos>();
|
||||
if(periodo.equals("monthly")) {
|
||||
jugadores = MySQL.getPlayerDataMonthly(plugin);
|
||||
}else if(periodo.equals("weekly")) {
|
||||
jugadores = MySQL.getPlayerDataWeekly(plugin);
|
||||
}else {
|
||||
jugadores = MySQL.getPlayerData(plugin);
|
||||
}
|
||||
for(JugadorDatos j : jugadores) {
|
||||
String name = j.getName();
|
||||
int total = 0;
|
||||
if(tipo.equals("kills")) {
|
||||
total = j.getKills();
|
||||
}else if(tipo.equals("wins")) {
|
||||
total = j.getWins();
|
||||
}
|
||||
playersList.add(name+";"+total);
|
||||
}
|
||||
for(int i=0;i<playersList.size();i++) {
|
||||
for(int k=i+1;k<playersList.size();k++) {
|
||||
String[] separadosI = playersList.get(i).split(";");
|
||||
int totalI = Integer.valueOf(separadosI[1]);
|
||||
String[] separadosK = playersList.get(k).split(";");
|
||||
int totalK = Integer.valueOf(separadosK[1]);
|
||||
if(totalI < totalK) {
|
||||
String aux = playersList.get(i);
|
||||
playersList.set(i, playersList.get(k));
|
||||
playersList.set(k, aux);
|
||||
}
|
||||
}
|
||||
}
|
||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// call the callback with the result
|
||||
callback.alTerminar(playersList);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public static void getTopPlayers(final PaintballBattle plugin,final ArrayList<JugadorDatos> jugadores,final String tipo,final MySQLCallback callback){
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final ArrayList<String> playersList = new ArrayList<String>();
|
||||
if(!MySQL.isEnabled(plugin.getConfig())) {
|
||||
for(JugadorDatos j : jugadores) {
|
||||
String name = j.getName();
|
||||
int total = 0;
|
||||
if(tipo.equals("kills")) {
|
||||
total = j.getKills();
|
||||
}else if(tipo.equals("wins")) {
|
||||
total = j.getWins();
|
||||
}
|
||||
playersList.add(name+";"+total);
|
||||
}
|
||||
}else {
|
||||
ArrayList<JugadorDatos> jugadores = MySQL.getPlayerData(plugin);
|
||||
for(JugadorDatos p : jugadores) {
|
||||
String name = p.getName();
|
||||
int total = 0;
|
||||
if(tipo.equals("kills")) {
|
||||
total = p.getKills();
|
||||
}else if(tipo.equals("wins")) {
|
||||
total = p.getWins();
|
||||
}
|
||||
playersList.add(name+";"+total);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0;i<playersList.size();i++) {
|
||||
for(int k=i+1;k<playersList.size();k++) {
|
||||
String[] separadosI = playersList.get(i).split(";");
|
||||
int totalI = Integer.valueOf(separadosI[1]);
|
||||
String[] separadosK = playersList.get(k).split(";");
|
||||
int totalK = Integer.valueOf(separadosK[1]);
|
||||
if(totalI < totalK) {
|
||||
String aux = playersList.get(i);
|
||||
playersList.set(i, playersList.get(k));
|
||||
playersList.set(k, aux);
|
||||
}
|
||||
}
|
||||
}
|
||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// call the callback with the result
|
||||
callback.alTerminar(playersList);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
136
src/main/java/pb/ajneb97/utils/UtilidadesItems.java
Normal file
136
src/main/java/pb/ajneb97/utils/UtilidadesItems.java
Normal file
@@ -0,0 +1,136 @@
|
||||
package pb.ajneb97.utils;
|
||||
|
||||
import pb.ajneb97.PaintballBattle;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.bukkit.profile.PlayerProfile;
|
||||
import org.bukkit.profile.PlayerTextures;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
|
||||
import pb.ajneb97.juego.JugadorPaintball;
|
||||
|
||||
public class UtilidadesItems {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static ItemStack crearItem(FileConfiguration config,String path){
|
||||
String id = config.getString(path+".item");
|
||||
String[] idsplit = new String[2];
|
||||
int DataValue = 0;
|
||||
ItemStack stack = null;
|
||||
if(id.contains(":")){
|
||||
idsplit = id.split(":");
|
||||
String stringDataValue = idsplit[1];
|
||||
DataValue = Integer.valueOf(stringDataValue);
|
||||
Material mat = Material.getMaterial(idsplit[0].toUpperCase());
|
||||
stack = new ItemStack(mat,1,(short)DataValue);
|
||||
}else{
|
||||
Material mat = Material.getMaterial(id.toUpperCase());
|
||||
stack = new ItemStack(mat,1);
|
||||
}
|
||||
ItemMeta meta = stack.getItemMeta();
|
||||
if(config.contains(path+".name")) {
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getString(path+".name")));
|
||||
}
|
||||
if(config.contains(path+".lore")) {
|
||||
List<String> lore = config.getStringList(path+".lore");
|
||||
for(int c=0;c<lore.size();c++) {
|
||||
lore.set(c, ChatColor.translateAlternateColorCodes('&', lore.get(c)));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
|
||||
}
|
||||
if (Arrays.stream(ItemFlag.values()).anyMatch(f -> f.name().equals("HIDE_POTION_EFFECTS"))) {
|
||||
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.valueOf("HIDE_POTION_EFFECTS"));
|
||||
} else {
|
||||
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE);
|
||||
}
|
||||
if(Bukkit.getVersion().contains("1.15") || UtilidadesOtros.isNew()) {
|
||||
meta.setUnbreakable(true);
|
||||
}
|
||||
stack.setItemMeta(meta);
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static void crearItemKillstreaks(JugadorPaintball jugador,FileConfiguration config) {
|
||||
if(config.getString("killstreaks_item_enabled").equals("true")) {
|
||||
int coins = jugador.getCoins();
|
||||
ItemStack item = UtilidadesItems.crearItem(config, "killstreaks_item");
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getString("killstreaks_item.name").replace("%amount%", coins+"")));
|
||||
item.setItemMeta(meta);
|
||||
if(coins <= 1) {
|
||||
item.setAmount(1);
|
||||
}else if(coins >= 64) {
|
||||
item.setAmount(64);
|
||||
}else {
|
||||
item.setAmount(coins);
|
||||
}
|
||||
jugador.getJugador().getInventory().setItem(8, item);
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemStack getCabeza(ItemStack item, String id,String textura){
|
||||
SkullMeta skullMeta = (SkullMeta) item.getItemMeta();
|
||||
|
||||
ServerVersion serverVersion = PaintballBattle.serverVersion;
|
||||
if(serverVersion.serverVersionGreaterEqualThan(serverVersion,ServerVersion.v1_20_R2)){
|
||||
UUID uuid = id != null ? UUID.fromString(id) : UUID.randomUUID();
|
||||
PlayerProfile profile = Bukkit.createPlayerProfile(uuid);
|
||||
PlayerTextures textures = profile.getTextures();
|
||||
URL url;
|
||||
try {
|
||||
String decoded = new String(Base64.getDecoder().decode(textura));
|
||||
String decodedFormatted = decoded.replaceAll("\\s", "");
|
||||
JsonObject jsonObject = new Gson().fromJson(decodedFormatted, JsonObject.class);
|
||||
String urlText = jsonObject.get("textures").getAsJsonObject().get("SKIN")
|
||||
.getAsJsonObject().get("url").getAsString();
|
||||
|
||||
url = new URL(urlText);
|
||||
} catch (Exception error) {
|
||||
error.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
textures.setSkin(url);
|
||||
profile.setTextures(textures);
|
||||
skullMeta.setOwnerProfile(profile);
|
||||
}else{
|
||||
GameProfile profile = null;
|
||||
if(id == null) {
|
||||
profile = new GameProfile(UUID.randomUUID(), "");
|
||||
}else {
|
||||
profile = new GameProfile(UUID.fromString(id), "");
|
||||
}
|
||||
profile.getProperties().put("textures", new Property("textures", textura));
|
||||
|
||||
try {
|
||||
Field profileField = skullMeta.getClass().getDeclaredField("profile");
|
||||
profileField.setAccessible(true);
|
||||
profileField.set(skullMeta, profile);
|
||||
} catch (IllegalArgumentException | NoSuchFieldException | SecurityException | IllegalAccessException error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
item.setItemMeta(skullMeta);
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
250
src/main/java/pb/ajneb97/utils/UtilidadesOtros.java
Normal file
250
src/main/java/pb/ajneb97/utils/UtilidadesOtros.java
Normal file
@@ -0,0 +1,250 @@
|
||||
package pb.ajneb97.utils;
|
||||
|
||||
import pb.ajneb97.PaintballBattle;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
|
||||
public class UtilidadesOtros {
|
||||
|
||||
public static boolean isChatNew() {
|
||||
ServerVersion serverVersion = PaintballBattle.serverVersion;
|
||||
if(serverVersion.serverVersionGreaterEqualThan(serverVersion,ServerVersion.v1_19_R1)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isNew() {
|
||||
ServerVersion serverVersion = PaintballBattle.serverVersion;
|
||||
if(serverVersion.serverVersionGreaterEqualThan(serverVersion,ServerVersion.v1_16_R1)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isLegacy() {
|
||||
ServerVersion serverVersion = PaintballBattle.serverVersion;
|
||||
if(serverVersion.serverVersionGreaterEqualThan(serverVersion,ServerVersion.v1_13_R1)){
|
||||
return false;
|
||||
}else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTiempo(int tiempo) {
|
||||
int minutos = tiempo/60;
|
||||
int segundos = tiempo - (minutos*60);
|
||||
String segundosMsg = "";
|
||||
String minutosMsg = "";
|
||||
if(segundos >= 0 && segundos <= 9) {
|
||||
segundosMsg = "0"+segundos;
|
||||
}else {
|
||||
segundosMsg = segundos+"";
|
||||
}
|
||||
|
||||
if(minutos >= 0 && minutos <= 9) {
|
||||
minutosMsg = "0"+minutos;
|
||||
}else {
|
||||
minutosMsg = minutos+"";
|
||||
}
|
||||
|
||||
return minutosMsg+":"+segundosMsg;
|
||||
}
|
||||
|
||||
public static int coinsGanados(Player jugador,FileConfiguration config) {
|
||||
String coinsString = config.getString("coins_per_kill");
|
||||
if(coinsString.contains("-")) {
|
||||
String[] separados = coinsString.split("-");
|
||||
int num1 = Integer.valueOf(separados[0]);
|
||||
int num2 = Integer.valueOf(separados[1]);
|
||||
return getNumeroAleatorio(num1,num2);
|
||||
}else {
|
||||
return Integer.valueOf(coinsString);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getNumeroAleatorio(int min, int max) {
|
||||
Random r = new Random();
|
||||
int numero = r.nextInt((max - min) + 1) + min;
|
||||
return numero;
|
||||
}
|
||||
|
||||
public static void generarParticula(String particle, Location l, float xOffset, float yOffset, float zOffset, float speed, int count) {
|
||||
if(UtilidadesOtros.isLegacy()) {
|
||||
float red = 0;
|
||||
float green = 0;
|
||||
float blue = 0;
|
||||
boolean redstone = false;
|
||||
if(particle.startsWith("REDSTONE;")) {
|
||||
redstone = true;
|
||||
String[] sep = particle.split(";");
|
||||
int rgb = Integer.valueOf(sep[1]);
|
||||
particle = sep[0];
|
||||
Color color = Color.fromRGB(rgb);
|
||||
red = (float) color.getRed()/255;
|
||||
green = (float) color.getGreen()/255;
|
||||
blue = (float) color.getBlue()/255;
|
||||
}
|
||||
try {
|
||||
//Revisar el particle de REDSTONE
|
||||
Class<?> packetEnumParticle = getNMSClass("EnumParticle");
|
||||
Method packetEnumMethod = packetEnumParticle.getMethod("valueOf", String.class);
|
||||
Object enumParticle = packetEnumMethod.invoke(null,particle);
|
||||
Class<?> packetClass = getNMSClass("PacketPlayOutWorldParticles");
|
||||
|
||||
Constructor<?> packetConstructor = null;
|
||||
for(Constructor<?> c : packetClass.getConstructors()) {
|
||||
if(c.toGenericString().contains("EnumParticle")) {
|
||||
packetConstructor = c;
|
||||
}
|
||||
}
|
||||
// Constructor<?> packetConstructor = packetClass.getConstructor(packetEnumParticle, boolean.class, float.class, float.class, float.class, float.class, float.class, float.class,
|
||||
// float.class, int.class, int.class);
|
||||
Object packet = null;
|
||||
if(redstone) {
|
||||
packet = packetConstructor.newInstance(enumParticle, true, (float)l.getX(), (float)l.getY(), (float)l.getZ(), red, green, blue, count, 0, null);
|
||||
}else {
|
||||
packet = packetConstructor.newInstance(enumParticle, false, (float)l.getX(), (float)l.getY(), (float)l.getZ(), (float)xOffset, (float)yOffset, (float)yOffset, speed, count, null);
|
||||
}
|
||||
Method sendPacket = getNMSClass("PlayerConnection").getMethod("sendPacket", getNMSClass("Packet"));
|
||||
for(Player player : Bukkit.getOnlinePlayers()) {
|
||||
sendPacket.invoke(getConnection(player), packet);
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
||||
| SecurityException | NoSuchMethodException | NoSuchFieldException | InstantiationException e) {
|
||||
|
||||
}
|
||||
}else {
|
||||
l.getWorld().spawnParticle(Particle.valueOf(particle),l,count,xOffset,yOffset,zOffset,speed);
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> getNMSClass(String nmsClassString) throws ClassNotFoundException {
|
||||
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
|
||||
String name = "net.minecraft.server." + version + nmsClassString;
|
||||
Class<?> nmsClass = Class.forName(name);
|
||||
return nmsClass;
|
||||
}
|
||||
|
||||
private static Object getConnection(Player player) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
|
||||
Method getHandle = player.getClass().getMethod("getHandle");
|
||||
Object nmsPlayer = getHandle.invoke(player);
|
||||
Field conField = nmsPlayer.getClass().getField("playerConnection");
|
||||
Object con = conField.get(nmsPlayer);
|
||||
return con;
|
||||
}
|
||||
|
||||
public static boolean pasaConfigInventario(Player jugador,FileConfiguration config) {
|
||||
if(config.getString("empty_inventory_to_join").equals("true")) {
|
||||
PlayerInventory inv = jugador.getInventory();
|
||||
for(ItemStack item : inv.getContents()) {
|
||||
if(item != null && !item.getType().equals(Material.AIR)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for(ItemStack item : inv.getArmorContents()) {
|
||||
if(item != null && !item.getType().equals(Material.AIR)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static double eval(final String str) {
|
||||
return new Object() {
|
||||
int pos = -1, ch;
|
||||
|
||||
void nextChar() {
|
||||
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
|
||||
}
|
||||
|
||||
boolean eat(int charToEat) {
|
||||
while (ch == ' ') nextChar();
|
||||
if (ch == charToEat) {
|
||||
nextChar();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
double parse() {
|
||||
nextChar();
|
||||
double x = parseExpression();
|
||||
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
|
||||
return x;
|
||||
}
|
||||
|
||||
// Grammar:
|
||||
// expression = term | expression `+` term | expression `-` term
|
||||
// term = factor | term `*` factor | term `/` factor
|
||||
// factor = `+` factor | `-` factor | `(` expression `)`
|
||||
// | number | functionName factor | factor `^` factor
|
||||
|
||||
double parseExpression() {
|
||||
double x = parseTerm();
|
||||
for (;;) {
|
||||
if (eat('+')) x += parseTerm(); // addition
|
||||
else if (eat('-')) x -= parseTerm(); // subtraction
|
||||
else return x;
|
||||
}
|
||||
}
|
||||
|
||||
double parseTerm() {
|
||||
double x = parseFactor();
|
||||
for (;;) {
|
||||
if (eat('*')) x *= parseFactor(); // multiplication
|
||||
else if (eat('/')) x /= parseFactor(); // division
|
||||
else return x;
|
||||
}
|
||||
}
|
||||
|
||||
double parseFactor() {
|
||||
if (eat('+')) return parseFactor(); // unary plus
|
||||
if (eat('-')) return -parseFactor(); // unary minus
|
||||
|
||||
double x;
|
||||
int startPos = this.pos;
|
||||
if (eat('(')) { // parentheses
|
||||
x = parseExpression();
|
||||
eat(')');
|
||||
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
|
||||
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
|
||||
x = Double.parseDouble(str.substring(startPos, this.pos));
|
||||
} else if (ch >= 'a' && ch <= 'z') { // functions
|
||||
while (ch >= 'a' && ch <= 'z') nextChar();
|
||||
String func = str.substring(startPos, this.pos);
|
||||
x = parseFactor();
|
||||
if (func.equals("sqrt")) x = Math.sqrt(x);
|
||||
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
|
||||
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
|
||||
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
|
||||
else throw new RuntimeException("Unknown function: " + func);
|
||||
} else {
|
||||
throw new RuntimeException("Unexpected: " + (char)ch);
|
||||
}
|
||||
|
||||
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
|
||||
|
||||
return x;
|
||||
}
|
||||
}.parse();
|
||||
}
|
||||
}
|
21
src/main/java/pb/ajneb97/utils/ValueOfPatch.java
Normal file
21
src/main/java/pb/ajneb97/utils/ValueOfPatch.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package pb.ajneb97.utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.Sound;
|
||||
|
||||
public class ValueOfPatch {
|
||||
|
||||
public static Sound valueOf(String soundName){
|
||||
// like "block.note_block.harp"
|
||||
NamespacedKey key = NamespacedKey.minecraft(soundName.toLowerCase());
|
||||
|
||||
Sound sound = Registry.SOUNDS.get(key);
|
||||
if (sound == null) {
|
||||
Bukkit.getLogger().warning("Sound "+soundName+" not found");
|
||||
}
|
||||
return sound;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user