Solved frame problem

Discussion in 'Plugin Development' started by BizeaxPvP, Jul 26, 2019.

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

    BizeaxPvP

    so i'm really new to coding and there was a plugin that i needed so i tried to but I had no clue :p It's a plugin where when you right click an item frame it gives you the item inside that itemframe. It does not change the rotation just gives you the item which is easy just by cancelling the event but idk how to do the rest.
    This is what I have:
    Code:
    
    package me.Zxoir.NectarClicker.Events;
    
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.ItemFrame;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class EventClass implements Listener{
        public void paintingRotate (final PlayerInteractEntityEvent event) {
            final Entity clicked = event.getRightClicked();
            if (clicked != null && clicked instanceof ItemFrame) {
                final ItemFrame frame = (ItemFrame) clicked;
                final ItemStack item = frame.getItem();
                Player player = event.getPlayer();
                    player.getInventory();
            }
        }
    }
    
    
     
    Last edited: Jul 26, 2019
  2. Offline

    Kars

    Your spacing got lost somehow. And your code is incomplete.
     
  3. It's hard to understand what you're trying to do with the spacing you have.
    Try to include a screenshot of exactly what you have.
     
  4. Offline

    BizeaxPvP

    fixed, now i need to know what to add to this code
     
  5. Offline

    KarimAKL

    @BizeaxPvP
    1. Cancel the event.
    2. Use ItemFrame#getItem() to get the item inside the frame.
    3. Use Inventory#addItem(ItemStack...) to add the item to the player's inventory.
     
  6. Offline

    BizeaxPvP

    may you show me how to do that in code? so i can avoid any errors. I know how to cancel an event just need the others
     
  7. Offline

    KarimAKL

    @BizeaxPvP You already have the ItemFrame and the Inventory, just use the methods i listed.
     
  8. Offline

    BizeaxPvP

    so like this?
    Code:
    event.setCancelled(true);
    ItemFrame#getItem() 
    Inventory#addItem(ItemStack...)
     
  9. Offline

    Machine Maker

    the # just replace the . so its more readable in a plain text format. You probably need a better understanding of what those methods do. You are already (in your code at the top of the thread) getting the item from the frame with your frame.getItem(). Now you want to add that item to the player's inventory. So you can use player.getInventory().addItem(item).

    In the future, if you are unsure of how a method works or what it returns, check the docs for Bukkit or Spigot.
     
  10. Offline

    KarimAKL

    @BizeaxPvP No, when using 'Something#Something(Something, Something)', it means the following:
    ClassName#MethodName(ParameterType, ParameterType)
     
  11. Offline

    BizeaxPvP

    So I did that and it didn't work in game, no errors tho
    EventClass:
    Code:
    public class EventClass implements Listener{
        public void paintingRotate (final PlayerInteractEntityEvent event) {
            final Entity clicked = event.getRightClicked();
            if (clicked != null && clicked instanceof ItemFrame) {
                final ItemFrame frame = (ItemFrame) clicked;
                final ItemStack item = frame.getItem();
                Player player = event.getPlayer();
                player.getInventory().addItem(item);
                    event.setCancelled(true);
            }
        }
    }
    
     
  12. Offline

    Machine Maker

    Don't you need the @EventHandler tag right before the event funtion?
     
  13. Offline

    BizeaxPvP

    Oh yea, so I added that and it worked but I can't put anything in that ItemFrame
     
  14. Offline

    KarimAKL

    @BizeaxPvP What do you mean? You can set the item with ItemFrame#setItem(ItemStack)
     
  15. Offline

    BizeaxPvP

    I added that code in and it still doesn't work
    Source:
    Code:
    public class EventClass implements Listener{
        @EventHandler
        public void paintingRotate (final PlayerInteractEntityEvent event) {
            final Entity clicked = event.getRightClicked();
            final ItemFrame frame = (ItemFrame) clicked;
            final ItemStack item = frame.getItem();
            frame.setItem(item);
            if (clicked != null && clicked instanceof ItemFrame) {
                Player player = event.getPlayer();
                player.getInventory().addItem(item);
                    event.setCancelled(true);
                    player.playSound(player.getLocation(), Sound.NOTE_PLING, 2F, 1F);
            }else {
               
            }
               
        }
    }
    
     
    Last edited: Jul 26, 2019
  16. Offline

    Machine Maker

    Ah yeah, you can’t manually click an item into the frame cause you are cancelling the event. Try adding a check to see if the frame is empty and if it is, allow the event to continue. That way you can place items in them.
     
  17. Offline

    BizeaxPvP

    um.. how do i do that? :p
     
  18. Offline

    Machine Maker

    Check of the item in the item frame is null. That means nothing is in it. If the item is not null, that means the player should get the item in their inventory and the event should be cancelled.
     
  19. Offline

    BizeaxPvP

    can you show me an example please? im really new
     
  20. Offline

    Machine Maker

    What I suggested isn’t new to you. You have all the skills you need based on your original code block. You have “item” which is what’s in the item frame. You just need an if statement to check IF item == null, meaning that the frame is empty, return out of the function to allow the event to continue.
     
  21. Offline

    BizeaxPvP

    oh it worked! thank you so much
     
Thread Status:
Not open for further replies.

Share This Page