[UNSOLVED] Preventing player from moving items

Discussion in 'Plugin Development' started by AJAW, Oct 29, 2012.

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

    AJAW

    Hey, I want to prevent players from moving an item in a inventory, I also want to stop him from dropping an item. Here's my code:

    Show Spoiler
    Code:
    package pl.mineville.minevillerpg;
     
    import java.io.File;
     
    import net.minecraft.server.NBTTagCompound;
    import net.minecraft.server.NBTTagList;
    import net.minecraft.server.NBTTagString;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
     
    import ru.tehkode.permissions.bukkit.PermissionsEx;
     
    public class BookItem implements Listener {
     
    public File configFile = null;
     
    public Main plugin;
     
    public BookItem(Main plugin) {
    this.plugin = plugin;
    }
     
    private net.minecraft.server.ItemStack item = null;
    private CraftItemStack stack = null;
     
    public BookItem(org.bukkit.inventory.ItemStack item) {
    if (item instanceof CraftItemStack) {
    stack = (CraftItemStack) item;
    this.item = stack.getHandle();
    } else if (item instanceof org.bukkit.inventory.ItemStack) {
    stack = new CraftItemStack(item);
    this.item = stack.getHandle();
    }
    }
     
    public String[] getPages() {
    NBTTagCompound tags = item.getTag();
    if (tags == null) {
    return null;
    }
    NBTTagList pages = tags.getList("pages");
    String[] pagestrings = new String[pages.size()];
    for (int i = 0; i < pages.size(); i++) {
    pagestrings[i] = pages.get(i).toString();
    }
    return pagestrings;
    }
     
    public String getAuthor() {
    NBTTagCompound tags = item.getTag();
    if (tags == null) {
    return null;
    }
    String author = tags.getString("author");
    return author;
    }
     
    public String getTitle() {
    NBTTagCompound tags = item.getTag();
    if (tags == null) {
    return null;
    }
    String title = tags.getString("title");
    return title;
    }
     
    public void setPages(String[] newpages) {
    NBTTagCompound tags = item.tag;
    if (tags == null) {
    tags = item.tag = new NBTTagCompound();
    }
    NBTTagList pages = new NBTTagList("pages");
    // we don't want to throw any errors if the book is blank!
    if (newpages.length == 0) {
    pages.add(new NBTTagString("1", ""));
    } else {
    for (int i = 0; i < newpages.length; i++) {
    pages.add(new NBTTagString("" + i + "", newpages[i]));
    }
    }
    tags.set("pages", pages);
    }
     
    public void addPages(String[] newpages) {
    NBTTagCompound tags = item.tag;
    if (tags == null) {
    tags = item.tag = new NBTTagCompound();
    }
    NBTTagList pages;
    if (getPages() == null) {
    pages = new NBTTagList("pages");
    } else {
    pages = tags.getList("pages");
    }
    // we don't want to throw any errors if the book is blank!
    if (newpages.length == 0 && pages.size() == 0) {
    pages.add(new NBTTagString("1", ""));
    } else {
    for (int i = 0; i < newpages.length; i++) {
    pages.add(new NBTTagString("" + pages.size() + "", newpages[i]));
    }
    }
    tags.set("pages", pages);
    }
     
    public void setAuthor(String author) {
    NBTTagCompound tags = item.tag;
    if (tags == null) {
    tags = item.tag = new NBTTagCompound();
    }
    if (author != null && !author.equals("")) {
    tags.setString("author", author);
    }
    }
     
    public void setTitle(String title) {
    NBTTagCompound tags = item.tag;
    if (tags == null) {
    tags = item.tag = new NBTTagCompound();
    }
    if (title != null && !title.equals("")) {
    tags.setString("title", title);
    }
    }
     
    public org.bukkit.inventory.ItemStack getItemStack() {
    return stack;
    }


    Events
    Show Spoiler
    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            BookItem ksiazka = new BookItem(new ItemStack(387, 1));
            String playerName = e.getPlayer().getName();
            Inventory inventory = e.getPlayer().getInventory();
            String[] pages;
            PlayerAccessor config = new PlayerAccessor(plugin, playerName + ".yml");
            pages = new String[2];
            pages[0] = ChatColor.BOLD
                    + "Statystyki\n"
                    + ChatColor.BLACK
                    + "\n"
                    + ChatColor.BLACK
                    + "Nick: "
                    + ChatColor.DARK_GRAY
                    + playerName
                    + "\n"
                    + ChatColor.BLACK
                    + "Honor: "
                    + ChatColor.DARK_GRAY
                    + config.getConfig().getInt("honor")
                    + "\n"
                    + ChatColor.BLACK
                    + "Krolstwo: "
                    + ChatColor.DARK_GRAY
                    + config.getConfig()
                            .getInt("krolestwo"
                                    + "\n"
                                    + ChatColor.BLACK
                                    + "Gildia: "
                                    + ChatColor.DARK_GRAY
                                    + config.getConfig()
                                            .getString(
                                                    "gildia"
                                                            + "\n"
                                                            + ChatColor.BLACK
                                                            + "Rola w gildii: "
                                                            + ChatColor.DARK_GRAY
                                                            + config.getConfig()
                                                                    .getString(
                                                                            "rola-gildia"
                                                                                    + "\n"
                                                                                    + ChatColor.BLACK
                                                                                    + "Ranga: "
                                                                                    + PermissionsEx
                                                                                            .getUser(
                                                                                                    e.getPlayer())
                                                                                            .getSuffix()
                                                                                    + "\n"
                                                                                    + ChatColor.BLACK)));
     
            pages[1] = "Awesome text here! One!";
     
            ksiazka.setPages(pages);
            ksiazka.setAuthor(playerName);
            ksiazka.setTitle("Statystyki");
            ItemStack writtenbook = ksiazka.getItemStack();
            inventory.setItem(8, writtenbook);
        }
     
        @EventHandler
        public void onPlayerDropItemEvent(PlayerDropItemEvent e) {
            Material droppeditem = e.getItemDrop().getItemStack().getType(); // Material
            if (droppeditem == Material.WRITTEN_BOOK) {
                BookItem ksiazka = new BookItem(new ItemStack(387, 1));
                if (ksiazka.getTitle() == "Statystyki") {
                    if (ksiazka.getAuthor() == e.getPlayer().getName()) {
                        e.setCancelled(true);
                        e.getItemDrop().remove();
                    }
                }
            }
        }
     
        @EventHandler
        public void onInventoryEvent(InventoryClickEvent e) {
            Inventory inv = e.getInventory();
            BookItem ksiazka = new BookItem(new ItemStack(387, 1));
            if (ksiazka.getTitle() == "Statystyki") {
                if (ksiazka.getAuthor() == e.getWhoClicked().getName()) {
                    int slot = e.getSlot();
                    if (inv.contains(ksiazka.getItemStack().getTypeId())) {
                        if(inv.)
                    }
                }
     
            }
        }
    }

    The code gives player a book when he join, the book is in slot 8. The book also updates everytime the player moves. I want to stop the player from dropping the book and moving it around the inventory. I also got another problem, it does not allow me to put nothing more into the book on the first place. When the book is opened it should look like this:
    Statystyki

    Nick: [variable]
    Honor: [variable]
    Krolestwo: [variable]
    Gildia: [variable]
    Rola w gildii: [variable]
    Ranga spoleczna: [variable]

    But... It looks like this:

    Statystyki
    Nick: [variable]
    Honor: [variable]
    Krolestwo: [variable]

    Thanks in advice, AJAW.

    bump

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

    Infamous Jeezy

    AJAW
    Well first of all, I'm not too sure how the NBT book API works so you may have to search around for more information on it. I do know that there is a character limit for a page, so try spreading your text over a couple pages.

    Secondly for both your InventoryClickEvent and your PlayerDropEvent you create the BookItem rather than getting an existing one so the code will not reach your if statements.
    So what I would do is in your main class write a new line:
    public BookItem ksiazka = new BookItem(new ItemStack(387, 1));
    So if your events are in a different class, you will have to reference your main class if you haven't already. Change MAIN to your main class's name, and OTHERCLASS to the other class's name.

    Code:
    private MAIN plugin;
    public OTHERCLASS(MAIN plugin)
    {
    this.plugin = plugin;
    } 
    Once you do that, you will want to remove all the lines where you define a new BookItem since it's now defined in the main class as a global object and you can call it from any class as long as you reference the main class.
    So as an example I will modify your inventory drop event, although I will not be able to test it.

    Code:
    @EventHandler
    public void onPlayerDropItemEvent(PlayerDropItemEvent e) {
    Material droppeditem = e.getItemDrop().getItemStack().getType(); // Material
    if (droppeditem == Material.WRITTEN_BOOK) {
    if (plugin.ksiazka.getTitle() == "Statystyki") {
    if (plugin.ksiazka.getAuthor() == e.getPlayer().getName()) {
    e.setCancelled(true);
    }
    }
    }
    } 
    So whenever you want to access or read values from the book you call on it with plugin.ksiazka.
    Or you replace plugin with whatever you named the reference variable.
     
  3. Offline

    Kazzababe

    Theres an inventoryClickEvent, you can just cancel it if you dont mind people not being able to move anything at all.
     
  4. Offline

    AJAW

    I want player from moving a specific item, the item is called a "ksiazka". I dont know how to get the book and cancel the event from it.
     
  5. Offline

    puyttre

    AJAW
    I was able to do this by doing the following:
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.MONITOR)
    3. public void onMove(InventoryClickEvent e){
    4. e.setCancelled(true); // This doesn't really work it's just there incase one day it actually does work.
    5. e.setCursor(new ItemStack(Material.AIR)); // Sets the cursor to hold air in their hand.
    6. e.getWhoClicked.closeInventory(); // Closes the players inventory when they try to move something. This is what actually makes this event works. Don't remove it.
    7. }
    8.  

    Of course you could add if() braces to check if the item is a book. You can do that with this code:
    Code:java
    1.  
    2. if(e.getCursorItem() == Material.BOOK){
    3. // of course you would have to check the data value of that item. Don't ask me how to do that; I don't know ;p
    4. // do the stuff above
    5. }
    6.  

    Good luck!
     
  6. Offline

    fireblast709

    To extend puyttre's code a bit:
    Code:java
    1. // Actually, LOWEST is called first (so this is the highest priority)
    2. @EventHandler(priority = EventPriority.LOWEST)
    3. public void onMove(InventoryClickEvent e){
    4. ItemStack i = e.getWhoClicked().getInventory().getItem(8);
    5. if(i != null)
    6. {
    7. System.out.println("Item in slot was: "+i.getType().toString());
    8. if(e.getSlot() == 8 && i.getType() == Material.WRITTEN_BOOK) // Get if the clicked slot was the 8th
    9. {
    10. e.setCancelled(true);
    11. }
    12. }
    13. }


    [edit] My code was wrong, I was getting the wrong inventory for some reason. I fixed what is in the above code block
     
  7. Offline

    mr.deek

    Hello,

    Yes you can do what you are looking for.

    To get you started.

    To make a book use the NBTAGS to add author,title and pages.

    To stop moving the item in the inventory use the 'InventoryClickEvent' event and 'cancel' the event when the player touches the book. (this will also stop them from dropping the item)

    To update the book, use the 'PlayerMoveEvent' event to detect and update the NBTTag data of the 'pages' of the book.

    MyInfo on NBTags
    http://forums.bukkit.org/threads/list-of-item-nbttags-used-standard-in-craftbukkit.113291/

    MrDeek.
     
  8. Offline

    antoniobradiano

    I tried to use Multivers inventories and it did like they said mess with others inventories. Is there another way to stop players from bringing items from one world to another?
     
  9. Offline

    RandomPanda30


    Try not to pull up an old thread. This is a year old.
     
    Garris0n likes this.
  10. Offline

    antoniobradiano

    Should be closed then. All that writing you think you could give insight and help instead of critisising and crap. That's wats wrong with half of all. Want to voice but don't want to help no one. That's why its classified as Unsolved
     
Thread Status:
Not open for further replies.

Share This Page