!Reduced nbt tag calculations for custom blocks

This commit is contained in:
Indyuce 2019-11-03 14:38:19 +01:00
parent d75e3bc5c0
commit 025ecdc2d6

View File

@ -31,65 +31,59 @@ import net.Indyuce.mmoitems.api.item.NBTItem;
public class CustomBlockListener implements Listener { public class CustomBlockListener implements Listener {
Random rnd = new Random(); Random rnd = new Random();
public CustomBlockListener() { public CustomBlockListener() {
if(MMOItems.plugin.getLanguage().replaceMushroomDrops) if (MMOItems.plugin.getLanguage().replaceMushroomDrops)
Bukkit.getPluginManager().registerEvents(new MushroomReplacer(), MMOItems.plugin); Bukkit.getPluginManager().registerEvents(new MushroomReplacer(), MMOItems.plugin);
} }
@EventHandler @EventHandler
public void a(BlockPhysicsEvent event) { public void a(BlockPhysicsEvent event) {
if(MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getChangedType())) { if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getChangedType())) {
event.setCancelled(true); event.setCancelled(true);
event.getBlock().getState().update(true, false); event.getBlock().getState().update(true, false);
} }
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void b(BlockBreakEvent event) { public void b(BlockBreakEvent event) {
Material type = event.getBlock().getType(); Material type = event.getBlock().getType();
if(MMOItems.plugin.getCustomBlocks().isMushroomBlock(type)) { if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(type)) {
CustomBlock block = CustomBlock.getFromData(event.getBlock().getBlockData()); CustomBlock block = CustomBlock.getFromData(event.getBlock().getBlockData());
if(block != null) { if (block != null) {
event.setDropItems(false); event.setDropItems(false);
event.setExpToDrop( event.setExpToDrop(event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0 : CustomBlockListener.getPickaxePower(event.getPlayer()) >= block.getRequiredPower() ? block.getMaxXPDrop() == 0 && block.getMinXPDrop() == 0 ? 0 : rnd.nextInt((block.getMaxXPDrop() - block.getMinXPDrop()) + 1) + block.getMinXPDrop() : 0);
event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0 :
CustomBlockListener.getPickaxePower(event.getPlayer()) >= block.getRequiredPower() ?
block.getMaxXPDrop() == 0 && block.getMinXPDrop() == 0 ? 0 :
rnd.nextInt((block.getMaxXPDrop() - block.getMinXPDrop()) + 1) + block.getMinXPDrop() : 0);
} }
} }
} }
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void c(PlayerInteractEvent event) { public void c(PlayerInteractEvent event) {
if(!event.hasItem()) return; if (!event.hasItem() || event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getHand() != EquipmentSlot.HAND || event.getClickedBlock().getType().isInteractable())
if(event.getAction() != Action.RIGHT_CLICK_BLOCK) return; return;
if(event.getHand() != EquipmentSlot.HAND) return; if (event.getItem().getType() == Material.CLAY_BALL) {
if(event.getClickedBlock().getType().isInteractable()) return; NBTItem nbtItem = MMOItems.plugin.getNMS().getNBTItem(event.getItem());
if(event.getItem().getType() == Material.CLAY_BALL) { if (nbtItem.getInteger("MMOITEMS_BLOCK_ID") > 160 || nbtItem.getInteger("MMOITEMS_BLOCK_ID") < 1)
if(NBTItem.get(event.getItem()).getInteger("MMOITEMS_BLOCK_ID") > 160 || return;
NBTItem.get(event.getItem()).getInteger("MMOITEMS_BLOCK_ID") < 1) return;
CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(NBTItem.get(event.getItem()).getInteger("MMOITEMS_BLOCK_ID")); CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(nbtItem.getInteger("MMOITEMS_BLOCK_ID"));
List<Material> mat = Arrays.asList(Material.GRASS, Material.TALL_GRASS, Material.SEAGRASS, Material.TALL_SEAGRASS, List<Material> mat = Arrays.asList(Material.GRASS, Material.TALL_GRASS, Material.SEAGRASS, Material.TALL_SEAGRASS, Material.FERN, Material.LARGE_FERN, Material.DEAD_BUSH, Material.SNOW);
Material.FERN, Material.LARGE_FERN, Material.DEAD_BUSH, Material.SNOW);
Block modify = mat.contains(event.getClickedBlock().getType()) ? event.getClickedBlock() : event.getClickedBlock().getRelative(event.getBlockFace());
Block modify = mat.contains(event.getClickedBlock().getType())
? event.getClickedBlock() : event.getClickedBlock().getRelative(event.getBlockFace()); if (isStandingInside(event.getPlayer().getLocation(), modify.getLocation()))
return;
if(isStandingInside(event.getPlayer().getLocation(), modify.getLocation())) return;
if (!mat.contains(modify.getType()))
if(!mat.contains(modify.getType())) switch (modify.getType()) {
switch(modify.getType()) { case AIR:
case AIR: case CAVE_AIR:
case CAVE_AIR: case STRUCTURE_VOID:
case STRUCTURE_VOID: break;
break; default:
default: return;
return;
} }
Block oldState = modify; Block oldState = modify;
@ -97,73 +91,77 @@ public class CustomBlockListener implements Listener {
BlockData cachedData = modify.getBlockData(); BlockData cachedData = modify.getBlockData();
modify.setType(block.getType(), false); modify.setType(block.getType(), false);
modify.setBlockData(block.getBlockData(), false); modify.setBlockData(block.getBlockData(), false);
MMOItems.plugin.getNMS().playArmAnimation(event.getPlayer()); MMOItems.plugin.getNMS().playArmAnimation(event.getPlayer());
modify.getWorld().playSound(event.getPlayer().getLocation(), MMOItems.plugin.getNMS().getBlockPlaceSound(modify), 0.8f, 1.0f); modify.getWorld().playSound(event.getPlayer().getLocation(), MMOItems.plugin.getNMS().getBlockPlaceSound(modify), 0.8f, 1.0f);
BlockPlaceEvent bpe = new BlockPlaceEvent(modify, oldState.getState(), event.getClickedBlock(), event.getItem(), event.getPlayer(), true, EquipmentSlot.HAND); BlockPlaceEvent bpe = new BlockPlaceEvent(modify, oldState.getState(), event.getClickedBlock(), event.getItem(), event.getPlayer(), true, EquipmentSlot.HAND);
Bukkit.getServer().getPluginManager().callEvent(bpe); Bukkit.getServer().getPluginManager().callEvent(bpe);
if(bpe.isCancelled()) { if (bpe.isCancelled()) {
modify.setType(cachedType); modify.setType(cachedType);
modify.setBlockData(cachedData); modify.setBlockData(cachedData);
} } else if (event.getPlayer().getGameMode() != GameMode.CREATIVE) {
else if(event.getPlayer().getGameMode() != GameMode.CREATIVE) { ItemStack stack = event.getItem();
ItemStack stack = event.getItem(); stack.setAmount(stack.getAmount() - 1); stack.setAmount(stack.getAmount() - 1);
event.getPlayer().getInventory().setItemInMainHand(stack.getAmount() > 0 ? stack : null); event.getPlayer().getInventory().setItemInMainHand(stack.getAmount() > 0 ? stack : null);
} }
} }
/**else if(MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getItem().getType())) { /**
event.setCancelled(true); * else
Block modify = event.getClickedBlock().getRelative(event.getBlockFace()); * if(MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getItem().getType()))
* { event.setCancelled(true); Block modify =
if(isStandingInside(event.getPlayer().getLocation(), modify.getLocation())) return; * event.getClickedBlock().getRelative(event.getBlockFace());
if(modify.getType() != Material.AIR) return; *
* if(isStandingInside(event.getPlayer().getLocation(),
Block oldState = modify; * modify.getLocation())) return; if(modify.getType() != Material.AIR)
modify.setType(event.getItem().getType(), false); * return;
modify.setBlockData(event.getItem().getType().createBlockData(), false); *
* Block oldState = modify; modify.setType(event.getItem().getType(),
BlockPlaceEvent bpe = new BlockPlaceEvent(modify, oldState.getState(), event.getClickedBlock(), event.getItem(), event.getPlayer(), true, EquipmentSlot.HAND); * false);
Bukkit.getServer().getPluginManager().callEvent(bpe); * modify.setBlockData(event.getItem().getType().createBlockData(),
if(bpe.isCancelled()) modify.setType(Material.AIR); * false);
}*/ *
* BlockPlaceEvent bpe = new BlockPlaceEvent(modify,
* oldState.getState(), event.getClickedBlock(), event.getItem(),
* event.getPlayer(), true, EquipmentSlot.HAND);
* Bukkit.getServer().getPluginManager().callEvent(bpe);
* if(bpe.isCancelled()) modify.setType(Material.AIR); }
*/
} }
@EventHandler @EventHandler
public void d(BlockIgniteEvent event) { public void d(BlockIgniteEvent event) {
if(event.isCancelled()) return; if (event.isCancelled())
if(event.getCause() == IgniteCause.LAVA || event.getCause() == IgniteCause.SPREAD) { return;
if (event.getCause() == IgniteCause.LAVA || event.getCause() == IgniteCause.SPREAD) {
BlockFace[] faces = { BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST }; BlockFace[] faces = { BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST };
for(BlockFace face : faces) for (BlockFace face : faces)
if(MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getRelative(face).getType()) && if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getRelative(face).getType()) && CustomBlock.getFromData(event.getBlock().getRelative(face).getBlockData()) != null)
CustomBlock.getFromData(event.getBlock().getRelative(face).getBlockData()) != null) event.setCancelled(true); event.setCancelled(true);
} }
} }
private boolean isStandingInside(Location p, Location b) { private boolean isStandingInside(Location p, Location b) {
return (p.getBlockX() == b.getBlockX() && return (p.getBlockX() == b.getBlockX() && (p.getBlockY() == b.getBlockY() || p.getBlockY() + 1 == b.getBlockY()) && p.getBlockZ() == b.getBlockZ());
(p.getBlockY() == b.getBlockY() || p.getBlockY() + 1 == b.getBlockY()) &&
p.getBlockZ() == b.getBlockZ());
} }
public static int getPickaxePower(Player player) { public static int getPickaxePower(Player player) {
ItemStack item = player.getInventory().getItemInMainHand(); ItemStack item = player.getInventory().getItemInMainHand();
if(item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
NBTItem nbt = NBTItem.get(item); NBTItem nbt = NBTItem.get(item);
if(nbt.hasType()) { if (nbt.hasType())
return nbt.getInteger("MMOITEMS_PICKAXE_POWER"); return nbt.getInteger("MMOITEMS_PICKAXE_POWER");
}
} }
return 0; return 0;
} }
public class MushroomReplacer implements Listener { public class MushroomReplacer implements Listener {
@EventHandler @EventHandler
public void d(BlockBreakEvent event) { public void d(BlockBreakEvent event) {
if(event.isCancelled()) return; if (event.isCancelled())
if(MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getType()) && return;
MMOItems.plugin.getDropTables().hasSilkTouchTool(event.getPlayer())) if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getType()) && MMOItems.plugin.getDropTables().hasSilkTouchTool(event.getPlayer()))
event.setDropItems(false); event.setDropItems(false);
} }
} }