BlockRedstoneEvent

Discussion in 'Plugin Development' started by bigboy2013, Oct 30, 2012.

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

    bigboy2013

    I've been randomly testing things on plugins, but have come across a problem with the BlockRedstoneEvent listener. I was trying to play around with it, but for some reason it seems it fires off more than once. Here is what I tried:
    Code:
    @EventHandler
        public void onRedstone(BlockRedstoneEvent event) {
            if(event.getBlock().getType() == Material.WALL_SIGN) {
                Sign sign = (Sign) event.getBlock().getState();
                if(sign.getLine(0).equals("§f[DuckHunt]")) {
                    plugin.getServer().broadcastMessage("Test");
                    if(sign.getLine(1).equals("§cOff")) {
                        plugin.getServer().broadcastMessage("Off");
                        org.bukkit.material.Sign matsign = (org.bukkit.material.Sign) sign.getData();
                        Block block = event.getBlock().getRelative(matsign.getAttachedFace());
                        sign.setLine(1, "§aOn");
                        sign.update();
                        block.setType(Material.REDSTONE_LAMP_ON);
                    }
                } else if(sign.getLine(0).equals("§f[DuckHunt]")) {
                    plugin.getServer().broadcastMessage("Test");
                    if(sign.getLine(1).equals("§aOn")) {
                        plugin.getServer().broadcastMessage("On");
                        org.bukkit.material.Sign matsign = (org.bukkit.material.Sign) sign.getData();
                        Block block = event.getBlock().getRelative(matsign.getAttachedFace());
                        sign.setLine(1, "§cOff");
                        sign.update();
                        block.setType(Material.REDSTONE_LAMP_OFF);
                    }
                }
            }
        }
    When I try hooking up redstone to it and using a lever, (NOTE: I made it so I must place the sign on a glowstonelamp), the redstonelamp it is connected to quickly flashing on then turns off in about a second. the line Off turns to On, but doesnt turn to off again. In fact, the sign wont change again after that. My code isn't exactly great, I'm just trying to test random things, but I couldn't understand my problem here. Here is the rest of the file:

    Code:
    package com.bigboy2013;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockRedstoneEvent;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.util.Vector;
     
    public class SignListener implements Listener {
     
        public BSCtest plugin;
     
        public SignListener(BSCtest instance) {
            plugin = instance;
        }
     
        @EventHandler(priority = EventPriority.HIGH)
        public void onSignFinish(SignChangeEvent event) {
            Player player = event.getPlayer();
            Sign sign = (Sign) event.getBlock().getState();
            Block error = event.getBlock();
            if(event.getLine(0).equals("[DuckHunt]")) {
                if(event.getBlock().getType() == Material.WALL_SIGN) {
                    org.bukkit.material.Sign matsign = (org.bukkit.material.Sign) sign.getData();
                    Block block = event.getBlock().getRelative(matsign.getAttachedFace());
                    if(block.getType() == Material.REDSTONE_LAMP_OFF) {
                        event.setLine(0, "§f[DuckHunt]");
                        event.setLine(1, "§cOff");
                        player.sendMessage(ChatColor.GREEN + "Correct sign placement!");
                    } else if(block.getType() == Material.REDSTONE_LAMP_ON) {
                        event.setLine(0, "§f[DuckHunt]");
                        event.setLine(1, "§aOn");
                        player.sendMessage(ChatColor.GREEN + "Correct sign placement!");
                        Entity chicken = error.getWorld().spawnEntity(new Location(error.getWorld(), error.getX(), error.getY() + 1, error.getZ()), EntityType.CHICKEN);
                        chicken.setVelocity(new Vector(0, 10, 5));
                    } else {
                        player.sendMessage(ChatColor.RED + "Incorrect sign placement!");
                        error.getWorld().dropItem(error.getLocation(), new ItemStack(Material.SIGN));
                        error.setType(Material.AIR);
                    }
                } else {
                    player.sendMessage(ChatColor.RED + "Incorrect sign placement!");
                    error.getWorld().dropItem(error.getLocation(), new ItemStack(Material.SIGN));
                    error.setType(Material.AIR);
                }
            }
        }
     
        @EventHandler
        public void onRedstone(BlockRedstoneEvent event) {
            if(event.getBlock().getType() == Material.WALL_SIGN) {
                Sign sign = (Sign) event.getBlock().getState();
                if(sign.getLine(0).equals("§f[DuckHunt]")) {
                    plugin.getServer().broadcastMessage("Test");
                    if(sign.getLine(1).equals("§cOff")) {
                        plugin.getServer().broadcastMessage("Off");
                        org.bukkit.material.Sign matsign = (org.bukkit.material.Sign) sign.getData();
                        Block block = event.getBlock().getRelative(matsign.getAttachedFace());
                        sign.setLine(1, "§aOn");
                        sign.update();
                        block.setType(Material.REDSTONE_LAMP_ON);
                    }
                } else if(sign.getLine(0).equals("§f[DuckHunt]")) {
                    plugin.getServer().broadcastMessage("Test");
                    if(sign.getLine(1).equals("§aOn")) {
                        plugin.getServer().broadcastMessage("On");
                        org.bukkit.material.Sign matsign = (org.bukkit.material.Sign) sign.getData();
                        Block block = event.getBlock().getRelative(matsign.getAttachedFace());
                        sign.setLine(1, "§cOff");
                        sign.update();
                        block.setType(Material.REDSTONE_LAMP_OFF);
                    }
                }
            }
        }
    }
    
    I already registered the event and everything. No errors pop up in console. Also, everytime I flip the lever, it says Test twice, both on and off. It also only says Off when it goes from off to on, but i cant get it to go from on to off.
     
  2. Offline

    Hoolean Retired Staff

    Offtopic: Sorry couldn't help noticing it said [DuckHunt]! What are you making?
     
  3. Offline

    Limeth

    Not sure, but I think that the BlockRedstoneEvent is fired whenever the redstone is toggled (off > on, on > off)
     
  4. Offline

    bigboy2013

    I actually wanted to eventually make a plugin like chicken hunt: ChickenHunter
    It has been inactive a long time, and I always wanted someone to revive it. (I've tried searching for an alternative, but couldn't really find any).

    But for some reason, it seems to fire off TWICE each time it is toggled on to off. Everytime I hit the lever, the message "Test" is displayed twice. Also, I tried testing newCurrent and oldCurrent, and both seem to always be the same. Is BlockRedstoneEvent what I should be using for something like this?
     
    MrBluebear3 likes this.
  5. Offline

    illegalprime

    BUMP please! This has been a problem for me as well, I have no idea to discern between the two events, I've tried almost anything. I think it is because one event fires when the redstone notifies the block being powered, and the other is when the block becomes powered. Here is some output from my test server showing the events:
    Code:
    16:51:30 [INFO] [Zeus] New Current: 15
    16:51:30 [INFO] [Zeus] Old Current: 15
    16:51:30 [INFO] [Zeus] Metadata:    5
    16:51:30 [INFO] [Zeus] Powered:    false
    16:51:30 [INFO] [Zeus] Powered Ind: true
    16:51:30 [INFO] [Zeus] Event Name:  BlockRedstoneEvent
    16:51:30 [INFO] [Zeus] Hash Code:  1523332131
    16:51:30 [INFO] [Zeus] Async:      false
    16:51:30 [INFO] [Zeus] Light Level: 15
    16:51:30 [INFO] [Zeus] New Current: 15
    16:51:30 [INFO] [Zeus] Old Current: 15
    16:51:30 [INFO] [Zeus] Metadata:    5
    16:51:30 [INFO] [Zeus] Powered:    false
    16:51:30 [INFO] [Zeus] Powered Ind: true
    16:51:30 [INFO] [Zeus] Event Name:  BlockRedstoneEvent
    16:51:30 [INFO] [Zeus] Hash Code:  343086183
    16:51:30 [INFO] [Zeus] Async:      false
    16:51:30 [INFO] [Zeus] Light Level: 15
    16:51:31 [INFO] [Zeus] New Current: 0
    16:51:31 [INFO] [Zeus] Old Current: 0
    16:51:31 [INFO] [Zeus] Metadata:    5
    16:51:31 [INFO] [Zeus] Powered:    false
    16:51:31 [INFO] [Zeus] Powered Ind: false
    16:51:31 [INFO] [Zeus] Event Name:  BlockRedstoneEvent
    16:51:31 [INFO] [Zeus] Hash Code:  255885486
    16:51:31 [INFO] [Zeus] Async:      false
    16:51:31 [INFO] [Zeus] Light Level: 15
    16:51:31 [INFO] [Zeus] New Current: 0
    16:51:31 [INFO] [Zeus] Old Current: 0
    16:51:31 [INFO] [Zeus] Metadata:    5
    16:51:31 [INFO] [Zeus] Powered:    false
    16:51:31 [INFO] [Zeus] Powered Ind: false
    16:51:31 [INFO] [Zeus] Event Name:  BlockRedstoneEvent
    16:51:31 [INFO] [Zeus] Hash Code:  1405101908
    16:51:31 [INFO] [Zeus] Async:      false
    16:51:31 [INFO] [Zeus] Light Level: 15
    
     
Thread Status:
Not open for further replies.

Share This Page