提取公共方法,优化部分结构

This commit is contained in:
zhangyuheng 2024-02-28 16:07:16 +08:00
parent 9f86548dd4
commit 4780ede21f
4 changed files with 103 additions and 54 deletions

View File

@ -6,7 +6,7 @@
<groupId>site.deercloud</groupId>
<artifactId>LiteWorldEdit</artifactId>
<version>2.3.5.1</version>
<version>2.3.5.2</version>
<packaging>jar</packaging>
<name>LiteWorldEdit</name>

View File

@ -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<Integer, ?> getNetherPickaxes(Player player) {
Inventory _inventory = player.getInventory();
return _inventory.all(Material.NETHERITE_PICKAXE);
}
public static ItemStack getUsableNetherPickaxe(HashMap<Integer, ?> 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;
}
}

View File

@ -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<Integer, ?> 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);
// 更新潜影盒

View File

@ -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<Integer, ?> pickaxes = _inventory.all(Material.NETHERITE_PICKAXE);
HashMap<Integer, ?> 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;