Moved tree detection to the server

This commit is contained in:
Sollace 2021-08-20 22:22:28 +02:00
parent 763510df5b
commit 121bcfb782
9 changed files with 65 additions and 19 deletions

View file

@ -3,7 +3,6 @@ package com.minelittlepony.unicopia;
import com.minelittlepony.unicopia.util.PosHelper; import com.minelittlepony.unicopia.util.PosHelper;
import com.minelittlepony.unicopia.util.Weighted; import com.minelittlepony.unicopia.util.Weighted;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -23,8 +22,8 @@ public final class TreeType {
new Identifier("unicopia", "none"), new Identifier("unicopia", "none"),
false, false,
new Weighted<Supplier<ItemStack>>(), new Weighted<Supplier<ItemStack>>(),
Collections.emptySet(), Set.of(),
Collections.emptySet() Set.of()
); );
private static final Direction[] WIDE_DIRS = new Direction[] { Direction.UP, Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST }; private static final Direction[] WIDE_DIRS = new Direction[] { Direction.UP, Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST };

View file

@ -73,6 +73,15 @@ public interface Ability<T extends Hit> {
return new TranslatableText("ability." + id.getNamespace() + "." + id.getPath().replace('/', '.')); 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. * Called to actually apply the ability.
* Only called on the server side. * Only called on the server side.

View file

@ -39,7 +39,6 @@ public class AbilityDispatcher implements Tickable, NbtSerialisable {
public void clear(AbilitySlot slot) { public void clear(AbilitySlot slot) {
Stat stat = getStat(slot); Stat stat = getStat(slot);
if (stat.canSwitchStates()) { if (stat.canSwitchStates()) {
stat.setActiveAbility(null); stat.setActiveAbility(null);
} }

View file

@ -1,7 +1,6 @@
package com.minelittlepony.unicopia.ability; package com.minelittlepony.unicopia.ability;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -60,20 +59,17 @@ public class EarthPonyKickAbility implements Ability<Pos> {
@Nullable @Nullable
@Override @Override
public Pos tryActivate(Pony player) { public Pos tryActivate(Pony player) {
Optional<BlockPos> p = RayTraceHelper.doTrace(player.getMaster(), 6, 1).getBlockPos(); return RayTraceHelper.doTrace(player.getMaster(), 6, 1).getBlockPos().map(Pos::new).orElse(null);
}
if (p.isPresent()) { @Override
BlockPos pos = p.get(); public boolean canApply(Pony player, Pos data) {
BlockPos pos = data.pos();
TreeType tree = TreeType.get(player.getWorld().getBlockState(pos)); TreeType tree = TreeType.get(player.getWorld().getBlockState(pos));
if (tree != TreeType.NONE) { return tree != TreeType.NONE && tree.findBase(player.getWorld(), pos).map(base -> {
return tree.findBase(player.getWorld(), pos).map(base -> { return tree.countBlocks(player.getWorld(), pos) > 0;
return tree.countBlocks(player.getWorld(), pos) > 0 ? new Pos(base) : null; }).orElse(false);
}).orElse(null);
}
}
return null;
} }
@Override @Override
@ -94,7 +90,6 @@ public class EarthPonyKickAbility implements Ability<Pos> {
BlockPos pos = data.pos(); BlockPos pos = data.pos();
BlockDestructionManager destr = ((BlockDestructionManager.Source)player.world).getDestructionManager(); BlockDestructionManager destr = ((BlockDestructionManager.Source)player.world).getDestructionManager();
if (destr.getBlockDestruction(pos) + 4 >= BlockDestructionManager.MAX_DAMAGE) { if (destr.getBlockDestruction(pos) + 4 >= BlockDestructionManager.MAX_DAMAGE) {

View file

@ -16,6 +16,7 @@ public interface Channel {
S2CPacketType<MsgPlayerCapabilities> SERVER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new); S2CPacketType<MsgPlayerCapabilities> SERVER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new);
S2CPacketType<MsgSpawnProjectile> SERVER_SPAWN_PROJECTILE = SimpleNetworking.serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new); S2CPacketType<MsgSpawnProjectile> SERVER_SPAWN_PROJECTILE = SimpleNetworking.serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new);
S2CPacketType<MsgBlockDestruction> SERVER_BLOCK_DESTRUCTION = SimpleNetworking.serverToClient(new Identifier("unicopia", "block_destruction"), MsgBlockDestruction::new); S2CPacketType<MsgBlockDestruction> SERVER_BLOCK_DESTRUCTION = SimpleNetworking.serverToClient(new Identifier("unicopia", "block_destruction"), MsgBlockDestruction::new);
S2CPacketType<MsgCancelPlayerAbility> CANCEL_PLAYER_ABILITY = SimpleNetworking.serverToClient(new Identifier("unicopia", "player_ability_cancel"), MsgCancelPlayerAbility::new);
Identifier SERVER_SELECT_TRIBE_ID = new Identifier("unicopia", "select_tribe"); Identifier SERVER_SELECT_TRIBE_ID = new Identifier("unicopia", "select_tribe");
S2CPacketType<MsgTribeSelect> SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(SERVER_SELECT_TRIBE_ID, MsgTribeSelect::new); S2CPacketType<MsgTribeSelect> SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(SERVER_SELECT_TRIBE_ID, MsgTribeSelect::new);

View file

@ -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<PlayerEntity> {
MsgCancelPlayerAbility(PacketByteBuf buffer) {
}
public MsgCancelPlayerAbility() {
}
@Override
public void toBuffer(PacketByteBuf buffer) {
}
@Override
public void handle(PlayerEntity sender) {
InteractionManager.instance().getClientNetworkHandler().handleCancelAbility(this);
}
}

View file

@ -43,6 +43,10 @@ public class MsgPlayerAbility<T extends Hit> implements Packet<ServerPlayerEntit
return; return;
} }
if (!power.canApply(player, data)) {
Channel.CANCEL_PLAYER_ABILITY.send(sender, new MsgCancelPlayerAbility());
} else {
power.apply(player, data); power.apply(player, data);
} }
} }
}

