Skip to content

Commit

Permalink
Add particle effect when breaking block in shiftdown mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Jul 22, 2023
1 parent a363bca commit 9ab9fda
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 9ab9fda

Please sign in to comment.