mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-06 15:52:03 +00:00
@@ -9,12 +9,10 @@ import net.momirealms.craftengine.core.item.Item;
|
|||||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||||
import net.momirealms.craftengine.core.world.World;
|
|
||||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||||
import org.bukkit.entity.FallingBlock;
|
import org.bukkit.entity.FallingBlock;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.entity.EntityRemoveEvent;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -22,47 +20,32 @@ import java.util.Optional;
|
|||||||
public final class FallingBlockRemoveListener implements Listener {
|
public final class FallingBlockRemoveListener implements Listener {
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onFallingBlockBreak(EntityRemoveEvent event) {
|
public void onFallingBlockBreak(org.bukkit.event.entity.EntityRemoveEvent event) {
|
||||||
if (!(event.getEntity() instanceof FallingBlock fallingBlock)) return;
|
if (event.getCause() == org.bukkit.event.entity.EntityRemoveEvent.Cause.DROP && event.getEntity() instanceof FallingBlock fallingBlock) {
|
||||||
try {
|
try {
|
||||||
if (event.getCause() == EntityRemoveEvent.Cause.DROP) {
|
|
||||||
Object fallingBlockEntity = CraftBukkitReflections.field$CraftEntity$entity.get(fallingBlock);
|
Object fallingBlockEntity = CraftBukkitReflections.field$CraftEntity$entity.get(fallingBlock);
|
||||||
Object blockState = CoreReflections.field$FallingBlockEntity$blockState.get(fallingBlockEntity);
|
|
||||||
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
|
|
||||||
if (optionalCustomState.isEmpty()) return;
|
|
||||||
ImmutableBlockState customState = optionalCustomState.get();
|
|
||||||
World world = new BukkitWorld(fallingBlock.getWorld());
|
|
||||||
WorldPosition position = new WorldPosition(world, CoreReflections.field$Entity$xo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$yo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$zo.getDouble(fallingBlockEntity));
|
|
||||||
boolean cancelDrop = (boolean) CoreReflections.field$FallingBlockEntity$cancelDrop.get(fallingBlockEntity);
|
boolean cancelDrop = (boolean) CoreReflections.field$FallingBlockEntity$cancelDrop.get(fallingBlockEntity);
|
||||||
if (!cancelDrop) {
|
if (cancelDrop) return;
|
||||||
ContextHolder.Builder builder = ContextHolder.builder()
|
|
||||||
.withParameter(DirectContextParameters.FALLING_BLOCK, true)
|
|
||||||
.withParameter(DirectContextParameters.POSITION, position);
|
|
||||||
for (Item<Object> item : customState.getDrops(builder, world, null)) {
|
|
||||||
world.dropItemNaturally(position, item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Object entityData = CoreReflections.field$Entity$entityData.get(fallingBlockEntity);
|
|
||||||
boolean isSilent = (boolean) CoreReflections.method$SynchedEntityData$get.invoke(entityData, CoreReflections.instance$Entity$DATA_SILENT);
|
|
||||||
if (!isSilent) {
|
|
||||||
world.playBlockSound(position, customState.settings().sounds().destroySound());
|
|
||||||
}
|
|
||||||
} else if (event.getCause() == EntityRemoveEvent.Cause.DESPAWN) {
|
|
||||||
Object fallingBlockEntity = CraftBukkitReflections.field$CraftEntity$entity.get(fallingBlock);
|
|
||||||
Object blockState = CoreReflections.field$FallingBlockEntity$blockState.get(fallingBlockEntity);
|
Object blockState = CoreReflections.field$FallingBlockEntity$blockState.get(fallingBlockEntity);
|
||||||
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
|
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
|
||||||
if (optionalCustomState.isEmpty()) return;
|
if (optionalCustomState.isEmpty()) return;
|
||||||
ImmutableBlockState customState = optionalCustomState.get();
|
ImmutableBlockState customState = optionalCustomState.get();
|
||||||
World world = new BukkitWorld(fallingBlock.getWorld());
|
net.momirealms.craftengine.core.world.World world = new BukkitWorld(fallingBlock.getWorld());
|
||||||
WorldPosition position = new WorldPosition(world, CoreReflections.field$Entity$xo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$yo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$zo.getDouble(fallingBlockEntity));
|
WorldPosition position = new WorldPosition(world, CoreReflections.field$Entity$xo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$yo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$zo.getDouble(fallingBlockEntity));
|
||||||
|
ContextHolder.Builder builder = ContextHolder.builder()
|
||||||
|
.withParameter(DirectContextParameters.FALLING_BLOCK, true)
|
||||||
|
.withParameter(DirectContextParameters.POSITION, position);
|
||||||
|
for (Item<Object> item : customState.getDrops(builder, world, null)) {
|
||||||
|
world.dropItemNaturally(position, item);
|
||||||
|
}
|
||||||
Object entityData = CoreReflections.field$Entity$entityData.get(fallingBlockEntity);
|
Object entityData = CoreReflections.field$Entity$entityData.get(fallingBlockEntity);
|
||||||
boolean isSilent = (boolean) CoreReflections.method$SynchedEntityData$get.invoke(entityData, CoreReflections.instance$Entity$DATA_SILENT);
|
boolean isSilent = (boolean) CoreReflections.method$SynchedEntityData$get.invoke(entityData, CoreReflections.instance$Entity$DATA_SILENT);
|
||||||
if (!isSilent) {
|
if (!isSilent) {
|
||||||
world.playBlockSound(position, customState.settings().sounds().destroySound());
|
world.playBlockSound(position, customState.settings().sounds().destroySound());
|
||||||
}
|
}
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
CraftEngine.instance().logger().warn("Failed to handle EntityRemoveEvent", e);
|
||||||
}
|
}
|
||||||
} catch (ReflectiveOperationException e) {
|
|
||||||
CraftEngine.instance().logger().warn("Failed to handle EntityRemoveEvent", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextPar
|
|||||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||||
import net.momirealms.craftengine.core.world.Vec3d;
|
import net.momirealms.craftengine.core.world.Vec3d;
|
||||||
import net.momirealms.craftengine.core.world.World;
|
|
||||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -81,20 +80,19 @@ public class FallingBlockBehavior extends BukkitBlockBehavior {
|
|||||||
if (VersionHelper.isOrAbove1_20_3()) return;
|
if (VersionHelper.isOrAbove1_20_3()) return;
|
||||||
Object level = args[0];
|
Object level = args[0];
|
||||||
Object fallingBlockEntity = args[2];
|
Object fallingBlockEntity = args[2];
|
||||||
|
boolean cancelDrop = (boolean) CoreReflections.field$FallingBlockEntity$cancelDrop.get(fallingBlockEntity);
|
||||||
|
if (cancelDrop) return;
|
||||||
Object blockState = CoreReflections.field$FallingBlockEntity$blockState.get(fallingBlockEntity);
|
Object blockState = CoreReflections.field$FallingBlockEntity$blockState.get(fallingBlockEntity);
|
||||||
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
|
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
|
||||||
if (optionalCustomState.isEmpty()) return;
|
if (optionalCustomState.isEmpty()) return;
|
||||||
ImmutableBlockState customState = optionalCustomState.get();
|
ImmutableBlockState customState = optionalCustomState.get();
|
||||||
World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
|
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
|
||||||
WorldPosition position = new WorldPosition(world, CoreReflections.field$Entity$xo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$yo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$zo.getDouble(fallingBlockEntity));
|
WorldPosition position = new WorldPosition(world, CoreReflections.field$Entity$xo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$yo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$zo.getDouble(fallingBlockEntity));
|
||||||
boolean cancelDrop = (boolean) CoreReflections.field$FallingBlockEntity$cancelDrop.get(fallingBlockEntity);
|
ContextHolder.Builder builder = ContextHolder.builder()
|
||||||
if (!cancelDrop) {
|
.withParameter(DirectContextParameters.FALLING_BLOCK, true)
|
||||||
ContextHolder.Builder builder = ContextHolder.builder()
|
.withParameter(DirectContextParameters.POSITION, position);
|
||||||
.withParameter(DirectContextParameters.FALLING_BLOCK, true)
|
for (Item<Object> item : customState.getDrops(builder, world, null)) {
|
||||||
.withParameter(DirectContextParameters.POSITION, position);
|
world.dropItemNaturally(position, item);
|
||||||
for (Item<Object> item : customState.getDrops(builder, world, null)) {
|
|
||||||
world.dropItemNaturally(position, item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Object entityData = CoreReflections.field$Entity$entityData.get(fallingBlockEntity);
|
Object entityData = CoreReflections.field$Entity$entityData.get(fallingBlockEntity);
|
||||||
boolean isSilent = (boolean) CoreReflections.method$SynchedEntityData$get.invoke(entityData, CoreReflections.instance$Entity$DATA_SILENT);
|
boolean isSilent = (boolean) CoreReflections.method$SynchedEntityData$get.invoke(entityData, CoreReflections.instance$Entity$DATA_SILENT);
|
||||||
|
|||||||
Reference in New Issue
Block a user