Help with signs and enchants

Discussion in 'Plugin Development' started by drpk, Jul 6, 2014.

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

    drpk

    Code:java
    1. package drpk;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Sign;
    6. import org.bukkit.enchantments.Enchantment;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.block.SignChangeEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15.  
    16. public class AddFortune1 extends JavaPlugin implements Listener {
    17.  
    18.  
    19. @EventHandler
    20. public void onSignChange(SignChangeEvent e) {
    21. if (e.getLine(0).equalsIgnoreCase("[etoken]"))
    22. if (e.getLine(1).equalsIgnoreCase("[Fortune1]"))
    23. e.setLine(0, "&3[etoken]");
    24. e.setLine(1, "&3[Fortune1]");
    25. e.setLine(2, "&3Click here with 2 etokens!");
    26. e.setLine(3, "&3to get an extra level of fortune!");
    27. }
    28.  
    29.  
    30. @EventHandler
    31.  
    32. public void onPlayerInteract(PlayerInteractEvent evt) {
    33. if (evt.getPlayer().getInventory().getType().equals(Material.RECORD_12));
    34.  
    35. //do whatever
    36.  
    37. // code
    38. {
    39. //makes sure it's an event
    40. if (evt.getAction() != Action.RIGHT_CLICK_BLOCK)
    41. if (evt.getClickedBlock().getState() instanceof Sign) {
    42. //if player right clicks sign with the set lines
    43. Sign s = (Sign) evt.getClickedBlock().getState();
    44. if (s.getLine(1).equalsIgnoreCase("[eTokens]")) {
    45. if(evt.getPlayer().getItemInHand().getType().equals(Material.DIAMOND_PICKAXE));{
    46. if(evt.getPlayer().getItemInHand().getType().equals(Material.GOLD_PICKAXE)); {
    47. if(evt.getPlayer().getItemInHand().getType().equals(Material.IRON_PICKAXE)); {
    48. if(evt.getPlayer().getItemInHand().getType().equals(Material.STONE_PICKAXE)); {
    49. if(evt.getPlayer().getItemInHand().getType().equals(Material.WOOD_PICKAXE)); {
    50. ItemStack item = evt.getPlayer().getItemInHand();
    51. evt.getPlayer().getInventory().removeItem(new ItemStack(Material.RECORD_12, 2));
    52. if (item != null) { // So you don't get errors in the console about NullErrorException
    53. if (item.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)) { // check if item in hand has fortune enchantment
    54. item.addEnchantment(Enchantment.LOOT_BONUS_BLOCKS, item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS) + 1); // Add the same enchantment but with level increase of +1
    55.  
    56. }
    57.  
    58. }
    59. }
    60. }
    61. }
    62.  
    63.  
    64. }
    65. }
    66. }
    67. }
    68. //they get healed
    69. evt.getPlayer().sendMessage(ChatColor.GREEN + "Your tool has 1 more level of fortune!");
    70. //message sent when healed
    71. }
    72. }
    73. }
    74.  
    75.  


    the signs wont make or enchant, I'm using Bukkit-1.7.2-R0.2
    there's also nothing in console that would suggest it wouldn't work.
     
  2. Offline

    Gater12

  3. Offline

    UaVxChallenge

    Also for the sign change event do this:

    Code:java
    1. if (e.getLine(0).equalsIgnoreCase("[etoken]") && (e.getLine(1).equalsIgnoreCase("[Fortune1]")){
    2. // What you're changing the signs into
    3. }
    4.  
    5.  


    It might not help but it will make it a little cleaner! Plus when you're getting the sign in the interact you have to get the color too or at least strip it of its color. Another problem is that you are getting the wrong line in the clicked block even line 1 is your fortune line not your [eToken] line which is line 0
     
  4. Offline

    Wizehh

    Don't be lazy; always use brackets, especially if you're a beginner.
     
  5. Offline

    UaVxChallenge

    The only time brackets are not necessary is when there is only one line of code after it.
     
  6. Offline

    Wizehh

    It's best not to omit them at all; the little time you save by doing so is made worthless when you run into unexpected logical errors. In general, especially for a beginner, it's best to always use brackets.
     
  7. Offline

    drpk

    Still wont work :/
     
  8. Offline

    drpk

    bump
     
  9. Offline

    xTigerRebornx

    drpk What "doesn't work" about it? Post explanation of how it "doesn't work" and the code you have.
     
  10. Offline

    drpk

    here's my code
    Code:java
    1. package com.drpk;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Sign;
    6. import org.bukkit.enchantments.Enchantment;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.block.SignChangeEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.inventory.ItemStack;
    13.  
    14.  
    15. public class AddFortune1 implements Listener {
    16.  
    17.  
    18. @EventHandler
    19. public void onSignChange(SignChangeEvent e) {
    20. if (e.getLine(0).equalsIgnoreCase("[etoken]"))
    21. if (e.getLine(1).equalsIgnoreCase("[Fortune1]"))
    22. e.setLine(0, "§3[etoken]");
    23. e.setLine(1, "§3[Fortune1]");
    24. e.setLine(2, "§3Click here with 2 etokens!");
    25. e.setLine(3, "§3to get an extra level of fortune!");
    26. }
    27.  
    28.  
    29. @EventHandler
    30.  
    31. public void onPlayerInteract(PlayerInteractEvent evt) {
    32. if (evt.getPlayer().getInventory().getType().equals(Material.RECORD_12));
    33.  
    34. //do whatever
    35.  
    36. // code
    37. {
    38. //makes sure it's an event
    39. if (!(evt.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    40. if (evt.getClickedBlock().getState() instanceof Sign) {
    41. //if player right clicks sign with the set lines
    42. Sign s = (Sign) evt.getClickedBlock().getState();
    43. if (s.getLine(1).equalsIgnoreCase("§3[Fortune1]")) {
    44. if (evt.getPlayer().getItemInHand().getType().equals(Material.DIAMOND_PICKAXE));{
    45. if (evt.getPlayer().getItemInHand().getType().equals(Material.GOLD_PICKAXE)); {
    46. if (evt.getPlayer().getItemInHand().getType().equals(Material.IRON_PICKAXE)); {
    47. if (evt.getPlayer().getItemInHand().getType().equals(Material.STONE_PICKAXE)); {
    48. if (evt.getPlayer().getItemInHand().getType().equals(Material.WOOD_PICKAXE)); {
    49.  
    50. }
    51.  
    52. }
    53. }
    54. }
    55. }
    56.  
    57. ItemStack item = evt.getPlayer().getItemInHand();
    58. if (item != null) { // So you don't get errors in the console about NullErrorException
    59. if (item.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)) { // check if item in hand has fortune enchantment
    60. item.addEnchantment(Enchantment.LOOT_BONUS_BLOCKS, item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS) + 1); // Add the same enchantment but with level increase of +1
    61. }
    62. }
    63. }
    64. }
    65. //they get healed
    66. evt.getPlayer().sendMessage(ChatColor.GREEN + "Your tool has 1 more level of fortune!");
    67. //message sent when enchanted
    68. }
    69. }
    70. }
    71.  
    72.  
    nothing will go wrong in console and the signs wont work.
     
  11. Code:java
    1. @EventHandler
    2. public void onSignChange(SignChangeEvent e) {
    3. if (e.getLine(0).equalsIgnoreCase("[etoken]"))
    4. if (e.getLine(1).equalsIgnoreCase("[Fortune1]")){
    5. e.setLine(0, "§3[etoken]");
    6. e.setLine(1, "§3[Fortune1]");
    7. e.setLine(2, "§3Click here with 2 etokens!");
    8. e.setLine(3, "§3to get an extra level of fortune!");
    9. }
    10. }
     
  12. Offline

    NonameSL

    Learn java first.
    You have so many problems with your code, so many...
    Such as:

    Code:java
    1. if(evt.getPlayer().getInventory().getType().equals(Material.RECORD_12));

    Declaring an if statement and putting a semicolon in the end, so it does absolutely nothing
    Code:java
    1. if(e.getLine(0).equalsIgnoreCase("[etoken]"))
    2. if(e.getLine(1).equalsIgnoreCase("[Fortune1]"))



    Declaring 2 if statements instead of one with &&, forgetting to put a bracket in the end of an if statement so it only does the line of code after that line.
    Code:java
    1. if(evt.getPlayer().getItemInHand().getType().equals(Material.DIAMOND_PICKAXE));{
    2. if(evt.getPlayer().getItemInHand().getType().equals(Material.GOLD_PICKAXE));{
    3. if(evt.getPlayer().getItemInHand().getType().equals(Material.IRON_PICKAXE));{
    4. if(evt.getPlayer().getItemInHand().getType().equals(Material.STONE_PICKAXE));{
    5. if(evt.getPlayer().getItemInHand().getType().equals(Material.WOOD_PICKAXE));{
    6.  
    7. }
    8.  
    9. }
    10. }
    11. }
    12. }


    Putting a semicolon then a bracket in the end of an if statement, making TONS of if statements instead of just checking if the getType().toString().contains("PICKAXTE"), putting if statements for no reasons (you are closing them right after checking them)

    Code:java
    1. //makes sure it's an event
    2. //they get healed



    Besides that, you are making a bunch of false comments - checking the action, is not making sure it's an event, sending a player a message, does not heal them, etc. It is not a problem but it makes your code extremely messy.

    Code:java
    1. e.setLine(0, "§3[etoken]");
    2. e.setLine(1, "§3[Fortune1]");
    3. e.setLine(2, "§3Click here with 2 etokens!");
    4. e.setLine(3, "§3to get an extra level of fortune!");



    Also, you cant put a line that is greater than 16 characters in a sign. But that won't matter, because of you forgot a bracket up there in the if statment, so it will only set the first line ([etoken]) of the statement, and all the others - it will set them no matter what.

    Code:java
    1. package drpk;

    Your package name should be:
    Code:
    me.drpk.<YOURPLUGINNAME>
    Also, your spacing is horrible.
    Conclousion: Learn Java, then learn Bukkit, then make plugins.
     
Thread Status:
Not open for further replies.

Share This Page