From 9ab9fdaf3b52780d6cc300401cc2c547fb03d566 Mon Sep 17 00:00:00 2001 From: Thomas Couchoud Date: Sat, 22 Jul 2023 15:41:04 +0200 Subject: [PATCH] Add particle effect when breaking block in shiftdown mode --- .../ShiftDownTreeBreakingHandler.java | 8 ++++++ .../common/wrapper/IServerLevel.java | 3 ++ .../fabric/common/wrapper/PlayerWrapper.java | 3 +- .../common/wrapper/ServerLevelWrapper.java | 28 ++++++++++++++++--- .../fabric/event/BlockBreakListener.java | 4 ++- .../forge/common/wrapper/PlayerWrapper.java | 3 +- .../common/wrapper/ServerLevelWrapper.java | 20 +++++++++++++ .../forge/event/BlockBreakListener.java | 12 ++++---- 8 files changed, 67 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java index f119b5fd..975d51a8 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java @@ -6,11 +6,14 @@ import fr.rakambda.fallingtree.common.wrapper.IItemStack; import fr.rakambda.fallingtree.common.wrapper.ILevel; import fr.rakambda.fallingtree.common.wrapper.IPlayer; +import fr.rakambda.fallingtree.common.wrapper.IServerLevel; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.jetbrains.annotations.NotNull; + import java.util.Collection; import java.util.List; + import static fr.rakambda.fallingtree.common.tree.TreePartType.NETHER_WART; import static java.util.Objects.isNull; @@ -65,6 +68,11 @@ private boolean breakElements(@NotNull Tree tree, @NotNull ILevel level, @NotNul if(damage > 0){ tool.damage(damage, player); } + + if (level instanceof IServerLevel serverLevel) { + serverLevel.spawnParticle(tree.getHitPos(), level.getBlockState(tree.getHitPos()), 10, 1, 1, 1, 5); + } + return true; } diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IServerLevel.java b/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IServerLevel.java index 9b1320f7..373265ed 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IServerLevel.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IServerLevel.java @@ -1,4 +1,7 @@ package fr.rakambda.fallingtree.common.wrapper; +import org.jetbrains.annotations.NotNull; + public interface IServerLevel extends ILevel{ + void spawnParticle(@NotNull IBlockPos blockPos, @NotNull IBlockState blockState, int count, float xDist, float yDist, float zDist, float maxSpeed); } diff --git a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/PlayerWrapper.java b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/PlayerWrapper.java index f3119c7a..37fb2566 100644 --- a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/PlayerWrapper.java +++ b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/PlayerWrapper.java @@ -10,6 +10,7 @@ import lombok.RequiredArgsConstructor; import lombok.ToString; import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.stats.Stats; import net.minecraft.world.entity.player.Player; @@ -69,7 +70,7 @@ public UUID getUUID(){ @Override @NotNull public ILevel getLevel(){ - return new LevelWrapper(raw.getCommandSenderWorld()); + return raw.getCommandSenderWorld() instanceof ServerLevel serverLevel ? new ServerLevelWrapper(serverLevel) : new LevelWrapper(raw.getCommandSenderWorld()); } @Override diff --git a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/ServerLevelWrapper.java b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/ServerLevelWrapper.java index 8e07ca53..d60b0558 100644 --- a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/ServerLevelWrapper.java +++ b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/ServerLevelWrapper.java @@ -1,11 +1,31 @@ package fr.rakambda.fallingtree.fabric.common.wrapper; +import fr.rakambda.fallingtree.common.wrapper.IBlockPos; +import fr.rakambda.fallingtree.common.wrapper.IBlockState; import fr.rakambda.fallingtree.common.wrapper.IServerLevel; +import net.minecraft.core.particles.BlockParticleOption; +import net.minecraft.core.particles.ParticleTypes; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; -public class ServerLevelWrapper extends LevelWrapper implements IServerLevel{ - public ServerLevelWrapper(@NotNull ServerLevel serverLevel){ - super(serverLevel); - } +public class ServerLevelWrapper extends LevelWrapper implements IServerLevel { + public ServerLevelWrapper(@NotNull ServerLevel serverLevel) { + super(serverLevel); + } + + @Override + @NotNull + public ServerLevel getRaw() { + return (ServerLevel) super.getRaw(); + } + + @Override + public void spawnParticle(@NotNull IBlockPos blockPos, @NotNull IBlockState blockState, int count, float xDist, float yDist, float zDist, float maxSpeed) { + getRaw().sendParticles(new BlockParticleOption(ParticleTypes.BLOCK, (BlockState) blockState.getRaw()), + blockPos.getX() + 0.5f, blockPos.getY() + 0.5f, blockPos.getZ() + 0.5f, + count, + xDist, yDist, zDist, + maxSpeed); + } } diff --git a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/event/BlockBreakListener.java b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/event/BlockBreakListener.java index 8035a402..3a8c6a86 100644 --- a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/event/BlockBreakListener.java +++ b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/event/BlockBreakListener.java @@ -10,9 +10,11 @@ import fr.rakambda.fallingtree.fabric.common.wrapper.BlockPosWrapper; import fr.rakambda.fallingtree.fabric.common.wrapper.LevelWrapper; import fr.rakambda.fallingtree.fabric.common.wrapper.PlayerWrapper; +import fr.rakambda.fallingtree.fabric.common.wrapper.ServerLevelWrapper; import lombok.RequiredArgsConstructor; import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents; import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; @@ -27,7 +29,7 @@ public class BlockBreakListener implements PlayerBlockBreakEvents.Before{ @Override public boolean beforeBlockBreak(Level level, Player player, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity){ var wrappedPlayer = new PlayerWrapper(player); - var wrappedLevel = new LevelWrapper(level); + var wrappedLevel = level instanceof ServerLevel serverLevel ? new ServerLevelWrapper(serverLevel) : new LevelWrapper(level); var wrappedPos = new BlockPosWrapper(blockPos); try{ diff --git a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/PlayerWrapper.java b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/PlayerWrapper.java index fbe785d2..e4ba1465 100644 --- a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/PlayerWrapper.java +++ b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/PlayerWrapper.java @@ -10,6 +10,7 @@ import lombok.RequiredArgsConstructor; import lombok.ToString; import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.stats.Stats; import net.minecraft.world.entity.player.Player; @@ -69,7 +70,7 @@ public UUID getUUID(){ @Override @NotNull public ILevel getLevel(){ - return new LevelWrapper(raw.getCommandSenderWorld()); + return raw.getCommandSenderWorld() instanceof ServerLevel serverLevel ? new ServerLevelWrapper(serverLevel) : new LevelWrapper(raw.getCommandSenderWorld()); } @Override diff --git a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/ServerLevelWrapper.java b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/ServerLevelWrapper.java index f0c35bee..ecd46c5e 100644 --- a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/ServerLevelWrapper.java +++ b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/ServerLevelWrapper.java @@ -1,11 +1,31 @@ package fr.rakambda.fallingtree.forge.common.wrapper; +import fr.rakambda.fallingtree.common.wrapper.IBlockPos; +import fr.rakambda.fallingtree.common.wrapper.IBlockState; import fr.rakambda.fallingtree.common.wrapper.IServerLevel; +import net.minecraft.core.particles.BlockParticleOption; +import net.minecraft.core.particles.ParticleTypes; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; public class ServerLevelWrapper extends LevelWrapper implements IServerLevel{ public ServerLevelWrapper(@NotNull ServerLevel serverLevel){ super(serverLevel); } + + @Override + @NotNull + public ServerLevel getRaw() { + return (ServerLevel) super.getRaw(); + } + + @Override + public void spawnParticle(@NotNull IBlockPos blockPos, @NotNull IBlockState blockState, int count, float xDist, float yDist, float zDist, float maxSpeed) { + getRaw().sendParticles(new BlockParticleOption(ParticleTypes.BLOCK, (BlockState) blockState.getRaw()), + blockPos.getX() + 0.5f, blockPos.getY() + 0.5f, blockPos.getZ() + 0.5f, + count, + xDist, yDist, zDist, + maxSpeed); + } } diff --git a/forge/src/main/java/fr/rakambda/fallingtree/forge/event/BlockBreakListener.java b/forge/src/main/java/fr/rakambda/fallingtree/forge/event/BlockBreakListener.java index 6cb51406..c405eb34 100644 --- a/forge/src/main/java/fr/rakambda/fallingtree/forge/event/BlockBreakListener.java +++ b/forge/src/main/java/fr/rakambda/fallingtree/forge/event/BlockBreakListener.java @@ -1,20 +1,18 @@ package fr.rakambda.fallingtree.forge.event; import fr.rakambda.fallingtree.common.FallingTreeCommon; -import fr.rakambda.fallingtree.common.tree.exception.NoTreeFoundException; -import fr.rakambda.fallingtree.common.tree.exception.NotServerException; -import fr.rakambda.fallingtree.common.tree.exception.PlayerNotInRightState; -import fr.rakambda.fallingtree.common.tree.exception.ToolUseForcedException; -import fr.rakambda.fallingtree.common.tree.exception.TreeBreakingException; -import fr.rakambda.fallingtree.common.tree.exception.TreeBreakingNotEnabledException; +import fr.rakambda.fallingtree.common.tree.exception.*; import fr.rakambda.fallingtree.forge.common.wrapper.BlockPosWrapper; import fr.rakambda.fallingtree.forge.common.wrapper.LevelWrapper; import fr.rakambda.fallingtree.forge.common.wrapper.PlayerWrapper; +import fr.rakambda.fallingtree.forge.common.wrapper.ServerLevelWrapper; import lombok.RequiredArgsConstructor; +import net.minecraft.server.level.ServerLevel; import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.level.BlockEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import org.jetbrains.annotations.NotNull; + import javax.annotation.Nonnull; @RequiredArgsConstructor @@ -54,7 +52,7 @@ public void onBlockBreakEvent(@Nonnull BlockEvent.BreakEvent event){ } var wrappedPlayer = new PlayerWrapper(event.getPlayer()); - var wrappedLevel = new LevelWrapper(event.getLevel()); + var wrappedLevel = event.getLevel() instanceof ServerLevel serverLevel ? new ServerLevelWrapper(serverLevel) : new LevelWrapper(event.getLevel()); var wrappedPos = new BlockPosWrapper(event.getPos()); try{