Warps, I need help. Diamonds cost a warp.

Discussion in 'Plugin Development' started by OppositeGamer, Jun 12, 2012.

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

    OppositeGamer

    Ok, so I have been programming this for a while and will probably decompile the warp class file in essentials and add this in. But in order to do this. I need to test it without the plugin. I want the plugin to check if the player has 20 diamonds in their inventory. If they dont it will tell them you dont have enough diamonds. Which I can do myself. I just need it to check the players inventory for diamonds. And if they have the 20 required it will remove the diamonds from the inventory and set the warp! Thanks!

    Code:
    package com.colbymchenry.warps;
     
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin{
    Logger log;
     
    public void onEnable(){
    log = this.getLogger();
    log.info("WARPS HAS BEEN ENABLED!");
    }
     
    public void onDisable(){
    log = this.getLogger();
    log.info("WARPS HAS BEEN DISABLED!");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player p = (Player) sender;
    String pname = p.getName();
    if(cmd.getName().equalsIgnoreCase("setwarp")){
        p.getInventory().clear();
    p.getInventory().setBoots(null);
    p.getInventory().setChestplate(null);
    p.getInventory().setHelmet(null);
    p.getInventory().setLeggings(null);
    p.getInventory().setDiamond(Material.DIAMOND);
    }
    return false;}}
     
  2. there is no Inventory.setDiamonds(Material) at
    p.getInventory().setDiamond(Material.DIAMOND);
     
    RobotA69 likes this.
  3. Offline

    OppositeGamer

    I know I was just trying that. That was just a try. You think there is a way to search for diamonds though?
     
  4. Offline

    RobotA69

    LOL

    if(playerinventory.contains(Material.DIAMOND) {
    playerinventory.add(Material.DIAMOND, 64)
    playerinventory.update;
    } else if(!playerinventory.contains(Material.DIAMOND) {
    player.sendMessage("Sucks to be you bro!");
    }
     
  5. You don't need an else if, only an else. Or can a boolean have more than two states? No.
     
    ferrybig likes this.
  6. Offline

    RobotA69

    It doesn't make a difference, I prefer to use else if.. in this occasion it doesn't change the code. Lol what? Nobody said anything about booleans.... :confused:
     
  7. Offline

    CorrieKay

    an if statement takes a boolean...
     
  8. Offline

    RobotA69

    I'm confused lol.. obviously it does, but why are we talking about booleans in the first place?
     
  9. see the post above. Also it makes a difference. Just an else is most of the times faster than another else if, when you don't need it, because in most of the times you're executing code which obviously takes its time.

    Code:
    if(<extermlycpuexpensivemethod>())
    {
    }
    else if(!<extremlycpuexpensivemthod())
    {
    }
    This is really inefficient. Because it's really cpu expensive twice, event if you need it only once, a simple else is not only faster but also less irritating.
     
    ferrybig likes this.
  10. Offline

    bartboy8

    Here, you just have to figure out the setwarp cmd hope I helped!
    Code:
    Player player = (Player) sender;
    PlayerInventory playerinventory = player.getInventory();
    if(playerinventory.contains(Material.DIAMOND, 20)) {
        ItemStack diamond = new ItemStack(Material.DIAMOND, 20);
        playerinventory.remove(diamond);
    [put your setwarp cmd here]
    }else{
        player.sendMessage("Sorry, you don't have enough Diamonds to set a warp!");
    }
     
  11. Offline

    Korvacs

    Actually in terms of raw code being executed by the processor you using an else if where an else is acceptable makes your code less efficient as it needs to perform an additional check which is completely redundant.

    Also an if statement determines if something is true or false, this is a boolean, the appearance of the boolean could be a method returning true or false, or a declared boolean which has been set within the method for example, so in your code the boolean is this "!playerinventory.contains(Material.DIAMOND".
     
  12. Offline

    XbannisherX

    just do it the easy way, when the command == /warp (something)
    go to his inventory and remove 1 diamond
     
  13. Offline

    RobotA69

    This is why it's called "pseudocode", if he knew Java then he would know to just use else, it's not there for him to copy & paste.
     
  14. Sadly, that's what people do.
     
  15. Offline

    CorrieKay

    imo, giving people bad psuedocode is just as bad as writing code like that..

    When you use psuedocode, youre trying to convey a message on how to do something, and writing bad psuedocode is teaching someone to code badly.
     
  16. Offline

    RobotA69

    If he doesn't know to change that one simple thing, he shouldn't be coding Java in the first place. I provide him with the foundation of what he asked for, if he hasn't already been taught about a 'if' statement, then he shouldn't be here.

    If he copy and pastes the code people gives him, I'm glad I made it 'else if', LQ dev = LQ plugin

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  17. Offline

    CorrieKay

    I dont mean to be rude, but thats not the point. The point is you going beyond what is needed and writing inefficient code, (that if check, instead of just writing else)

    In the end, its not the person youre giving the "psuedocode" to, its that youre refusing to acknowledge that your psuedocode is inefficient and you dont seem to think its a problem at all.

    What if there was an error in an arithmetic book, that said 2+2 = 3 and the teacher just said "Dont worry, the students should just figure it out themselves"

    Again, im not trying to be rude, and i know the example i provided is extreme, but you get my point, right? :O
     
  18. Offline

    RobotA69

    Sure, I get your point, I was just lazy and don't understand what the big fuss was. I just think that as a Bukkit, at the least a Java, developer that he should understand that simple change :oops:
     
  19. Offline

    Korvacs

    Dont blame your mistakes on pseudocode, you state yourself it was your preference to use else if over else, that has nothing to do with this situation, or what you had written in pseudocode, you just have a bad coding preference, and certainly dont then try and twist it into you helping the guy by giving him a foundation to build upon...
     
    CorrieKay likes this.
  20. Offline

    RobotA69

    • This is an unacceptable way to handle your own mistakes
    Yes it is my preference, mainly because I hardly ever code Bukkit plugins, I code hacks which is very different, the only reason I'm mainly here is to find bypasses for hacks.. Twist it? Are you ****ing stupid? The whole point of this post is for that very reason, but yet you guys find a way to ***** about how I code against the norm.
     
  21. Offline

    CorrieKay

    it has nothing to do with bukkit, minecraft, or hacking.

    its plain java.

    Taking pride in being inefficient (and saying its your style) is like taking pride in being stupid.
     
    Mike111177 likes this.
  22. Offline

    RobotA69

    Who's taking pride in it? If your "implying", I'm stupid. Your instigating, a immature stupid thing to do :p
     
  23. Offline

    Mike111177

    Well...
    in all honestly just writing "else" seems a bit easier
     
  24. Offline

    CorrieKay

    im not instigating, nor am i implying that youre stupid.

    Im comparing what you said, and i quote:

    You're implying that your if(condition){}else if(!condition){} code is how you normally code, and when confronted about how inefficient it is, you guard your stance of coding inefficiently.

    Im not saying im perfect, i dont think anyone is. Hell, ive probably made this exact mistake before.

    The difference, it seems, between me and you, is that you refuse to acknowledge your mistakes, and even when shown your mistake, you made an excuse.

    Why cant you just say "Oh, youre right, this code IS inefficient. I stand corrected" instead of trying to defend it based on "coding style"?
     
  25. I'll just squeeze through here...

    OppositeGamer

    First, check if your comandsender is actually a player, if not, send it a message :)
    Then, get the player's inventory, check for diamonds and then just remove one (or how many you want).

    Quick example:
    Code:
    	@Override
    	public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    	{
    		if(!(sender instanceof Player)) // if sender is NOT an instance of Player object
    		{
    			sender.sendMessage("This command can only be used by players.");
    			return true;
    		}
    		
    		Player player = (Player)sender;
    		PlayerInventory inventory = player.getInventory();
    		
    		if(inventory.contains(Material.DIAMOND, 1)) // 1 is the minimum amount to check for
    		{
    			inventory.removeItem(new ItemStack(Material.DIAMOND, 1));
    			// unsure if this works as expected, never did something like this
    		}
    		else
    			player.sendMessage("You've not enough diamonds.");
    		
    		return true;
    	}
     
  26. Offline

    CorrieKay

    agh, sorry, i didnt mean to keep the thread derailed. Apologies, OP.
     
  27. Offline

    RobotA69

    Your agreement is invalid. Please come again.
     
  28. Offline

    CorrieKay

    So you already know you're in the wrong. I'd say its time to stop derailing the thread now.
     
  29. Offline

    RobotA69

    You started, so I'll stop. Good day, sir
     
  30. Offline

    CorrieKay

    Im not a sir, and dont imply that correcting your mistake was "starting" anything. Im just glad we're done with this.

    Again, sorry for the derail OP...
     
Thread Status:
Not open for further replies.

Share This Page