Having trouble setting the lines on a sign

Discussion in 'Plugin Development' started by Prothean_IGS, Feb 3, 2014.

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

    Prothean_IGS

    Alright so I am trying to make it so that if a player places a sign with a certain lore, it'll automatically update the sign with the correct format of strings on it. I am using a BlockPlaceEvent but yet it doesn't seem to be working. Any ideas why?

    Here's my code:
    Code:java
    1. package me.InfiniumDevelopments.DarkRP;
    2.  
    3. import java.util.List;
    4. import java.util.Random;
    5.  
    6. import net.minecraft.server.v1_6_R3.Block;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Effect;
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.block.Sign;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.BlockPlaceEvent;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.PlayerInventory;
    21. import org.bukkit.inventory.meta.ItemMeta;
    22. import org.bukkit.potion.PotionEffect;
    23. import org.bukkit.potion.PotionEffectType;
    24. import org.bukkit.scheduler.BukkitScheduler;
    25.  
    26. public class DrugHandler implements Listener {
    27. public DarkRPMain plugin;
    28.  
    29. public PotionEffect withdraw1 = new PotionEffect(PotionEffectType.SLOW, 72000, 1);
    30. public PotionEffect withdraw2 = new PotionEffect(PotionEffectType.SLOW_DIGGING, 72000, 1);
    31. public PotionEffect withdraw3 = new PotionEffect(PotionEffectType.CONFUSION, 72000, 0);
    32.  
    33. public DrugHandler(DarkRPMain instance) {
    34. plugin = instance;
    35. }
    36.  
    37. @EventHandler
    38. public boolean onBlockPlace(BlockPlaceEvent event) {
    39. final Player player = (Player) event.getPlayer();
    40. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    41. ItemStack i = event.getItemInHand();
    42. ItemMeta im = i.getItemMeta();
    43. // Strings for the lores of special items ..
    44. String dl = "Generic Drug";
    45. if (i.getType() == Material.SIGN) {
    46. player.sendMessage("You placed a sign did you not?");
    47. if (im.getLore().contains(dl)) {
    48. Sign sign = (Sign) event.getBlockPlaced();
    49. sign.setLine(0, "[Drug Lab]");
    50. sign.setLine(1, player.getName());
    51. }
    52. }
    53.  
    54.  
    55. return false;
    56. }


    Here's my error saying it can not be passed to BlockPlaceEvent ( I see it says can not cast block to sign class, so how should I do this in order to be able ot set the lines on the sign?):
    Code:
    11:47:24 PM CONSOLE: WARN]: [WorldEdit] No compatible nms block class found.
    11:47:24 PM CONSOLE: ERROR]: Could not pass event BlockPlaceEvent to DarkRP v0.5
    11:47:24 PM CONSOLE: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.block.CraftBlock cannot be cast to org.bukkit.block.Sign
    11:47:25 PM CONSOLE: Method) ~[?:1.7.0]
    11:47:25 PM CONSOLE: Source) ~[?:1.7.0]
    11:47:25 PM CONSOLE: Source) ~[?:1.7.0]
    11:47:25 PM CONSOLE: Source) ~[?:1.7.0]
    11:47:34 PM CONSOLE: ERROR]: Could not pass event BlockPlaceEvent to DarkRP v0.5
    11:47:35 PM CONSOLE: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.block.CraftBlock cannot be cast to org.bukkit.block.Sign
    11:47:35 PM CONSOLE: Method) ~[?:1.7.0]
    11:47:35 PM CONSOLE: Source) ~[?:1.7.0]
    11:47:35 PM CONSOLE: Source) ~[?:1.7.0]
    11:47:35 PM CONSOLE: Source) ~[?:1.7.0]
    11:48:11 PM CONSOLE: ERROR]: Could not pass event BlockPlaceEvent to DarkRP v0.5
    11:48:12 PM CONSOLE: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.block.CraftBlock cannot be cast to org.bukkit.block.Sign
    11:48:12 PM CONSOLE: Method) ~[?:1.7.0]
    11:48:12 PM CONSOLE: Source) ~[?:1.7.0]
    11:48:12 PM CONSOLE: Source) ~[?:1.7.0]
    11:48:12 PM CONSOLE: Source) ~[?:1.7.0]
     
  2. Offline

    Bloxcraft

    You need to use SignChangeEvent. BlockPlaceEvent gets called when they right-click with the sign.
     
  3. Offline

    xTrollxDudex

    Bloxcraft
    What....?

    Prothean_IGS
    Cast Sign to the placed block's state, not the block itself. Like so:
    PHP:
    Sign sign = (Signevent.getBlockPlaced().getState();
     
  4. Offline

    Bloxcraft


    Sorry, I meant SignChangeEvent gets called when the player clicks the "Done" button after placing the sign,
    while BlockPlaceEvent gets called when the Sign GUI is opened.
     
  5. Offline

    Prothean_IGS

    xTrollxDudex I tried your method, but it didn't work, the sign did not change to what it was supposed to be, I'm guessing because of the gui that opens to write on the sign, is there anyway to cancel that from opening? Bloxcraft So if I use your method how could I check to see if the sign the player used had a specific lore? Because I do not want this to happen every time someone places a sign, just when the sign has a specific lore attached to it.
     
  6. Offline

    xTrollxDudex

  7. Offline

    Prothean_IGS

    xTrollxDudex Yes but how would I check to see if the sign had a specific lore? player.getItemInHand()?
     
  8. Offline

    xTrollxDudex

  9. Offline

    Prothean_IGS

  10. Offline

    xTigerRebornx

    Prothean_IGS When you tested the code you had after TrollDude helped, how were you placing the sign? Because in the code, you only check for the 1 type of Sign material, when there are 3 different signs (WALL_SIGN, SIGN, etc) Try checking if the Block's state is an instanceof Sign, then change it. For the SignChangeEvent, TrollDude is saying that you'd have to log when they place the sign, and check if it is there then, because you cannot get it from the SignChangeEvent itself, so you'd need to "track" the Sign to see if it was placed with the lore
     
  11. Offline

    Bloxcraft

    Use BlockPlaceEvent, check the lore, add them to an ArrayList if they have the lore, then use SignChangeEvent to check if they are in the ArrayList, then remove them from the ArrayList.
     
Thread Status:
Not open for further replies.

Share This Page