I'm trying to make something similar to the Buy Sign that Essentials has. Couple things I need help with: I don't know how to check to see if an item ID is null I want to make it so that a player can put in the item name and it will work. Also the third and fourth lines HAVE to be integers. Here is what I have: Code:java @SuppressWarnings("deprecation") @EventHandler public void signChange(SignChangeEvent event){ if (event.getLine(0).contains("-Buy-")){ Material i = Material.getMaterial(Integer.parseInt(event.getLine(1))); if (event.getLine(1).equals(i)){ if (i != null){ int in = Integer.parseInt(event.getLine(2)); if (event.getLine(2).equals(in)){ int price = Integer.parseInt(event.getLine(3)); if (event.getLine(3).equals(price)){ event.setLine(0, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.first-line-color") + "- Buy -")); event.setLine(1, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.second-line-color") + i)); event.setLine(2, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.third-line-color") + in)); event.setLine(3, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.fourth-line-color") + price)); } } } } } }
Blah1 I do not know the answer to your question, as I am still learning. I simply have one quick question, why not use an "Inventory Store"? In my opinion, It is much more appealing to the eye. Let me give is a shot, try using something similar to Code:java //place your import with anything else you have imported at the top of the file.import org.bukkit.Material; if((player.getInventory().contains(Material.COBBLESTONE, 10))){//Basically if the player has 10 COBBLESTONE, it will run the code here.} Player will of course change to however you are calling the player. but this should work.
achap1989 That's not what I'm asking I just want to know how to check to see if the words on line two come out to be a material. And also if the third and fourth lines are integers. Perhaps the amazing and online drtshock could help? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
So just some random comments from me: * You should DEFINITLY catch those NumberFormatExceptions from Integer.parseInt(). * You can not compare an Material to a String. That would simply not work. You would need something like that: if(i.name().equals(event.getLine(2))) But this also has problems with names that are longer than 16 chars. So also check that. * Check what sign.setLine() does when the text is longer than 16 chars. Earlier it threw an exception. I don't know how it is now.
tobiyas how do I check if the item ID is valid? Also, how do i check to see if the third and fourth line are numbers only?
Give me a sec to check in Eclipse for synthax. Okay. I think I got it: Code:java @SuppressWarnings("deprecation")@EventHandlerpublic void signChange(SignChangeEvent event){if (event.getLine(0).contains("-Buy-")){//we have a buy sign.int materialId = -1;int amount = -1;int price = -1;Material material = null; try{materialId = Integer.parseInt(event.getLine(1));material = Material.getMaterial(materialId); amount = Integer.parseInt(event.getLine(2));price = Integer.parseInt(event.getLine(3));}catch(NumberFormatException exp){//No Numbers found in line return;} if(material == null){//we got an invalid Material ID here... Do somethingreturn;} event.setLine(0, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.first-line-color") + "- Buy -"));event.setLine(1, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.second-line-color") + materialId));event.setLine(2, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.third-line-color") + amount));event.setLine(3, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.fourth-line-color") + price));}} You could also change the materialId to material.name(). To have the Name of the Material. But like I said above, many are longer than 16 chars.
Making buy and sell signs will be tricky without having item ids. Especially when materials and damage values or w/e they'll be called will definitely be more than 16 characters. An inventory shop would be a great idea. I know there are a few out there but none really do what I like so I was thinking of making my own ;o But yes, definitely try / catch the Integer#valueOf checks. If you still have issues after what tobiyas said I'll help you out ;3
Blah1 , I apologize, I must of misunderstood the question. drtshock , I am acctually attempting to make a customizable inventory shop via a handy config file. Problem is it will take a bit as I am still very new to this lol. Hell, I can not even change the tutorial Menu inventory, from wool, to diamond armor xD. (yet) But rest assured this will be made, lol.