Change numbers to material

Discussion in 'Plugin Development' started by PieMan456, Dec 22, 2013.

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

    PieMan456

    Hello Everyone,

    I am making a simple plugin that allows you to buy things from a sign. I have used Material.matchMaterial(); for string names for the sings but how would I make it so I can use numbers such as 5 or a potion like 373:8193. Thanks for any help!
     
  2. Offline

    MayoDwarf

    Material.getMaterial(material thing, could be config list or config item);'
    Supports item ids.
     
  3. Offline

    xTigerRebornx

    PieMan456 Even thought Item IDs are going to be replaced by names soon, you can still use the deprecated method of Material.getMaterial(int ItemID)
     
  4. Offline

    PieMan456

  5. Offline

    xTigerRebornx

    PieMan456 I believe that after you get the material, you would need to make a new ItemStack with the proper data for the damage value
     
  6. Offline

    ZeusAllMighty11

    PieMan456

    Code:
    String[] ss = myString.split(":");
    int id = Integer.parseInt(ss[0]);
    short damage = Short.parseShort(ss[1]);
    
     
    calebbfmv likes this.
  7. Offline

    xTigerRebornx

  8. Offline

    PieMan456

    xTigerRebornx ZeusAllMighty11 MayoDwarf
    Ok I have this:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onPlayerInteract(PlayerInteractEvent e){
    4. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    5. if(e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN){
    6. Sign s = (Sign) e.getClickedBlock().getState();
    7. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    8. int id = Integer.parseInt(s.getLine(1));
    9. int id2 = Integer.parseInt(s.getLine(3));
    10. Player p = e.getPlayer();
    11. try {
    12. Integer.parseInt(s.getLine(2));
    13. } catch(NumberFormatException ex){
    14. String lol = s.getLine(2);
    15.  
    16. Inventory pi = p.getInventory();
    17. if(getConfig().getInt(p.getName()) < id2){
    18. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    19. return;
    20. }
    21.  
    22. Material m = Material.matchMaterial(lol);
    23. pi.addItem(new ItemStack(m, id));
    24. p.updateInventory();
    25. int cash = getConfig().getInt(p.getName());
    26. p.sendMessage(ChatColor.GREEN + "You have bought "+ id + " " + m + " for " + id2 + "!");
    27. getConfig().set(p.getName(), cash - id2);
    28. saveConfig();
    29. return;
    30. }
    31.  
    32. String[] ss = s.getLine(2).split(":");
    33. int id1 = Integer.parseInt(ss[0]);
    34. short damage = Short.parseShort(ss[1]);
    35.  
    36. Inventory pi = p.getInventory();
    37. if(getConfig().getInt(p.getName()) < id2){
    38. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    39. return;
    40. }
    41.  
    42. pi.addItem(new ItemStack(id1, id, damage));
    43. p.updateInventory();
    44. int cash = getConfig().getInt(p.getName());
    45. p.sendMessage(ChatColor.GREEN + "You have bought "+ id + " " + id1 + " for " + id2 + "!");
    46. getConfig().set(p.getName(), cash - id2);
    47. saveConfig();
    48. return;
    49. }
    50. }
    51. }
    52. }


    But it doesn't work.
     
  9. Offline

    xTigerRebornx

    PieMan456 What doesn't work? We need details, not just "doesn't work"
     
  10. Offline

    PieMan456

    xTigerRebornx
    There are no errors. It won't give you the item or anything.
     
  11. Offline

    ThunderWaffeMC

    You will have to have a data and byte.

    Code:java
    1.  
    2. int first = event.getClickedBlock().getTypeId();
    3. byte second = event.getClickedBlock().getData();
    4. new ItemStack(first, 64, (short) second));
    5.  
     
  12. Offline

    PieMan456

    ThunderWaffeMC PieMan456 xTigerRebornx ZeusAllMighty11
    How would I be able to allow strings and int's for the sign?
    My whole class:
    Code:java
    1. package me.pieman.godtokeneco;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Sign;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.block.SignChangeEvent;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class GodTokenEco extends JavaPlugin implements Listener {
    22.  
    23. public void onEnable(){
    24. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    25. getConfig().options().copyDefaults(true);
    26. saveConfig();
    27. }
    28.  
    29. public void onDisable(){
    30. saveConfig();
    31. }
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    34. if(cmd.getName().equalsIgnoreCase("gtokens")){
    35. if(args.length == 0){
    36. sender.sendMessage(ChatColor.RED + "Usage: /gtokens bal");
    37. return true;
    38. }
    39. if(args[0].equalsIgnoreCase("bal")){
    40. if(!(sender instanceof Player)){
    41. sender.sendMessage(ChatColor.RED + "Only players can use this command!");
    42. return true;
    43. }
    44. Player p = (Player) sender;
    45. if(p.hasPermission("gtokens.bal")){
    46. int cash = getConfig().getInt(p.getName());
    47. p.sendMessage(ChatColor.GREEN + "GodTokens Balance: " + ChatColor.DARK_PURPLE + cash);
    48. return true;
    49. } else {
    50. p.sendMessage(ChatColor.RED + "You do not have permission!");
    51. return true;
    52. }
    53. } if (args[0].equalsIgnoreCase("give")){
    54. if(sender.hasPermission("gtokens.give")){
    55. Player target = Bukkit.getServer().getPlayer(args[1]);
    56. if(target == null){
    57. sender.sendMessage(ChatColor.RED + "That player is not online!");
    58. return true;
    59. }
    60. int dood = Integer.parseInt(args[2]);
    61. int cash = getConfig().getInt(target.getName());
    62. getConfig().set(target.getName(), cash + dood);
    63. saveConfig();
    64. sender.sendMessage(ChatColor.GREEN + "Added " + dood + " GodTokens to " + target.getName() + "'s account!");
    65. return true;
    66. } else {
    67. sender.sendMessage(ChatColor.RED + "You do not have permission!");
    68. return true;
    69. }
    70. }
    71. }
    72.  
    73. return true;
    74. }
    75.  
    76. @EventHandler
    77. public void onSignChange(SignChangeEvent e){
    78. Player p = e.getPlayer();
    79. if(!(e.getLines().length > 0)) return;
    80. if(!e.getLine(0).equalsIgnoreCase("[GodToken]")) return;
    81. if(e.getLines().length < 3){
    82. e.setLine(0, ChatColor.RED + "[GodToken]");
    83. p.sendMessage(ChatColor.RED + "A GodToken sign must have at least 4 lines!");
    84. return;
    85. }
    86. if(!p.isOp()){
    87. p.sendMessage(ChatColor.RED + "You are not aloud to make these signs!");
    88. e.getBlock().breakNaturally();
    89. } else {
    90. e.setLine(0, ChatColor.BLUE + "[GodToken]");
    91. }
    92. }
    93.  
    94. @SuppressWarnings("deprecation")
    95. @EventHandler
    96. public void onPlayerInteract(PlayerInteractEvent e){
    97. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    98. if(e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN){
    99. Sign s = (Sign) e.getClickedBlock().getState();
    100. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    101. int id = Integer.parseInt(s.getLine(1));
    102. int id2 = Integer.parseInt(s.getLine(3));
    103. Player p = e.getPlayer();
    104. try {
    105. Integer.parseInt(s.getLine(2));
    106. } catch(NumberFormatException ex){
    107. String lol = s.getLine(2);
    108.  
    109. Inventory pi = p.getInventory();
    110. if(getConfig().getInt(p.getName()) < id2){
    111. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    112. return;
    113. }
    114.  
    115. Material m = Material.matchMaterial(lol);
    116. pi.addItem(new ItemStack(m, id));
    117. p.updateInventory();
    118. int cash = getConfig().getInt(p.getName());
    119. p.sendMessage(ChatColor.GREEN + "You have bought "+ id + " " + m + " for " + id2 + "!");
    120. getConfig().set(p.getName(), cash - id2);
    121. saveConfig();
    122. return;
    123. }
    124.  
    125. String[] ss = s.getLine(2).split(":");
    126. int id1 = Integer.parseInt(ss[0]);
    127. short damage = Short.parseShort(ss[1]);
    128.  
    129. Inventory pi = p.getInventory();
    130. if(getConfig().getInt(p.getName()) < id2){
    131. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    132. return;
    133. }
    134.  
    135. pi.addItem(new ItemStack(id1, id, damage));
    136. p.updateInventory();
    137. int cash = getConfig().getInt(p.getName());
    138. p.sendMessage(ChatColor.GREEN + "You have bought "+ id + " " + id1 + " for " + id2 + "!");
    139. getConfig().set(p.getName(), cash - id2);
    140. saveConfig();
    141. return;
    142. }
    143. }
    144. }
    145. }
    146.  
    147. @EventHandler
    148. public void onPlayerJoin(PlayerJoinEvent e){
    149. Player p = e.getPlayer();
    150. if(getConfig().get(p.getName()) == null){
    151. int cash = 0;
    152. getConfig().set(p.getName(), cash);
    153. saveConfig();
    154. }
    155. }
    156. }
    157.  
     
  13. Offline

    ThunderWaffeMC

    I'm not sure where you want us to look in the code you supplied but you can use this (an example):

    Code:java
    1.  
    2. int first = event.getClickedBlock().getTypeId();
    3. byte second = event.getClickedBlock().getData();
    4. sign.setLine(0, "" + first + ":" + second);
    5.  


    This would do the same:

    Code:java
    1.  
    2. String[] ss = sign.getLine(2).split(":");
    3. int id1 = Integer.parseInt(ss[0]);
    4. short damage = Short.parseShort(ss[1]);
    5. sign.setLine(0, "" + ss);
    6.  
     
  14. Offline

    PieMan456

    ThunderWaffeMC
    I am trying to give people the blocks on the sign like the essentials buy sign. Not set the sign line to something.
     
  15. Offline

    ThunderWaffeMC

    Yeah so what you have to do is with PlayerInteractEvent, get when a player right clicks a sign and check if the sign has a specific string on it. If it does, withdraw money from the player and add the item on line 2 (or whatever you have) to the players inventory.
     
  16. Offline

    PieMan456

    ThunderWaffeMC
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onPlayerInteract(PlayerInteractEvent e){
    4. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    5. if(e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN){
    6. Sign s = (Sign) e.getClickedBlock().getState();
    7. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    8. int id = Integer.parseInt(s.getLine(1));
    9. int id2 = Integer.parseInt(s.getLine(3));
    10. Player p = e.getPlayer();
    11. try {
    12. Integer.parseInt(s.getLine(2));
    13. } catch(NumberFormatException ex){
    14. String lol = s.getLine(2);
    15.  
    16. Inventory pi = p.getInventory();
    17. if(getConfig().getInt(p.getName()) < id2){
    18. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    19. return;
    20. }
    21.  
    22. Material m = Material.matchMaterial(lol);
    23. pi.addItem(new ItemStack(m, id));
    24. p.updateInventory();
    25. int cash = getConfig().getInt(p.getName());
    26. p.sendMessage(ChatColor.GREEN + "You have bought "+ id + " " + m + " for " + id2 + "!");
    27. getConfig().set(p.getName(), cash - id2);
    28. saveConfig();
    29. return;
    30. }
    31.  
    32. String[] ss = s.getLine(2).split(":");
    33. int id1 = Integer.parseInt(ss[0]);
    34. short damage = Short.parseShort(ss[1]);
    35.  
    36. Inventory pi = p.getInventory();
    37. if(getConfig().getInt(p.getName()) < id2){
    38. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    39. return;
    40. }
    41.  
    42. pi.addItem(new ItemStack(id1, id, damage));
    43. p.updateInventory();
    44. int cash = getConfig().getInt(p.getName());
    45. p.sendMessage(ChatColor.GREEN + "You have bought "+ id + " " + id1 + " for " + id2 + "!");
    46. getConfig().set(p.getName(), cash - id2);
    47. saveConfig();
    48. return;
    49. }
    50. }
    51. }
    52. }


    This doesn't work for me. I tried what you said.

    Does anyone know why this isn't working or how to do this a different way?

    Bump!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! bump

    So no one can do this?

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

    ThunderWaffeMC

    You're only allowed to bump every 12 hours and what's wrong with the code you have? Any errors? Have you tried debugging it?
     
Thread Status:
Not open for further replies.

Share This Page