Solved Getting block data on Event

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

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

    TGF

    Hi. I have a little problem over here.
    This is a part of my code:
    Code:
    @EventHandler
    public void onBlockPlaceEvent(Block block, Player player){
    block.getData();
    int Id = block.getTypeId();
    player.sendMessage(ChatColor.GREEN + "\nPBlock Id is " + Id);
    }
    I thought it should work but after placeing a block nothing in server log or player chat happend.
     
  2. Offline

    the_merciless

    This is completely wrong, it should look like this

    Code:
    @EventHandler
    public void placeblock(onBlockPlaceEvent event){
    Block block = event.getBlock();
    int Id = block.getTypeId();
    player.sendMessage(ChatColor.GREEN + "\nPBlock Id to " + Id);
    }
    'placeblock' is the name of the method. You can rename this to anything you like.
    (onBlockPlaceEvent event) is the event you are working with. and we have given the variable 'event'

    You can then work with that event to get the information you need
     
  3. Offline

    TGF

    Hmm:
    Code:
    public class keeper extends JavaPlugin{
    @EventHandler
        public void place(onBlockPlaceEvent event){
            Player player;
            Block block = event.getData();
            int Id = block.getTypeId();
            player.sendMessage(ChatColor.GREEN + "\nPBlock Id is " + Id);
    } }
    Eclipse underline me onBlockPlaceEvent as not existed type or sth like that. Why?
     
  4. Offline

    Milkywayz

    Change it to BlockPlaceEvent
     
  5. Offline

    TGF

    Yep, then it make me to import
    import org.bukkit.event.block.BlockPlaceEvent;
    and i see getData(); as underlined, and that tells me to add cast.
     
  6. Offline

    TGF

    Nou i shouldn't :p
    Just misread.

    God...
    Code:
    @EventHandler
        public void place(BlockPlaceEvent event){
            Player player = null;
            Block block = event.getBlock();
            int Id = block.getTypeId();
            player.sendMessage(ChatColor.GREEN + "\nPBlock Id to " + Id);
    Without errors but nothing happened ingame;/

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

    the_merciless

    Where did u get this from...

    Code:
    Player player = null;
    change it to

    Code:
    Player player = event.getPlayer(); 
    Otherwise you ar sending a message to null which is nobody
     
  8. Offline

    d33k40

    Omg, really, read the tutorials arround.
     
  9. Offline

    TGF

    I know;/

    Code:
        @EventHandler
        public void place(BlockPlaceEvent event){
            Block block = event.getBlock();
            int Id = block.getTypeId();
            this.logger.info(Id);
    Still doesn't work.

    Should i register an event?
    If not then what?


    Maybe i tell what i want to do.
    My plan is to get id and other data of every placed block on the map and load that as strign in console.
     
  10. Offline

    the_merciless

  11. Offline

    TGF

    the_merciless Had i registered good?
    Code:
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
    Pandemoneus I have changed player msg to server log and still nothing;/
    Code:
    @EventHandler
        public void place(BlockPlaceEvent event){
            Block block = event.getBlock();
            int Id = block.getTypeId();
            this.logger.info(ChatColor.GREEN + "\nPBlock Id to " + Id);
    Oh god, just help me quickly overcome it because i am very frustrated;/
     
  12. Offline

    the_merciless

    Is the event in your main class. PLEASE post the entire class and tell us what errors are showing up in eclipse and where

    And use bukkit.broadcastMessage to output the message.

    Why are u using this.logger

    Code:
    @EventHandler
        public void place(BlockPlaceEvent event){
            Player p = event.getPlayer();
            Block block = event.getBlock();
            int Id = block.getTypeId();
            p.sendMessage("this message is sent to the player who placed the block");
            Bukkit.broadcastMessage("this message Is sent to everyone on server");
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  13. Offline

    TGF

    Logger is to sent message to console, isn't it?

    I don't have any errors in eclipse.

    If blok typeId is int then i should can normally load it in message, yes?
    Then maybe the problem is with checking who placed that block?
     
  14. Offline

    the_merciless

    If you had tried that last code I posted you would know. If its not recognising the player you would of only got one of the messages. Use that code I posted and tell me the results. And for the last time POST YOUR ENTIRE CODE
     
  15. Offline

    tom1000o

    someones having a bad day, lol.
    and in my experience .getType() is a bit easier. (returns a material)
     
  16. Offline

    TGF

    tom1000o I need to save it in the datebase, int is smaller then char.

    the_merciless It is a code:
    Show Spoiler
    package pl.shaquu.terraindata;

    import java.util.logging.Logger;

    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.*;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;

    public class keeper extends JavaPlugin implements Listener{
    public final Logger logger = Logger.getLogger("Minecraft");

    @Override
    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info("========" + pdfFile.getName() + " disabled!========");
    }

    @Override
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info("========" + pdfFile.getName() + " enabled!!!========");
    }


    @EventHandler
    public void place(BlockPlaceEvent event){
    Block block = event.getBlock();
    int Id = block.getTypeId();
    this.logger.info(ChatColor.GREEN + "\nPBlock Id is " + Id);
    int X = block.getX();
    this.logger.info(ChatColor.GREEN + "\nPBlock X is " + X);
    int Y = block.getY();
    this.logger.info(ChatColor.GREEN + "\nPBlock Y is " + Y);
    int Z = block.getZ();
    this.logger.info(ChatColor.GREEN + "\nPBlock Z is " + Z);

    }
    @EventHandler
    public void brek(BlockBreakEvent event){
    Block block = event.getBlock();
    int Id = block.getTypeId();
    this.logger.info(ChatColor.GREEN + "\nDBlock Id is " + Id);
    int X = block.getX();
    this.logger.info(ChatColor.GREEN + "\nDBlock X is " + X);
    int Y = block.getY();
    this.logger.info(ChatColor.GREEN + "\nDBlock Y is " + Y);
    int Z = block.getZ();
    this.logger.info(ChatColor.GREEN + "\nDBlock Z is " + Z);

    }
    }
     
  17. Offline

    the_merciless

    my bad, the code works fine for me, perhaps you have an error in your plugin.yml, does the plugin load properly?
     
  18. Offline

    TGF

    But dude if u look into my code u can find registering exact how you wrote...
    My onEnable and onDisable are swaped, first is Disable.
     
  19. Offline

    the_merciless

    See up there ^^^
     
  20. Offline

    TGF

    the_merciless
    plugin.yml is in the main folder of the plugin
    and yep, plugin load properly;/
     
  21. Offline

    the_merciless

    What do u mean, in the main folder?
     
  22. Offline

    TGF

    my plugin.yml is placed well.
    I think it is problem with events... Hmm can u show me your source?
     
  23. Offline

    the_merciless

    I just copied yours and it worked for me. Show us your plugin.yml
     
  24. Offline

    TGF

    the_merciless Here it is:
     
  25. Offline

    the_merciless

    There is no reason this shouldnt work for you. Can you delete your server.log, test it, then copy and paste your new server.log here
     
  26. Offline

    TGF

    Roger, catch it:
    server.log (open)
    Code:
    2012-11-01 23:28:07 [INFO] Starting minecraft server version 1.4.2
    2012-11-01 23:28:07 [INFO] Loading properties
    2012-11-01 23:28:07 [INFO] Default game type: SURVIVAL
    2012-11-01 23:28:07 [INFO] Generating keypair
    2012-11-01 23:28:08 [INFO] Starting Minecraft server on *:25565
    2012-11-01 23:28:08 [WARNING] **** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!
    2012-11-01 23:28:08 [WARNING] The server will make no attempt to authenticate usernames. Beware.
    2012-11-01 23:28:08 [WARNING] While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.
    2012-11-01 23:28:08 [WARNING] To change this, set "online-mode" to "true" in the server.properties file.
    2012-11-01 23:28:08 [INFO] This server is running CraftBukkit version git-Bukkit-1.3.2-R3.0-9-g17de435-b2413jnks (MC: 1.4.2) (Implementing API version 1.4.2-R0.1-SNAPSHOT)
    2012-11-01 23:28:08 [INFO] [TerrainData] Loading TerrainData v0.1
    2012-11-01 23:28:08 [INFO] Preparing level "world"
    2012-11-01 23:28:09 [INFO] Preparing start region for level 0 (Seed: 1090571616790763528)
    2012-11-01 23:28:10 [INFO] Preparing spawn area: 52%
    2012-11-01 23:28:11 [INFO] [TerrainData] Enabling TerrainData v0.1
    2012-11-01 23:28:11 [INFO] ========TerrainData wlaczony!!!========
    2012-11-01 23:28:11 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2012-11-01 23:28:11 [INFO] Done (2,471s)! For help, type "help" or "?"
    2012-11-01 23:28:40 [INFO] Shaquu[/127.0.0.1:49317] logged in with entity id 223 at ([world] -35.67170498199503, 68.0, 182.69999998807907)
    2012-11-01 23:29:11 [INFO] <Shaquu> PLACEING A BLOCK
    2012-11-01 23:29:20 [INFO] <Shaquu> BLOCK PLACED
    2012-11-01 23:29:24 [INFO] Connection reset
    2012-11-01 23:29:24 [INFO] Shaquu lost connection: disconnect.quitting
    2012-11-01 23:29:38 [INFO] CONSOLE: Stopping the server..
    2012-11-01 23:29:38 [INFO] Stopping server
    2012-11-01 23:29:38 [INFO] [TerrainData] Disabling TerrainData v0.1
    2012-11-01 23:29:38 [INFO] ========TerrainData wylaczony!========
    2012-11-01 23:29:38 [WARNING] DSCT: Socket closed
    2012-11-01 23:29:38 [INFO] Closing listening thread
    2012-11-01 23:29:38 [INFO] Saving players
    2012-11-01 23:29:38 [INFO] Saving worlds
    2012-11-01 23:29:38 [INFO] Saving chunks for level 'world'/Overworld
     
  27. Offline

    the_merciless

    Hmm, .... What version of bukkit are you using.
     
  28. Offline

    TGF

    1.4.2 DEV

    bukkit-1.4.2-R0.1-20121028.021811-2
     
Thread Status:
Not open for further replies.

Share This Page