Item Frame Always Spawn Facing East?

Discussion in 'Plugin Development' started by Vandrake, Nov 20, 2012.

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

    Vandrake

    Ok So I got it that Item Frames are entities, and I manage to spawn them on a certain event. However I was trying to make them face the oposite direction of the player. I also got defining the player facing direction too. However, when I do
    ItemFrame.setFacingDirection(playerdirection.getOpositeDirection)//or something like this
    it will not change the itemframe'se direction :/ Any clue why TnT desh ?
    Here's my code:
    Code:
    public void CreateFrame(Block x,int itemid,Player p)
    {
    Hanging h = (ItemFrame) p.getWorld().spawn(x.getLocation(), ItemFrame.class);
    h.setFacingDirection(getPlayerDirection(p).getOppositeFace());
    //ItemFrame If = (ItemFrame) p.getWorld().spawn(x.getLocation(), ItemFrame.class);
    p.sendMessage(getPlayerDirection(p)+"");
    //If.setItem(new ItemStack(itemid));
    }
    As you can see the bold text are just one or two of my many tries... Before I post it here :/ Hope anyone can help. Neither of the code above helps me spawn it anywhere else but East.

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

    SnowGears

    Im having this same problem. Did you ever figure out how to change the blockface they are on?
    Vandrake
     
  3. Offline

    Vandrake

    nope. I even tried to spawn it twice and see if it spawned somewhere else but no luck
     
  4. Offline

    SnowGears

    I thought about spawning another one at a location, setting the direction, then teleporting it(Its an entity so it can be teleported) to the block wanted so hopefully it would come out facing the correct direction but no luck. I would really like to figure this out so if you find out anything I would love to know.

    EDIT: I have submitted a ticket with Bukkit. You can support it HERE if you want.
     
  5. Offline

    SnowGears

    I got it to spawn on a different blockface than facing east but still no luck in getting two to spawn on one block
     
  6. Offline

    Vandrake

    is that so? mind telling me how? :3
     
  7. Offline

    SnowGears

    This will spawn an itemFrame facing a different direction than east. Its a bit of a workaround but it works.
    Code:java
    1.  
    2. ItemFrame i = (ItemFrame) player.getWorld().spawn(bodyBlock.getLocation(), ItemFrame.class);
    3. i.setFacingDirection(BlockFace.SOUTH);
    4. HangingPlaceEvent hEvent = new HangingPlaceEvent(i, player, bodyBlock, BlockFace.NORTH);
    5. plugin.getServer().getPluginManager().callEvent(hEvent);
    6.  


    I would be very grateful if you could help me figure out how to spawn item frames on more than one blockface of a block. (like this) [​IMG]
     
  8. Offline

    Vandrake

    did you try to re-run that piece of code?(ofcourse you did I just had to ask, coz I have no idea :confused:)
     
  9. Offline

    SnowGears

    Here is what I have to try to spawn an item frame on two blockfaces of the same block (bodyBlock)
    Code:java
    1.  
    2. ItemFrame i = (ItemFrame) player.getWorld().spawn(bodyBlock.getLocation(), ItemFrame.class);
    3. i.setFacingDirection(BlockFace.SOUTH);
    4. HangingPlaceEvent hEvent = new HangingPlaceEvent(i, player, bodyBlock, BlockFace.NORTH);
    5. plugin.getServer().getPluginManager().callEvent(hEvent);
    6.  
    7.  
    8. ItemFrame i2 = (ItemFrame) player.getWorld().spawn(bodyBlock.getLocation(), ItemFrame.class);
    9. i2.setFacingDirection(BlockFace.EAST);
    10. HangingPlaceEvent hEvent2 = new HangingPlaceEvent(i2, player, bodyBlock, BlockFace.WEST);
    11. plugin.getServer().getPluginManager().callEvent(hEvent2);
    12.  


    This currently spawns two item frames on one another facing south.
     
  10. Offline

    Vandrake

    Let's help each other xD I'll be honest I deleted my old plugin which I was working on with this but I can easily create it again. Add me on skype for real time discussion on this @.@ Vanndrake <<<<<

    Tooner101 when you read this contact me. I finally managed to spawn ItemFrames properly. Each facing whatever direction I want. I will be working on 2 or more in 1 block for you now. The only problem is still that relog issue. Peace

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
    Tooner101 likes this.
  11. Offline

    fireblast709

  12. Offline

    Vandrake

    I was thinking that maybe if I made an API would be better for people ya know?I also managed to fix the relog issue but somehow the WEST itemframes are not being updated dunno why. So I'm still in early stages
     
  13. Offline

    fireblast709

    Vandrake no worries, might even be able to help if you post :p
     
  14. Offline

    Vandrake

    I just fixed the west issue eheh. So now all that's left is to compile that API.

    Ok First we call the ItemFrame depending on the event. BlockPlace, SignChange, whatever you want:
    I used BlockPlace just to start working with this Entity.
    p is the player that placed the block.
    b is the block duh xD
    di is the class I used to store the snippets I created. All of which are below.
    Code:
    if(di.getPlayerDirection(p) == BlockFace.WEST)
            {
                HangingPlaceEvent hEvent = new HangingPlaceEvent(p.getWorld().spawn(b.getRelative(BlockFace.UP).getLocation(), ItemFrame.class),
                p, b,BlockFace.WEST);
                Bukkit.getServer().getPluginManager().callEvent(hEvent);
            }
            else if(di.getPlayerDirection(p) == BlockFace.NORTH)
            {
                HangingPlaceEvent hEvent = new HangingPlaceEvent(p.getWorld().spawn(b.getRelative(BlockFace.UP).getLocation(), ItemFrame.class),
                p, b,BlockFace.NORTH);
                Bukkit.getServer().getPluginManager().callEvent(hEvent);
            }
            else if(di.getPlayerDirection(p) == BlockFace.SOUTH)
            {
                HangingPlaceEvent hEvent = new HangingPlaceEvent(p.getWorld().spawn(b.getRelative(BlockFace.UP).getLocation(), ItemFrame.class),
                p, b,BlockFace.SOUTH);
                Bukkit.getServer().getPluginManager().callEvent(hEvent);
            }
            else if(di.getPlayerDirection(p) == BlockFace.EAST)
            {
                p.getWorld().spawn(b.getRelative(BlockFace.UP).getLocation(), ItemFrame.class);
            }
            di.refreshChunkAtPlayer(p);
    Make sure you work with HashMaps, so you don't mix up different ItemFrames. I recommend EntityId.
    Code:
    @EventHandler
        public void hang(HangingPlaceEvent e)//The event that will set the direction of the ItemFrame
        {
          if(e.getEntity() instanceof ItemFrame)
    {
    Player p = e.getPlayer();
    if(getPlayerDirection(p) == BlockFace.WEST)
    {
    e.getEntity().teleport(e.getBlock().getRelative(BlockFace.WEST).getRelative(BlockFace.UP).getLocation());
    e.getEntity().setFacingDirection(BlockFace.WEST,true);
    }
    else if(getPlayerDirection(p) == BlockFace.NORTH)
    {
    e.getEntity().teleport(e.getBlock().getRelative(BlockFace.SOUTH).getRelative(BlockFace.UP).getLocation());
    e.getEntity().setFacingDirection(BlockFace.SOUTH,true);
    }
    else if(getPlayerDirection(p) == BlockFace.SOUTH)
    {
    e.getEntity().teleport(e.getBlock().getRelative(BlockFace.NORTH).getRelative(BlockFace.UP).getLocation());
    e.getEntity().setFacingDirection(BlockFace.NORTH,true);
    }
    }
        }
    In my case, I was trying to put the ItemFrames on the block above of where I was placing another block. But you can change it to suit your needs.
    A piece of code I found on the internet, tbh I cannot remember who created it.

    Code:
    public BlockFace getPlayerDirection(Player player)//Gets the direction the player is looking at
        {
            BlockFace dir = null;
            float y = player.getLocation().getYaw();
            if (y < 0.0F) y += 360.0F;
            y %= 360.0F;
            int i = (int)((y + 8.0F) / 22.5D);
            if (i == 0) dir = BlockFace.WEST;
            else if (i == 1) dir = BlockFace.WEST_NORTH_WEST;
            else if (i == 2) dir = BlockFace.NORTH_WEST;
            else if (i == 3) dir = BlockFace.NORTH_NORTH_WEST;
            else if (i == 4) dir = BlockFace.NORTH;
            else if (i == 5) dir = BlockFace.NORTH_NORTH_EAST;
            else if (i == 6) dir = BlockFace.NORTH_EAST;
            else if (i == 7) dir = BlockFace.EAST_NORTH_EAST;
            else if (i == 8) dir = BlockFace.EAST;
            else if (i == 9) dir = BlockFace.EAST_SOUTH_EAST;
            else if (i == 10) dir = BlockFace.SOUTH_EAST;
            else if (i == 11) dir = BlockFace.SOUTH_SOUTH_EAST;
            else if (i == 12) dir = BlockFace.SOUTH;
            else if (i == 13) dir = BlockFace.SOUTH_SOUTH_WEST;
            else if (i == 14) dir = BlockFace.SOUTH_WEST;
            else if (i == 15) dir = BlockFace.WEST_SOUTH_WEST;
            else dir = BlockFace.WEST;
            return dir;
        }
    And the piece of code I created that actually makes this work xD It may be raw but it works. I tried to refresh the chunk and other stuff but it just wouldn't work.

    Code:
    public void refreshChunkAtPlayer(Player p)//Refresh the chunk by teleporting the player elsewhere and re-teleporting him back
        {
           Location loc = p.getLocation();
    Location to = loc;
    if(getPlayerDirection(p) == BlockFace.WEST)
    {
    to = new Location(loc.getWorld(),loc.getX(),loc.getY()+2000,loc.getZ());
    }
    else
    {
    to = new Location(loc.getWorld(),loc.getX(),loc.getY(),loc.getZ()+2000);
    }
    p.teleport(to);
    p.getWorld().unloadChunk(loc.getChunk());
    p.teleport(loc);
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  15. Offline

    fireblast709

    Vandrake just saying, but BlockFaces like WEST_NORTH_WEST are not really useful for your code :3 (and cause your code to not work propery if you look at the wrong angle)
     
  16. Offline

    Vandrake

    yes I am aware of that o.o
     
  17. Offline

    DarkBladee12

    This doesn't work for me! The ItemFrame still keeps hanging east :/
     
  18. Offline

    Vandrake

    If you relog are they facing the proper direction?
     
  19. Offline

    DarkBladee12

    No they aren't :(
     
  20. Offline

    Vandrake

    then your problem is the hangingplaceevent. retweak it
     
  21. Offline

    DarkBladee12

    I used this event and did also a chunk update but nothing works :(
     
  22. Offline

    Vandrake

    the chunk update is just a raw method I created to replace the relog. If you can't see the changes with relogging then the problem is not the chunk update.

    With that said, make sure youre listening to events, and copy paste my code. It will work. At least with 1.4.2 bukkit

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

    DarkBladee12

    I did an other method for the chunk update :
    Code:
    Chunk c = player.getLocation().getChunk();
    c.getWorld().refreshChunk(c.getX(), c.getZ());
     
  24. Offline

    Vandrake

    That did not work for me. That's why I did that lol read what I wrote man, also you don't want to update the players chunk. You want to update the itemframe's chunk
     
  25. Offline

    DarkBladee12

    Vandrake that's the code i have so far:
    Code:
    public void createFrame(Block body, Player player) {
            if (getPlayerDirection(player) == BlockFace.WEST) {
                HangingPlaceEvent hEvent = new HangingPlaceEvent(player.getWorld().spawn(body.getLocation(), ItemFrame.class), player, body, BlockFace.WEST);
                Bukkit.getServer().getPluginManager().callEvent(hEvent);
            } else if (getPlayerDirection(player) == BlockFace.NORTH) {
                HangingPlaceEvent hEvent = new HangingPlaceEvent(player.getWorld().spawn(body.getLocation(), ItemFrame.class), player, body, BlockFace.NORTH);
                Bukkit.getServer().getPluginManager().callEvent(hEvent);
            } else if (getPlayerDirection(player) == BlockFace.SOUTH) {
                HangingPlaceEvent hEvent = new HangingPlaceEvent(player.getWorld().spawn(body.getLocation(), ItemFrame.class), player, body, BlockFace.SOUTH);
                Bukkit.getServer().getPluginManager().callEvent(hEvent);
            } else if (getPlayerDirection(player) == BlockFace.EAST) {
                player.getWorld().spawn(body.getLocation(), ItemFrame.class);
            }
        }
    Code:
    Location loc = player.getLocation();
                    int x = loc.getBlockX();
                    int y = loc.getBlockY();
                    int z = loc.getBlockZ();
                    if (direction == BlockFace.NORTH)
                        x = x - 1;
                    else if (direction == BlockFace.EAST)
                        z = z - 1;
                    else if (direction == BlockFace.SOUTH)
                        x = x + 1;
                    else if (direction == BlockFace.WEST)
                        z = z + 1;
                    Block b = loc.getWorld().getBlockAt(x, y, z);
                    b.setType(Material.WOOD);
                    plugin.smu.createFrame(b, player);
    Code:
    @EventHandler
        public void onHangingPlace(HangingPlaceEvent e) {
            if (e.getEntity() instanceof ItemFrame) {
                Player p = e.getPlayer();
                if (plugin.smu.getPlayerDirection(p) == BlockFace.WEST) {
                    e.getEntity().teleport(e.getBlock().getRelative(BlockFace.WEST).getLocation());
                    e.getEntity().setFacingDirection(BlockFace.WEST, true);
                } else if (plugin.smu.getPlayerDirection(p) == BlockFace.NORTH) {
                    e.getEntity().teleport(e.getBlock().getRelative(BlockFace.SOUTH).getLocation());
                    e.getEntity().setFacingDirection(BlockFace.SOUTH, true);
                } else if (plugin.smu.getPlayerDirection(p) == BlockFace.SOUTH) {
                    e.getEntity().teleport(e.getBlock().getRelative(BlockFace.NORTH).getLocation());
                    e.getEntity().setFacingDirection(BlockFace.NORTH, true);
                }
            }
     
        }
     
  26. Offline

    Vandrake

    TBH, when my BukkitGUI gave me a warning about a new update, I was thrilled. Ok cool new events to code with. But now I can't spawn my item frames anymore XD So I'm being unable to spawn them with this code. That's why I will try to downgrade and will report back here. If it works again...The problem is the CB update

    Sigh... It works with 1.4.5 - R 0.2 Beta Build. But not with 1.4.5 - R 1.0. So I will try to fix it for the new craftbukkit x.x Grrrr...

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

    DarkBladee12

    Thanks for your efforts ;)
     
  28. Offline

    Vandrake

    Code:
    private void CreateFrameOnBlock(Player p,Block b)
    {
    ItemFrame i = (ItemFrame) p.getWorld().spawn(b.getLocation(), ItemFrame.class);
    if(getNewCardinalDirection(p) != BlockFace.EAST)
    {
    i.teleport(b.getRelative(getNewCardinalDirection(p)).getLocation());
    i.setFacingDirection(getNewCardinalDirection(p),true);
    }
    }
     
    public static BlockFace getNewCardinalDirection(Player p)
    {
            double rotation = (p.getLocation().getYaw() ) % 360;
            if (rotation < 0) {rotation += 360.0;}
            if (0 <= rotation && rotation < 22.5){return BlockFace.NORTH;}
            else if (22.5 <= rotation && rotation < 67.5) {return BlockFace.NORTH_EAST;}
            else if (67.5 <= rotation && rotation < 112.5) {return BlockFace.EAST;}
            else if (112.5 <= rotation && rotation < 157.5) {return BlockFace.SOUTH_EAST;}
            else if (157.5 <= rotation && rotation < 202.5) {return BlockFace.SOUTH;}
            else if (202.5 <= rotation && rotation < 247.5) {return BlockFace.SOUTH_WEST;}
            else if (247.5 <= rotation && rotation < 292.5) {return BlockFace.WEST;}
            else if (292.5 <= rotation && rotation < 337.5) {return BlockFace.NORTH_WEST;}
            else if (337.5 <= rotation && rotation < 360.0) {return BlockFace.NORTH;}
            else {return null;}
        }
    This is the 1.4.5 R 1.0 Fix x.x Still working on the Chunk update. Will try now to use the bukkit's method. As Always dont forget to check if The instance Id is The Same
     
    DarkBladee12 likes this.
  29. Offline

    netherfoam

    Good god that is a big pain to work around. I'm upvoting :| Here I was thinking it was my fault I couldn't place item frames with bukkit.
     
  30. Offline

    DarkBladee12

    Vandrake works fine but I don't think that refreshing chunk helps. I think you have to send the player a packet or something like that, because on the server the sign is on the right BlockFace ;)
     
Thread Status:
Not open for further replies.

Share This Page