Solved Creating Signs usable by OPs/players with perm

Discussion in 'Plugin Development' started by RAFA_GATO_FOFO, Apr 17, 2014.

Thread Status:
Not open for further replies.
  1. Hey guys,

    Here's the situation, say I have a plugin that transforms signs with the first line [Heal] to signs that actually heal you. How would I restrain the sign transformation to Operators?
    Is there a listener that can verify who is typing the sign lines?

    Here's the code I'm running now. This is a Listener Class (not the main one):
    Code:java
    1. package me.rafagatofofo.Medic;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.block.Sign;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.block.SignChangeEvent;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10.  
    11. public class MedicListeners implements Listener {
    12.  
    13. @EventHandler
    14. public void onSignChange(SignChangeEvent e){
    15. if (e.getLine(0).equalsIgnoreCase("[Heal]")){
    16. e.setLine(0, "§2[Heal]");
    17. e.setLine(1, "§aClick here");
    18. e.setLine(2, "§aTo be Healed!");
    19. }
    20. if (e.getLine(0).equalsIgnoreCase("[Feed]")){
    21. e.setLine(0, "§2[Feed]");
    22. e.setLine(1, "§aClick here");
    23. e.setLine(2, "§aTo be Fed!");
    24. }
    25. }
    26.  
    27. @EventHandler
    28. public void onPlayerInteract(PlayerInteractEvent e){
    29. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
    30. if (e.getClickedBlock().getState() instanceof Sign){
    31. Sign s = (Sign) e.getClickedBlock().getState();
    32. if(s.getLine(0).equalsIgnoreCase("§2[Heal]")){
    33. e.getPlayer().setHealth(20);
    34. e.getPlayer().sendMessage(ChatColor.GREEN + "Healed to full health!");
    35. }
    36. if(s.getLine(0).equalsIgnoreCase("§2[Feed]")){
    37. e.getPlayer().setFoodLevel(20);
    38. e.getPlayer().sendMessage(ChatColor.GREEN + "You've been fed!");
    39. }
    40. }
    41. }
    42. }
     
  2. Offline

    rfsantos1996

    To check who placed the sign and edited it click here, also, I would use ChatColor.strip(String str) so you don't need to check for "§2"
    Code:java
    1. if(ChatColor.stripColor(s.getLine(0)).equalsIgnoreCase("[Feed]")){
     
  3. Offline

    Trevor1134

    RAFA_GATO_FOFO This is the general idea, for certain events the player may be different instances such as Entity, but you can cast Entity to Player after a check.

    Player p = e.getPlayer();

    Code:
    if(p.isOp())
     
  4. rfsantos1996 Trevor1134

    Cheers guys, that was a dumb question. I can get the player from any event with e.getPlayer() :p My brain is mushy right now.

    One other thing, I actually know how to use this on my main class but on a listener class I don't know how to name the plugin on the first argument.I assumed this would work but it doesn't:
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.class, new Runnable() {
    2.  
    3. public void run() {
    4.  
    5. }
    6.  
    7. }, 0, 100);

    It says it's not applicable.

    Nevermind I found it:




    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page