feat(hamster-chain-break): 添加Energy支持
This commit is contained in:
@@ -1,2 +1,6 @@
|
||||
version = '1.0.0'
|
||||
setArchivesBaseName("HamsterChainBreak")
|
||||
|
||||
dependencies {
|
||||
compileOnly "de.tr7zw:item-nbt-api-plugin:2.10.0"
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package cn.hamster3.mc.plugin.chain.breaks.listener;
|
||||
import cn.hamster3.mc.plugin.chain.breaks.config.ToolGroup;
|
||||
import cn.hamster3.mc.plugin.chain.breaks.core.ConfigManager;
|
||||
import cn.hamster3.mc.plugin.chain.breaks.util.ChainBreakUtils;
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
@@ -31,7 +32,6 @@ public final class MainListener implements Listener {
|
||||
private MainListener() {
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
@@ -53,43 +53,15 @@ public final class MainListener implements Listener {
|
||||
if (searchResultBlocks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<ItemStack> dropItemList = new ArrayList<>();
|
||||
World blockWorld = eventBlock.getWorld();
|
||||
Location blockLocation = eventBlock.getLocation();
|
||||
if (player.getGameMode() == GameMode.CREATIVE) {
|
||||
for (Block block : searchResultBlocks) {
|
||||
dropItemList.addAll(block.getDrops(stack, player));
|
||||
block.setType(Material.AIR, true);
|
||||
}
|
||||
for (ItemStack dropItem : dropItemList) {
|
||||
blockWorld.dropItem(blockLocation, dropItem);
|
||||
}
|
||||
breakBlockWithCreative(player, stack, eventBlock, searchResultBlocks);
|
||||
return;
|
||||
}
|
||||
short maxDurability = stack.getType().getMaxDurability();
|
||||
short durability = stack.getDurability();
|
||||
if (durability >= maxDurability) {
|
||||
return;
|
||||
}
|
||||
for (Block block : searchResultBlocks) {
|
||||
boolean leaves = block.getType().name().endsWith("_LEAVES");
|
||||
dropItemList.addAll(block.getDrops(stack, player));
|
||||
block.setType(Material.AIR, true);
|
||||
if (!leaves) {
|
||||
durability++;
|
||||
if (durability >= maxDurability) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (ItemStack dropItem : dropItemList) {
|
||||
blockWorld.dropItem(blockLocation, dropItem);
|
||||
}
|
||||
if (durability >= maxDurability) {
|
||||
player.getInventory().setItemInMainHand(null);
|
||||
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
|
||||
NBTItem nbtItem = new NBTItem(stack);
|
||||
if (nbtItem.hasKey("Energy")) {
|
||||
breakBlockWithEnergy(player, stack, nbtItem, eventBlock, searchResultBlocks);
|
||||
} else {
|
||||
stack.setDurability(durability);
|
||||
breakBlockWithDurability(player, stack, eventBlock, searchResultBlocks);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,4 +92,75 @@ public final class MainListener implements Listener {
|
||||
bfsSearchBlock(group, result, nextSearchBlocks);
|
||||
}
|
||||
|
||||
private void breakBlockWithCreative(Player player, ItemStack stack, Block eventBlock, List<Block> searchResultBlocks) {
|
||||
List<ItemStack> dropItemList = new ArrayList<>();
|
||||
World blockWorld = eventBlock.getWorld();
|
||||
Location blockLocation = eventBlock.getLocation();
|
||||
for (Block block : searchResultBlocks) {
|
||||
dropItemList.addAll(block.getDrops(stack, player));
|
||||
block.setType(Material.AIR, true);
|
||||
}
|
||||
for (ItemStack dropItem : dropItemList) {
|
||||
blockWorld.dropItem(blockLocation, dropItem);
|
||||
}
|
||||
}
|
||||
|
||||
private void breakBlockWithEnergy(Player player, ItemStack stack, NBTItem nbtItem, Block eventBlock, List<Block> searchResultBlocks) {
|
||||
List<ItemStack> dropItemList = new ArrayList<>();
|
||||
World blockWorld = eventBlock.getWorld();
|
||||
Location blockLocation = eventBlock.getLocation();
|
||||
int energy = nbtItem.getInteger("Energy");
|
||||
if (energy <= 0) {
|
||||
return;
|
||||
}
|
||||
for (Block block : searchResultBlocks) {
|
||||
boolean leaves = block.getType().name().endsWith("_LEAVES");
|
||||
dropItemList.addAll(block.getDrops(stack, player));
|
||||
block.setType(Material.AIR, true);
|
||||
if (!leaves) {
|
||||
energy = energy - 200;
|
||||
if (energy <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (ItemStack dropItem : dropItemList) {
|
||||
blockWorld.dropItem(blockLocation, dropItem);
|
||||
}
|
||||
nbtItem.setInteger("Energy", energy);
|
||||
player.getInventory().setItemInMainHand(nbtItem.getItem());
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void breakBlockWithDurability(Player player, ItemStack stack, Block eventBlock, List<Block> searchResultBlocks) {
|
||||
List<ItemStack> dropItemList = new ArrayList<>();
|
||||
World blockWorld = eventBlock.getWorld();
|
||||
Location blockLocation = eventBlock.getLocation();
|
||||
short maxDurability = stack.getType().getMaxDurability();
|
||||
short durability = stack.getDurability();
|
||||
if (durability >= maxDurability) {
|
||||
return;
|
||||
}
|
||||
for (Block block : searchResultBlocks) {
|
||||
boolean leaves = block.getType().name().endsWith("_LEAVES");
|
||||
dropItemList.addAll(block.getDrops(stack, player));
|
||||
block.setType(Material.AIR, true);
|
||||
if (!leaves) {
|
||||
durability++;
|
||||
if (durability >= maxDurability) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (ItemStack dropItem : dropItemList) {
|
||||
blockWorld.dropItem(blockLocation, dropItem);
|
||||
}
|
||||
if (durability >= maxDurability) {
|
||||
player.getInventory().setItemInMainHand(null);
|
||||
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
|
||||
} else {
|
||||
stack.setDurability(durability);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user