Created
August 15, 2018 09:51
-
-
Save Jannyboy11/52c45bcde2a7b9e1c2a28dfc7a9a7c39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
import org.bukkit.Material; | |
import org.bukkit.Server; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.command.TabExecutor; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.inventory.InventoryClickEvent; | |
import org.bukkit.event.inventory.InventoryType; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.InventoryHolder; | |
import org.bukkit.inventory.ItemStack; | |
public class TestCommandExecutor implements TabExecutor, Listener { | |
private final ExamplePlugin plugin; | |
public TestCommandExecutor(ExamplePlugin plugin) { | |
this.plugin = plugin; | |
plugin.getCommand("test").setExecutor(this); | |
plugin.getServer().getPluginManager().registerEvents(this, plugin); | |
} | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | |
if (sender instanceof Player) { | |
Player player = (Player) sender; | |
if (args.length == 0) { | |
Inventory testInventory = new TestInventoryHolder(player.getServer()).getInventory(); | |
testInventory.addItem(new ItemStack(Material.COBBLESTONE), new ItemStack(Material.DIAMOND), new ItemStack(Material.OAK_LOG)); | |
player.openInventory(testInventory); | |
} else { | |
String firstArg = args[0]; | |
Inventory testInventory = new TestInventoryHolder(player.getServer(), InventoryType.valueOf(firstArg.toUpperCase())).getInventory(); | |
testInventory.addItem(new ItemStack(Material.COBBLESTONE), new ItemStack(Material.DIAMOND), new ItemStack(Material.OAK_LOG)); | |
player.openInventory(testInventory); | |
} | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { | |
return Stream.of(InventoryType.values()).map(InventoryType::name).collect(Collectors.toList()); | |
} | |
@EventHandler | |
public void onInventoryClick(InventoryClickEvent event) { | |
Inventory topInventory = event.getView().getTopInventory(); | |
if (topInventory.getHolder() instanceof TestInventoryHolder) { | |
/* Correct behaviour - branch is correctly taken for the following inventory types: | |
* ANVIL | |
* CHEST | |
* ENCHANTING | |
* ENDER_CHEST | |
* PLAYER | |
* SHULKER_BOX | |
* WORKBENCH | |
*/ | |
plugin.getLogger().info("DEBUG clicked TestInventoryHolder - plugin is " + (plugin.isEnabled() ? "enabled" : "disabled") + ". cancelling event."); | |
event.setCancelled(true); | |
} else { | |
/* Spigot bug - this branch is taken for the following inventory types because the inventory holder is null: | |
* BEACON | |
* BREWING | |
* DISPENSER | |
* DROPPER | |
* FURNACE | |
* HOPPER | |
*/ | |
plugin.getLogger().info("DEBUG clicked an inventory not held by TestInventoryHolder"); | |
plugin.getLogger().info("DEBUG topinventory holder = " + topInventory.getHolder()); | |
} | |
} | |
private static class TestInventoryHolder implements InventoryHolder { | |
private final Inventory inventory; | |
public TestInventoryHolder(Server server) { | |
this.inventory = server.createInventory(this, 3 * 9, "Test Inventory"); | |
} | |
public TestInventoryHolder(Server server, InventoryType type) { | |
this.inventory = server.createInventory(this, type); | |
} | |
public Inventory getInventory() { | |
return inventory; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment