1.12.2 Commande inutilisable



  • Bonjour,
    J'essaie de créer une commande pour incrémenter une valeur(capability) du joueur.
    Ici de la money.
    Problème : quand je lance la commande, j'ai une erreur sur le jeu : an error occured...
    Et une autre dans la console :

    java.lang.ArrayIndexOutOfBoundsException: 0
    	at com.drastic.redskyll.commands.CommandMoney.execute(CommandMoney.java:31) ~[CommandMoney.class:?]
    	at net.minecraft.command.CommandHandler.tryExecute(CommandHandler.java:126) [CommandHandler.class:?]
    	at net.minecraft.command.CommandHandler.executeCommand(CommandHandler.java:98) [CommandHandler.class:?]
    	at net.minecraft.network.NetHandlerPlayServer.handleSlashCommand(NetHandlerPlayServer.java:1005) [NetHandlerPlayServer.class:?]
    	at net.minecraft.network.NetHandlerPlayServer.processChatMessage(NetHandlerPlayServer.java:981) [NetHandlerPlayServer.class:?]
    	at net.minecraft.network.play.client.CPacketChatMessage.processPacket(CPacketChatMessage.java:47) [CPacketChatMessage.class:?]
    	at net.minecraft.network.play.client.CPacketChatMessage.processPacket(CPacketChatMessage.java:8) [CPacketChatMessage.class:?]
    	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) [PacketThreadUtil$1.class:?]
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_251]
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_251]
    	at net.minecraft.util.Util.runTask(Util.java:53) [Util.class:?]
    	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:798) [MinecraftServer.class:?]
    	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743) [MinecraftServer.class:?]
    	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592) [MinecraftServer.class:?]
    	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_251]
    [18:38:35] [Server thread/INFO] [STDOUT]: [com.drastic.redskyll.commands.CommandMoney:execute:37]: give
    [18:38:35] [Server thread/WARN] [minecraft/CommandHandler]: Couldn't process command: eco give
    java.lang.ArrayIndexOutOfBoundsException: 1
    	at com.drastic.redskyll.commands.CommandMoney.execute(CommandMoney.java:38) ~[CommandMoney.class:?]
    	at net.minecraft.command.CommandHandler.tryExecute(CommandHandler.java:126) [CommandHandler.class:?]
    	at net.minecraft.command.CommandHandler.executeCommand(CommandHandler.java:98) [CommandHandler.class:?]
    	at net.minecraft.network.NetHandlerPlayServer.handleSlashCommand(NetHandlerPlayServer.java:1005) [NetHandlerPlayServer.class:?]
    	at net.minecraft.network.NetHandlerPlayServer.processChatMessage(NetHandlerPlayServer.java:981) [NetHandlerPlayServer.class:?]
    	at net.minecraft.network.play.client.CPacketChatMessage.processPacket(CPacketChatMessage.java:47) [CPacketChatMessage.class:?]
    	at net.minecraft.network.play.client.CPacketChatMessage.processPacket(CPacketChatMessage.java:8) [CPacketChatMessage.class:?]
    	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) [PacketThreadUtil$1.class:?]
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_251]
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_251]
    	at net.minecraft.util.Util.runTask(Util.java:53) [Util.class:?]
    	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:798) [MinecraftServer.class:?]
    	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743) [MinecraftServer.class:?]
    	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592) [MinecraftServer.class:?]
    	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_251]
    

    Mon code :

    public class CommandMoney extends CommandBase
    {
        private final List<String> aliases = Lists.newArrayList("eco", "money", "purse");
    
        @Override
        public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
        {
    
            EntityPlayerMP player = (EntityPlayerMP)sender;
    
            String s = args[0];
    
            if(player instanceof EntityPlayer)
            {
                IMoney money = (player).getCapability(MoneyProvider.MONEY_CAP, null);
    
                System.out.println(args[0]);
                System.out.println(args[1]);
                System.out.println(args[2]);
    
                if(s == null)
                {
                    sender.sendMessage(new TextComponentString(TextFormatting.GREEN + (money.getMoney() + "$")));
                }
                else
                {
                    if(s == "give")
                    {
                        if(args[1] == null)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                        }
                        else
                        {
                            money.addMoney(Integer.parseInt(args[1]));
                        }
                    }
    
                    else if(s == "remove")
                    {
                        if(args[1] == null)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                        }
                        if(args[2] != null)
                        {
                            money.shrinkMoney(Integer.parseInt(args[1]));
                        }
    
                    }
                }
            }
        }
    
        @Override
        public String getName()
        {
            return "money";
        }
    
        @Override
        public String getUsage(ICommandSender sender)
        {
            return "money (give/remove) (count) (player)";
        }
    
        @Override
        public List<String> getAliases()
        {
            return aliases;
        }
    
        @Override
        public boolean checkPermission(MinecraftServer server, ICommandSender sender)
        {
            return true;
        }
    
        @Override
        public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos targetPos)
        {
            return java.util.Collections.EMPTY_LIST;
        }
    
    }
    


  • C'est bon !
    Voici le code ^^

    package com.drastic.redskyll.commands;
    
    import java.util.List;
    
    import com.drastic.redskyll.util.interfaces.IMoney;
    import com.drastic.redskyll.util.provider.MoneyProvider;
    import com.google.common.collect.Lists;
    
    import net.minecraft.command.CommandBase;
    import net.minecraft.command.CommandException;
    import net.minecraft.command.ICommandSender;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.server.MinecraftServer;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.text.TextComponentString;
    import net.minecraft.util.text.TextFormatting;
    
    public class CommandMoneyGive extends CommandBase
    {
        private final List<String> aliases = Lists.newArrayList("eco", "money", "purse");
    
        @Override
        public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
        {
    
            EntityPlayerMP player = (EntityPlayerMP)sender;
    
            if(player instanceof EntityPlayer)
            {
                IMoney money = (player).getCapability(MoneyProvider.MONEY_CAP, null);
    
                if(args.length == 0)
                {
                    sender.sendMessage(new TextComponentString(TextFormatting.GREEN + (money.getMoney() + "$")));
                }
                else
                {
                    System.out.println(args[0]);
    
                    if("give".equalsIgnoreCase(args[0]))
                    {
                        if(args.length < 2)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect, utilisez la commande comme ça : /money give <nombre>"));
                        }
                        else if(args.length == 2)
                        {
                            try
                            {
                                money.addMoney(Integer.parseInt(args[1]));
                                sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Vous venez de recevoir " + args[1] + "$"));
                            }
                            catch(NumberFormatException e)
                            {
                                sender.sendMessage(new TextComponentString(TextFormatting.RED + "Chiffre incorrect"));
                            }
                        }
                    }
                    else if("remove".equalsIgnoreCase(args[0]))
                    {
                        if(args.length == 1)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect, utilisez la commande comme ça : /money remove <nombre>"));
                        }
                        else if(args.length == 2)
                        {
                            try
                            {
                                money.shrinkMoney(Integer.parseInt(args[1]));
                                sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Vous venez de perdre " + args[1] + "$"));
                            }
                            catch(NumberFormatException e)
                            {
                                sender.sendMessage(new TextComponentString(TextFormatting.RED + "Chiffre incorrect"));
                            }
    
                        }
    
                    }
                    else
                    {
                        player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                    }
                }
            }
        }
    
        @Override
        public String getName()
        {
            return "money";
        }
    
        @Override
        public String getUsage(ICommandSender sender)
        {
            return "/money [action] [count]";
        }
    
        @Override
        public List<String> getAliases()
        {
            return aliases;
        }
    
        @Override
        public boolean checkPermission(MinecraftServer server, ICommandSender sender)
        {
            return true;
        }
    
        @Override
        public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos targetPos)
        {
            return java.util.Collections.EMPTY_LIST;
        }
    
    }
    
    

  • Moddeurs confirmés Rédacteurs Administrateurs

    Bonsoir,
    Quel est la ligne 31 ? (comme tu n'as pas mis les importations, elles ne correspondent pas avec ce qui est affiché).



  • @robin4002 Désolé ^^

    Voici toute la class

    package com.drastic.redskyll.commands;
    
    import java.util.List;
    
    import com.drastic.redskyll.util.interfaces.IMoney;
    import com.drastic.redskyll.util.provider.MoneyProvider;
    import com.google.common.collect.Lists;
    
    import net.minecraft.command.CommandBase;
    import net.minecraft.command.CommandException;
    import net.minecraft.command.ICommandSender;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.server.MinecraftServer;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.text.TextComponentString;
    import net.minecraft.util.text.TextFormatting;
    
    public class CommandMoney extends CommandBase
    {
        private final List<String> aliases = Lists.newArrayList("eco", "money", "purse");
    
        @Override
        public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
        {
    
            EntityPlayerMP player = (EntityPlayerMP)sender;
    
            String s = args[0];
    
            if(player instanceof EntityPlayer)
            {
                IMoney money = (player).getCapability(MoneyProvider.MONEY_CAP, null);
    
                System.out.println(args[0]);
                System.out.println(args[1]);
                System.out.println(args[2]);
    
                if(s == null)
                {
                    sender.sendMessage(new TextComponentString(TextFormatting.GREEN + (money.getMoney() + "$")));
                }
                else
                {
                    if(s == "give")
                    {
                        if(args[1] == null)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                        }
                        else
                        {
                            money.addMoney(Integer.parseInt(args[1]));
                        }
                    }
    
                    else if(s == "remove")
                    {
                        if(args[1] == null)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                        }
                        if(args[2] != null)
                        {
                            money.shrinkMoney(Integer.parseInt(args[1]));
                        }
    
                    }
                }
            }
        }
    
        @Override
        public String getName()
        {
            return "money";
        }
    
        @Override
        public String getUsage(ICommandSender sender)
        {
            return "money (give/remove) (count)";
        }
    
        @Override
        public List<String> getAliases()
        {
            return aliases;
        }
    
        @Override
        public boolean checkPermission(MinecraftServer server, ICommandSender sender)
        {
            return true;
        }
    
        @Override
        public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos targetPos)
        {
            return java.util.Collections.EMPTY_LIST;
        }
    
    }
    
    

  • Moddeurs confirmés Rédacteurs Administrateurs

    Ton code a changé depuis que tu as eu le rapport de crash ? Car je ne vois pas comment tu peux avoir un ArrayIndexOutOfBoundsException sur ta ligne 31 sachant qu'il n'y a aucune array à cette ligne.
    Cela semble plutôt être la ligne 29 :

            String s = args[0];
    

    Et si tu as cette erreur, c'est probablement car tu n'as pas mis d'argument lorsque tu as tapé ta commande.



  • @robin4002 pas possible de rendre des arguments non obligatoires ?


  • Moddeurs confirmés Rédacteurs Administrateurs

    Si, pour cela il faut que tu adapte ton code.

    Il faut commencer avec un if sur args.length et ensuite tu adapte ta logique en fonction du nombre d'arguments.



  • @robin4002 j'ai modifié mon code mais maintenant le jeu me met toujours "format incorrect" quel que sois le premier arg

    package com.drastic.redskyll.commands;
    
    import java.util.List;
    
    import com.drastic.redskyll.util.interfaces.IMoney;
    import com.drastic.redskyll.util.provider.MoneyProvider;
    import com.google.common.collect.Lists;
    
    import net.minecraft.command.CommandBase;
    import net.minecraft.command.CommandException;
    import net.minecraft.command.ICommandSender;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.server.MinecraftServer;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.text.TextComponentString;
    import net.minecraft.util.text.TextFormatting;
    
    public class CommandMoney extends CommandBase
    {
        private final List<String> aliases = Lists.newArrayList("eco", "money", "purse");
    
        @Override
        public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
        {
    
            EntityPlayerMP player = (EntityPlayerMP)sender;
    
            if(player instanceof EntityPlayer)
            {
                IMoney money = (player).getCapability(MoneyProvider.MONEY_CAP, null);
    
                if(args.length == 0)
                {
                    sender.sendMessage(new TextComponentString(TextFormatting.GREEN + (money.getMoney() + "$")));
                }
                else
                {
                    if(args[0] == "give")
                    {
                        if(args.length < 2)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                        }
                        else if(args.length == 2)
                        {
                            money.addMoney(Integer.parseInt(args[1]));
                        }
                    }
    
                    else if(args[0] == "remove")
                    {
                        if(args.length == 1)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                        }
                        else if(args.length == 2)
                        {
                            money.shrinkMoney(Integer.parseInt(args[1]));
                        }
    
                    }
                    else
                    {
                        player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                    }
                }
            }
        }
    
        @Override
        public String getName()
        {
            return "money";
        }
    
        @Override
        public String getUsage(ICommandSender sender)
        {
            return "/money [action] [count]";
        }
    
        @Override
        public List<String> getAliases()
        {
            return aliases;
        }
    
        @Override
        public boolean checkPermission(MinecraftServer server, ICommandSender sender)
        {
            return true;
        }
    
        @Override
        public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos targetPos)
        {
            return java.util.Collections.EMPTY_LIST;
        }
    
    }
    

  • Moddeurs confirmés Rédacteurs Administrateurs

    Est-ce qu'il y a une erreur dans la console ?





  • @robin4002 il détecte pas que c'est le bon texte



  • C'est bon !
    Voici le code ^^

    package com.drastic.redskyll.commands;
    
    import java.util.List;
    
    import com.drastic.redskyll.util.interfaces.IMoney;
    import com.drastic.redskyll.util.provider.MoneyProvider;
    import com.google.common.collect.Lists;
    
    import net.minecraft.command.CommandBase;
    import net.minecraft.command.CommandException;
    import net.minecraft.command.ICommandSender;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.server.MinecraftServer;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.text.TextComponentString;
    import net.minecraft.util.text.TextFormatting;
    
    public class CommandMoneyGive extends CommandBase
    {
        private final List<String> aliases = Lists.newArrayList("eco", "money", "purse");
    
        @Override
        public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
        {
    
            EntityPlayerMP player = (EntityPlayerMP)sender;
    
            if(player instanceof EntityPlayer)
            {
                IMoney money = (player).getCapability(MoneyProvider.MONEY_CAP, null);
    
                if(args.length == 0)
                {
                    sender.sendMessage(new TextComponentString(TextFormatting.GREEN + (money.getMoney() + "$")));
                }
                else
                {
                    System.out.println(args[0]);
    
                    if("give".equalsIgnoreCase(args[0]))
                    {
                        if(args.length < 2)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect, utilisez la commande comme ça : /money give <nombre>"));
                        }
                        else if(args.length == 2)
                        {
                            try
                            {
                                money.addMoney(Integer.parseInt(args[1]));
                                sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Vous venez de recevoir " + args[1] + "$"));
                            }
                            catch(NumberFormatException e)
                            {
                                sender.sendMessage(new TextComponentString(TextFormatting.RED + "Chiffre incorrect"));
                            }
                        }
                    }
                    else if("remove".equalsIgnoreCase(args[0]))
                    {
                        if(args.length == 1)
                        {
                            player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect, utilisez la commande comme ça : /money remove <nombre>"));
                        }
                        else if(args.length == 2)
                        {
                            try
                            {
                                money.shrinkMoney(Integer.parseInt(args[1]));
                                sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Vous venez de perdre " + args[1] + "$"));
                            }
                            catch(NumberFormatException e)
                            {
                                sender.sendMessage(new TextComponentString(TextFormatting.RED + "Chiffre incorrect"));
                            }
    
                        }
    
                    }
                    else
                    {
                        player.sendMessage(new TextComponentString(TextFormatting.RED + "Format Incorrect"));
                    }
                }
            }
        }
    
        @Override
        public String getName()
        {
            return "money";
        }
    
        @Override
        public String getUsage(ICommandSender sender)
        {
            return "/money [action] [count]";
        }
    
        @Override
        public List<String> getAliases()
        {
            return aliases;
        }
    
        @Override
        public boolean checkPermission(MinecraftServer server, ICommandSender sender)
        {
            return true;
        }
    
        @Override
        public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos targetPos)
        {
            return java.util.Collections.EMPTY_LIST;
        }
    
    }
    
    

  • Moddeurs confirmés Rédacteurs Administrateurs

    Attention par contre, au début de ta fonction execute il faut faire le cast après le check d'instance, comme ceci :

            if(player instanceof EntityPlayerMP)
            {
                EntityPlayerMP player = (EntityPlayerMP)sender;
    

    Sinon tu vas avoir une erreur si tu utilises la commande depuis la console du serveur.


Log in to reply