Skip to content

Commit

Permalink
Implement nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolykt committed Oct 28, 2023
1 parent 7188a20 commit b069ff1
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fr.rakambda.fallingtree.common.tree;

public enum BreakAbortionCause implements IBreakAttemptResult {
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum AbortedResult implements IBreakAttemptResult {
NOT_SERVER(false),
NOT_ENABLED(false),
REQUIRED_TOOL_ABSENT(true),
Expand All @@ -11,10 +14,6 @@ public enum BreakAbortionCause implements IBreakAttemptResult {

private final boolean cancel;

BreakAbortionCause(boolean cancel) {
this.cancel = cancel;
}

@Override
public boolean shouldCancel(){
return this.cancel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Record that denotes that an attempt to break a tree with the given mode has
* succeeded. Failures are instead denoted as {@link BreakAbortionCause}.
* succeeded. Failures are instead denoted as {@link AbortedResult}.
*
* @param shouldCancel Whether the event which triggered the query should be cancelled as a result of this result.
* @param breakMode The mode with which the block was broken.
Expand All @@ -14,8 +14,4 @@ public record BreakTreeResult(
boolean shouldCancel,
@NotNull BreakMode breakMode
) implements IBreakAttemptResult{
@Override
public boolean shouldCancel(){
return this.shouldCancel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import fr.rakambda.fallingtree.common.wrapper.IPlayer;

/**
* The result of a {@link TreeHandler#attemptTreeBreaking(ILevel, IPlayer, IBlockPos)}, whether it succeeded or not.
* Failures are generally instances of {@link BreakAbortionCause}, where are succeeded attempts are instances of
* The result of a {@link TreeHandler#breakTree(ILevel, IPlayer, IBlockPos)}, whether it succeeded or not.
* Failures are generally instances of {@link AbortedResult}, where are succeeded attempts are instances of
* {@link BreakTreeResult}.
*/
public interface IBreakAttemptResult{
public sealed interface IBreakAttemptResult permits BreakTreeResult, AbortedResult{
boolean shouldCancel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

import fr.rakambda.fallingtree.common.tree.breaking.*;
import fr.rakambda.fallingtree.common.tree.builder.TreeTooBigException;
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.FallingTreeCommon;
import fr.rakambda.fallingtree.common.config.enums.BreakMode;
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.utils.CacheSpeed;
import fr.rakambda.fallingtree.common.wrapper.IBlockPos;
import fr.rakambda.fallingtree.common.wrapper.IEnchantment;
Expand All @@ -34,27 +28,27 @@ public class TreeHandler{
private final Map<UUID, CacheSpeed> speedCache = new ConcurrentHashMap<>();

@NotNull
public IBreakAttemptResult attemptTreeBreaking(@NotNull ILevel level, @NotNull IPlayer player, @NotNull IBlockPos blockPos) {
public IBreakAttemptResult breakTree(@NotNull ILevel level, @NotNull IPlayer player, @NotNull IBlockPos blockPos) {
if(!level.isServer()){
return BreakAbortionCause.NOT_SERVER;
return AbortedResult.NOT_SERVER;
}
if(!mod.getConfiguration().getTrees().isTreeBreaking()){
return BreakAbortionCause.NOT_ENABLED;
return AbortedResult.NOT_ENABLED;
}

if(!mod.checkForceToolUsage(player, level, blockPos)){
mod.notifyPlayer(player, mod.translate("chat.fallingtree.force_tool_usage", mod.getConfiguration().getTrees().getMaxScanSize()));
return BreakAbortionCause.REQUIRED_TOOL_ABSENT;
return AbortedResult.REQUIRED_TOOL_ABSENT;
}

if(!mod.isPlayerInRightState(player)){
return BreakAbortionCause.INVALID_PLAYER_STATE;
return AbortedResult.INVALID_PLAYER_STATE;
}

try{
var treeOptional = mod.getTreeBuilder().getTree(player, level, blockPos);
if(treeOptional.isEmpty()){
return BreakAbortionCause.NO_SUCH_TREE;
return AbortedResult.NO_SUCH_TREE;
}

var tree = treeOptional.get();
Expand All @@ -64,35 +58,13 @@ public IBreakAttemptResult attemptTreeBreaking(@NotNull ILevel level, @NotNull I
}
catch(TreeTooBigException e){
mod.notifyPlayer(player, mod.translate("chat.fallingtree.tree_too_big", mod.getConfiguration().getTrees().getMaxScanSize()));
return BreakAbortionCause.TREE_TOO_BIG_SCAN;
return AbortedResult.TREE_TOO_BIG_SCAN;
}
catch(BreakTreeTooBigException e){
mod.notifyPlayer(player, mod.translate("chat.fallingtree.break_tree_too_big", mod.getConfiguration().getTrees().getMaxSize()));
return BreakAbortionCause.TREE_TOO_BIG_BREAK;
return AbortedResult.TREE_TOO_BIG_BREAK;
}
}

@Deprecated
@NotNull
public BreakTreeResult breakTree(@NotNull ILevel level, @NotNull IPlayer player, @NotNull IBlockPos blockPos) throws TreeBreakingNotEnabledException, PlayerNotInRightState, ToolUseForcedException, TreeBreakingException, NoTreeFoundException, NotServerException{
IBreakAttemptResult result = this.attemptTreeBreaking(level, player, blockPos);
if (result instanceof BreakTreeResult ret) {
return ret;
}
if (result instanceof BreakAbortionCause cause) {
switch (cause) {
case NOT_SERVER -> throw new NotServerException();
case TREE_TOO_BIG_SCAN -> throw new TreeBreakingException("Tree exceeded scanning limits");
case NO_SUCH_TREE -> throw new NoTreeFoundException();
case INVALID_PLAYER_STATE -> throw new PlayerNotInRightState();
case NOT_ENABLED -> throw new TreeBreakingNotEnabledException();
case TREE_TOO_BIG_BREAK -> throw new TreeBreakingException("Tree exceeded breaking limits");
case REQUIRED_TOOL_ABSENT -> throw new ToolUseForcedException();
}
}

throw new TreeBreakingException("Unknown/Unsupported break result: " + result);
}

@NotNull
private BreakMode getBreakMode(@NotNull IItemStack itemStack){
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public boolean beforeBlockBreak(Level level, Player player, BlockPos blockPos, B
var wrappedLevel = level instanceof ServerLevel serverLevel ? new ServerLevelWrapper(serverLevel) : new LevelWrapper(level);
var wrappedPos = new BlockPosWrapper(blockPos);

var result = mod.getTreeHandler().attemptTreeBreaking(wrappedLevel, wrappedPlayer, wrappedPos);
var result = mod.getTreeHandler().breakTree(wrappedLevel, wrappedPlayer, wrappedPos);
if (result instanceof BreakTreeResult breakTreeResult) {
return switch(breakTreeResult.breakMode()){
case INSTANTANEOUS, FALL_ITEM, FALL_BLOCK, FALL_ALL_BLOCK -> !result.shouldCancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void onBlockBreakEvent(@Nonnull BlockEvent.BreakEvent event){
var wrappedLevel = event.getLevel() instanceof ServerLevel serverLevel ? new ServerLevelWrapper(serverLevel) : new LevelWrapper(event.getLevel());
var wrappedPos = new BlockPosWrapper(event.getPos());

var result = mod.getTreeHandler().attemptTreeBreaking(wrappedLevel, wrappedPlayer, wrappedPos);
var result = mod.getTreeHandler().breakTree(wrappedLevel, wrappedPlayer, wrappedPos);
if (result instanceof BreakTreeResult breakTreeResult) {
switch(breakTreeResult.breakMode()){
case INSTANTANEOUS, FALL_ITEM, FALL_BLOCK, FALL_ALL_BLOCK -> event.setCanceled(result.shouldCancel());
Expand Down

0 comments on commit b069ff1

Please sign in to comment.