How i get a certain sign?

Discussion in 'Plugin Development' started by Gonmarte, Oct 30, 2015.

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

    Gonmarte

    Hi guys,
    When i click with a right click on a sigh it would run something, but i want to check the sign, and i dont know how i check this sign that i have created :
    Code:
        @EventHandler
        public void onSignChange(SignChangeEvent event){
           
        if(event.getLine(0).equalsIgnoreCase("test")){
                    event.setLine(0, "Help is apreciate =)");
          
                     
            }
           
        }
    
    Thank you!
     
  2. you need to use event.getSign().update(); after making changes to it
     
  3. Offline

    Gonmarte

    @FisheyLP Thank u! But do you know how can i get that sign?
     
  4. @Gonmarte
    SignChangeEvent extends BlockEvent, which has a method called getBlock(). To get the block, use
    Code:
    Block block = event.getBlock();
    You can then cast the block's state (Block#getState()) to Sign.
    Code:
    Sign sign = (Sign) block.getState();
     
  5. Offline

    Gonmarte

    @Assist But i dont wannt the block.
    When this event that ill show you down here, it runs when i click in a sign, doesnt matter what sign it is, can be any1. I want that, the sign needs to have writeen in the 0 line what i set in the 0 line.
    Code:
        @EventHandler
        public void on(PlayerInteractEntityEvent event){
            if(event.getRightClicked() instanceof Sign){
               
            }
        }
    
    
     
  6. @Gonmarte
    I don't quite understand what you mean due the language barrier (your english is bad, no offence). But if I understood right, you want to do something only when the sign contains specific text on the first line? You can use the code I showed in my previous post to get the Sign object of the block. Sign class has a method called getLine(index) that returns the current text on the specified line on the sign. For example:
    Code:
    Sign sign = (Sign) block.getState();
    
    if(sign.getLine(0).equals("Hello")) {
        // Line 1 on the sign is "Hello"
    }
     
  7. @Gonmarte

    I can't quite understand what you are getting at here, but check out my code from one of my plugins:

    Code:
        @EventHandler
        public void signUpdateEvent(SignChangeEvent e) {
            if (e.getLine(0) == null) return;
            if (e.getLine(0).equalsIgnoreCase("%rpsign%")) {
                e.setLine(0, "Resource");
            }
        }
    If, when a sign is placed, the first line is "%rpsign%", it will change the first line to "Resource"
     
  8. Offline

    Gonmarte

    @Assist @HeyAwesomePeople
    Forget everything that i sad.
    Lets start again!
    Firstoble, i need to change the sign when i create one with this event:
    Code:
    [LIST=1]
    [*]@EventHandler
    [*]   public void sign(SignChangeEvent e) {
    [*]    
    [*]       if (e.getLine(0).equalsIgnoreCase("hi")) {
    [*]            e.setLine(0, "yes");
    [*]       }
    [*]   }
    [/LIST]
    
    
    So, what im doing is when i place a sign, and if i write at the first line ( line 0 in java) "hi" it will create a sign, and in the first line (line 0 ) it will show "yes".
    NOW, lets say that i want to make another event AND when i right click in THAT sign it would give me an apple. THE PROBLEM is that i cant figerout how i get, in this e.g, a sign with "yes" in the line 0! I just can make the event for all the signs, not for the sign that i creates, ( the one that says "yes" in line 0 ).
    Did you understand??
     
  9. @Gonmarte
    You'd probably want to save the location of the sign somewhere, and in PlayerInteractEvent, compare the clicked block's location to the saved location to see if the clicked block is the sign you want. Pseudo code:
    Code:
    private Sign sign;
    
    on sign change event {
        if line 0 is "hi" {
            set line 0 to "yes"
            sign = (Sign) event.getBlock().getState()
        }
    }
    
    on player interact event {
        if event.getClickedBlock().getLocation() == sign.getLocation() {
            // do something
        }
    }
     
    Last edited: Oct 30, 2015
  10. Offline

    Gonmarte

    @Assist Tank you, im going to try =)

    @Assist

    When i place aa sign, even before i right on the sign it runs this error on the consolve:
    Code:
    [23:00:36 ERROR]: Could not pass event PlayerInteractEvent to Powerful Tools v1.
    0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b29
    74jnks]
            at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInt
    eractManager.java:374) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
            at me.gonmarte.listeners.SignShop.on(SignShop.java:39) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _60]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _60]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_60]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            ... 15 more
    
    
    The event:
    Code:
    
    @EventHandler
    public void onA(PlayerInteractEvent event)
    {
    if(event.getAction()==Action.RIGHT_CLICK_BLOCK || event.getAction()==Action.LEFT_CLICK_BLOCK){
    if(event.getClickedBlock().getLocation()==sign.{
    event.getPlayer().sendMessage("hi);
    }
    }
    
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2015
  11. @Gonmarte
    First of all, you have unresolved compilation errors in your code. Second, the sign variable is most likely null. You should first make sure that the sign isn't null.
    Code:
    @EventHandler
    public void onA(PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_BLOCK) {
            if (sign != null) {
                if (event.getClickedBlock().getLocation() == sign.getLocation()) {
                    event.getPlayer().sendMessage("hi);
                }
            }
        }
    }
     
  12. Offline

    Gonmarte

    @Assist Ok, now i cant test it, but when i do it ill share you the results!
     
  13. Offline

    tkuiyeager1

    @FisheyLP if i dont use event.getSign().update(); it won't save him after the server has been reloaded/stopped?
     
  14. Offline

    DeJay007

    @Gonmarte
    Code:
        @EventHandler
        public void onSignChange(SignChangeEvent e) {
                if (e.getLine(0).equalsIgnoreCase("[Start]")) {
                        e.setLine(0, "§4[Start]");
                        e.setLine(1, "§8Click here");
                        e.setLine(2, "§8to get started!");
                }
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
                if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
                if (e.getClickedBlock().getState() instanceof Sign) {
                        Sign s = (Sign) e.getClickedBlock().getState();
                        if (s.getLine(0).equalsIgnoreCase("§4[Start]")) {
                                            e.getPlayer().sendMessage("Edit");
        }
                }
        }
    }
    All of the code to get a sign where you type [Start] on top line and it will change it to a set sign. and when its clicked it will do whatever you have defined
     
  15. Wtf what a spoonfeed... And it isn't even made very good. When changing the text on a sign in the SignChangeEvent you need to update the sign. Also if(!(something == something)) should be if (something != something)
     
Thread Status:
Not open for further replies.

Share This Page