diff --git a/plugin.yml b/plugin.yml index 73c16523..73c7510a 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,4 +1,4 @@ name: mcore3 -version: 1.0.1 +version: 1.0.2 main: com.massivecraft.mcore3.MCore load: startup \ No newline at end of file diff --git a/src/com/massivecraft/mcore3/cmd/Cmd.java b/src/com/massivecraft/mcore3/cmd/Cmd.java index 55550c6f..41336848 100644 --- a/src/com/massivecraft/mcore3/cmd/Cmd.java +++ b/src/com/massivecraft/mcore3/cmd/Cmd.java @@ -1,5 +1,6 @@ package com.massivecraft.mcore3.cmd; +import java.lang.reflect.Field; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -7,6 +8,7 @@ import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.command.Command; import org.bukkit.command.SimpleCommandMap; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.Player; @@ -57,4 +59,19 @@ public class Cmd CraftServer craftServer = (CraftServer)Bukkit.getServer(); return craftServer.getCommandMap(); } + + @SuppressWarnings("unchecked") + public static Map getKnownCommandsFromSimpleCommandMap(SimpleCommandMap scm) + { + try + { + Field field = SimpleCommandMap.class.getDeclaredField("knownCommands"); + field.setAccessible(true); + return (Map) field.get(scm); + } + catch (Exception e) + { + return null; + } + } } diff --git a/src/com/massivecraft/mcore3/cmd/MCommand.java b/src/com/massivecraft/mcore3/cmd/MCommand.java index 9b775270..2d6d6f67 100644 --- a/src/com/massivecraft/mcore3/cmd/MCommand.java +++ b/src/com/massivecraft/mcore3/cmd/MCommand.java @@ -4,7 +4,9 @@ import java.util.*; import java.util.Map.Entry; import java.util.logging.Level; +import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.command.SimpleCommandMap; import org.bukkit.entity.Player; import com.massivecraft.mcore3.Lang; @@ -148,9 +150,23 @@ public abstract class MCommand public boolean register() { - // TODO: Save this somewhere? And update it on changes to the aliases? + return register(false); + } + + public boolean register(boolean override) + { BukkitGlueCommand bgc = new BukkitGlueCommand(this); - return Cmd.getBukkitCommandMap().register("mcore", bgc); + SimpleCommandMap scm = Cmd.getBukkitCommandMap(); + + if (override) + { + // Our commands are more important than your commands :P + Map knownCommands = Cmd.getKnownCommandsFromSimpleCommandMap(scm); + String lowerLabel = bgc.getName().trim().toLowerCase(); + knownCommands.remove(lowerLabel); + } + + return scm.register("mcore", bgc); } // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore3/util/TextWrapper.java b/src/com/massivecraft/mcore3/util/TextWrapper.java new file mode 100644 index 00000000..99a5f703 --- /dev/null +++ b/src/com/massivecraft/mcore3/util/TextWrapper.java @@ -0,0 +1,107 @@ +package com.massivecraft.mcore3.util; + +// This is a modified version of: https://github.com/Bukkit/CraftBukkit/blob/dbd06b46f2bb6a0be4f93642fe0bf57d97d8feb4/src/main/java/org/bukkit/craftbukkit/TextWrapper.java +public class TextWrapper +{ + private static final int[] characterWidths = new int[] { + 1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9, + 8, 8, 8, 8, 9, 9, 8, 9, 8, 8, 8, 8, 8, 9, 9, 9, + 4, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6, + 7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6, + 3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6, + 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, + 6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2, 6, 6, + 8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 9, 5, 9, 9, + 8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7, + 7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1 + }; + + private static final char COLOR_CHAR = '\u00A7'; + private static final int CHAT_WINDOW_WIDTH = 320; + private static final int CHAT_STRING_LENGTH = 119; + private static final String allowedChars = net.minecraft.server.SharedConstants.allowedCharacters; + + public static int getCharPixelWidth(char ch) + { + // Figure out if it's allowed + int index = allowedChars.indexOf(ch); + if (index == -1) + { + return 9; + } + else + { + // Sadly needed as the allowedChars string misses the first + index += 32; + } + + return characterWidths[index]; + } + + public static String[] wrapText(final String text) + { + final StringBuilder out = new StringBuilder(); + char colorChar = 'f'; + int lineWidth = 0; + int lineLength = 0; + + // Go over the message char by char. + for (int i = 0; i < text.length(); i++) + { + char ch = text.charAt(i); + + // Get the color + if (ch == COLOR_CHAR && i < text.length() - 1) + { + // We might need a linebreak ... so ugly ;( + if (lineLength + 2 > CHAT_STRING_LENGTH) + { + out.append('\n'); + lineLength = 0; + //if (colorChar != 'f' && colorChar != 'F') + //{ + out.append(COLOR_CHAR).append(colorChar); + lineLength += 2; + //} + } + colorChar = text.charAt(++i); + out.append(COLOR_CHAR).append(colorChar); + lineLength += 2; + continue; + } + + // Find the width + final int width = getCharPixelWidth(ch); + + // See if we need a linebreak + if (lineLength + 1 > CHAT_STRING_LENGTH || lineWidth + width >= CHAT_WINDOW_WIDTH) + { + out.append('\n'); + lineLength = 0; + + // Re-apply the last color if it isn't the default + //if (colorChar != 'f' && colorChar != 'F') + //{ + out.append(COLOR_CHAR).append(colorChar); + lineLength += 2; + //} + lineWidth = width; + } + else + { + lineWidth += width; + } + out.append(ch); + lineLength++; + } + + // Return it split + return out.toString().split("\n"); + } +} diff --git a/src/com/massivecraft/mcore3/util/Txt.java b/src/com/massivecraft/mcore3/util/Txt.java index 62e81bf2..314bf3d9 100644 --- a/src/com/massivecraft/mcore3/util/Txt.java +++ b/src/com/massivecraft/mcore3/util/Txt.java @@ -8,7 +8,6 @@ import java.util.regex.Pattern; import org.bukkit.ChatColor; import org.bukkit.Material; -import org.bukkit.craftbukkit.TextWrapper; public class Txt {