When a player breaks a certain block it does this...

Discussion in 'Plugin Development' started by javoris767, May 28, 2012.

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

    javoris767

    I'm basically trying to get to when a player breaks a certain block, It logs it in a file.
    Here is my attempt:

    Listener:
    Code:
        public void onBlockBreak(final BlockBreakEvent event) {
            Player player = event.getPlayer();
            String playername = player.getName();
            String d = "diamond";
            World world = player.getWorld();
            String worldname = world.getName();
            double x = (int) Math.floor(player.getLocation().getX());
            double y = (int) Math.floor(player.getLocation().getY());
            double z = (int) Math.floor(player.getLocation().getZ());
            if(event.getBlock().getType() == Material.DIAMOND_ORE);
            filehandler.logDiamondBreak(playername, d, worldname, x, y, z);
    Filehandler:
    Code:
    public static void logDiamondBreak(String playername, String d, String worldname, double x, double y, double z) {
            File user = new File("plugins/MiningLogger/Users/" + playername + ".properties");
            try {
                props.load(new FileInputStream(config));
                //userprops.load(new FileInputStream(user));
                FileWriter outfile = new FileWriter(user, true);
                PrintWriter out = new PrintWriter(outfile);
                user.createNewFile();
                if (checkEnabled(props.getProperty("LogDiamonds"))) {
                    out.println("["+worldname+"]"+playername + " broke: " +d+" " + "(" + (int)x + " " + (int)y + " " + (int)z + ") ("+getTimestamp()+")");
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
     
  2. Offline

    Coryf88

    I'm not seeing where you explain the problem that you're experiencing.

    Also, you should close the I/O streams in a finally block on your try/catch.
     
  3. dont use file handles from the main server thread, if theres an problem whit io, the server may hang
     
  4. Offline

    javoris767

    Never mind that... But how do I set an event to when a certain block is broken.
     
  5. Offline

    Coryf88

    Change
    Code:java
    1. if(event.getBlock().getType() == Material.DIAMOND_ORE);

    to
    Code:java
    1. if(event.getBlock().getType() == Material.DIAMOND_ORE)
     
  6. Offline

    javoris767

    OHHH.... forgot the event handler -______________- thx btw
     
Thread Status:
Not open for further replies.

Share This Page