Browse Source

Verbesserungen an den Permissions, dynamischer Overloader für Vanilla Commands (für Permissions), Client-Event-Handler zum ignorieren / ändern ungewollter Messages

Kajetan Johannes Hammerle 7 years ago
parent
commit
2652ab3a42

+ 29 - 0
src/main/java/me/km/ClientEvents.java

@@ -0,0 +1,29 @@
+package me.km;
+
+import me.km.api.GlobalText;
+import net.minecraft.util.text.TextComponentString;
+import net.minecraft.util.text.TextComponentTranslation;
+import net.minecraftforge.client.event.ClientChatReceivedEvent;
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
+import net.minecraftforge.fml.relauncher.Side;
+import net.minecraftforge.fml.relauncher.SideOnly;
+
+public class ClientEvents 
+{
+    @SideOnly(Side.CLIENT)
+    @SubscribeEvent
+    public void preventChatMessage(ClientChatReceivedEvent e) 
+    {
+        if(e.getMessage() instanceof TextComponentTranslation)
+        {
+            TextComponentTranslation trans = (TextComponentTranslation) e.getMessage();
+            switch(trans.getKey())
+            {
+                case "commands.generic.permission":
+                {
+                    e.setMessage(new TextComponentString("[§5Perms§r] " + GlobalText.noPermission()));
+                }
+            }   
+        }
+    }
+}

+ 4 - 0
src/main/java/me/km/KajetansMod.java

@@ -2,6 +2,7 @@ package me.km;
 
 import me.km.api.Module;
 import me.km.api.SimpleConfig;
+import me.km.api.VanillaCommandOverloader;
 import me.km.blockprotections.BlockProtectionBank;
 import me.km.blocks.ModBlocks;
 import me.km.chatmanager.ChatManager;
@@ -22,6 +23,7 @@ import me.km.snuviscript.QuestAPI;
 import me.km.snuviscript.QuestBank;
 import net.minecraft.server.MinecraftServer;
 import net.minecraft.util.text.TextFormatting;
+import net.minecraftforge.common.MinecraftForge;
 import net.minecraftforge.fml.common.Mod;
 import net.minecraftforge.fml.common.SidedProxy;
 import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@@ -114,6 +116,7 @@ public class KajetansMod
         // Grundlegende Commands
         generalCommands = new Module("GeneralCommands", "Commands", TextFormatting.GOLD);
         generalCommands.registerCommands(e, "me.km.commands");
+        VanillaCommandOverloader.overloadVanillaCommands(e, generalCommands);
         
         // Chatmanager
         chatManager = new ChatManager("ChatManager", "Chat", TextFormatting.BLUE);
@@ -200,6 +203,7 @@ public class KajetansMod
     public void init(FMLInitializationEvent e) 
     {
         proxy.initEntities();
+        MinecraftForge.EVENT_BUS.register(new ClientEvents());
     }
 
     @Mod.EventHandler

+ 14 - 9
src/main/java/me/km/api/Module.java

@@ -56,6 +56,19 @@ public class Module extends MessageSender
     // Command-Methoden
     //--------------------------------------------------------------------------
     
