[Help] Kit Signs

Discussion in 'Plugin Development' started by DeluXeNationMC, Apr 29, 2014.

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

    DeluXeNationMC

    Hello, i would like to know how i can make signs with text on them for example [Melee] execute commands from my class

    This is my code currently:

    package me.DeluXeNationMC.DeluXeSigns;

    import org.bukkit.ChatColor;
    import org.bukkit.block.Sign;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{

    @EventHandler
    public void onSignChange(SignChangeEvent event) {
    if (event.getLine(0).equalsIgnoreCase("[Melee]")) {
    event.setLine(0, "§3[Melee]");
    event.setLine(1, "§3Click here");
    }
    }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
    if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    if (event.getClickedBlock().getState() instanceof Sign) {
    Sign s = (Sign) event.getClickedBlock().getState();
    if (s.getLine(0).equalsIgnoreCase("§3[Melee]")) {
    event.getPlayer().sendMessage("[" + ChatColor.GOLD + "DeluXePvP" + "" + ChatColor.GRAY + "]" + " You Were Given The Melee Kit!");
    //Give Melee Kit ..
    }
    }
    }
    }
     
  2. Offline

    badboysteee98

    Okay so you want to make a class that will give the kit, also in the onPlayerInteractEvent use this code
    Code:java
    1. Player player = (Player) player

    As it's good practices and then when you send them the message instead of typeing
    Code:java
    1. event.getPlayer().sendMessage("This all the time you can type this");
    2. player.sendMessage("This will save time :)");


    So you want to give them the kit? On the interact event do the follow
    Code:java
    1. PlayerInventory pi = player.getInventory(); //Make sure you have added Player player = (Player) e.getPlayer for this to work otherwise use PlayerInventory pi = event.getPlayer().getInventory();
    2.  
    3. @EventHandler
    4. public void onPlayerInteract(PlayerInteractEvent event) {
    5. if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    6. if (event.getClickedBlock().getState() instanceof Sign) {
    7. Sign s = (Sign) event.getClickedBlock().getState();
    8. if (s.getLine(0).equalsIgnoreCase("§3[Melee]")) {
    9. event.getPlayer().sendMessage("[" + ChatColor.GOLD + "DeluXePvP" + "" + ChatColor.GRAY + "]" + " You Were Given The Melee Kit!");
    10.  
    11. PlayerInventory pi = player.getInventory();
    12.  
    13. pi.addItem(new ItemStack(Material.GOLD_SWORD, 1)); //This will give them 1 gold sword when they hit the sign
    14.  


    Note: Please use the Code syntax that is provided, it is not there for nothing
     
  3. Offline

    DeluXeNationMC

    Code:java
    1. //Melee Kit
    2. if(label.equalsIgnoreCase("Melee")){
    3. if(!(sender instanceof Player)){
    4. sender.sendMessage(ChatColor.RED + "[DeluXePvP] You Must Be A Player To Get The Melee Kit!");
    5. return true;
    6. }
    7. Player player = (Player) sender;
    8. Location location = player.getLocation();
    9. player.sendMessage("[" + ChatColor.GOLD + "DeluXePvP" + "" + ChatColor.GRAY + "]" + " You Were Given The Melee Kit!");
    10. player.playSound(location, Sound.ITEM_PICKUP, 1, 1);
    11. player.getInventory().clear();
    12. player.getActivePotionEffects().clear();
    13. player.getInventory().addItem(new ItemStack(Material.IRON_SWORD));
    14. ItemStack meleeBootsItemStack = new ItemStack(Material.IRON_BOOTS, 1); //Melee's Boots
    15. meleeBootsItemStack.addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 4);
    16. ItemStack meleeHelmetItemStack = new ItemStack(Material.IRON_HELMET, 1); //Melee's Helmet
    17. meleeHelmetItemStack.addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 4);
    18. player.getInventory().setHelmet(new ItemStack(meleeHelmetItemStack));
    19. player.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    20. player.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    21. player.getInventory().setBoots(new ItemStack(meleeBootsItemStack));
    22. return true;
    23. }
    24. }


    But I Dont Understand How I Execute This Code When The Player Right Clicks The Sign

    Currently This Is My Code

    Code:java
    1. @EventHandler
    2. public void onSignChange(SignChangeEvent event){
    3. if (event.getLine(0).equalsIgnoreCase("[Melee]")) {
    4. event.setLine(0, "§3[Melee]");
    5. event.setLine(1, "§3Click here");
    6. }
    7. }
    8. @EventHandler
    9. public void onPlayerInteract(PlayerInteractEvent event) {
    10. if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    11. if (event.getClickedBlock().getState() instanceof Sign) {
    12. Sign s = (Sign) event.getClickedBlock().getState();
    13. if (s.getLine(0).equalsIgnoreCase("§3[Melee]")) {
    14. event.getPlayer().sendMessage("[" + ChatColor.GOLD + "DeluXePvP" + "" + ChatColor.GRAY + "]" + " You Were Given The Melee Kit!");
    15. }
    16. }
    17. }
    18. public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
    19. if(label.equalsIgnoreCase("Melee")){
    20. if(!(sender instanceof Player)){
    21. sender.sendMessage(ChatColor.RED + "[DeluXePvP] You Must Be A Player To Get The Melee Kit!");
    22. return true;
    23. }
    24. //Melee Kit
    25. if(label.equalsIgnoreCase("Melee")){
    26. if(!(sender instanceof Player)){
    27. sender.sendMessage(ChatColor.RED + "[DeluXePvP] You Must Be A Player To Get The Melee Kit!");
    28. return true;
    29. }
    30. Player player = (Player) sender;
    31. Location location = player.getLocation();
    32. player.sendMessage("[" + ChatColor.GOLD + "DeluXePvP" + "" + ChatColor.GRAY + "]" + " You Were Given The Melee Kit!");
    33. player.playSound(location, Sound.ITEM_PICKUP, 1, 1);
    34. player.getInventory().clear();
    35. player.getActivePotionEffects().clear();
    36. player.getInventory().addItem(new ItemStack(Material.IRON_SWORD));
    37. ItemStack meleeBootsItemStack = new ItemStack(Material.IRON_BOOTS, 1); //Melee's Boots
    38. meleeBootsItemStack.addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 4);
    39. ItemStack meleeHelmetItemStack = new ItemStack(Material.IRON_HELMET, 1); //Melee's Helmet
    40. meleeHelmetItemStack.addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 4);
    41. player.getInventory().setHelmet(new ItemStack(meleeHelmetItemStack));
    42. player.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    43. player.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    44. player.getInventory().setBoots(new ItemStack(meleeBootsItemStack));
    45. return true;
    46. }
    47. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. badboysteee98 Actually, when you only use it once, there's no need to make a variable out of it, and he only used it once. Plus the "use this code" line you suggested doesn't make any sense anyway. And you don't let him know about checking for nulls.

    DeluXeNationMC Well you'd either copy the code, or you could look into dispatchCommand()
     
  5. Offline

    Rocoty

    badboysteee98 declaring a player variable and then assign it to itself casted to its own type...that's good practice? Am I missing something?
     
    AdamQpzm likes this.
  6. Offline

    DeluXeNationMC

    I would not copy the code i am here to learn. I would just like you to point me in the right direction :)
     
  7. DeluXeNationMC I meant copy it from the command that (I assume) you have already made.
     
  8. Offline

    DeluXeNationMC

    Oh sorry about that, read it wrong :(
    I copy/pasted the code:
    Code:java
    1. @EventHandler
    2. public void onSignChange(SignChangeEvent event){
    3. if (event.getLine(0).equalsIgnoreCase("[Melee]")) {
    4. event.setLine(0, "§3[Melee]");
    5. event.setLine(1, "§3Click here");
    6. }
    7. }
    8. @EventHandler
    9. public void onPlayerInteract(PlayerInteractEvent event) {
    10. if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    11. if (event.getClickedBlock().getState() instanceof Sign) {
    12. Sign s = (Sign) event.getClickedBlock().getState();
    13. if (s.getLine(0).equalsIgnoreCase("§3[Melee]")) {
    14. event.getPlayer().sendMessage("[" + ChatColor.GOLD + "DeluXePvP" + "" + ChatColor.GRAY + "]" + " You Were Given The Melee Kit!");
    15. Player player = (Player)event;
    16. Location location = player.getLocation();
    17. player.playSound(location, Sound.ITEM_PICKUP, 1, 1);
    18. player.getInventory().clear();
    19. player.getActivePotionEffects().clear();
    20. player.getInventory().addItem(new ItemStack(Material.IRON_SWORD));
    21. ItemStack meleeBootsItemStack = new ItemStack(Material.IRON_BOOTS, 1); //Melee's Boots
    22. meleeBootsItemStack.addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 4);
    23. ItemStack meleeHelmetItemStack = new ItemStack(Material.IRON_HELMET, 1); //Melee's Helmet
    24. meleeHelmetItemStack.addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 4);
    25. player.getInventory().setHelmet(new ItemStack(meleeHelmetItemStack));
    26. player.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    27. player.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    28. player.getInventory().setBoots(new ItemStack(meleeBootsItemStack));
    29. }
    30. }
    31. }

    But it only sends me a message [DeluXePvP] you were given the melee kit and displays an error in the console "Could not pass event playerinteractevent to deluxepvp eventexeption" :(

    I guess it would be easier to use dispatchCommand()
    but i have no clue on how to use it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  9. DeluXeNationMC Normally it's best to give the full stack trace so that we can know exactly what the problem is :) Luckily I don't need it in this case.

    PHP:
    Player player = (Player)event;
    Do you see what's wrong with trying to do this?

    Rocoty He just really wanted to make it clear it was a Player.
     
  10. Offline

    DeluXeNationMC

    I am casting the player to the event?
     
  11. DeluXeNationMC You're casting the event to a player. An event isn't a player, so you get a problem. You meant to use event.getPlayer()
     
  12. Offline

    TGRHavoc

    Surly you meant "Player player = event.getPlayer();"
     
  13. Offline

    DeluXeNationMC

    Wow... thanks that was dumb of me....
    it does not give any console errors anymore :) but it does not give me any item when i right click the sign.
     
  14. Offline

    TGRHavoc

    DeluXeNationMC
    Try adding some debug lines (Messages/Broadcasts) to see if the code is adding the items to the inventory or not.
     
  15. Offline

    DeluXeNationMC

    It is getting fired it sends me a message [DeluXePvP] You Were Given The Melee Kit.
     
  16. Offline

    TGRHavoc

    DeluXeNationMC likes this.
  17. Offline

    DeluXeNationMC

    Awesome ! it works i will keep the Player.updateInventory(); in mind from now on
    Thank you so much everyone that helped!
     
    AdamQpzm likes this.
  18. Offline

    TGRHavoc

Thread Status:
Not open for further replies.

Share This Page