Minecraft plugin program Item Move or Drop ban? This is my source code:
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class RankUpCommand implements CommandExecutor {
@Override
public boolean onCommand (CommandSender sender, Command cmd, String label, String [] args) {
Player p = (player) transmitter;
if (p.hasPermission ("rankup.open")) {
if ((sender instanceof Player)) {
if (args.length == 0) {
Inventory inventory = Bukkit.createInventory (null, 9 * 3, "§6§lRank-Up");
p.openInventory (inventory);
p.sendMessage ("§7You have the Rank-Up menu open!");
ItemStack diamond = new ItemStack (Material.DIAMOND);
ItemMeta itemMeta = diamond.getItemMeta ();
itemMeta.setDisplayName ("§b§lDiamond Rank");
diamond.setItemMeta (itemMeta);
inventory.setItem (11, diamond);
ItemStack ultra = new ItemStack (Material.BEACON);
ItemMeta ItemMeta = ultra.getItemMeta ();
ItemMeta.setDisplayName ("§b§lUltra rank");
ultra.setItemMeta (ItemMeta);
inventory.setItem (13, ultra);
ItemStack supreme = new ItemStack (Material.REDSTONE_BLOCK);
ItemMeta iTemMeta = supreme.getItemMeta ();
iTemMeta.setDisplayName ("§4§lSupreme Rank");
supreme.setItemMeta (iTemMeta);
inventory.setItem (15, supreme);
ItemStack glass = new ItemStack (Material.STAINED_GLASS_PANE);
ItemMeta itemmeta = glass.getItemMeta ();
itemmeta.setDisplayName ( "");
supreme.setItemMeta (itemmeta);
inventory.setItem (0, glass);
inventory.setItem (1, glass);
inventory.setItem (2, glass);
inventory.setItem (3, glass);
inventory.setItem (4, glass);
inventory.setItem (5, glass);
inventory.setItem (6, glass);
inventory.setItem (7, glass);
inventory.setItem (8, glass);
inventory.setItem (9, glass);
inventory.setItem (10, glass);
inventory.setItem (12, glass);
inventory.setItem (14, glass);
inventory.setItem (16, glass);
inventory.setItem (17, glass);
inventory.setItem (18, glass);
inventory.setItem (19, glass);
inventory.setItem (20, glass);
inventory.setItem (21, glass);
inventory.setItem (22, glass);
inventory.setItem (23, glass);
inventory.setItem (24, glass);
inventory.setItem (25, glass);
inventory.setItem (26, glass);
inventory.setItem (27, glass);
inventory.setItem (28, glass); {
}
}
}
}
return false;
}
You can be in an event (InventoryMoveItemEvent would have to be) with
@EventHandler (priority = EventPriority.NORMAL)
public void onInventoryMoveItemEvent (final InventoryMoveItemEvent event) {
event.setCancelled (true);
}
You can also check whether the inventory is named as you named it, currently he prohibits this with every inventory.
Okay, so you already have a command that you use to create the inventory, fill it with items, and open it.
Now you need the code that prevents the items from being moved and the drop, right?
Let's start with the InventoryClickEvent:
@EventHandler
public void onClick (InventoryClickEvent e) {
if (e.getInventory (). GetName (). Equals ("§6§lRank-Up")) {
if (e.getCurrentItem ()! = null && e.getCurrentItem (). GetType ()! = Material.AIR) {
e.setCancelled (true);
}
}
}
And for dropping you need the PlayerDropItemEvent:
@EventHandler
public void onDrop (PlayerDropItemEvent e) {
if (e.getPlayer (). GetOpenInventory (). GetTitle (). Equals ("§6§lRank-Up") {
e.setCancelled (true);
}
}
Of course, both listeners must be implemented in the respective class and registered in the main class.
What I just noticed while reading your code is that you open the inventory directly after you create it, and then fill in the items, etc.
I would in your place "p.openInventory (inventory);" rather put to the end.
I give no guarantee of accuracy. I just wrote it down quickly without testing it.
If problems still arise, I'm willing to help you further.
This event is also used by e.g. Hoppers who push things into other inventories are triggered.
If this code is correct now, because the move is still going on:
package de.diego.rankup.commands;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class RankUpCommand implements CommandExecutor {
@Override
public boolean onCommand (CommandSender sender, Command cmd, String label, String [] args) {
Player p = (player) transmitter;
if (p.hasPermission ("rankup.open")) {
if ((sender instanceof Player)) {
if (args.length == 0) {
Inventory inventory = Bukkit.createInventory (null, 9 * 3, "§6§lRank-Up");
p.sendMessage ("§7You have the Rank-Up menu open!");
ItemStack diamond = new ItemStack (Material.DIAMOND);
ItemMeta itemMeta = diamond.getItemMeta ();
itemMeta.setDisplayName ("§b§lDiamond Rank");
diamond.setItemMeta (itemMeta);
inventory.setItem (11, diamond);
ItemStack ultra = new ItemStack (Material.BEACON);
ItemMeta ItemMeta = ultra.getItemMeta ();
ItemMeta.setDisplayName ("§b§lUltra rank");
ultra.setItemMeta (ItemMeta);
inventory.setItem (13, ultra);
ItemStack supreme = new ItemStack (Material.REDSTONE_BLOCK);
ItemMeta iTemMeta = supreme.getItemMeta ();
iTemMeta.setDisplayName ("§4§lSupreme Rank");
supreme.setItemMeta (iTemMeta);
inventory.setItem (15, supreme);
ItemStack glass = new ItemStack (Material.STAINED_GLASS_PANE);
ItemMeta itemmeta = glass.getItemMeta ();
itemmeta.setDisplayName ( "");
supreme.setItemMeta (itemmeta);
inventory.setItem (0, glass);
inventory.setItem (1, glass);
inventory.setItem (2, glass);
inventory.setItem (3, glass);
inventory.setItem (4, glass);
inventory.setItem (5, glass);
inventory.setItem (6, glass);
inventory.setItem (7, glass);
inventory.setItem (8, glass);
inventory.setItem (9, glass);
inventory.setItem (10, glass);
inventory.setItem (12, glass);
inventory.setItem (14, glass);
inventory.setItem (16, glass);
inventory.setItem (17, glass);
inventory.setItem (18, glass);
inventory.setItem (19, glass);
inventory.setItem (20, glass);
inventory.setItem (21, glass);
inventory.setItem (22, glass);
inventory.setItem (23, glass);
inventory.setItem (24, glass);
inventory.setItem (25, glass);
inventory.setItem (26, glass);
inventory.setItem (27, glass);
inventory.setItem (28, glass);
p.openInventory (inventory); {
}
}
}
}
return false;
}
@EventHandler
public void onClick (InventoryClickEvent e) {
if (e.getInventory (). GetName (). Equals ("§6§lRank-Up")) {
if (e.getCurrentItem ()! = null && e.getCurrentItem (). GetType ()! = Material.AIR) {
e.setCancelled (true);
}
}
}
@EventHandler
public void onDrop (PlayerDropItemEvent e) {
if (e.getPlayer (). GetOpenInventory (). GetTitle (). Equals ("§6§lRank-Up") {
e.setCancelled (true);
}
}
}
You have to implement the listener too.
public class RankUpCommand implements CommandExecutor, Listener {
and in the Main class (in onEnable ()) register the listener:
getServer (). GetPluginManager (). RegisterEvents (new RankUpCommand (), this);
That's right, thanks
But I can't register it in the Main class. Then there's always the message: The method registerEvents (Listener, Plugin) in the type PluginManager is not applicable for the arguments (RankUpCommand, Main)
Send all the code of the Main class. I suspect you have imported the wrong listener. This would be correct:
import org.bukkit.event.Listener;
The class must be able to execute events
listener
to implement.
In addition, you need
getServer (). GetPluginManager (). RegisterEvents (new RankUpCommand (), this);
in your onEnable () method in the mainstream plugin.