Why doesnt it work ?

Discussion in 'Plugin Development' started by killerrj8, May 14, 2011.

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

    killerrj8

    cause i wanted to test sumthing . So my code is this
    Code:
          public void onBlockPlace(BlockPlaceEvent event){
                Player player = event.getPlayer();
                Block block = event.getBlock();
                Material mat = block.getType();
                Location playerLoc = player.getLocation();
                if(block.getType() == Material.TNT) {
    
                getServer().broadcastMessage(player.getName() + ChatColor.DARK_RED + "placed TNT at" + playerLoc.getX() + playerLoc.getY() + playerLoc.getZ());
              }
    
            }
    but when i place TNT in game it doesnt show a message as it should
     
  2. did you register the event in the onEnable() ?
     
  3. Offline

    aPunch

    Register the event in onEnable() in your main class.

    Code:
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.BLOCK_PLACE, listenerClassInstanceGoesHere, Event.Priority.Normal, this);
     
  4. Offline

    0ffn00b

    yea you have to register the event as said above and also why not use your variable mat in your if statement instead of block.getType()
     
  5. Offline

    DreadKyller

    also, you should use event.getBlock.getLocation() instead of the player location.
     
  6. Offline

    killerrj8

    k thanks
    ill try

    hmm but when i do it in the listener
    Code:
    getServer().broadcastMessage(player.getName() + ChatColor.DARK_RED + "placed TNT at" + playerLoc.getX() + playerLoc.getY() + playerLoc.getZ());
              }
    doesnt work anymore. it gets underlined and says : "The method getServer() is undefined for the type TestBlockListener"
    so my Plugin is called Test thats why... yea its called TestBlockListener.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 16, 2016
  7. Offline

    mindless728

    thats because 1) BlockListener that TestBlockListener extends does not contain a method called getServer() and i am assuming you didn't add a method called

    the reason why plugin classes get getServer() is because they extend JavaPlugin that does contain getServer() so the plugin inherits it from JavaPlugin

    the way i solve this is by making a constructor for my listeners that takes the plugin
    in your case:
    Code:
    public class TestBlockListener extends BlockListener {
        TestPlugin plugin;
        public TestBlockListener(TestPlugin p) {
            plugin = p;
        }
    
        public void onBlockPlavce(BlockPlaceEvent event) {
            Player player = event.getPlayer();
            Block block = event.getBlock();
            Material mat = block.getType();
            Location playerLoc = player.getLocation();
            if(mat = Material.TNT) {
                plugin.getServer().BroadcastMessage("TNT Placed"); //change to your message here
            }
        }
    }
    
     
  8. Offline

    killerrj8

    of first of all Thanks mindless. But it still doesnt work i can still place TNT without its being said to the server . I dont get errors in Eclipse and it is in a BlockListener. And how i said i doesnt get any Errors in Eclipse it jsut doesnt pop up in game :(

    EDIT: Oh yes, i forgot i get an Error in the Server. in CMD.
     
  9. Offline

    DreadKyller

    do you register the event?

    PHP:
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import blah.yourname.TestBlockListener;

    public class 
    TestPlugin extends JavaPlugin{
        public 
    TestBlockListener tbl = new TestBlockListener(this);
        public 
    void onDisable(){
            
    System.out.println("TestPlugin Disabled");
        }
        public 
    void onEnable(){
            
    System.out.println("TestPlugin Enabled");
            
    PluginManager pm getServer().getPluginManager();
            
    pm.registerEvent(Event.Type.BLOCK_PLACEtblEvent.Priority.Normalthis);
        }
    }
    And post the server error please.
     
  10. Offline

    killerrj8

    Sure i registered the Event. but an Error pops up . Error Code below

    Code:
    2011-05-15 18:04:02 [SEVERE] Could not pass event BLOCK_PLACE to BroadCaster
    java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader$25.execute(JavaPluginLoader.java:352)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:59)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:257)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:84)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:68)
        at net.minecraft.server.ItemBlock.a(ItemBlock.java:74)
        at net.minecraft.server.ItemStack.placeItem(ItemStack.java:56)
        at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:217)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:519)
        at net.minecraft.server.Packet15Place.a(SourceFile:57)
        at net.minecraft.server.NetworkManager.a(NetworkManager.java:195)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:74)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:100)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:370)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:285)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 16, 2016
  11. Offline

    DreadKyller

    post your main code as well, it might help us to see it.
     
  12. Offline

    killerrj8

    which Main Code ?
    u mean the whole onEnabled onDisable and onInitialize stuff ?
    everything besides the Listeners ?
     
  13. Offline

    DreadKyller

    your main class and the listener if possible, if the listener hasn't changed don't worry cause it's at top and you not need to post it, it just might help.
     
  14. Offline

    killerrj8

    ok some are not necessary for my problem but Ill write everything :
    Code:
    package me.Fredbuffer96.Test;
    
    import java.io.File;
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.block.BlockPlaceEvent;
    public class Test extends JavaPlugin{
        private TestPlayerListener playerListener = null;
        private TestBlockListener blockListener = null;
        private static Logger log = Logger.getLogger("Minecraft");
         public static String name = "Test";
         public static String version = "1.1";
    
         public static File configFile = new File("plugins/Test/config.yml");
            Configuration config = new Configuration(configFile);
    
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            String[] split = args;
            String commandName = command.getName().toLowerCase();
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (commandName.equals("commands")) {
                        if (split.length == 0) {
                            player.sendMessage(ChatColor.RED + "Test...Lol");
                        }
      
                    }
                    }
    
                String commandName1 = command.getName().toLowerCase();
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (commandName1.equals("send")) {
                        if (split.length == 0) {
                            try {
                            getServer().broadcastMessage(ChatColor.BLUE + config.getString("commands.send")); }
                            catch(Exception x) { System.out.println("Error reading Config!"); }
    
                        }
                    }
                }
                return true;
    
            }
        
        public void onDisable() {
            // TODO Auto-generated method stub
            log.info(name + " Version " + version + " deaktiviert.");
        }
    
        public void onEnable() {
            // TODO Auto-generated method stub
            this.playerListener = new TestPlayerListener();
    
            registerHooks();
    
            new File("plugins/Testr").mkdir();
    
            File configFile = new File("plugins/Test/config.yml");
    
            if(!configFile.exists()){
    
                try { configFile.createNewFile(); } catch(Exception e){ System.out.println("Error when creating config file."); }
     
            }
            Configuration config = new Configuration(configFile);
            config.load();
    
            config.getProperty("commands.send");
            config.setProperty("commands.send","Willlkommen!");
    
            config.save();
            log.info(name + " Version " + version + " aktiviert.");
        }
        public void initialize() {
            log = Logger.getLogger("Minecraft");
            log.info(name + " version " + version + " initialized.");
    
        }
        public void registerHooks() {
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_KICK, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener, Priority.Normal, this);
     
        }
     
    }
    
     
  15. Offline

    DreadKyller

    onCommand must be in a completely different class file, because the main class can not extend java plugin and implement commandExecutor at the save time, I think that was said earlyer and you said you changed it. It will not work because onCommand you also never use getCommand("command").setExecutor(executor);

    so the command will not work first off.

    secondly I take it your TestBlockListener has changed right? could you please post that too.
     
  16. Offline

    nisovin

    This is not true. You can leave onCommand in your plugin class, as JavaPlugin is a CommandExecutor. It already implements that interface.

    @killerrj8 it looks like your block listener is never initialized, so it remains null. Unless I'm missing it.
     
  17. Offline

    DreadKyller

    @nisovin um, whenever I use onCommand in my main it gives me an error saying that "onCommand not implemented by Executor" so idk.

    @killerrj8 you need to change this:
    PHP:
    private TestPlayerListener playerListener null;
    private 
    TestBlockListener blockListener null;
    with
    PHP:
    private TestPlayerListener playerListener = new TestPlayerListener(this);
    private 
    TestBlockListener blockListener = new TestBlockListener(this);
     
  18. Offline

    killerrj8

    the commands arent necessary they work fine ^^
    ok @nisovin
    yea i changed that. but i still get the same Error here is my TestBlockListener Code:
    Code:
    public class TestBlockListener extends BlockListener{
    private Test plugin = null;
    
        public void TestBlockListener(Test plugin) {
            this.plugin = plugin;
        }
        public void onBlockPlace(BlockPlaceEvent event){
                Player player = event.getPlayer();
                Block block = event.getBlock();
                Material mat = block.getType();
                Location playerLoc = player.getLocation();
                Location blockLoc = block.getLocation();
                if(block.getType() == Material.TNT) {
                    plugin.getServer().broadcastMessage(player.getName() + ChatColor.DARK_RED + "placed TNT at" + ChatColor.YELLOW + blockLoc.getX() + blockLoc.getY() + blockLoc.getZ());
              }
    
            }
    }
     
  19. Offline

    0ffn00b

    well somewhere in your code your trying to use an object that has no value.

    from what i see this could be the error. When registering an event it should be this:
    pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener, Event.Priority.Normal, this);

    You were missing the Event before priority. Let me know if it works
     
  20. Offline

    DreadKyller

    @0ffn00b Priority.Normal works just as well and is shorter, you don't need the Event, of coarse it could be a good choice
     
  21. Offline

    killerrj8

    does it matter if i write blockListener OR BlockListener ?

    lol i cant get that to work :( now i get one more line in the error code this one:
    Code:
    at me.Fredbuffer96.Test.TestBlockListener.onBlockPlace
    what does this me.NAME.Plugin thing mean ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 16, 2016
  22. Offline

    DreadKyller

    ok, um, if you pm me the full code, I promise not to leech it, but I will find out what's wrong.

    just a few things to try first

    make sure your onBlockPlace has a parameter BlockPlaceEvent and not a different type.
    make sure you have initalized your blockListener
    make sure your block listener class extends BlockListener (Case sensitive)

    the me.name.plugin , for example, when I make a plugin i do it under org.styllex.whatever_my_plugin_is_called for a package, the org is usually like org, com, net, eu or au, the name is either your real name, your account here, or some name you go by. the plugin name should be simple enough, whatever you have you plugin named. now none of this is nessesary, you could have a package called mypackage and have everything in it, but this is reccommended.

    at earlyer :
    BlockListener is the class from bukkit that your listener must extend, unless your listener is called blockListener you'll use whatever it is the class is called.

    also if your script for your listener is still basically the same as above, another error is you're missing a "}"
    PHP:
        public void onBlockPlace(BlockPlaceEvent event){
                 
    Player player event.getPlayer();
                 
    Block block event.getBlock();
                 
    Material mat block.getType();
                 
    Location playerLoc player.getLocation();
                 
    Location blockLoc block.getLocation();
                 if(
    block.getType() == Material.TNT) {
                        
    plugin.getServer().broadcastMessage(player.getName() + ChatColor.DARK_RED "placed TNT at" ChatColor.YELLOW blockLoc.getX() + blockLoc.getY() + blockLoc.getZ());
                 }
             }
    }
    should be:

    PHP:
        public void onBlockPlace(BlockPlaceEvent event){
                 
    Player player event.getPlayer();
                 
    Block block event.getBlock();
                 
    Material mat block.getType();
                 
    Location playerLoc player.getLocation();
                 
    Location blockLoc block.getLocation();
                 if(
    block.getType() == Material.TNT) {
                        
    plugin.getServer().broadcastMessage(player.getName() +  ChatColor.DARK_RED "placed TNT at" ChatColor.YELLOW +  blockLoc.getX() + blockLoc.getY() + blockLoc.getZ());
                 }
             }
         }
     }
    otherwise the void never really finishes, it reaches the end of the script and is like "woah, what is next?" sort of. you have to close the void.
     
Thread Status:
Not open for further replies.

Share This Page