Solved ItemFrame Issue

Discussion in 'Plugin Development' started by BlueMond416, Dec 18, 2014.

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

    BlueMond416

    What I'm looking to do is when someone puts an item in a frame, I want to check what that item is.

    This is what I have so far..
    Code:
    public void onRightClick(PlayerInteractEvent event){
         Action action = event.getAction();
       
         if(action == Action.RIGHT_CLICK_BLOCK){
           Block block = event.getClickedBlock();
           if(block instanceof ItemFrame){
             ItemFrame itemFrame = (ItemFrame) block;
             ItemStack item = itemFrame.getItem();
    
           }
         }
       }
    
    The problem with this is it will check whenever someone right clicks a frame and get me the item in the frame whether or whether not the item was just placed into the frame by this player interaction.. Does anyone know of a way to only do this when the player just placed that item into the frame? I was thinking maybe there is an event for placing an item into a frame but I couldn't find anything.
     
  2. Offline

    Burnett

    @BlueMond416 Item frames are entity's are they not? So if you place an item into them they might technically change state. This would allow you to use one of the entity events.
     
  3. Offline

    BlueMond416

    Alright.. so that actually didn't work because an ItemFrame is an entity so I switched it to a PlayerInteractEntityEvent and redid some code and that works but the problem is that it doesn't get the item from the ItemFrame when it is being placed but if you rightclick it again after it's already in it, it gets it. The problem is I want to get the item when the player places it into the frame and nothing before or after..
     
  4. Offline

    Burnett

    @BlueMond416 check if its empty and if so check what item the player is holding?
     
    BlueMond416 likes this.
  5. Offline

    BlueMond416

    Oh, wow.. that's a good idea..
    Thanks, @Burnett!

    I am using the PlayerInteractEntityEvent to use with ItemFrames. Everytime I rightclick an ItemFrame, this event runs twice for some odd reason.. Any idea as to what is causing this?

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
    Last edited by a moderator: Dec 18, 2014
  6. Offline

    Rocoty

    @BlueMond416 I can assure you it only runs once. Although your plugin may be handling it twice; you may have registered it twice. Can't know though since you haven't posted your code ;)
     
  7. Offline

    BlueMond416

    @Rocoty Here's the code

    Code:
    @EventHandler
       public void onRightClick(PlayerInteractEntityEvent event){
         Entity entity = event.getRightClicked();
         Player player = event.getPlayer();
     
         System.out.println("In the function");
         if(entity instanceof ItemFrame){
           System.out.println("frame");
           ItemFrame itemFrame = (ItemFrame) entity;
           ItemStack item = itemFrame.getItem();
           Material air = Material.AIR;
         
           if(item.getType() == air){
             ItemStack pItem = player.getItemInHand();
             System.out.println("putting item in frame of type: " + pItem.getType());
           }
         }
       }
    
    Only one event handler, only one listener class, and it's only registered once..
     
  8. Offline

    mrCookieSlime

    Merged Threads.
     
  9. Offline

    Rocoty

  10. Offline

    BlueMond416

    Code:
    package com.bluemond.bookwarp;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.ItemFrame;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEditBookEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    
    public class BlueListener implements Listener{
       BookWarp bookwarp;
       
       public BlueListener(BookWarp bw){
         bookwarp = bw;
       }
       
       //splits the page into its lines
       public ArrayList<String> getLines(String page){
         ArrayList<String> pageData = new ArrayList<String>();
         //removes quotes
         page = page.substring(1, (page.length()-1));
         
         while(page.contains("\\")){
           int start = page.indexOf("\\");
           pageData.add(page.substring(0, (start)));
           page = page.substring((start+2), page.length());
         }
         
         pageData.add(page);
         return pageData;
       }
       
       public void printLines(Player player, ArrayList<String> lines, String reason){
         //send player page lines
         player.sendRawMessage("Printing lines on page 1 : " + reason);
         for(int x=0;x<lines.size();x++){
           player.sendRawMessage(ChatColor.BLUE + "~" + x + "~ " + ChatColor.WHITE + lines.get(x));
         }
       }
       
       @EventHandler
       public void onRightClick(PlayerInteractEntityEvent event){
         Entity entity = event.getRightClicked();
         Player player = event.getPlayer();
         
         System.out.println("In the function");
         if(entity instanceof ItemFrame){
           System.out.println("frame");
           ItemFrame itemFrame = (ItemFrame) entity;
           ItemStack item = itemFrame.getItem();
           Material air = Material.AIR;
             
           if(item.getType() == air){
             ItemStack pItem = player.getItemInHand();
             Material book = Material.WRITTEN_BOOK;
             
             if(pItem.getType() == book){
               System.out.println("Book");
    
               BookMeta bookMeta = (BookMeta) pItem.getItemMeta();
               if(bookMeta.getTitle().equals("[BookWarp]") && bookMeta.hasPages()){
                 String page = bookMeta.getPage(1);
                 ArrayList<String> pageLines = getLines(page);
    
                 printLines(player, pageLines, "RightClick");
               }
             }
           }
         }
       }
       
       @EventHandler
       public void onBookEdit(PlayerEditBookEvent event){
         BookMeta book = event.getNewBookMeta();
         Player player = event.getPlayer();
         
         //if book meets qualifications for [BookWarp]
         if(event.isSigning() && book.getTitle().equals("[BookWarp]") && book.hasPages()){
           String page = book.getPage(1);
           ArrayList<String> pageLines = getLines(page);
           
           //debug
           printLines(player, pageLines, "BookEdit");
         }
       }
    }
    
     
  11. Offline

    Rocoty

    @BlueMond416 Everything looks perfectly fine. Are you entirely sure you're only registering it once? (Post your main class?) Another possibility is that you have two instances of the plugin running on your server. Although this is unlikely, because two plugins with the same name cannot run at the same time on the same server.
     
  12. Offline

    BlueMond416

    Code:
    package com.bluemond.bookwarp;
    
    import java.util.ArrayList;
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class BookWarp extends JavaPlugin{
       private final BlueListener listener = new BlueListener(this);
       ArrayList<Integer> usedSigs = new ArrayList<Integer>();
       
       Logger log = Logger.getLogger("Minecraft");
         
       public void onDisable(){
         
       }
         
       public void onEnable(){
         PluginManager pm = getServer().getPluginManager();
         pm.registerEvents(listener, this);
       }
       
       public void addId(int id){
         usedSigs.add(id);
       }
       
       public boolean checkList(int id){
         if(usedSigs.contains(id)){
           return true;
         }else{
           return false;
         }
       }
    }
    
    
     
  13. Offline

    Rocoty

    Unfortunately I still can't see anything wrong. So let me get this straight. You see the "In the function" message twice?
     
  14. Offline

    BlueMond416

    Correct, I see all the Command Line prints twice. Including the player raw messages that are sent.

    @Rocoty This only started happening recently :/

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

    Rocoty

    @BlueMond416 Have you tried removing all other plugins just in case you do have duplicates? Does this also happen with the PlayerEditBookEvent?
     
  16. Offline

    BlueMond416

    @Rocoty It only happens with that event. I did take a look to see if I had overwritten any other plugins accidentally by looking at the amount of space they take up but they are all of different amounts.
     
  17. Offline

    Rocoty

    This is a shot in the dark. It might be that the Bukkit implementation you are running has a bug in it, which calls that particular event twice for ItemFrames. Does this happen only for ItemFrames or for other Entities as well? Have you tried with a different version of CraftBukkit, or running (sorry) Spigot?
     
  18. Offline

    BlueMond416

    @Rocoty It happens with all entities, not just the ItemFrame. I'm actually running a build of Spigot.
     
  19. Offline

    Avygeil

    @BlueMond416 Then it's probably the PlayerInteractAtEntityEvent that also fires. It's a subclass of PlayerInteractEntityEvent that was added for armor stand support, but the way it was added made it incompatible with most existing events unfortunately (like registering twice in most logging plugins).

    Add something like :
    Code:
    if (event instanceof PlayerInteractAtEntityEvent) return;
    On top of your method. Or just use the PlayerInteractAtEntityEvent itself and check if it's an item frame, although I wouldn't trust this evil event. :(
     
    Rocoty likes this.
  20. Offline

    BlueMond416

    That worked perfectly, thank you very much! :D
     
    Avygeil likes this.
  21. Offline

    Rocoty

    @Avygeil Bukkit's implementation is just getting worse and worse.
     
    Avygeil likes this.
Thread Status:
Not open for further replies.

Share This Page