+    public void registerCommand(FMLServerStartingEvent e, ModuleCommand command)
+    {
+        e.registerServerCommand(command);
+        if(commands.put(command.getName(), command) != null) 
+        {
+            this.sendWarningToConsole("Der Command '" + command.getName() + "' wurde ein weiteres Mal geladen.");
+        }
+        else
+        {
+           this.sendToConsole("Der Command '" + command.getName() + "' wurde geladen.");
+        }
+    }
+    
     @SuppressWarnings("unchecked")
     public void registerCommands(FMLServerStartingEvent e, String packageName)
     {
@@ -64,15 +77,7 @@ public class Module extends MessageSender
             try 
             {
                 ModuleCommand command = (ModuleCommand) c.getDeclaredConstructor(Module.class).newInstance(this);
-                e.registerServerCommand(command);
-                if (commands.put(command.getName(), command) != null) 
-                {
-                    this.sendWarningToConsole("Der Command '" + command.getName() + "' wurde ein weiteres Mal geladen.");
-                }
-                else
-                {
-                   this.sendWarningToConsole("Der Command '" + command.getName() + "' wurde geladen.");
-                }
+                registerCommand(e, command);
             }
             catch(ClassCastException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
             { 

+ 1 - 6
src/main/java/me/km/api/ModuleCommand.java

@@ -89,12 +89,7 @@ public abstract class ModuleCommand extends CommandBase
     
     @Override
     public void execute(MinecraftServer server, ICommandSender cs, String[] args) throws CommandException
-    {
-        if(!KajetansMod.perms.has(cs, perm))
-        {
-            m.sendWarning(cs, GlobalText.noPermission());
-            return;
-        }       
+    {   
         if(!execute(cs, args))
         {
             m.send(cs, this.getUsage(cs));

+ 26 - 0
src/main/java/me/km/api/Utils.java

@@ -212,6 +212,32 @@ public class Utils
         return connectSpaces(parts, 0);
     }
     
+    public static String formatToEnumString(String original)
+    {
+        StringBuilder sb = new StringBuilder();
+        for(char c : original.toCharArray())
+        {
+            if(c == ' ')
+            {
+                sb.append("_");
+            }
+            else if(Character.isUpperCase(c))
+            {
+                sb.append("_");
+                sb.append(c);
+            }
+            else
+            {
+                sb.append(Character.toUpperCase(c));
+            }
+        }
+        if(sb.charAt(0) == '_')
+        {
+            sb.deleteCharAt(0);
+        }
+        return sb.toString();
+    }
+    
     public static String connect(String[] array, int start, String c)
     {
         if(start >= array.length)

+ 32 - 0
src/main/java/me/km/api/VanillaCommandOverloader.java

@@ -0,0 +1,32 @@
+package me.km.api;
+
+import me.km.KajetansMod;
+import me.km.permissions.Permissions;
+import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
+
+public class VanillaCommandOverloader 
+{
+    public static void overloadVanillaCommands(FMLServerStartingEvent e, Module m)
+    {
+        KajetansMod.server.commandManager.getCommands().values().stream()
+                // Overload only vanilla commands
+                .filter(command -> !(command instanceof ModuleCommand))
+                // Prevent overloading of already overloaded commands (summon, ...)
+                .filter(command -> m.getCommand(command.getName()) == null) 
+                .forEach(command -> 
+        {
+            Permissions perm;
+            try
+            {
+                perm = Permissions.valueOf(command.getName());
+            }
+            catch(IllegalArgumentException ex)
+            {
+                m.sendWarningToConsole("Ungültige Permission für Command-Name: " + command.getName());
+                perm = Permissions.VANILLA;
+            }
+            m.registerCommand(e, new VanillaModuleCommand(command, m, perm));
+            m.sendToConsole(command.getName() + " wurde überschrieben.");
+        });
+    }
+}

+ 40 - 0
src/main/java/me/km/api/VanillaModuleCommand.java

@@ -0,0 +1,40 @@
+package me.km.api;
+
+import me.km.KajetansMod;
+import me.km.permissions.Permissions;
+import net.minecraft.command.CommandException;
+import net.minecraft.command.ICommand;
+import net.minecraft.command.ICommandSender;
+import net.minecraft.util.text.TextComponentTranslation;
+import net.minecraft.util.text.TextFormatting;
+
+public class VanillaModuleCommand extends ModuleCommand
+{
+    private final ICommand vanillaCommand;
+    
+    public VanillaModuleCommand(ICommand command, Module m, Permissions perm) 
+    {
+        super(command.getName(), m);
+        super.setDescription("A vanilla command");
+        TextComponentTranslation text = new TextComponentTranslation(command.getUsage(null));
+        super.setUsage(text.getFormattedText());
+        super.setPermission(perm);
+        vanillaCommand = command;
+    }
+
+    @Override
+    public boolean execute(ICommandSender cs, String[] arg) 
+    {
+        try
+        {
+            vanillaCommand.execute(KajetansMod.server, cs, arg);
+        }
+        catch (CommandException ex)
+        {
+            TextComponentTranslation text = new TextComponentTranslation(ex.getMessage(), ex.getErrorObjects());
+            text.getStyle().setColor(TextFormatting.RED);
+            cs.sendMessage(text);
+        }
+        return true;
+    }
+}

+ 2 - 2
src/main/java/me/km/commands/Answer.java → src/main/java/me/km/commands/CommandAnswer.java

@@ -11,9 +11,9 @@ import me.km.permissions.Permissions;
 import net.minecraft.command.ICommandSender;
 import net.minecraft.util.text.TextComponentString;
 
-public class Answer extends ModuleCommand
+public class CommandAnswer extends ModuleCommand
 {
-    public Answer(Module m) 
+    public CommandAnswer(Module m) 
     {
         super("answer", m);
         super.setDescription("Schreibt dem letzten Spieler eine Privatnachricht");

+ 20 - 18
src/main/java/me/km/events/ModDedicatedPlayerList.java

@@ -13,7 +13,6 @@ import net.minecraft.nbt.NBTTagCompound;
 import net.minecraft.network.NetHandlerPlayServer;
 import net.minecraft.network.NetworkManager;
 import net.minecraft.network.PacketBuffer;
-import net.minecraft.network.play.server.SPacketChat;
 import net.minecraft.network.play.server.SPacketCustomPayload;
 import net.minecraft.network.play.server.SPacketEntityEffect;
 import net.minecraft.network.play.server.SPacketHeldItemChange;
@@ -196,30 +195,33 @@ public class ModDedicatedPlayerList extends DedicatedPlayerList
         if(component instanceof TextComponentTranslation)
         {
             TextComponentTranslation trans = (TextComponentTranslation) component;
-            if(trans.getKey().equals("multiplayer.player.left"))
+            switch(trans.getKey())
             {
-                // trying to get a player
-                Object[] o = trans.getFormatArgs();
-                if(o.length >= 1)
+                case "multiplayer.player.left":
                 {
-                    EntityPlayer p = Utils.getPlayerByDisplayName(o[0].toString());
-                    if(p == null)
+                    // trying to get a player
+                    Object[] o = trans.getFormatArgs();
+                    if(o.length >= 1)
                     {
-                        KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen());
-                        return;
+                        EntityPlayer p = Utils.getPlayerByDisplayName(o[0].toString());
+                        if(p == null)
+                        {
+                            KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen());
+                            return;
+                        }
+                        PlayerLeaveMessageEvent event = new PlayerLeaveMessageEvent(p, "No message was set.");;
+                        if(!MinecraftForge.EVENT_BUS.post(event))
+                        {
+                            this.sendMessage(new TextComponentString(event.getMessage()), true);
+                        }
                     }
-                    PlayerLeaveMessageEvent event = new PlayerLeaveMessageEvent(p, "No message was set.");;
-                    if(!MinecraftForge.EVENT_BUS.post(event))
+                    else
                     {
-                        this.sendMessage(new TextComponentString(event.getMessage()), true);
+                        KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen());
                     }
+                    return;
                 }
-                else
-                {
-                    KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen());
-                }
-                return;
-            }
+            }   
         }
         // Custom Leave Message - End
         this.sendMessage(component, true);

+ 0 - 15
src/main/java/me/km/events/ModNetHandlerPlayServer.java

@@ -1,15 +0,0 @@
-package me.km.events;
-
-import net.minecraft.entity.player.EntityPlayerMP;
-import net.minecraft.network.NetHandlerPlayServer;
-import net.minecraft.network.NetworkManager;
-import net.minecraft.server.MinecraftServer;
-
-public class ModNetHandlerPlayServer extends NetHandlerPlayServer
-{
-    public ModNetHandlerPlayServer(MinecraftServer server, NetworkManager networkManagerIn, EntityPlayerMP playerIn) 
-    {
-        super(server, networkManagerIn, playerIn);
-    }
-    
-}

+ 0 - 34
src/main/java/me/km/permissions/PermissionListener.java

@@ -1,34 +0,0 @@
-package me.km.permissions;
-
-import me.km.KajetansMod;
-import me.km.api.GlobalText;
-import me.km.api.Module;
-import me.km.api.ModuleCommand;
-import me.km.api.ModuleListener;
-import net.minecraft.command.ICommand;
-import net.minecraftforge.event.CommandEvent;
-import net.minecraftforge.fml.common.eventhandler.EventPriority;
-import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
-
-public class PermissionListener extends ModuleListener
-{
-    public PermissionListener(Module m) 
-    {
-        super(m);
-    }
-    
-    @SubscribeEvent(priority = EventPriority.HIGHEST)
-    public void CommandProtection(CommandEvent e)
-    {
-        ICommand command = e.getCommand();
-        if(command instanceof ModuleCommand)
-        {
-            return;
-        }
-        if(!KajetansMod.perms.has(e.getSender(), command.getName()))
-        {
-            e.setCanceled(true);
-            KajetansMod.perms.send(e.getSender(), GlobalText.noPermission());
-        }
-    }
-}

+ 2 - 1
src/main/java/me/km/permissions/Permissions.java

@@ -39,7 +39,8 @@ public enum Permissions
     SCROLL,
     
     // Quests / Scripts
-    QUEST, SCRIPT, QUESTINFO, GIVEUP, SCRIPT_ERROR
+    QUEST, SCRIPT, QUESTINFO, GIVEUP, SCRIPT_ERROR,
     
     // Special
+    VANILLA
 }