From 4780ede21fe925a8ed8c888670e54828510197ba Mon Sep 17 00:00:00 2001 From: zhangyuheng Date: Wed, 28 Feb 2024 16:07:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=85=AC=E5=85=B1=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../deercloud/liteworldedit/Jobs/Job.java | 86 +++++++++++++++++-- .../deercloud/liteworldedit/Jobs/Place.java | 17 ++-- .../deercloud/liteworldedit/Jobs/Remove.java | 52 +++-------- 4 files changed, 103 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index edab1bb..09aab82 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ site.deercloud LiteWorldEdit - 2.3.5.1 + 2.3.5.2 jar LiteWorldEdit diff --git a/src/main/java/site/deercloud/liteworldedit/Jobs/Job.java b/src/main/java/site/deercloud/liteworldedit/Jobs/Job.java index 998f860..9b79bb8 100644 --- a/src/main/java/site/deercloud/liteworldedit/Jobs/Job.java +++ b/src/main/java/site/deercloud/liteworldedit/Jobs/Job.java @@ -1,18 +1,25 @@ package site.deercloud.liteworldedit.Jobs; -import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; +import site.deercloud.liteworldedit.LoggerX; import site.deercloud.liteworldedit.Notification; +import java.util.HashMap; + public class Job { - protected World _world; - protected Location _location; - protected Long _time; - protected Player _creator; - protected Inventory _inventory; + private final World _world; + private final Location _location; + private final Long _time; + private final Player _creator; + private final Inventory _inventory; public Long get_time() { return _time; @@ -22,6 +29,26 @@ public class Job { return _creator; } + public Inventory get_inventory() { + return _inventory; + } + + public Location get_location() { + return _location; + } + + public World get_world() { + return _world; + } + + public Job(World world, Location location, Player player) { + _world = world; + _location = location; + _creator = player; + _time = System.currentTimeMillis(); + _inventory = player.getInventory(); + } + public JobErrCode Do() { // nothing return JobErrCode.OK; @@ -38,4 +65,51 @@ public class Job { return true; } + public static HashMap getNetherPickaxes(Player player) { + Inventory _inventory = player.getInventory(); + return _inventory.all(Material.NETHERITE_PICKAXE); + } + + public static ItemStack getUsableNetherPickaxe(HashMap pickaxes, Player player) { + Inventory _inventory = player.getInventory(); + ItemStack pickaxe = null; + Damageable pickaxe_damage = null; + for (Integer index : pickaxes.keySet()) { + ItemStack p = _inventory.getItem(index); + if (p == null) { + LoggerX.debug(index + " 获取到的下界合金镐为空!"); + continue; + } + ItemMeta pickaxe_meta = p.getItemMeta(); + if (pickaxe_meta == null) { + LoggerX.debug(index + " 获取到的下界合金镐元数据为空!"); + continue; + } + if (!(pickaxe_meta instanceof Damageable)) { + LoggerX.debug(index + " 无法转换为Damageable!"); + continue; + } + // 如果耐久小于10,提示玩家 + pickaxe_damage = (Damageable) pickaxe_meta; + if (pickaxe_damage.getDamage() >= 2031 - 10) { + LoggerX.debug(index + " 下界合金镐耐久太低!"); + continue; + } + pickaxe = p; + break; + } + return pickaxe; + } + + public static ItemStack useNetherPickaxe(ItemStack pickaxe) { + int durability = pickaxe.getEnchantmentLevel(Enchantment.DURABILITY); + double random = Math.random(); + if (random < 1.0 / (durability + 1)) { + // 扣除耐久 + Damageable pickaxe_damage = (Damageable) pickaxe.getItemMeta(); + pickaxe_damage.setDamage(pickaxe_damage.getDamage() + 1); + pickaxe.setItemMeta(pickaxe_damage); + } + return pickaxe; + } } diff --git a/src/main/java/site/deercloud/liteworldedit/Jobs/Place.java b/src/main/java/site/deercloud/liteworldedit/Jobs/Place.java index 807e67b..3b4485f 100644 --- a/src/main/java/site/deercloud/liteworldedit/Jobs/Place.java +++ b/src/main/java/site/deercloud/liteworldedit/Jobs/Place.java @@ -18,16 +18,16 @@ public class Place extends Job { public Place(Location location, Player player, Material material) { - _world = player.getWorld(); - _location = location; - _creator = player; - _time = System.currentTimeMillis(); + super(player.getWorld(), location, player); _block = material; - _inventory = player.getInventory(); } @Override public JobErrCode Do() { + Player _creator = this.get_creator(); + Location _location = this.get_location(); + World _world = this.get_world(); + Inventory _inventory = this.get_inventory(); // 超出距离 if (!in_range(_creator, _location)) { LoggerX.debug("超出距离!"); @@ -80,6 +80,7 @@ public class Place extends Job { } private boolean moveBlockFromShulkerBoxToInv() { + Inventory _inventory = this.get_inventory(); HashMap shulkerBoxes = _inventory.all(Material.SHULKER_BOX); for (Integer index : shulkerBoxes.keySet()) { LoggerX.debug("找到潜影盒:" + index); @@ -102,8 +103,12 @@ public class Place extends Job { if (item_idx == -1) { continue; } + ItemStack i = boxInv.getItem(item_idx); + if (i == null) { + continue; + } // 把物品放到玩家物品栏 - _inventory.addItem(boxInv.getItem(item_idx)); + _inventory.addItem(i); // 把潜影盒中的物品移除 shulkerBox.getInventory().setItem(item_idx, null); // 更新潜影盒 diff --git a/src/main/java/site/deercloud/liteworldedit/Jobs/Remove.java b/src/main/java/site/deercloud/liteworldedit/Jobs/Remove.java index ce4eff2..ac29ea5 100644 --- a/src/main/java/site/deercloud/liteworldedit/Jobs/Remove.java +++ b/src/main/java/site/deercloud/liteworldedit/Jobs/Remove.java @@ -3,12 +3,14 @@ package site.deercloud.liteworldedit.Jobs; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.Inventory; import org.bukkit.entity.Player; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import site.deercloud.liteworldedit.LiteWorldEdit; import site.deercloud.liteworldedit.LoggerX; @@ -18,15 +20,14 @@ import java.util.HashMap; public class Remove extends Job { public Remove(Location location, Player player) { - _world = player.getWorld(); - _location = location; - _creator = player; - _time = System.currentTimeMillis(); - _inventory = player.getInventory(); + super(player.getWorld(), location, player); } @Override public JobErrCode Do() { + Player _creator = this.get_creator(); + Location _location = this.get_location(); + World _world = this.get_world(); // 超出距离 if (!in_range(_creator, _location)) { LoggerX.debug("超出距离!"); @@ -39,36 +40,11 @@ public class Remove extends Job { return JobErrCode.NO_BREAKABLE; } // 获取玩家背包中的下届合金镐 - HashMap pickaxes = _inventory.all(Material.NETHERITE_PICKAXE); + HashMap pickaxes = getNetherPickaxes(_creator); if (pickaxes.size() == 0) { return JobErrCode.NO_PICKAXE; } - ItemStack pickaxe = null; - Damageable pickaxe_damage = null; - for (Integer index : pickaxes.keySet()) { - ItemStack p = _inventory.getItem(index); - if (p == null) { - LoggerX.debug(index + " 获取到的下界合金镐为空!"); - continue; - } - ItemMeta pickaxe_meta = p.getItemMeta(); - if (pickaxe_meta == null) { - LoggerX.debug(index + " 获取到的下界合金镐元数据为空!"); - continue; - } - if (!(pickaxe_meta instanceof Damageable)) { - LoggerX.debug(index + " 无法转换为Damageable!"); - continue; - } - // 如果耐久小于10,提示玩家 - pickaxe_damage = (Damageable) pickaxe_meta; - if (pickaxe_damage.getDamage() >= 2031 - 10) { - LoggerX.debug(index + " 下界合金镐耐久太低!"); - continue; - } - pickaxe = p; - break; - } + ItemStack pickaxe = getUsableNetherPickaxe(pickaxes, _creator); // 没有合适的镐 if (pickaxe == null) { return JobErrCode.NOT_ENOUGH_DURATION; @@ -80,14 +56,8 @@ public class Remove extends Job { if (LiteWorldEdit.instance.getConfigMgr().isDropItems()) { raw_block.getWorld().dropItemNaturally(raw_block.getLocation(), new ItemStack(raw_block.getType())); } - // 获取耐久附魔 - int durability = pickaxe.getEnchantmentLevel(Enchantment.DURABILITY); - double random = Math.random(); - if (random < 1.0 / (durability + 1)) { - // 扣除耐久 - pickaxe_damage.setDamage(pickaxe_damage.getDamage() + 1); - pickaxe.setItemMeta((ItemMeta) pickaxe_damage); - } + // 损坏镐 + useNetherPickaxe(pickaxe); return JobErrCode.OK; } else { return JobErrCode.NO_PERMISSION;