Stupid Noobie Problem

Discussion in 'Plugin Development' started by lpjz2, Mar 23, 2011.

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

    lpjz2

    Hey im starting to learn how to code plugins and i followed samkio's basic torch destroy and it worked fine.

    however i tryed to edit the plugin to make it give you a message when you place a torch instead of destroy one. but nothing is happing when i place a torch... here is some code...

    Code:
    public class TD extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        private final TDBlockListener blockListener = new TDBlockListener(this);
        public final HashMap<Player, ArrayList<Block>> tdUsers = new HashMap<Player, ArrayList<Block>>();
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener,
                    Event.Priority.Normal, this);
            log.info("TD STARTED");
        }
    
        public void onDisable() {
            log.info("TD DISABLED");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("TD")){
                toggleTD((Player) sender);
                return true;
            }
            return false;
        }
        public void toggleTD(Player player){
            if(enabled(player)){
                this.tdUsers.remove(player);
                player.sendMessage("TD Disabled");
            }else{
                this.tdUsers.put(player, null);
                player.sendMessage("TD Enabled");
            }
        }
        public boolean enabled(Player player){
            return this.tdUsers.containsKey(player);
        }
    }
    
    and my block listener...

    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.block.BlockListener;
    
    
    public class TDBlockListener extends BlockListener {
        public static TD plugin;
    
        public TDBlockListener(TD instance) {
            plugin = instance;
        }
        public void onBlockPlace(BlockPlaceEvent event){
            Block block2 = event.getBlockPlaced();
            Player player = event.getPlayer();
            if(block2.getType() == Material.TORCH && plugin.enabled(player)){
                player.sendMessage(ChatColor.BLUE + "You Placed a Torch!");
            }
        }
    }
    
    what am i doing wrong, i have changed all the BlockBreaks to BlockPlace?
     
  2. Offline

    Edward Hand

    Code:
    block2.getType() == Material.TORCH
    should be
    Code:
    block2.getType().equals(Material.TORCH)
     
  3. Offline

    Cheesier

    Have you tried log.info() printing anything right after onBlockPlace() to see if the hook fires

    Maybe the plugin.enabled(player) is is problem?
     
  4. Offline

    lpjz2

    yh even when i do this...

    Code:
    public void onBlockPlace(BlockPlaceEvent event){
            Block block2 = event.getBlockPlaced();
            Player player = event.getPlayer();
            if(block2.getType().equals(Material.TORCH) && plugin.enabled(player)){
                player.sendMessage(ChatColor.BLUE + "You Placed a Torch!");
            }
            else
                player.sendMessage(ChatColor.BLUE + "FAIL");
        }
    
    even if i place a torch or not nothing works, it doesnt even say fail....
     
  5. Offline

    Edward Hand

    In your registerEvent function:
    Code:
    BLOCK_PLACE
    should be:
    Code:
    BLOCK_PLACED
     
  6. Offline

    lpjz2

    BLOCK_PLACED doesnt exist, just tells me to rename it =S
     
  7. Offline

    robin0van0der0v

    Try this. ;)

    Code:
    public void onBlockPlace(BlockPlaceEvent event)
    {
        Block block2 = event.getBlockPlaced();
        Player player = event.getPlayer();
        if(plugin.enabled(player))
        {
            if (block2.getType().equals(Material.TORCH))
            {
                player.sendMessage(ChatColor.BLUE + "You Placed a Torch!");
            }
            else
            {
                player.sendMessage(ChatColor.BLUE + "FAIL");
            }
        }
    }
     
  8. Offline

    Edward Hand

    In version 556 its BLOCK_PLACED but in the latest version its BLOCK_PLACE

    I swear sometimes the bukkit team have a personal vendetta against plugin developers, breaking things for no apparent reason left right and centre.
    I'd rather minor grammatical inconsistencies than having to track down these little things at the whim of some n00b coder...
     
  9. Offline

    lpjz2

    not working, its not saying "FAIL" aswell so its got to be a problem with calling the event.

    I changed it so i tells me when i DESTROY dirt it says "You Broke some dirt" and when i break anything else accept dirt it says "fail" and that works fine, all i did what change all the PLACE to DESTROY and it worked!

    Code:
    public void onBlockBreak(BlockBreakEvent event)
        {
            Block block2 = event.getBlock();
            Player player = event.getPlayer();
            if(plugin.enabled(player))
            {
                if (block2.getType().equals(Material.DIRT))
                {
                    player.sendMessage(ChatColor.BLUE + "You Broke SOME DIRT!");
                }
                else
                {
                    player.sendMessage(ChatColor.BLUE + "FAIL");
                }
            }
        }
    }
    
    im reely confuzed... back to square one.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  10. Offline

    Edward Hand

    How strange.
    You can see what material it thinks you're placing with this:
    Code:
    System.out.println(event.getBlockPlaced().getType());
    (it will print to the server console)
    Perhaps that will give some clues.
     
  11. Offline

    lpjz2

    Ill do that now.

    AHHH!!! i see command errors when launching

    Code:
    17:59:30 [SEVERE] BLOCK_PLACE loading TD v1 (Is it up to date?)
    java.lang.NoSuchFieldError: BLOCK_PLACE
            at me.samkio.TD.TD.onEnable(TD.java:20)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:118)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:451)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:217)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:92)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:70)
            at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:260)
            at org.bukkit.command.SimpleCommandMap$ReloadCommand.execute(SimpleComma
    ndMap.java:196)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:80
    )
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:2
    21)
            at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:380)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:366)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:272)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:366)
    
    i think its reffering to line 20... this is whats on line 20....

    Code:
    pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener,
                    Event.Priority.Normal, this);
    


    Also nothing is printed when i destroy a block, this is what i added to the code and where...

    Code:
    public void onBlockPlace(BlockPlaceEvent event,Block placedBlock)
        {
            System.out.println(event.getBlockPlaced().getType());
    


    thanks for taking the time to help me by the way [cake]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  12. Offline

    Edward Hand

    This is probably what I was saying about the recent name change. Make sure your server is up to date.
     
  13. Offline

    lpjz2

    When i update to the Developers snapshot it works! Thanks!

    Come on Bukkit, lets add it to the "Recomened Build"
     
  14. Offline

    nomiros

    Hello. I know this topic is a bit old but I have the same problem for BLOCK_PLACE :

    here is my code for onEnable() :

    Code:
        public void onEnable() {
            // Register our events
            PluginManager pm = getServer().getPluginManager();
            getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PLACE, blockListener, Priority.Normal, this);
            getServer().getPluginManager().registerEvent(Event.Type.BLOCK_BREAK, blockListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Normal, this);
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            PluginDescriptionFile pdfFile = this.getDescription();
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
        }
    I've downloaded the latests versions of bukkit and craftBukkit (development snapshot), so I don't know how to resolve that -_-.

    I've noticed that in lpjz2's post, the error is "[SEVERE] BLOCK_PLACE ...", and I have " [GRAVE] BLOCK_PLACE ...".

    Can somebody help me ? Thanks.
     
  15. Offline

    cjc343

    Your server needs to be running a later version of CB or you need to not use the bukkit namespace.
     
  16. Offline

    nomiros

    I used theses links "Download CraftBukkit - Development Snapshot (12 March 2011, 19:46GMT)" and "Download Bukkit API - For Developers (12 March 2011, 15:01GMT)", so I hope theses versions are compatibles ...

    For the "namespace" ... I'm a bit noob, what do you mean exactly ?
     
  17. Offline

    cjc343

    "at com.bukkit.nomiros.specraft.specraft.onEnable(specraft.java:43)"

    that means that at the top of your source, you've probably got something that says "package com.bukkit.nomiros.specraft;" and you should change it to say something like "nomiros.specraft" or "com.domain-you-own.nomiros.specraft"
     
  18. Offline

    nomiros

    It became
    :(
     
  19. Offline

    Edward Hand

    Make sure your craftbukkit version is up to date. BLOCK_PLACE was BLOCK_PLACED a few versions back
     
  20. Offline

    nomiros

    I use the "Development Snapshot" link on the wiki, if this version isn't up to date, I don't know where to find it ...
     
  21. Offline

    cjc343

  22. Offline

    nomiros

    It works ! \o/ thanks everyone ;)
     
Thread Status:
Not open for further replies.

Share This Page