Cleaned up custom block breaking

This commit is contained in:
Indyuce 2022-11-03 19:12:00 +01:00
parent 49d92288d8
commit 555c6e2284
4 changed files with 164 additions and 169 deletions

View File

@ -7,67 +7,71 @@ import net.Indyuce.mmoitems.api.util.MushroomState;
import net.Indyuce.mmoitems.stat.data.BooleanData;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.Random;
public class CustomBlock {
private final int id;
private final MushroomState state;
private final int id;
private final MushroomState state;
private final MMOItem mmoitem;
private final MMOItem mmoitem;
private final WorldGenTemplate template;
private final int minExp, maxExp, requiredPower;
private final boolean requirePowerToBreak;
@Nullable
private final WorldGenTemplate template;
private final int minExp, maxExp, requiredPower;
private final boolean requirePowerToBreak;
public CustomBlock(MushroomState state, MMOItem mmoitem) {
this.mmoitem = mmoitem;
private static final Random RANDOM = new Random();
this.id = (mmoitem.hasData(ItemStats.BLOCK_ID)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.BLOCK_ID)).getValue() : 0;
this.state = state;
public CustomBlock(MushroomState state, MMOItem mmoitem) {
this.mmoitem = mmoitem;
this.minExp = (mmoitem.hasData(ItemStats.MIN_XP)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MIN_XP)).getValue() : 0;
this.maxExp = (mmoitem.hasData(ItemStats.MAX_XP)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MAX_XP)).getValue() : 0;
this.requiredPower = (mmoitem.hasData(ItemStats.REQUIRED_POWER)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.REQUIRED_POWER)).getValue()
: 0;
this.id = mmoitem.hasData(ItemStats.BLOCK_ID) ? (int) ((DoubleData) mmoitem.getData(ItemStats.BLOCK_ID)).getValue() : 0;
this.state = state;
this.requirePowerToBreak = (mmoitem.hasData(ItemStats.REQUIRE_POWER_TO_BREAK)) ? (boolean) ((BooleanData) mmoitem.getData(ItemStats.REQUIRE_POWER_TO_BREAK)).isEnabled() : false;
this.minExp = mmoitem.hasData(ItemStats.MIN_XP) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MIN_XP)).getValue() : 0;
this.maxExp = mmoitem.hasData(ItemStats.MAX_XP) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MAX_XP)).getValue() : 0;
this.requiredPower = mmoitem.hasData(ItemStats.REQUIRED_POWER) ? (int) ((DoubleData) mmoitem.getData(ItemStats.REQUIRED_POWER)).getValue() : 0;
this.requirePowerToBreak = mmoitem.hasData(ItemStats.REQUIRE_POWER_TO_BREAK) && ((BooleanData) mmoitem.getData(ItemStats.REQUIRE_POWER_TO_BREAK)).isEnabled();
this.template = mmoitem.hasData(ItemStats.GEN_TEMPLATE) ? MMOItems.plugin.getWorldGen().getOrThrow((mmoitem.getData(ItemStats.GEN_TEMPLATE)).toString()) : null;
}
this.template = (mmoitem.hasData(ItemStats.GEN_TEMPLATE))
? MMOItems.plugin.getWorldGen().getOrThrow((mmoitem.getData(ItemStats.GEN_TEMPLATE)).toString())
: null;
}
public int getId() {
return id;
}
public int getId() {
return id;
}
public MushroomState getState() {
return state;
}
public boolean hasGenTemplate() {
return template != null;
}
public MushroomState getState() {
return state;
}
public WorldGenTemplate getGenTemplate() {
return template;
}
public boolean hasGenTemplate() {
return template != null;
}
public int getMinExpDrop() {
return minExp;
}
public WorldGenTemplate getGenTemplate() {
return template;
}
public int getMaxExpDrop() {
return maxExp;
}
public int getMinExpDrop() {
return minExp;
}
public int rollExperience() {
return Math.max(0, maxExp < minExp ? minExp : RANDOM.nextInt(maxExp - minExp + 1) + minExp);
}
public int getMaxExpDrop() {
return maxExp;
}
public int getRequiredPower() {
return requiredPower;
}
public int getRequiredPower() {
return requiredPower;
}
public ItemStack getItem() {
return mmoitem.newBuilder().build();
}
public ItemStack getItem() {
return mmoitem.newBuilder().build();
}
public boolean requirePowerToBreak() {
return requirePowerToBreak;

View File

@ -1,11 +1,19 @@
package net.Indyuce.mmoitems.stat.block;
import net.Indyuce.mmoitems.stat.type.BooleanStat;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
import org.bukkit.Material;
/**
* Vanilla behaviour:
* When a player tries to break a block but doesn't
* have enough pickaxe power, the block is broken but
* does NOT drop anything.
* <p>
* This option:
* When toggled on, the block simply won't break/drop
*/
public class RequirePowerToBreak extends BooleanStat {
public RequirePowerToBreak() {
super("REQUIRE_POWER_TO_BREAK", Material.BEDROCK, "Require Power to Break", new String[] { "If you need the required pickaxe", "power to break this custom block." }, new String[] { "block" });
super("REQUIRE_POWER_TO_BREAK", Material.BEDROCK, "Require Power to Break", new String[]{"When enabled, the block will NOT break", "if the player doesn't have enough pickaxe", "power, unlike vanilla block behaviour."}, new String[]{"block"});
}
}

View File

@ -82,34 +82,32 @@ public class MMOUtils {
}
public static int getPickaxePower(Player player) {
ItemStack item = player.getInventory().getItemInMainHand();
if (item != null && item.getType() != Material.AIR) {
NBTItem nbt = NBTItem.get(item);
if (nbt.hasType())
return nbt.getInteger("MMOITEMS_PICKAXE_POWER");
else {
switch (item.getType().name()) {
case "WOODEN_PICKAXE":
case "WOOD_PICKAXE":
return 5;
case "STONE_PICKAXE":
return 10;
case "GOLDEN_PICKAXE":
case "GOLD_PICKAXE":
return 15;
case "IRON_PICKAXE":
return 20;
case "DIAMOND_PICKAXE":
return 25;
case "NETHERITE_PICKAXE":
return 30;
default:
return 0;
}
}
}
final ItemStack item = player.getInventory().getItemInMainHand();
if (item == null || item.getType() == Material.AIR)
return 0;
return 0;
final NBTItem nbt = NBTItem.get(item);
if (nbt.hasTag("MMOITEMS_PICKAXE_POWER"))
return nbt.getInteger("MMOITEMS_PICKAXE_POWER");
switch (item.getType().name()) {
case "WOODEN_PICKAXE":
case "WOOD_PICKAXE":
return 5;
case "STONE_PICKAXE":
return 10;
case "GOLDEN_PICKAXE":
case "GOLD_PICKAXE":
return 15;
case "IRON_PICKAXE":
return 20;
case "DIAMOND_PICKAXE":
return 25;
case "NETHERITE_PICKAXE":
return 30;
default:
return 0;
}
}
/**

View File

@ -1,10 +1,10 @@
package net.Indyuce.mmoitems.listener;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.util.MMOUtils;
import net.Indyuce.mmoitems.api.block.CustomBlock;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.NBTItem;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.block.CustomBlock;
import net.Indyuce.mmoitems.util.MMOUtils;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
@ -22,113 +22,98 @@ import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.EquipmentSlot;
import java.util.Optional;
import java.util.Random;
import java.util.logging.Level;
public class CustomBlockListener implements Listener {
private static final Random RANDOM = new Random();
public CustomBlockListener() {
if (MMOItems.plugin.getLanguage().replaceMushroomDrops)
Bukkit.getPluginManager().registerEvents(new MushroomReplacer(), MMOItems.plugin);
}
public CustomBlockListener() {
if (MMOItems.plugin.getLanguage().replaceMushroomDrops)
Bukkit.getPluginManager().registerEvents(new MushroomReplacer(), MMOItems.plugin);
}
@EventHandler(ignoreCancelled = true)
public void a(BlockPhysicsEvent event) {
if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getChangedType())) {
event.setCancelled(true);
event.getBlock().getState().update(true, false);
}
}
@EventHandler(ignoreCancelled = true)
public void a(BlockPhysicsEvent event) {
if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getChangedType())) {
event.setCancelled(true);
event.getBlock().getState().update(true, false);
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void b(BlockBreakEvent event) {
Optional<CustomBlock> opt = MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getBlockData());
if (!opt.isPresent()) {
return;
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void b(BlockBreakEvent event) {
if (event.getPlayer().getGameMode() == GameMode.CREATIVE)
return;
CustomBlock block = opt.get();
Optional<CustomBlock> opt = MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getBlockData());
if (!opt.isPresent())
return;
final int power = MMOUtils.getPickaxePower(event.getPlayer());
final CustomBlock block = opt.get();
final int power = MMOUtils.getPickaxePower(event.getPlayer());
if (power < block.getRequiredPower()) {
if (block.requirePowerToBreak()) {
event.setCancelled(true);
} else {
event.setDropItems(false);
event.setExpToDrop(0);
}
return;
}
if ( block.requirePowerToBreak() && block.getRequiredPower()>0 ){
if (power < block.getRequiredPower()) {
event.setCancelled(true);
return;
}
else if (power >= block.getRequiredPower()) {
event.setDropItems(false);
event.setExpToDrop(event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0
: MMOUtils.getPickaxePower(event.getPlayer()) >= block.getRequiredPower()
? block.getMaxExpDrop() == 0 && block.getMinExpDrop() == 0 ? 0
: RANDOM.nextInt((block.getMaxExpDrop() - block.getMinExpDrop()) + 1) + block.getMinExpDrop()
: 0);
return;
}
event.setCancelled(true);
return;
}
event.setDropItems(false);
event.setExpToDrop(event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0 : block.rollExperience());
}
event.setDropItems(false);
event.setExpToDrop(event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0
: MMOUtils.getPickaxePower(event.getPlayer()) >= block.getRequiredPower()
? block.getMaxExpDrop() == 0 && block.getMinExpDrop() == 0 ? 0
: RANDOM.nextInt((block.getMaxExpDrop() - block.getMinExpDrop()) + 1) + block.getMinExpDrop()
: 0);
}
@Deprecated
private static int getPickaxePower(Player player) {
return MMOUtils.getPickaxePower(player);
}
@EventHandler(priority = EventPriority.HIGHEST)
public void c(BlockPlaceEvent event) {
if (!event.isCancelled() && !isMushroomBlock(event.getBlockPlaced().getType())) {
NBTItem nbtItem = MythicLib.plugin.getVersion().getWrapper().getNBTItem(event.getItemInHand());
int blockId = nbtItem.getInteger("MMOITEMS_BLOCK_ID");
if (blockId > 160 || blockId < 1 || blockId == 54) // checks if block is a custom block
return;
if (MMOItems.plugin.getCustomBlocks().getBlock(blockId) == null) {
MMOItems.plugin.getLogger().log(Level.SEVERE, "Could not load custom block '" + blockId + "': " + " Block is not registered.");
MMOItems.plugin.getLogger().log(Level.SEVERE, "Try reloading the plugin to solve the issue.");
event.setCancelled(true);
return;
}
@Deprecated
private static int getPickaxePower(Player player) {
return MMOUtils.getPickaxePower(player);
}
CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(blockId); // stores the custom block
Block newBlock = event.getBlockPlaced();
newBlock.setType(block.getState().getType(), false);
newBlock.setBlockData(block.getState().getBlockData(), false);
@EventHandler(priority = EventPriority.HIGHEST)
public void c(BlockPlaceEvent event) {
if (!event.isCancelled() && !isMushroomBlock(event.getBlockPlaced().getType())) {
NBTItem nbtItem = MythicLib.plugin.getVersion().getWrapper().getNBTItem(event.getItemInHand());
int blockId = nbtItem.getInteger("MMOITEMS_BLOCK_ID");
if (blockId > 160 || blockId < 1 || blockId == 54) // checks if block is a custom block
return;
if (MMOItems.plugin.getCustomBlocks().getBlock(blockId) == null) {
MMOItems.plugin.getLogger().log(Level.SEVERE, "Could not load custom block '" + blockId + "': " + " Block is not registered.");
MMOItems.plugin.getLogger().log(Level.SEVERE, "Try reloading the plugin to solve the issue.");
event.setCancelled(true);
return;
}
BlockPlaceEvent bpe = new BlockPlaceEvent(newBlock, newBlock.getState(), event.getBlockAgainst(), event.getItemInHand(), event.getPlayer(), true,
EquipmentSlot.HAND);
Bukkit.getServer().getPluginManager().callEvent(bpe);
}
}
CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(blockId); // stores the custom block
Block newBlock = event.getBlockPlaced();
newBlock.setType(block.getState().getType(), false);
newBlock.setBlockData(block.getState().getBlockData(), false);
@EventHandler(ignoreCancelled = true)
public void d(BlockIgniteEvent event) {
if (event.getCause() == IgniteCause.LAVA || event.getCause() == IgniteCause.SPREAD) {
BlockFace[] faces = {BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST};
for (BlockFace face : faces)
if (MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getRelative(face).getBlockData()).isPresent())
event.setCancelled(true);
}
}
BlockPlaceEvent bpe = new BlockPlaceEvent(newBlock, newBlock.getState(), event.getBlockAgainst(), event.getItemInHand(), event.getPlayer(), true,
EquipmentSlot.HAND);
Bukkit.getServer().getPluginManager().callEvent(bpe);
}
}
private boolean isMushroomBlock(Material material) {
return (material == Material.BROWN_MUSHROOM_BLOCK || material == Material.MUSHROOM_STEM || material == Material.RED_MUSHROOM_BLOCK);
}
@EventHandler(ignoreCancelled = true)
public void d(BlockIgniteEvent event) {
if (event.getCause() == IgniteCause.LAVA || event.getCause() == IgniteCause.SPREAD) {
BlockFace[] faces = { BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST };
for (BlockFace face : faces)
if (MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getRelative(face).getBlockData()).isPresent())
event.setCancelled(true);
}
}
private boolean isMushroomBlock(Material material) {
return (material == Material.BROWN_MUSHROOM_BLOCK || material == Material.MUSHROOM_STEM || material == Material.RED_MUSHROOM_BLOCK);
}
public static class MushroomReplacer implements Listener {
@EventHandler(ignoreCancelled = true)
public void d(BlockBreakEvent event) {
if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getType())
&& MMOItems.plugin.getDropTables().hasSilkTouchTool(event.getPlayer()))
event.setDropItems(false);
}
}
public static class MushroomReplacer implements Listener {
@EventHandler(ignoreCancelled = true)
public void d(BlockBreakEvent event) {
if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getType())
&& MMOItems.plugin.getDropTables().hasSilkTouchTool(event.getPlayer()))
event.setDropItems(false);
}
}
}