made block breaking use fakeBlockBreak()

also made blocks broken with enchantments drop exp
and made collectable collect that exp
This commit is contained in:
XTerPL 2025-01-21 02:14:26 +01:00
parent b5f0832fae
commit 9d914d026c
3 changed files with 102 additions and 15 deletions

View file

@ -23,16 +23,26 @@ import de.blazemcworld.blazinggames.enchantments.sys.CustomEnchantments;
import de.blazemcworld.blazinggames.enchantments.sys.EnchantmentHelper;
import de.blazemcworld.blazinggames.items.recipes.RecipeHelper;
import de.blazemcworld.blazinggames.teleportanchor.LodestoneStorage;
import de.blazemcworld.blazinggames.utils.Drops;
import de.blazemcworld.blazinggames.utils.InventoryUtils;
import de.blazemcworld.blazinggames.utils.ItemUtils;
import de.blazemcworld.blazinggames.utils.Pair;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.Vec3;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.*;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.block.data.type.Leaves;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -97,11 +107,7 @@ public class BreakBlockEventListener implements Listener {
}
}
Collection<ItemStack> drops = getBlockDrops(player, event.getBlock());
onAnyBlockBreak(event.getBlock());
InventoryUtils.collectableDrop(player, event.getBlock().getLocation(), drops);
fakeBreakBlock(player, event.getBlock(), false);
if (EnchantmentHelper.hasActiveCustomEnchantment(mainHand, CustomEnchantments.TREE_FELLER)) {
if (logs.contains(event.getBlock().getType())) {
@ -221,15 +227,17 @@ public class BreakBlockEventListener implements Listener {
Bukkit.getScheduler().runTaskLater(BlazingGames.get(), () -> treeFeller(player, blocksToBreak), 1);
}
public static Collection<ItemStack> getBlockDrops(Player player, Block block) {
public static Drops getBlockDrops(Player player, Block block) {
ItemStack mainHand = player.getInventory().getItemInMainHand();
return getBlockDrops(mainHand, block);
}
public static Collection<ItemStack> getBlockDrops(ItemStack mainHand, Block block) {
public static Drops getBlockDrops(ItemStack mainHand, Block block) {
BlockState state = block.getState();
Collection<ItemStack> drops = block.getDrops(mainHand);
Drops drops = new Drops(block.getDrops(mainHand));
drops.addExperience(getDroppedExp(block, mainHand));
if (block.getType() == Material.CHISELED_BOOKSHELF) {
if (mainHand.getEnchantmentLevel(Enchantment.SILK_TOUCH) <= 0) {
@ -246,6 +254,7 @@ public class BreakBlockEventListener implements Listener {
mainHand.getType() == Material.DIAMOND_PICKAXE ||
mainHand.getType() == Material.NETHERITE_PICKAXE
)) {
drops.setExperience(0);
CreatureSpawner spawner = (CreatureSpawner) block.getState();
ItemStack item = new ItemStack(Material.SPAWNER);
BlockStateMeta meta = (BlockStateMeta) item.getItemMeta();
@ -299,22 +308,26 @@ public class BreakBlockEventListener implements Listener {
if (ComputerRegistry.getComputerByLocationRounded(block.getLocation()) != null) {
BootedComputer computer = ComputerRegistry.getComputerByLocationRounded(block.getLocation());
return List.of(ComputerRegistry.addAttributes(computer.getType().getType().getDisplayItem(computer), computer));
return new Drops(ComputerRegistry.addAttributes(computer.getType().getType().getDisplayItem(computer), computer));
} else {
return drops;
}
}
public static void fakeBreakBlock(Player player, Block block) {
fakeBreakBlock(player, block, true);
}
public static void fakeBreakBlock(Player player, Block block, boolean playEffects) {
if (block.isEmpty() || block.getType().getHardness() < 0 || block.isLiquid()) {
return;
}
Collection<ItemStack> drops = getBlockDrops(player, block);
Drops drops = getBlockDrops(player, block);
onAnyBlockBreak(block);
block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getBlockData());
if(playEffects) block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getBlockData());
boolean waterlogged = false;
@ -338,4 +351,23 @@ public class BreakBlockEventListener implements Listener {
ComputerRegistry.unload(computer.getId());
}
}
private static int getDroppedExp(Block block, ItemStack tool) {
if (!(block instanceof CraftBlock craftBlock)) return 0;
net.minecraft.world.level.block.state.BlockState nmsBlockState = craftBlock.getNMS();
BlockPos pos = ((CraftBlock) block).getPosition();
ServerLevel level = ((CraftBlock) block).getCraftWorld().getHandle();
return nmsBlockState.getBlock().getExpDrop(nmsBlockState, level, pos, CraftItemStack.asNMSCopy(tool), true);
}
public static void awardBlock(Location location, int amount, Player trigger) {
if(!(location.getWorld() instanceof CraftWorld world)) { return; }
if(!(trigger instanceof CraftPlayer player)) { return; }
net.minecraft.world.entity.ExperienceOrb.award(
world.getHandle(), new Vec3(location.x(), location.y(), location.z()), amount,
ExperienceOrb.SpawnReason.BLOCK_BREAK, player.getHandleRaw());
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright 2025 The Blazing Games Maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.blazemcworld.blazinggames.utils;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Drops extends ArrayList<ItemStack> {
private int expToGive = 0;
public Drops() {
super();
}
public Drops(Collection<ItemStack> drops) {
this();
this.addAll(drops);
}
public Drops(ItemStack... drops) {
this(List.of(drops));
}
public int getExperienceDropped() {
return expToGive;
}
public void addExperience(int expAmount) {
expToGive += expAmount;
}
public void setExperience(int expAmount) {
expToGive = expAmount;
}
}

View file

@ -18,6 +18,7 @@ package de.blazemcworld.blazinggames.utils;
import de.blazemcworld.blazinggames.computing.ComputerRegistry;
import de.blazemcworld.blazinggames.enchantments.sys.CustomEnchantments;
import de.blazemcworld.blazinggames.enchantments.sys.EnchantmentHelper;
import de.blazemcworld.blazinggames.events.BreakBlockEventListener;
import de.blazemcworld.blazinggames.items.CustomItem;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -26,8 +27,6 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
public class InventoryUtils {
@ -69,17 +68,21 @@ public class InventoryUtils {
}
public static void collectableDrop(Player player, Location location, ItemStack... drops) {
collectableDrop(player, location, Arrays.asList(drops));
collectableDrop(player, location, new Drops(drops));
}
public static void collectableDrop(Player player, Location location, Collection<ItemStack> drops) {
public static void collectableDrop(Player player, Location location, Drops drops) {
if (EnchantmentHelper.hasActiveCustomEnchantment(player.getInventory().getItemInMainHand(), CustomEnchantments.COLLECTABLE)) {
player.giveExp(drops.getExperienceDropped(), true);
for (ItemStack drop : drops) {
for (Map.Entry<Integer, ItemStack> overflow : player.getInventory().addItem(drop).entrySet()) {
drop(player, location, overflow.getValue());
}
}
} else {
BreakBlockEventListener.awardBlock(location, drops.getExperienceDropped(), player);
for (ItemStack drop : drops) {
drop(player, location, drop);
}