View file

@ -1,6 +1,7 @@
package com.minelittlepony.unicopia.network.handler; package com.minelittlepony.unicopia.network.handler;
import com.minelittlepony.unicopia.network.MsgBlockDestruction; import com.minelittlepony.unicopia.network.MsgBlockDestruction;
import com.minelittlepony.unicopia.network.MsgCancelPlayerAbility;
import com.minelittlepony.unicopia.network.MsgSpawnProjectile; import com.minelittlepony.unicopia.network.MsgSpawnProjectile;
import com.minelittlepony.unicopia.network.MsgTribeSelect; import com.minelittlepony.unicopia.network.MsgTribeSelect;
@ -11,4 +12,6 @@ public interface ClientNetworkHandler {
void handleSpawnProjectile(MsgSpawnProjectile packet); void handleSpawnProjectile(MsgSpawnProjectile packet);
void handleBlockDestruction(MsgBlockDestruction packet); void handleBlockDestruction(MsgBlockDestruction packet);
void handleCancelAbility(MsgCancelPlayerAbility packet);
} }

View file

@ -3,13 +3,16 @@ package com.minelittlepony.unicopia.network.handler;
import com.minelittlepony.unicopia.Owned; import com.minelittlepony.unicopia.Owned;
import com.minelittlepony.unicopia.client.ClientBlockDestructionManager; import com.minelittlepony.unicopia.client.ClientBlockDestructionManager;
import com.minelittlepony.unicopia.client.gui.TribeSelectionScreen; import com.minelittlepony.unicopia.client.gui.TribeSelectionScreen;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.network.MsgBlockDestruction; import com.minelittlepony.unicopia.network.MsgBlockDestruction;
import com.minelittlepony.unicopia.network.MsgCancelPlayerAbility;
import com.minelittlepony.unicopia.network.MsgSpawnProjectile; import com.minelittlepony.unicopia.network.MsgSpawnProjectile;
import com.minelittlepony.unicopia.network.MsgTribeSelect; import com.minelittlepony.unicopia.network.MsgTribeSelect;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld; import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.sound.SoundEvents;
public class ClientNetworkHandlerImpl implements ClientNetworkHandler { public class ClientNetworkHandlerImpl implements ClientNetworkHandler {
@ -50,4 +53,10 @@ public class ClientNetworkHandlerImpl implements ClientNetworkHandler {
}); });
} }
@Override
public void handleCancelAbility(MsgCancelPlayerAbility packet) {
client.player.playSound(SoundEvents.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1, 1);
Pony.of(client.player).getAbilities().getStats().forEach(s -> s.setCooldown(0));
}
} }