Solved A small problem

Discussion in 'Plugin Development' started by CraterHater, Jun 24, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    CraterHater

    I almost did what I wanted but I don't know how to do one thing, I know I am doing it wrong but I don't know how it has to be done. here is my code:

    Code:
     @EventHandler
                                                            public void onPlayerInteract8(PlayerInteractEvent e){                                                    
                                                             Player p = e.getPlayer();
                                                               if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                                                                    Block b = e.getClickedBlock();
                                                                  if(b.getType().equals(Material.DRAGON_EGG)){
                                                                        e.setCancelled(true);
                                                                        if(getConfig().getConfigurationSection("loc") == null) {
                                                                           
                                                                             //SET IT:
                                                                            getConfig().set("loc.world", p.getLocation().getWorld().getName());
                                                                            getConfig().set("loc.x", p.getLocation().getX());
                                                                            getConfig().set("loc.y", p.getLocation().getY());
                                                                            getConfig().set("loc.z", p.getLocation().getZ());
                                                                            saveConfig();
                                                                            p.sendMessage(ChatColor.GREEN + "Location set, sir!");
                                                                            return;
                                                                           
                                                                           
                                                                           
                                                                        }else{
    
                                                                            int x1 = b.getX();
                                                                            int y1 = b.getY();
                                                                            int z1 = b.getZ();
                                                                            String world = b.getWorld().getName();
    
                                                                            Location t = new Location(Bukkit.getWorld(world), x1, y1, z1);
                                                                             if(getConfig().getConfigurationSection("loc") == b) {
                                                                                
                                                                                p.sendMessage("Cooldown initialized");
                                                                                 if (Cooldowns.tryCooldown(p, "xx", 5000)) {
                                                                                     //HATCH
                                                                                     p.sendMessage("Hatched!");
                                                      
                                                                                 }else{
                                                                               
                                                                                     p.sendMessage("§b§2[Spells]:§rYou have " + (Cooldowns.getCooldown(p, "xx") / 1000) + " seconds left!");
                                                                                 }
                                                                                 }else{
                                                                                     p.sendMessage("You are already breeding an egg!");
                                                                                   
                                                                                 }
                                                                 
                                                                                 }
                                                                               
                                                                                 }
                                                                           
                                                                     
     
  2. Offline

    Coopah

  3. Offline

    meguy26

    @CraterHater
    as a tip the following:
    Code:
    int x1 = b.getX();
    int y1 = b.getY();
    int z1 = b.getZ();
    String world = b.getWorld().getName();
    Location t = new Location(Bukkit.getWorld(world), x1, y1, z1);
    can be condensed to:
    Code:
    Location t = b.getLocation();
    Also, what @Coopah said, you say "what im trying to do isnt working," but we cannot figure what or why it is not work if we do not know what it is.
     
  4. Offline

    CraterHater

    @meguy26 @Coopah Ow yeah XD I need it to check when a player right clicks a dragon egg to see if its location is stored in the config file if there is no location, set the eggs location and if the location in the config matches the location of the egg start a cooldown and after the cooldown start some stuff, but don't worry about the cooldown, And Meguy26 why I didn't do Location t = b.getLocation(); is because I guess I'll have to take the x y and z separately from the config.
     
  5. Offline

    meguy26

    @CraterHater
    Code:
    if(getConfig().getConfigurationSection("loc") == b)
    will never return true, a configuration section CANNOT, under any circumstance, be the same object as a block. its just impossible.
     
  6. Offline

    CraterHater

    When I posted this thread I already knew I did that wrong I just didn't knew how it should be done. And that b is supposed to be T, but that still doesn't work. I think I have to check if the x's are the same in the config and at the location of the block and so for the y and the z and the world. I just don't know how.
     
  7. Offline

    meguy26

    @CraterHater
    Code:
    int x1 = b.getX();
    int y1 = b.getY();
    int z1 = b.getZ();
    String world1 = b.getWorld().getName();
    
    FileConfiguration config = getConfig();
    int x2 = Integer.parseInt(config.get("loc.x"));
    int y2 = Integer.parseInt(config.get("loc.y"));
    int z2 = Integer.parseInt(config.get("loc.z"));
    String world2 = config.get("loc.world");
    
    //replaces if(configsection == b)
    if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2 && world1.equals(world2)){
    //do your thing
    }
    
     
  8. Offline

    Drkmaster83

    @CraterHater A ConfigurationSection object will never be a Location object. Surely, when I put it like that, it makes sense, no? An apple can never be a banana, nor any other fruit than an apple. A ConfigurationSection cannot be a Location, nor any other object than another ConfigurationSection.

    With that said, you need to store 4 values in the config: world name, x, y, and z. Then, to access those under a common section, you use ConfigurationSection#getKeys(false).
     
  9. Offline

    CraterHater

    He says:
    The method parseInt(String) in the type Integer is not applicable for the arguments (Object)
    This happens at the parseints

    and:
    Type mismatch: cannot convert from Object to String
    this happens at
    config.get("loc.world");
     
  10. Offline

    MCMatters

    @CraterHater @meguy26
    Code:
    int x1 = b.getX();
    int y1 = b.getY();
    int z1 = b.getZ();
    
    String world1 = b.getWorld().getName();
    FileConfiguration config = getConfig();
    int x2 = Integer.parseInt(config.getInt("loc.x"));
    int y2 = Integer.parseInt(config.getInt("loc.y"));
    int z2 = Integer.parseInt(config.getInt("loc.z"));
    String world2 = config.getString("loc.world");
     
  11. Offline

    CraterHater

    It says at parseInt:

    The method parseInt(String) in the type Integer is not applicable for the arguments (int)


    Code:
       @EventHandler
                                                            public void onPlayerInteract8(PlayerInteractEvent e){                                                    
                                                             Player p = e.getPlayer();
                                                               if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                                                                    Block b = e.getClickedBlock();
                                                                  if(b.getType().equals(Material.DRAGON_EGG)){
                                                                        e.setCancelled(true);
                                                                        if(getConfig().getConfigurationSection("loc") == null) {
                                                                           
                                                                             //SET IT:
                                                                            getConfig().set("loc.world", p.getLocation().getWorld().getName());
                                                                            getConfig().set("loc.x", p.getLocation().getX());
                                                                            getConfig().set("loc.y", p.getLocation().getY());
                                                                            getConfig().set("loc.z", p.getLocation().getZ());
                                                                            saveConfig();
                                                                            p.sendMessage(ChatColor.GREEN + "Location set, sir!");
                                                                            return;
                                                                           
                                                                           
                                                                           
                                                                        }else{
    
    
                                                                                int x1 = b.getX();
                                                                                int y1 = b.getY();
                                                                                int z1 = b.getZ();
                                                                                String world1 = b.getWorld().getName();
                                                                                FileConfiguration config = getConfig();
                                                                                int x2 = Integer.parseInt(config.getInt("loc.x"));
                                                                                int y2 = Integer.parseInt(config.getInt("loc.y"));
                                                                                int z2 = Integer.parseInt(config.getInt("loc.z"));
                                                                                String world2 = config.getString("loc.world");
    
    
    
                                                                }
                                                               }
                                                               }
                                                            }
    }
                                                           
    
     
  12. Offline

    meguy26

    @MCMatters @CraterHater
    lol the mistakes weve made:
    Code:
    //should work
    int x1 = b.getX();
    int y1 = b.getY();
    int z1 = b.getZ();
    String world1 = b.getWorld().getName();
    
    FileConfiguration config = getConfig();
    int x2 = config.getInt("loc.x");
    int y2 = config.getInt("loc.y");
    int z2 = onfig.getInt("loc.z");
    String world2 = config.getString("loc.world");
     
  13. Offline

    CraterHater

    Not quite... all of the variables aren't used he says...
    Please put the code in your eclipse so you understand:
    Code:
                                                            @EventHandler
                                                            public void onPlayerInteract8(PlayerInteractEvent e){                                                    
                                                             Player p = e.getPlayer();
                                                               if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                                                                    Block b = e.getClickedBlock();
                                                                  if(b.getType().equals(Material.DRAGON_EGG)){
                                                                        e.setCancelled(true);
                                                                        if(getConfig().getConfigurationSection("loc") == null) {
                                                                           
                                                                             //SET IT:
                                                                            getConfig().set("loc.world", p.getLocation().getWorld().getName());
                                                                            getConfig().set("loc.x", p.getLocation().getX());
                                                                            getConfig().set("loc.y", p.getLocation().getY());
                                                                            getConfig().set("loc.z", p.getLocation().getZ());
                                                                            saveConfig();
                                                                            p.sendMessage(ChatColor.GREEN + "Location set, sir!");
                                                                            return;
                                                                           
                                                                           
                                                                           
                                                                        }else{
    
    
                                                                            //should work
                                                                            int x1 = b.getX();
                                                                            int y1 = b.getY();
                                                                            int z1 = b.getZ();
                                                                            String world1 = b.getWorld().getName();
                                                                            FileConfiguration config = getConfig();
                                                                            int x2 = config.getInt("loc.x");
                                                                            int y2 = config.getInt("loc.y");
                                                                            int z2 = config.getInt("loc.z");
                                                                            String world2 = config.getString("loc.world");
    
    
    
                                                                }
                                                               }
                                                               }
                                                            }
    }
                                                            
     
  14. Offline

    MCMatters

    @CraterHater You need to use it? I understand it. The varibles arent used. What are you trying to achieve.

    @CraterHater also instead of "he" just say "eclipse", it annoys me

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  15. Offline

    CraterHater

    @MCMatters I'll try to adapt my ways of speaking... it can be quite odd some days... You know, but do you know how to do it.

    I tried this: Doesn't work
    he says

    The method getInt(String) in the type MemorySection is not applicable for the arguments (int)
    Code:
                                                         @EventHandler
                                                            public void onPlayerInteract8(PlayerInteractEvent e){                                                    
                                                             Player p = e.getPlayer();
                                                               if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                                                                    Block b = e.getClickedBlock();
                                                                  if(b.getType().equals(Material.DRAGON_EGG)){
                                                                        e.setCancelled(true);
                                                                        if(getConfig().getConfigurationSection("loc") == null) {
                                                                           
                                                                             //SET IT:
                                                                            getConfig().set("loc.world", p.getLocation().getWorld().getName());
                                                                            getConfig().set("loc.x", p.getLocation().getX());
                                                                            getConfig().set("loc.y", p.getLocation().getY());
                                                                            getConfig().set("loc.z", p.getLocation().getZ());
                                                                            saveConfig();
                                                                            p.sendMessage(ChatColor.GREEN + "Location set, sir!");
                                                                            return;
                                                                           
                                                                           
                                                                           
                                                                        }else{
    
    
                                                                                int x1 = b.getX();
                                                                                int y1 = b.getY();
                                                                                int z1 = b.getZ();
                                                                                String world1 = b.getWorld().getName();
                                                                                FileConfiguration config = getConfig();
                                                                                int x2 = config.getInt(config.getInt("loc.x"));
                                                                                int y2 = config.getInt(config.getInt("loc.y"));
                                                                                int z2 = config.getInt(config.getInt("loc.z"));
                                                                                String world2 = config.getString("loc.world");
    
    
    
                                                                }
                                                               }
                                                               }
                                                            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  16. Offline

    meguy26

    @CraterHater
    You need to combine it with the if statement i gave in an earlier post...
    Code:
    //should work
    int x1 = b.getX();
    int y1 = b.getY();
    int z1 = b.getZ();
    String world1 = b.getWorld().getName();
    FileConfiguration config = getConfig();
    int x2 = config.getInt("loc.x");
    int y2 = config.getInt("loc.y");
    int z2 = onfig.getInt("loc.z");
    String world2 = config.getString("loc.world");
    
    if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2 && world1.equals(world2)){
    //do your thing
    }
    
    @MCMatters
    He says what he needs to do in previous posts...
     
  17. Offline

    Zombie_Striker

    @CraterHater
    Why is the spacing so huge? Your code box looks blank because your code is pushed so far to the right.

    Why not use the contains feature for configs? It returns a boolean anyway.

    Why create a new instance of Config, especially if its not going to really do anything that saveConfig() can't?

    Also, you are creating instances that are never being called. If there is suppost to be more code, what would be the purpos of getting those instances?
     
    _Filip likes this.
  18. Offline

    CraterHater

    @meguy26 ok so if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2 && world1.equals(world2)){ returns false, it isn't equal while it should be.

    Code:
    Code:
     @EventHandler
                                                            public void onPlayerInteract8(PlayerInteractEvent e){                                                    
                                                             Player p = e.getPlayer();
                                                               if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                                                                    Block b = e.getClickedBlock();
                                                                  if(b.getType().equals(Material.DRAGON_EGG)){
                                                                        e.setCancelled(true);
                                                                        if(getConfig().getConfigurationSection("loc") == null) {
                                                                           
                                                                             //SET IT:
                                                                            getConfig().set("loc.world", p.getLocation().getWorld().getName());
                                                                            getConfig().set("loc.x", p.getLocation().getX());
                                                                            getConfig().set("loc.y", p.getLocation().getY());
                                                                            getConfig().set("loc.z", p.getLocation().getZ());
                                                                            saveConfig();
                                                                            p.sendMessage(ChatColor.GREEN + "Location set, sir!");
                                                                            return;
                                                                           
                                                                           
                                                                           
                                                                        }else{
                                                                            p.sendMessage("1");
                                                                            int x1 = b.getX();
                                                                            int y1 = b.getY();
                                                                            int z1 = b.getZ();
                                                                            String world1 = b.getWorld().getName();
                                                                            FileConfiguration config = getConfig();
                                                                            int x2 = config.getInt("loc.x");
                                                                            int y2 = config.getInt("loc.y");
                                                                            int z2 = config.getInt("loc.z");
                                                                            String world2 = config.getString("loc.world");
                                                                            if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2 && world1.equals(world2)){
                                                                            p.sendMessage("Initialized");
                                                                           
                                                                            }else{
                                                                                p.sendMessage("You already breeding an egg!");
    
    
                                                                            }
    
    
                                                                       
    
    
    
    
                                                                }
                                                               }
                                                               }
                                                            }
    }
     
  19. Offline

    MCMatters

    int x2 = config.getInt("loc.x");
    int y2 = config.getInt("loc.y");
    int z2 = config.getInt("loc.z");

    Doubles not integers.
     
  20. Offline

    CraterHater

    @MCMatters @meguy26 @Zombie_Striker nothing works, Code:
    Code:
                                                            @EventHandler
                                                            public void onPlayerInteract8(PlayerInteractEvent e){                                                    
                                                             Player p = e.getPlayer();
                                                               if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                                                                    Block b = e.getClickedBlock();
                                                                  if(b.getType().equals(Material.DRAGON_EGG)){
                                                                        e.setCancelled(true);
                                                                        if(getConfig().getConfigurationSection("loc") == null) {
                                                                           
                                                                             //SET IT:
                                                                            getConfig().set("loc.world", p.getLocation().getWorld().getName());
                                                                            getConfig().set("loc.x", p.getLocation().getX());
                                                                            getConfig().set("loc.y", p.getLocation().getY());
                                                                            getConfig().set("loc.z", p.getLocation().getZ());
                                                                            saveConfig();
                                                                            p.sendMessage(ChatColor.GREEN + "Location set, sir!");
                                                                            return;
                                                                           
                                                                           
                                                                           
                                                                        }else{
                                                                            p.sendMessage("1");
                                                                            double x1 = b.getX();
                                                                            double y1 = b.getY();
                                                                            double z1 = b.getZ();
                                                                            String world1 = b.getWorld().getName();
                                                                            FileConfiguration config = getConfig();
                                                                            double x2 = config.getDouble("loc.x");
                                                                            double y2 = config.getDouble("loc.y");
                                                                            double z2 = config.getDouble("loc.z");
                                                                            String world2 = config.getString("loc.world");
                                                                            if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2 && world1.equals(world2)){
                                                                            p.sendMessage("Initialized");
                                                                           
                                                                            }else{
                                                                                p.sendMessage("You already breeding an egg!");
    
    
                                                                            }
    
    
                                                                       
    
    
    
    
                                                                }
                                                               }
                                                               }
                                                            }
    }
                                                           
    
    
                                                          
                                                           
    
    
     
  21. Offline

    Boomer

    What is your definition of "nothing works"
    What points of your debugging do you see, do you not see. How far is it getting, what is happening, what is happening in the console when you do the action trigger - does it blast a java error there at that time?
     
  22. Offline

    CraterHater

    It gives no errors but it says: "You are already breeding an egg!" which indicates that if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2 && world1.equals(world2)){ does not equal to the location it has set, so that's not working.
     
  23. Offline

    MCMatters

    @CraterHater Echo all the values to console, show code and show the output. If possible I can get on your test server and try to debug it myself. Private Message me on the forums if you'd like me to try that.
     
  24. Offline

    CraterHater

    Well I did not port forward my test server so that can't be possible. But isn't there just a simple way to do this?
     
  25. Offline

    Boomer

    yes. Do at least part of that.
    Echo the values of x1, x2 y1 y2 z1 z2 world1 world2 before doing the ifs
    See if something goes against the assumptions (also, later you can reduce the if to not have the redundant z check)
    You may find that the doubles have a different degree of precision, for example
     
  26. Offline

    CraterHater

    How to echo :D
     
  27. Offline

    Boomer

    Echo = send text to somewhere you can see

    player.sendMessage()
    Bukkit.broadcastMessage()
    System.out.print()
    Bukkit.getLogger().info()

    etc

    Any method that you are comfortable with and have access to (player send message works good in methods using player objects, broadcastMessage works almost anywhere .. ).
     
  28. Offline

    CraterHater

    It says "1" and than it says the else, of the if. he says: "You're already breeding an egg!" and that means that
    if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2 && world1.equals(world2)){
    does not give true as it should
     
  29. Offline

    Boomer

    which is why you should be echoing the VALUES of x1, x2, z1, z2 y1, y2 world1, world2 prior to the if
    so that you can see what they are in comparison to each other, in order to back-track the reasoning

    IE are all the coordinates mismatching by fractions? Is it just the y coordinate different each time? Is there a space in the worldname?
     
  30. Offline

    MCMatters

    @CraterHater
    do getLogger().info("x1="+x1+" x2="+x2+" y1="+y1 + " y2="+y2+" z1="+z1+" z2="+z2)
    before the if
     
Thread Status:
Not open for further replies.

Share This Page