Checking someone elses int.

Discussion in 'Plugin Development' started by AtillaBosma, Sep 5, 2012.

Thread Status:
Not open for further replies.
  1. This is what I want, I want to have a command, /marry, it sends a request.
    When you type /marryaccept, it accepts the request, but if /marry has not been done to that person they can't do /marryaccept.

    Here is what I have right now:
    Code:
    package com.github.AtillaBosma.Marriage;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MarriageMain extends JavaPlugin {
     
        public void onEnable(){
            getLogger().info("Marriage has been enabled!");
        }
     
        public void onDisable(){
            getLogger().info("Marriage has been stopped!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){     
             
            if (cmd.getName().equalsIgnoreCase("marryaccept")){
                 
                    Player target = (Bukkit.getServer().getPlayer(args[0]));
                 
                    Bukkit.broadcastMessage(ChatColor.GREEN + sender.getName() + " has gotten married to " + target.getName());
            }
         
            if (cmd.getName().equalsIgnoreCase("marry")){
             
                if (args.length > 1) {
                      sender.sendMessage(ChatColor.DARK_RED + "Too many arguments!");
                      return false;
                    }
                 
                if (args.length < 1) {
                      sender.sendMessage(ChatColor.DARK_RED + "Not enough arguments!");
                      return false;
                    }
             
                Player target = (Bukkit.getServer().getPlayer(args[0]));
         
            if( target != null && target.isOnline() ){
                sender.sendMessage(ChatColor.GREEN + " You have sent a marriage request to " + target.getName());
                target.sendMessage(ChatColor.GREEN + sender.getName() + " has sent a marriage request, type /marryaccept or /marrydeny");
                } else {
                sender.sendMessage(ChatColor.DARK_RED + args[0]+" isn't online!");
                }
    }
            return false;
    }
    }
    Alright, so, what I want to do is to check the int that someone else has.
    When A == 0 for you and A == 2 for the target it will tell you that A == 2 for the other person, and not just ignore A for the target.
    Is there a way to do that, to check if A == 2 for the target instead of only being able to check your own int A?

    Could I do
    Code:
    int a = 0;
     
    if (cmd.getName().equalsIgnoreCase("marryaccept")){
     
    Player target = (Bukkit.getServer().getPlayer(args[0]));
     
    if(a == 1){
    Bukkit.broadcastMessage(ChatColor.GREEN + sender.getName() + " has gotten married to " + target.getName());
    }else{
    sender.sendMessage(ChatColor.DARK_RED + "No one has proposed to you yet!");
    }
    }
     
    if (cmd.getName().equalsIgnoreCase("marry")){
     
    if (args.length > 1) {
    sender.sendMessage(ChatColor.DARK_RED + "Too many arguments!");
    return false;
    }
     
    if (args.length < 1) {
    sender.sendMessage(ChatColor.DARK_RED + "Not enough arguments!");
    return false;
    }
     
     
    Player target = (Bukkit.getServer().getPlayer(args[0]));
     
    if( target != null && target.isOnline() ){
    a +=1;
    sender.sendMessage(ChatColor.GREEN + " You have sent a marriage request to " + target.getName());
    target.sendMessage(ChatColor.GREEN + sender.getName() + " has sent a marriage request, type /marryaccept or /marrydeny");
    } else {
    sender.sendMessage(ChatColor.DARK_RED + args[0]+" isn't online!");
    }
    
    Nope didn't work, could someone help me with adding +1 to int A?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  2. place the a= 0 outside the OnCommand
     
  3. Alright, lemme try it out.
    Code:
    05.09 21:41:23 [Server] INFO ... 15 more
    05.09 21:41:23 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    05.09 21:41:23 [Server] INFO at com.github.AtillaBosma.Marriage.MarriageMain.onCommand(MarriageMain.java:26)
    05.09 21:41:23 [Server] INFO Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    05.09 21:41:23 [Server] INFO at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.ServerConnection.b(SourceFile:35)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
    05.09 21:41:23 [Server] INFO at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
    05.09 21:41:23 [Server] INFO at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
    05.09 21:41:23 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    05.09 21:41:23 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    05.09 21:41:23 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing
    
    Full Code
    Code:
    package com.github.AtillaBosma.Marriage;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MarriageMain extends JavaPlugin {
     
    public void onEnable(){
    getLogger().info("Marriage has been enabled!");
    }
     
    public void onDisable(){
    getLogger().info("Marriage has been stopped!");
    }
     
    int a = 0;
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){   
     
    if (cmd.getName().equalsIgnoreCase("marryaccept")){
       
           Player target = (Bukkit.getServer().getPlayer(args[0]));
           
           if(a == 1){
           Bukkit.broadcastMessage(ChatColor.GREEN + sender.getName() + " has gotten married to " + target.getName());
           }else{
           sender.sendMessage(ChatColor.DARK_RED + "No one has proposed to you yet!");
           }
        }
       
       if (cmd.getName().equalsIgnoreCase("marry")){
     
       if (args.length > 1) {
              sender.sendMessage(ChatColor.DARK_RED + "Too many arguments!");
              return false;
           } 
           
    if (args.length < 1) {
              sender.sendMessage(ChatColor.DARK_RED + "Not enough arguments!");
              return false;
           }
     
       
    Player target = (Bukkit.getServer().getPlayer(args[0]));
     
    if( target != null && target.isOnline() ){
                a += 1;
    sender.sendMessage(ChatColor.GREEN + " You have sent a marriage request to " + target.getName());
    target.sendMessage(ChatColor.GREEN + sender.getName() + " has sent a marriage request, type /marryaccept or /marrydeny");
    } else {
    sender.sendMessage(ChatColor.DARK_RED + args[0]+" isn't online!");
    }
    }
    return false;
     
    }
    }
    
     
  4. Offline

    stelar7

    you are trying to define a player(target) from a non-existing argument(args[0])
     
  5. Could you mark out the part of the code that is wrong?
    Maybe I can try to fix it myself.
     
  6. Offline

    stelar7

    Player target = (Bukkit.getServer().getPlayer(args[0]));
     
  7. What can I replace it with so target.getName(); works?
     
  8. Offline

    stelar7

    just add a args.length check before
     
  9. You mean
    Code:
    if (cmd.getName().equalsIgnoreCase("marryaccept")){
       
       if (args.length > 0) {
              sender.sendMessage(ChatColor.DARK_RED + "Too many arguments!");
              return false;
           } 
           
    if (args.length < 0) {
              sender.sendMessage(ChatColor.DARK_RED + "Not enough arguments!");
              return false;
           }
     
           Player target = (Bukkit.getServer().getPlayer(args[0]));
    
    More Errors.
    Code:
    05.09 22:31:26 [Server] INFO ... 15 more
    05.09 22:31:26 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    05.09 22:31:26 [Server] INFO at com.github.AtillaBosma.Marriage.MarriageMain.onCommand(MarriageMain.java:26)
    05.09 22:31:26 [Server] INFO Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    05.09 22:31:26 [Server] INFO at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.ServerConnection.b(SourceFile:35)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
    05.09 22:31:26 [Server] INFO at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
    05.09 22:31:26 [Server] INFO at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
    05.09 22:31:26 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    05.09 22:31:26 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    05.09 22:31:26 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'marryaccept' in plugin Marriage v0.1
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  10. Offline

    stelar7

    like I said, you are trying to get a player from a argument that does not exsist...
     
  11. Offline

    HON95

    Code:JAVA
    1.  
    2. if (cmd.getName().equalsIgnoreCase("marryaccept")){
    3.  
    4. if (args.length > 1) { // Changed this
    5. sender.sendMessage(ChatColor.DARK_RED + "Too many arguments!");
    6. return false;
    7. }
    8.  
    9. if (args.length < 1) { // and this
    10. sender.sendMessage(ChatColor.DARK_RED + "Not enough arguments!");
    11. return false;
    12. }
    13.  
    14. Player target = (Bukkit.getServer().getPlayer(args[0]));
    15.  
     
  12. Offline

    Phinary

    Would like to point out that your plugin is going to be very sketchy if lots of people are trying to use the command at the same time...
     
  13. Offline

    TheSmallBones

    To prevent this, I think OP needs to add sender to a list instead...
     
  14. Offline

    Phinary

    Well, he would need to store the sender and the person he sent the request to, otherwise this is so very sketchy :s
     
  15. It would be rather great if you could tell me how to, or if oyu could give me a link to a tutorial how to do this.
    I am still rather new to this.
     
  16. Offline

    TheSmallBones

    Hashmap<String, String>
     
  17. I'll look into hashmaps.
    I've got another question though, is there a way to check the INT of someone else?
    At the moment, when one person has A == 0, and another has A == 2, the person can still send a request.
    I want it to check if the other person has A == 2, because at the moment it only checks your own INT.
    So what I am basically asking, if Sender has A=0, and if Target has A=2, it doesnt send a request.
    How can I do that?
     
  18. Offline

    Phinary

    That is exactly why your code doesn't work. The integers aren't per person, only way to do it is with a hashmap. (Well, there are other ways, but your best bet is hashmap)
     
  19. I want it to check INT for Target.
    What I want to have is something like:
    Code:
    if(target.hasInt(a == 0){
    //dosomething
    }
    
    But .hasInt doesn't exist, is there a way to do that?
     
  20. Offline

    Phinary

    Do you not understand how variables work in java? There isn't magically an "a" variable for every player that does that command. You should really look into learning java more before you do more coding. Copy and pasting code that you don't understand is a terrible thing to do.
     
  21. Ah, I posted that just before I refreshed so I didn't see your response before posting that, I am also looking into Hashmaps right now, it does look kind of complicated, but I will figure it out.
     
Thread Status:
Not open for further replies.

Share This Page