Reminder, this plugin itself works, just gives errors when hitting.
I am attempting to completely change how chests work in minecraft. In my plugin, when you open them they give you an item then disappear, which has been working but there is an issue.
I call the PlayerInteractEvent and use an if statement to see if the block type is a chest, if it is then it will cancel the event, make the chest disappear and give the player an item. But as the main class passes the events to the ChestRewards class, it also seems to pass the hitting event or an Action of punching from PlayerInteractEvent which was expected, but that is why I used an if statement to only use the event passed if the block type is a chest. But it would seem that it errors in the console as cannot pass event, please help!
@EventHandler
public void catchChestOpen(PlayerInteractEvent event) {
Entity p = event.getPlayer();
Player player = (Player) p;
if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.PHYSICAL || event.getAction() == Action.LEFT_CLICK_BLOCK){
// TODO: 2016-12-06 Nothing
}
if (event.getClickedBlock().getType() == Material.CHEST) {
event.getClickedBlock().setType(Material.AIR);
event.setCancelled(true);
int rando = (int) (Math.random() * 10);
//---------------------------------------------------------------------------------
if (rando == 1) {
ItemStack Axe = new ItemStack(Material.DIAMOND_AXE, 1);
ItemMeta meta = Axe.getItemMeta();
List<String> lores = new ArrayList<String>();
lores.add(ChatColor.DARK_PURPLE + "The axe of a long lost survivor");
meta.setDisplayName(ChatColor.DARK_RED + "Survivor Axe");
meta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
meta.addEnchant(Enchantment.DURABILITY, 2, true);
meta.setLore(lores);
Axe.setItemMeta(meta);
Axe.setDurability((short) 800);
player.getInventory().addItem(Axe);
player.sendMessage(ChatColor.GOLD + "You opened a" + ChatColor.AQUA + " RARE " + ChatColor.GOLD + "loot chest!");
}
//---------------------------------------------------------------------------------
if (rando > 1) {
ItemStack IronSword = new ItemStack(Material.IRON_SWORD, 1);
ItemMeta meta2 = IronSword.getItemMeta();
List<String> lores2 = new ArrayList<String>();
lores2.add(ChatColor.DARK_PURPLE + "A reliable iron sword");
meta2.setDisplayName(ChatColor.DARK_RED + "Reliable Iron Sword");
meta2.addEnchant(Enchantment.DURABILITY, 3, true);
meta2.setLore(lores2);
IronSword.setItemMeta(meta2);
IronSword.setDurability((short) 800);
player.getInventory().addItem(IronSword);
player.sendMessage(ChatColor.GOLD + "You opened a loot chest!");
}
//---------------------------------------------------------------------------------
}
}
This is the error itself
[09:32:00 ERROR]: Could not pass event PlayerInteractEvent to Apocalypse v1.0
Since you are not stopping the execution of the code if the Action
is not a RIGHT_CLICK_BLOCK
or LEFT_CLICK_BLOCK
, then the event.getClickedBlock()
will be null when you call block.getType()
on it to compare it to a chest if the player left or right clicked air.
I suggest you add a check to see if the action has something to do with a block. The below snippet will check if the player either left or right clicked a chest:
if ((event.getAction() == Action.RIGHT_CLICK_BLOCK ||
event.getAction() == Action.LEFT_CLICK_BLOCK) &&
event.getClickedBlock().getType() == Material.CHEST) {