Lumberjack and BlastMining performance workaround

This commit is contained in:
Auxilor 2021-08-12 23:48:23 +01:00
parent 134f737a0b
commit 2c4ee1c022
2 changed files with 47 additions and 0 deletions

View File

@ -7,10 +7,15 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import org.bukkit.Bukkit;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
@ -78,6 +83,12 @@ public class BlastMining extends EcoEnchant {
}
}
}
ItemStack itemStack = player.getInventory().getItemInMainHand();
ItemMeta beforeMeta = itemStack.getItemMeta();
assert beforeMeta != null;
beforeMeta.setUnbreakable(true);
itemStack.setItemMeta(beforeMeta);
int blocks = toBreak.size();
toBreak.forEach((block1 -> {
block1.setMetadata("block-ignore", this.getPlugin().getMetadataValueFactory().create(true));
@ -85,6 +96,18 @@ public class BlastMining extends EcoEnchant {
block1.removeMetadata("block-ignore", this.getPlugin());
}));
ItemMeta afterMeta = itemStack.getItemMeta();
assert afterMeta != null;
afterMeta.setUnbreakable(false);
itemStack.setItemMeta(afterMeta);
PlayerItemDamageEvent mockEvent = new PlayerItemDamageEvent(player, itemStack, blocks);
Bukkit.getPluginManager().callEvent(mockEvent);
ItemMeta wayAfterMeta = itemStack.getItemMeta();
assert wayAfterMeta != null;
((Damageable) wayAfterMeta).setDamage(((Damageable) wayAfterMeta).getDamage() + mockEvent.getDamage());
itemStack.setItemMeta(wayAfterMeta);
AnticheatManager.unexemptPlayer(player);
}
}

View File

@ -6,10 +6,15 @@ import com.willfp.eco.util.BlockUtils;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@ -50,6 +55,13 @@ public class Lumberjack extends EcoEnchant {
AnticheatManager.exemptPlayer(player);
ItemStack itemStack = player.getInventory().getItemInMainHand();
ItemMeta beforeMeta = itemStack.getItemMeta();
assert beforeMeta != null;
beforeMeta.setUnbreakable(true);
itemStack.setItemMeta(beforeMeta);
int blocks = treeBlocks.size();
for (Block treeBlock : treeBlocks) {
treeBlock.setMetadata("block-ignore", this.getPlugin().getMetadataValueFactory().create(true));
if (!AntigriefManager.canBreakBlock(player, treeBlock)) {
@ -61,6 +73,18 @@ public class Lumberjack extends EcoEnchant {
this.getPlugin().getScheduler().runLater(() -> treeBlock.removeMetadata("block-ignore", this.getPlugin()), 1);
}
ItemMeta afterMeta = itemStack.getItemMeta();
assert afterMeta != null;
afterMeta.setUnbreakable(false);
itemStack.setItemMeta(afterMeta);
PlayerItemDamageEvent mockEvent = new PlayerItemDamageEvent(player, itemStack, blocks);
Bukkit.getPluginManager().callEvent(mockEvent);
ItemMeta wayAfterMeta = itemStack.getItemMeta();
assert wayAfterMeta != null;
((Damageable) wayAfterMeta).setDamage(((Damageable) wayAfterMeta).getDamage() + mockEvent.getDamage());
itemStack.setItemMeta(wayAfterMeta);
AnticheatManager.unexemptPlayer(player);
}
}