From 7b2944c466cbb5a4610176f261ac7333c33d2467 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Mon, 8 Jun 2015 20:30:46 +1000 Subject: [PATCH] FIxed plot cluster + some debug stuff --- .../intellectualcrafters/plot/BukkitMain.java | 4 + .../plot/commands/Debug.java | 4 - .../plot/generator/HybridPlotWorld.java | 5 +- .../plot/util/RUtils.java | 92 ----------- .../plot/util/ReflectionUtil.java | 148 ++++++++++++++++++ .../plot/util/ReflectionUtils.java | 135 ++++++++++++++++ .../plot/util/bukkit/OfflinePlayerUtil.java | 92 +++++++++++ 7 files changed, 383 insertions(+), 97 deletions(-) delete mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/util/RUtils.java create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtil.java create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/OfflinePlayerUtil.java diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index 49becc340..5d67fe1e5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -25,6 +25,7 @@ import com.intellectualcrafters.plot.commands.BukkitCommand; import com.intellectualcrafters.plot.commands.Chat; import com.intellectualcrafters.plot.commands.Claim; import com.intellectualcrafters.plot.commands.Clear; +import com.intellectualcrafters.plot.commands.Cluster; import com.intellectualcrafters.plot.commands.Comment; import com.intellectualcrafters.plot.commands.Condense; import com.intellectualcrafters.plot.commands.Confirm; @@ -253,6 +254,9 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { MainCommand.subCommands.add(new Clear()); MainCommand.subCommands.add(new Delete()); MainCommand.subCommands.add(new SetOwner()); + if (Settings.ENABLE_CLUSTERS) { + MainCommand.subCommands.add(new Cluster()); + } MainCommand.subCommands.add(new Trust()); MainCommand.subCommands.add(new Add()); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Debug.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Debug.java index a443c7473..b3b50dea6 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Debug.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Debug.java @@ -25,7 +25,6 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.Lag; import com.intellectualcrafters.plot.util.MainUtil; -import com.intellectualcrafters.plot.util.RUtils; public class Debug extends SubCommand { public Debug() { @@ -63,9 +62,6 @@ public class Debug extends SubCommand { information.append(getSection(section, "PlotWorld")); information.append(getLine(line, "Plot Worlds", worlds)); information.append(getLine(line, "Owned Plots", PlotSquared.getPlots().size())); - information.append(getSection(section, "RAM")); - information.append(getLine(line, "Free Ram", RUtils.getFreeRam() + "MB")); - information.append(getLine(line, "Total Ram", RUtils.getTotalRam() + "MB")); information.append(getSection(section, "Messages")); information.append(getLine(line, "Total Messages", C.values().length)); information.append(getLine(line, "View all captions", "/plot debug msg")); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java index f6dce7876..b2e9a4250 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java @@ -22,6 +22,7 @@ package com.intellectualcrafters.plot.generator; import java.util.HashMap; import java.util.HashSet; +import java.util.Map.Entry; import org.apache.commons.lang.StringUtils; import org.bukkit.configuration.ConfigurationSection; @@ -150,6 +151,9 @@ public class HybridPlotWorld extends ClassicPlotWorld { } } } + + this.ROAD_SCHEMATIC_ENABLED = true; + if ((schem1 == null) || (schem2 == null) || (this.ROAD_WIDTH == 0)) { PlotSquared.log(C.PREFIX.s() + "&3 - schematic: &7false"); return; @@ -192,7 +196,6 @@ public class HybridPlotWorld extends ClassicPlotWorld { } } } - this.ROAD_SCHEMATIC_ENABLED = true; } public static byte wrap(byte data, int start) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/RUtils.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/RUtils.java deleted file mode 100644 index 35e019709..000000000 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/RUtils.java +++ /dev/null @@ -1,92 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////////////////////// -// PlotSquared - A plot manager and world generator for the Bukkit API / -// Copyright (c) 2014 IntellectualSites/IntellectualCrafters / -// / -// This program is free software; you can redistribute it and/or modify / -// it under the terms of the GNU General Public License as published by / -// the Free Software Foundation; either version 3 of the License, or / -// (at your option) any later version. / -// / -// This program is distributed in the hope that it will be useful, / -// but WITHOUT ANY WARRANTY; without even the implied warranty of / -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the / -// GNU General Public License for more details. / -// / -// You should have received a copy of the GNU General Public License / -// along with this program; if not, write to the Free Software Foundation, / -// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA / -// / -// You can contact us via: support@intellectualsites.com / -//////////////////////////////////////////////////////////////////////////////////////////////////// -package com.intellectualcrafters.plot.util; - -import com.intellectualcrafters.plot.config.C; - -/** - * Random utilities - * - * @author Citymonstret - */ -@SuppressWarnings("unused") -public class RUtils { - /** - * Get the total allocated ram - * - * @return total ram - */ - public static long getTotalRam() { - return (Runtime.getRuntime().totalMemory() / 1024) / 1024; - } - - /** - * Get the total free ram - * - * @return free ram - */ - public static long getFreeRam() { - return (Runtime.getRuntime().freeMemory() / 1024) / 1024; - } - - /** - * Percentage of used ram - * - * @return percentage - */ - public static long getRamPercentage() { - return (getFreeRam() / getTotalRam()) * 100; - } - - /** - * Get formatted time - * - * @param sec seconds - * - * @return formatted time - */ - public static String formatTime(final double sec) { - final double h = sec / 3600; - final double m = (sec % 3600) / 60; - final double s = sec % 60; - final String string = C.TIME_FORMAT.s(); - final String s_h = (int) h + " " + ((int) h != 1 ? "hours" : "hour"); - final String s_m = (int) m + " " + ((int) m != 1 ? "minutes" : "minute"); - final String s_s = (int) s + " " + ((int) s != 1 ? "seconds" : "second"); - return string.replaceAll("%sec%", s_s).replaceAll("%min%", s_m).replaceAll("%hours%", s_h); - } - - enum Direction { - SOUTH(0), - EAST(1), - NORTH(2), - WEST(3); - private final int i; - - Direction(final int i) { - this.i = i; - } - - public int getInt() { - return this.i; - } - } -} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtil.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtil.java new file mode 100644 index 000000000..27376e0b1 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtil.java @@ -0,0 +1,148 @@ +package com.intellectualcrafters.plot.util; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.bukkit.Bukkit; + +/** + * Reflection Utilities for minecraft + * + */ +public class ReflectionUtil { + public static Class getNmsClass(final String name) { + final String className = "net.minecraft.server." + getVersion() + "." + name; + return getClass(className); + } + + public static Class getCbClass(final String name) { + final String className = "org.bukkit.craftbukkit." + getVersion() + "." + name; + return getClass(className); + } + + public static Class getUtilClass(final String name) { + try { + return Class.forName(name); //Try before 1.8 first + } catch (final ClassNotFoundException ex) { + try { + return Class.forName("net.minecraft.util." + name); //Not 1.8 + } catch (final ClassNotFoundException ex2) { + return null; + } + } + } + + public static String getVersion() { + final String packageName = Bukkit.getServer().getClass().getPackage().getName(); + return packageName.substring(packageName.lastIndexOf('.') + 1); + } + + public static Object getHandle(final Object wrapper) { + final Method getHandle = makeMethod(wrapper.getClass(), "getHandle"); + return callMethod(getHandle, wrapper); + } + + //Utils + public static Method makeMethod(final Class clazz, final String methodName, final Class... paramaters) { + try { + return clazz.getDeclaredMethod(methodName, paramaters); + } catch (final NoSuchMethodException ex) { + return null; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + @SuppressWarnings("unchecked") + public static T callMethod(final Method method, final Object instance, final Object... paramaters) { + if (method == null) { + throw new RuntimeException("No such method"); + } + method.setAccessible(true); + try { + return (T) method.invoke(instance, paramaters); + } catch (final InvocationTargetException ex) { + throw new RuntimeException(ex.getCause()); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + @SuppressWarnings("unchecked") + public static Constructor makeConstructor(final Class clazz, final Class... paramaterTypes) { + try { + return (Constructor) clazz.getConstructor(paramaterTypes); + } catch (final NoSuchMethodException ex) { + return null; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static T callConstructor(final Constructor constructor, final Object... paramaters) { + if (constructor == null) { + throw new RuntimeException("No such constructor"); + } + constructor.setAccessible(true); + try { + return constructor.newInstance(paramaters); + } catch (final InvocationTargetException ex) { + throw new RuntimeException(ex.getCause()); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static Field makeField(final Class clazz, final String name) { + try { + return clazz.getDeclaredField(name); + } catch (final NoSuchFieldException ex) { + return null; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + @SuppressWarnings("unchecked") + public static T getField(final Field field, final Object instance) { + if (field == null) { + throw new RuntimeException("No such field"); + } + field.setAccessible(true); + try { + return (T) field.get(instance); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static void setField(final Field field, final Object instance, final Object value) { + if (field == null) { + throw new RuntimeException("No such field"); + } + field.setAccessible(true); + try { + field.set(instance, value); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static Class getClass(final String name) { + try { + return Class.forName(name); + } catch (final ClassNotFoundException ex) { + return null; + } + } + + public static Class getClass(final String name, final Class superClass) { + try { + return Class.forName(name).asSubclass(superClass); + } catch (ClassCastException | ClassNotFoundException ex) { + return null; + } + } +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java index 6afb88dc7..83cebe89a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java @@ -22,6 +22,7 @@ package com.intellectualcrafters.plot.util; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; @@ -75,6 +76,140 @@ public class ReflectionUtils { } } } + + public static Class getNmsClass(final String name) { + final String className = "net.minecraft.server." + getVersion() + "." + name; + return getClass(className); + } + + public static Class getCbClass(final String name) { + final String className = "org.bukkit.craftbukkit." + getVersion() + "." + name; + return getClass(className); + } + + public static Class getUtilClass(final String name) { + try { + return Class.forName(name); //Try before 1.8 first + } catch (final ClassNotFoundException ex) { + try { + return Class.forName("net.minecraft.util." + name); //Not 1.8 + } catch (final ClassNotFoundException ex2) { + return null; + } + } + } + + public static String getVersion() { + final String packageName = Bukkit.getServer().getClass().getPackage().getName(); + return packageName.substring(packageName.lastIndexOf('.') + 1); + } + + public static Object getHandle(final Object wrapper) { + final Method getHandle = makeMethod(wrapper.getClass(), "getHandle"); + return callMethod(getHandle, wrapper); + } + + //Utils + public static Method makeMethod(final Class clazz, final String methodName, final Class... paramaters) { + try { + return clazz.getDeclaredMethod(methodName, paramaters); + } catch (final NoSuchMethodException ex) { + return null; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + @SuppressWarnings("unchecked") + public static T callMethod(final Method method, final Object instance, final Object... paramaters) { + if (method == null) { + throw new RuntimeException("No such method"); + } + method.setAccessible(true); + try { + return (T) method.invoke(instance, paramaters); + } catch (final InvocationTargetException ex) { + throw new RuntimeException(ex.getCause()); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + @SuppressWarnings("unchecked") + public static Constructor makeConstructor(final Class clazz, final Class... paramaterTypes) { + try { + return (Constructor) clazz.getConstructor(paramaterTypes); + } catch (final NoSuchMethodException ex) { + return null; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static T callConstructor(final Constructor constructor, final Object... paramaters) { + if (constructor == null) { + throw new RuntimeException("No such constructor"); + } + constructor.setAccessible(true); + try { + return constructor.newInstance(paramaters); + } catch (final InvocationTargetException ex) { + throw new RuntimeException(ex.getCause()); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static Field makeField(final Class clazz, final String name) { + try { + return clazz.getDeclaredField(name); + } catch (final NoSuchFieldException ex) { + return null; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + @SuppressWarnings("unchecked") + public static T getField(final Field field, final Object instance) { + if (field == null) { + throw new RuntimeException("No such field"); + } + field.setAccessible(true); + try { + return (T) field.get(instance); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static void setField(final Field field, final Object instance, final Object value) { + if (field == null) { + throw new RuntimeException("No such field"); + } + field.setAccessible(true); + try { + field.set(instance, value); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + public static Class getClass(final String name) { + try { + return Class.forName(name); + } catch (final ClassNotFoundException ex) { + return null; + } + } + + public static Class getClass(final String name, final Class superClass) { + try { + return Class.forName(name).asSubclass(superClass); + } catch (ClassCastException | ClassNotFoundException ex) { + return null; + } + } /** * @return true if server has forge classes diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/OfflinePlayerUtil.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/OfflinePlayerUtil.java new file mode 100644 index 000000000..27c32f8b3 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/OfflinePlayerUtil.java @@ -0,0 +1,92 @@ +package com.intellectualcrafters.plot.util.bukkit; + +import static com.intellectualcrafters.plot.util.ReflectionUtils.callConstructor; +import static com.intellectualcrafters.plot.util.ReflectionUtils.callMethod; +import static com.intellectualcrafters.plot.util.ReflectionUtils.getCbClass; +import static com.intellectualcrafters.plot.util.ReflectionUtils.getNmsClass; +import static com.intellectualcrafters.plot.util.ReflectionUtils.getUtilClass; +import static com.intellectualcrafters.plot.util.ReflectionUtils.makeConstructor; +import static com.intellectualcrafters.plot.util.ReflectionUtils.makeMethod; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; + +public class OfflinePlayerUtil { + + public static Player loadPlayer(final String name) { + return loadPlayer(Bukkit.getOfflinePlayer(name)); + } + + public static Player loadPlayer(final UUID id) { + return loadPlayer(Bukkit.getOfflinePlayer(id)); + } + + public static Player loadPlayer(final OfflinePlayer player) { + if (player == null) { + return null; + } + if (player instanceof Player) { + return (Player) player; + } + return loadPlayer(player.getUniqueId(), player.getName()); + } + + private static Player loadPlayer(final UUID id, final String name) { + final Object server = getMinecraftServer(); + final Object interactManager = newPlayerInteractManager(); + final Object worldServer = getWorldServer(); + final Object profile = newGameProfile(id, name); + final Class entityPlayerClass = getNmsClass("EntityPlayer"); + final Constructor entityPlayerConstructor = makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"), getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"), getNmsClass("PlayerInteractManager")); + final Object entityPlayer = callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager); + final Player player = (Player) getBukkitEntity(entityPlayer); + return player; + } + + private static Object newGameProfile(final UUID id, final String name) { + final Class gameProfileClass = getUtilClass("com.mojang.authlib.GameProfile"); + if (gameProfileClass == null) { //Before uuids + return name; + } + Constructor gameProfileConstructor = null; + gameProfileConstructor = makeConstructor(gameProfileClass, UUID.class, String.class); + if (gameProfileConstructor == null) { //Verson has string constructor + gameProfileConstructor = makeConstructor(gameProfileClass, String.class, String.class); + return callConstructor(gameProfileConstructor, id.toString(), name); + } else { //Version has uuid constructor + return callConstructor(gameProfileConstructor, id, name); + } + } + + private static Object newPlayerInteractManager() { + final Object worldServer = getWorldServer(); + final Class playerInteractClass = getNmsClass("PlayerInteractManager"); + final Class worldClass = getNmsClass("World"); + final Constructor c = makeConstructor(playerInteractClass, worldClass); + return callConstructor(c, worldServer); + } + + private static Object getWorldServer() { + final Object server = getMinecraftServer(); + final Class minecraftServerClass = getNmsClass("MinecraftServer"); + final Method getWorldServer = makeMethod(minecraftServerClass, "getWorldServer", int.class); + return callMethod(getWorldServer, server, 0); + } + + //NMS Utils + + private static Object getMinecraftServer() { + return callMethod(makeMethod(getCbClass("CraftServer"), "getServer"), Bukkit.getServer()); + } + + private static Entity getBukkitEntity(final Object o) { + final Method getBukkitEntity = makeMethod(o.getClass(), "getBukkitEntity"); + return callMethod(getBukkitEntity, o); + } +}