From 121bcfb782ea5ff30b1bbe90ea25aaa85cc3b89b Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 20 Aug 2021 22:22:28 +0200 Subject: [PATCH] Moved tree detection to the server --- .../com/minelittlepony/unicopia/TreeType.java | 5 ++-- .../unicopia/ability/Ability.java | 9 +++++++ .../unicopia/ability/AbilityDispatcher.java | 1 - .../ability/EarthPonyKickAbility.java | 23 +++++++--------- .../unicopia/network/Channel.java | 1 + .../network/MsgCancelPlayerAbility.java | 27 +++++++++++++++++++ .../unicopia/network/MsgPlayerAbility.java | 6 ++++- .../network/handler/ClientNetworkHandler.java | 3 +++ .../handler/ClientNetworkHandlerImpl.java | 9 +++++++ 9 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java diff --git a/src/main/java/com/minelittlepony/unicopia/TreeType.java b/src/main/java/com/minelittlepony/unicopia/TreeType.java index 334028dd..a37471fa 100644 --- a/src/main/java/com/minelittlepony/unicopia/TreeType.java +++ b/src/main/java/com/minelittlepony/unicopia/TreeType.java @@ -3,7 +3,6 @@ package com.minelittlepony.unicopia; import com.minelittlepony.unicopia.util.PosHelper; import com.minelittlepony.unicopia.util.Weighted; -import java.util.Collections; import java.util.HashSet; import java.util.Optional; import java.util.Set; @@ -23,8 +22,8 @@ public final class TreeType { new Identifier("unicopia", "none"), false, new Weighted>(), - Collections.emptySet(), - Collections.emptySet() + Set.of(), + Set.of() ); private static final Direction[] WIDE_DIRS = new Direction[] { Direction.UP, Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST }; diff --git a/src/main/java/com/minelittlepony/unicopia/ability/Ability.java b/src/main/java/com/minelittlepony/unicopia/ability/Ability.java index 53daac5f..fff869cb 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/Ability.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/Ability.java @@ -73,6 +73,15 @@ public interface Ability { return new TranslatableText("ability." + id.getNamespace() + "." + id.getPath().replace('/', '.')); } + /** + * Server-side counterpart to canActivate. + * + * Called before applying to determine whether to cancel the command or not. + */ + default boolean canApply(Pony player, T data) { + return true; + } + /** * Called to actually apply the ability. * Only called on the server side. diff --git a/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java b/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java index fba1c708..46418532 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/AbilityDispatcher.java @@ -39,7 +39,6 @@ public class AbilityDispatcher implements Tickable, NbtSerialisable { public void clear(AbilitySlot slot) { Stat stat = getStat(slot); - if (stat.canSwitchStates()) { stat.setActiveAbility(null); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java index 83b59fd3..b41a55a9 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java @@ -1,7 +1,6 @@ package com.minelittlepony.unicopia.ability; import java.util.List; -import java.util.Optional; import org.jetbrains.annotations.Nullable; @@ -60,20 +59,17 @@ public class EarthPonyKickAbility implements Ability { @Nullable @Override public Pos tryActivate(Pony player) { - Optional p = RayTraceHelper.doTrace(player.getMaster(), 6, 1).getBlockPos(); + return RayTraceHelper.doTrace(player.getMaster(), 6, 1).getBlockPos().map(Pos::new).orElse(null); + } - if (p.isPresent()) { - BlockPos pos = p.get(); - TreeType tree = TreeType.get(player.getWorld().getBlockState(pos)); + @Override + public boolean canApply(Pony player, Pos data) { + BlockPos pos = data.pos(); + TreeType tree = TreeType.get(player.getWorld().getBlockState(pos)); - if (tree != TreeType.NONE) { - return tree.findBase(player.getWorld(), pos).map(base -> { - return tree.countBlocks(player.getWorld(), pos) > 0 ? new Pos(base) : null; - }).orElse(null); - } - } - - return null; + return tree != TreeType.NONE && tree.findBase(player.getWorld(), pos).map(base -> { + return tree.countBlocks(player.getWorld(), pos) > 0; + }).orElse(false); } @Override @@ -94,7 +90,6 @@ public class EarthPonyKickAbility implements Ability { BlockPos pos = data.pos(); - BlockDestructionManager destr = ((BlockDestructionManager.Source)player.world).getDestructionManager(); if (destr.getBlockDestruction(pos) + 4 >= BlockDestructionManager.MAX_DAMAGE) { diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index e69ee11f..14f56ebe 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -16,6 +16,7 @@ public interface Channel { S2CPacketType SERVER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new); S2CPacketType SERVER_SPAWN_PROJECTILE = SimpleNetworking.serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new); S2CPacketType SERVER_BLOCK_DESTRUCTION = SimpleNetworking.serverToClient(new Identifier("unicopia", "block_destruction"), MsgBlockDestruction::new); + S2CPacketType CANCEL_PLAYER_ABILITY = SimpleNetworking.serverToClient(new Identifier("unicopia", "player_ability_cancel"), MsgCancelPlayerAbility::new); Identifier SERVER_SELECT_TRIBE_ID = new Identifier("unicopia", "select_tribe"); S2CPacketType SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(SERVER_SELECT_TRIBE_ID, MsgTribeSelect::new); diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java b/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java new file mode 100644 index 00000000..dbcc1e3f --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgCancelPlayerAbility.java @@ -0,0 +1,27 @@ +package com.minelittlepony.unicopia.network; + +import com.minelittlepony.unicopia.InteractionManager; +import com.minelittlepony.unicopia.util.network.Packet; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.PacketByteBuf; + +/** + * Sent to the client when an ability fails its server-side activation checks. + */ +public class MsgCancelPlayerAbility implements Packet { + + MsgCancelPlayerAbility(PacketByteBuf buffer) { + } + + public MsgCancelPlayerAbility() { + } + + @Override + public void toBuffer(PacketByteBuf buffer) { + } + + @Override + public void handle(PlayerEntity sender) { + InteractionManager.instance().getClientNetworkHandler().handleCancelAbility(this); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java index 3c25576e..fe1b2cfb 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java @@ -43,6 +43,10 @@ public class MsgPlayerAbility implements Packet s.setCooldown(0)); + } + }