Block Data for Rotated Logs

Discussion in 'Plugin Development' started by TonyFox, Aug 5, 2012.

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

    TonyFox

    UPDATE: It works now, I had to use the scheduler to delay the setting of the log block's data. Thanks, nisovin! I uploaded the completed plugin here.

    As you all know, 1.3 added rotated log blocks. If you place a log without looking almost straight up or down, it'll be placed on its side instead. I'm trying to write a plugin that forces all placed logs to be placed vertically instead of whatever way the player was facing. My problem is that no matter what I set the log's data to, it never changes the facing of the log, only the bark texture.

    First, here's the way log data is supposed to work:
    0-3: Vertical oak, spruce, birch, then jungle
    4-7: East/west oak, spruce, birch, jungle
    8-11: North/south oak, spruce, birch, jungle
    12-15: Uses oak, spruce, birch, jungle bark texture on all six faces

    What seems to be happening is when I set the log's data, it only changes the bark texture, but not the direction the log is facing. It's as if the data is actually two values, one for texture and one for facing, and setting the data only changes the first one. Changing the block's value with WorldEdit works fine:

    block.setData((byte)6); - Will change the log's bark texture to birch, but not change the facing (INCORRECT)
    /set log:6 - Will change the log to an east/west facing birch log (CORRECT)

    Anyone know what's going on?

    Edit: Turns out everything works fine if I set the data of a nearby block, but not if I set the data of the block that triggered the BlockPlaceEvent event. Am I doing something wrong or is this something that Bukkit hasn't accounted for yet?

    Edit 2: Also, block.getData() returns 0-3 for the different log types, and doesn't take into account facing. So three oak logs, one facing vertically, one facing east/west, and one facing north/south will all return 0 for getData().

    Here's the code I'm currently using:

    Code:
    package me.tony311.sidewayslogfix;
     
    import java.util.logging.Logger;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
     
    public class SidewaysLogFix extends JavaPlugin
    {
      Logger log = Logger.getLogger("Minecraft");
      private final MyBlockListener blockListener = new MyBlockListener();
     
      public void onEnable()
      {
          PluginManager pm = getServer().getPluginManager();
          pm.registerEvents(this.blockListener, this);
          log.info("SidewaysLogFix enabled.");
      }
     
      public void onDisable()
      {
          log.info("SidewaysLogFix disabled.");
      }
    }
    
    Code:
    package me.tony311.sidewayslogfix;
     
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
     
    public class MyBlockListener implements Listener
    {
      @EventHandler
      public void onBlockPlace(BlockPlaceEvent event)
      {
        Block block = event.getBlockPlaced();
     
        if (block.getTypeId() == 17)
        {
          //block.setData((byte) (block.getData() % 4));
          block.setData((byte) 2); // Should set it to a vertical birch log, but it keeps the orientation and only changes the bark texture
          block.getState().update(true);
        }
      }
    }
    
     
  2. Offline

    Malikk

    Run this after you set the data. Sometimes with block data you have to force the changes to get shown to the clients.

    Code:
    block.getState().update(true);
    
     
  3. Offline

    TonyFox

    Nope, doesn't do anything I'm afraid :(
     
  4. Offline

    hawkfalcon

    I was working on a plugin called NoRotateLogs, I guess same functionality as yours, and I ran into the same problem:(
     
  5. Offline

    skore87

    Does setting the MaterialData do anything different for you?

    e.getBlock().getState().getData().setData((byte) 6); // MaterialData

    vs

    e.getBlock().setData((byte)6); //Data
     
  6. Offline

    TonyFox

    Nope, that doesn't alter the block at all :(
     
  7. Offline

    hawkfalcon

    Want to work together? I already started this. I would recommend doing event.setCancelled(true); before setting the data. Might work.
     
  8. Offline

    TonyFox

    That just seems to prevent the block from being placed no matter what.

    As far as working together, I know almost nothing about Java or Bukkit plugin development and I'm pretty much just copy/pasting code together, so you probably won't get much help out of me :p . All I really need right now is a plugin that disables horizontal placement of logs (so they're always placed vertical) with a single command to enable/disable it.
     
  9. Offline

    hawkfalcon

    I'll continue to hack at it and I'll let you know if I manage to make it work;) I'll add you as an author on bukkit dev.
     
  10. Offline

    Malikk

    Is this on github?
     
  11. Offline

    hawkfalcon

    I'll add it tomorrow. It will be on my github, icon below:)
     
  12. Offline

    nisovin

    It seems that the direction is determined after the BlockPlaceEvent is fired. You can fix it by delaying the data setting:

    Code:
    
        @EventHandler
        public void onPlace(final BlockPlaceEvent event) {
            if (event.getBlock().getType() == Material.LOG) {
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    public void run() {
                        event.getBlock().setData((byte)(event.getBlock().getData() & 3));
                    }
                });
            }
        }
    
     
    hawkfalcon likes this.
  13. Offline

    TonyFox

    That worked, thanks :D
    The completed plugin can now be found here.
     
  14. Offline

    hawkfalcon

    :3 /me stops work on mine. :(
     
Thread Status:
Not open for further replies.

Share This Page