Skip to content

Commit

Permalink
Formatting & refactor shouldCancel & add enums values computed once
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Oct 28, 2023
1 parent b069ff1 commit b28c70c
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public enum BreakMode{
FALL_ALL_BLOCK(true, true),
SHIFT_DOWN(false, false);

@Getter
private final static BreakMode[] values = values();

private final boolean checkLeavesAround;
private final boolean applySpeedMultiplier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ConfigurationPacket read(IFriendlyByteBuf buf){
return builder()
.speedMultiplicand(buf.readDouble())
.forceToolUsage(buf.readBoolean())
.breakMode(BreakMode.values()[buf.readInteger()])
.breakMode(BreakMode.getValues()[buf.readInteger()])
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

@RequiredArgsConstructor
public enum AbortedResult implements IBreakAttemptResult {
NOT_SERVER(false),
NOT_ENABLED(false),
REQUIRED_TOOL_ABSENT(true),
INVALID_PLAYER_STATE(false),
NO_SUCH_TREE(false),
TREE_TOO_BIG_SCAN(false),
TREE_TOO_BIG_BREAK(false);
private final boolean cancel;
@Override
public boolean shouldCancel(){
return this.cancel;
}
INVALID_PLAYER_STATE(false),
NOT_ENABLED(false),
NOT_SERVER(false),
NO_SUCH_TREE(false),
REQUIRED_TOOL_ABSENT(true),
TREE_TOO_BIG_BREAK(false),
TREE_TOO_BIG_SCAN(false);

private final boolean cancel;

@Override
public boolean shouldCancel() {
return cancel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
* 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.
* @param breakMode The mode with which the block was broken.
*/
public record BreakTreeResult(
boolean shouldCancel,
@NotNull BreakMode breakMode
) implements IBreakAttemptResult{
@Override
public boolean shouldCancel(){
return switch(breakMode()){
case INSTANTANEOUS, FALL_ITEM, FALL_BLOCK, FALL_ALL_BLOCK -> shouldCancel;
case SHIFT_DOWN -> true;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void removePartsHigherThan(int y, @NotNull TreePartType partType){
}

public int getBreakableCount(){
return Arrays.stream(TreePartType.values())
return Arrays.stream(TreePartType.getValues())
.filter(TreePartType::isBreakable)
.mapToInt(this::getPartCount)
.sum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class TreeHandler{
private final Map<UUID, CacheSpeed> speedCache = new ConcurrentHashMap<>();

@NotNull
public IBreakAttemptResult breakTree(@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 AbortedResult.NOT_SERVER;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public enum TreePartType{
MANGROVE_ROOTS(true),
OTHER(false);

@Getter
private static final TreePartType[] values = values();

private final boolean breakable;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.rakambda.fallingtree.fabric.event;

import fr.rakambda.fallingtree.common.FallingTreeCommon;
import fr.rakambda.fallingtree.common.tree.BreakTreeResult;
import fr.rakambda.fallingtree.fabric.common.wrapper.BlockPosWrapper;
import fr.rakambda.fallingtree.fabric.common.wrapper.LevelWrapper;
import fr.rakambda.fallingtree.fabric.common.wrapper.PlayerWrapper;
Expand All @@ -21,20 +20,16 @@ public class BlockBreakListener implements PlayerBlockBreakEvents.Before{
@NotNull
private final FallingTreeCommon<?> mod;

/**
* @return true if event is handled successful (not cancelling it), false otherwise (cancelling event)
*/
@Override
public boolean beforeBlockBreak(Level level, Player player, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity){
var wrappedPlayer = new PlayerWrapper(player);
var wrappedLevel = level instanceof ServerLevel serverLevel ? new ServerLevelWrapper(serverLevel) : new LevelWrapper(level);
var wrappedPos = new BlockPosWrapper(blockPos);

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();
case SHIFT_DOWN -> false;
};
} else {
return !result.shouldCancel();
}
return !result.shouldCancel();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.rakambda.fallingtree.forge.event;

import fr.rakambda.fallingtree.common.FallingTreeCommon;
import fr.rakambda.fallingtree.common.tree.BreakTreeResult;
import fr.rakambda.fallingtree.forge.common.wrapper.BlockPosWrapper;
import fr.rakambda.fallingtree.forge.common.wrapper.LevelWrapper;
import fr.rakambda.fallingtree.forge.common.wrapper.PlayerWrapper;
Expand Down Expand Up @@ -55,15 +54,8 @@ public void onBlockBreakEvent(@Nonnull BlockEvent.BreakEvent event){
var wrappedPos = new BlockPosWrapper(event.getPos());

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());
case SHIFT_DOWN -> event.setCanceled(true);
}
} else {
if(result.shouldCancel() && event.isCancelable()){
event.setCanceled(true);
}
if(result.shouldCancel() && event.isCancelable()){
event.setCanceled(true);
}
}
}

0 comments on commit b28c70c

Please sign in to comment.