Getting the integer from a item name.

Status
This thread has been locked.

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
I need to get the integer from an item name on rightclick then remove it from their inventory!


please help<3
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Justis

Community Member
Management
Feedback score
61
Posts
2,117
Reactions
2,414
Resources
0
Java:
public void onClick(ItemClickEvent e) {
  String name = e.getClicked().getItemMeta().getDisplayName();
  int = -1;
  try {
    i = Integer.parseInt(name);
  } catch (NumberFormatException x) {}
  if (i > -1) // do stuff
}
 

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
Java:
public void onClick(ItemClickEvent e) {
  String name = e.getClicked().getItemMeta().getDisplayName();
  int = -1;
  try {
    i = Integer.parseInt(name);
  } catch (NumberFormatException x) {}
  if (i > -1) // do stuff
}
So like this?:
Code:
public void onClick(ItemClickEvent e) {
  String name = e.getClicked().getItemMeta().getDisplayName();
  int = -1;
  try {
    i = Integer.parseInt(name);
  } catch (NumberFormatException x) {}
  if (i > -1) // do stuff
p.sendMessage("You have recieved " + x + "!");
}

I'm guessing not
 

Justis

Community Member
Management
Feedback score
61
Posts
2,117
Reactions
2,414
Resources
0
I have absolutely no idea what you're trying to do. :p
The code I gave you will get an integer from the clicked item's name.

What are you trying to tell the player they received? x is the exception that's thrown if there item's name isn't a number... You're not even using it in the correct scope.
You can either use "name" or "i", but why would you need to even get an integer from the name if you're just going to send the player a string?
You might as well keep it as a string, and just send them the item's displayname.
 

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
I have absolutely no idea what you're trying to do. :p
The code I gave you will get an integer from the clicked item's name.

What are you trying to tell the player they received? x is the exception that's thrown if there item's name isn't a number... You're not even using it in the correct scope.
You can either use "name" or "i", but why would you need to even get an integer from the name if you're just going to send the player a string?
You might as well keep it as a string, and just send them the item's displayname.
I'm basically making a tiny note plugin, which gives them the value of the note + removes it from their inv, could you give a small example?
 

Justis

Community Member
Management
Feedback score
61
Posts
2,117
Reactions
2,414
Resources
0
I'm sorry, I still have no idea what you're trying to do.

If you want to remove an item:
inv.remove(item);
 

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
I'm sorry, I still have no idea what you're trying to do.

If you want to remove an item:
inv.remove(item);
Where would I put this though? Does the "i" variable carry the value?
 

Justis

Community Member
Management
Feedback score
61
Posts
2,117
Reactions
2,414
Resources
0
The i variable holds an integer value of the item's name.
It's -1 if the item's name is not an integer.
 

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
The i variable holds an integer value of the item's name.
It's -1 if the item's name is not an integer.

Event class:

Code:
public class WithdrawEvent implements Listener {

    Main plugin;
    public WithdrawEvent(Main instance) {
        this.plugin = instance;
    }
 
    @SuppressWarnings("static-access")
    @EventHandler
    public void onClick(PlayerInteractEvent e) {
        if(e.getItem().getType() == Material.GOLD_NUGGET){
            if(e.getAction() == Action.RIGHT_CLICK_AIR | e.getAction() == Action.RIGHT_CLICK_BLOCK){
                Player p = e.getPlayer();
                p.sendMessage(plugin.color("&6awdawd"));
                String name = e.getItem().getItemMeta().getDisplayName();
                ItemStack item = e.getItem();
                int i = -1;
                try {
                    i = Integer.parseInt(name);
                } catch (NumberFormatException x) {}
                if (i > -1){
           
                    plugin.econ.depositPlayer(p, i);
                    p.sendMessage(plugin.color("&6You have recieved " + i + "!"));
                    p.getInventory().remove(item);
           
                  }
            }
        }
    }
}

Command class:

Code:
public class WithdrawMain implements CommandExecutor{

    Main plugin;
    public WithdrawMain(Main instance) {
        this.plugin = instance;
    }
  
    @Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
      
        Player p = (Player) s;
      
        if(c.getName().equalsIgnoreCase("withdraw")){
          
            if(p.hasPermission("fable.admin.withdraw")){
              
                if(args.length == 1){
                  
                    ItemStack nuggetWithdraw = new ItemStack(Material.GOLD_NUGGET);
                    nuggetWithdraw.addUnsafeEnchantment(Enchantment.DURABILITY, 5);
                    ItemMeta nuggetWithdrawMeta = nuggetWithdraw.getItemMeta();
                    nuggetWithdrawMeta.setDisplayName(plugin.color("&bNugget &f" + args[0]));
                    nuggetWithdraw.setItemMeta(nuggetWithdrawMeta);
                  
                    p.getInventory().addItem(nuggetWithdraw);
                  
                } else {
                  
                    p.sendMessage("Incorrect arguments!");
                  
                }
              
            } else{
              
                p.sendMessage("No permission!");
              
            }
          
        }
        return false;
    }

}

Any reason why it's not working?
It sends me the first message but not the last part.[DOUBLEPOST=1454879401,1454875676][/DOUBLEPOST]Still not working :S
 
Status
This thread has been locked.
Top