Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/github/voxxin/blockhunt/BlockHunt.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.github.voxxin.blockhunt.game.BlockHuntConfig;
import com.github.voxxin.blockhunt.game.BlockHuntWaiting;
import net.fabricmc.api.ModInitializer;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -29,4 +32,13 @@ public static Identifier id(String value) {
return Identifier.of(ID, value);
}
public static List<Integer> deniedIDs = new ArrayList<>();

public static boolean shouldCancel(Packet<?> packet, ServerPlayNetworkHandler handler) {
if (packet instanceof EntityEquipmentUpdateS2CPacket entityEquipmentUpdateS2CPacket) {
int packetID = entityEquipmentUpdateS2CPacket.getEntityId();
return handler.player.getWorld().getPlayers().stream().noneMatch(p -> p.getId() == packetID) || BlockHunt.deniedIDs.contains(packetID);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static void open(GameSpace gameSpace, ServerWorld world, BlockHuntMap map
game.listen(GameActivityEvents.ENABLE, active::onOpen);

game.listen(GamePlayerEvents.OFFER, JoinOffer::accept);
game.listen(GamePlayerEvents.ACCEPT, (offer) -> offer.teleport(world, Vec3d.ZERO));
game.listen(GamePlayerEvents.ACCEPT, (offer) -> offer.teleport(world, map.getSpawnPos()));
game.listen(GamePlayerEvents.ADD, active::addPlayer);
game.listen(GamePlayerEvents.REMOVE, active::removePlayer);

Expand Down Expand Up @@ -269,7 +269,7 @@ private EventResult blockAttack(ServerPlayerEntity serverPlayerEntity, Direction
}

private void addPlayer(ServerPlayerEntity player) {
if (gameSpace.getTime() < stageManager.finishTime && gameSpace.getTime() > stageManager.startTime) {
if (world.getTime() < stageManager.finishTime && world.getTime() > stageManager.startTime) {
this.spawnLogic.resetPlayer(player, GameMode.SPECTATOR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static GameOpenProcedure open(GameOpenContext<BlockHuntConfig> context) {
game.listen(GameActivityEvents.REQUEST_START, waiting::requestStart);
game.listen(GamePlayerEvents.ADD, waiting::addPlayer);
game.listen(GamePlayerEvents.OFFER, JoinOffer::accept);
game.listen(GamePlayerEvents.ACCEPT, (offer) -> offer.teleport(world, map.spawns().containsKey("spawn_everyone") ? map.spawns().get("spawn_everyone") : map.spawns().get("spawn_hider")));
game.listen(GamePlayerEvents.ACCEPT, (offer) -> offer.teleport(world, map.getSpawnPos()));
game.listen(PlayerDeathEvent.EVENT, waiting::onPlayerDeath);

game.listen(BlockUseEvent.EVENT, waiting::allowInteraction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

public record BlockHuntMap(Map<String, Vec3d> spawns, ArrayList<Object> noInteractList,
ArrayList<Object> allowedDisguises, ArrayList<BlockHuntAnimation> animations, RuntimeWorldConfig worldConfig) {
public Vec3d getSpawnPos() {
return this.spawns().getOrDefault("spawn_everyone", this.spawns().get("spawn_hider"));
}

public static BlockHuntMap from(GameOpenContext<BlockHuntConfig> context) throws IOException {
var server = context.server();
var config = context.config();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.github.voxxin.blockhunt.BlockHunt;
import io.netty.channel.ChannelFutureListener;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.BundleS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -13,19 +14,43 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.ArrayList;
import java.util.List;

@Mixin(ServerCommonNetworkHandler.class)
public class ServerPlayerNetworkHandlerMixin {

@Inject(at = @At("HEAD"), method = "send", cancellable = true)
public void sendPacket(Packet<?> packet, ChannelFutureListener listener, CallbackInfo ci) {
//noinspection ConstantValue
if (packet instanceof EntityEquipmentUpdateS2CPacket entityEquipmentUpdateS2CPacket && ((Object) this) instanceof ServerPlayNetworkHandler handler) {
int packetID = entityEquipmentUpdateS2CPacket.getEntityId();
if (handler.player.getWorld().getPlayers().stream().noneMatch(p -> p.getId() == packetID)) { ci.cancel(); }
if (((Object) this) instanceof ServerPlayNetworkHandler handler && BlockHunt.shouldCancel(packet, handler)) {
ci.cancel();
}
}

if (BlockHunt.deniedIDs.contains(packetID)) { ci.cancel(); }
@ModifyVariable(at = @At("HEAD"), method = "send", argsOnly = true)
public Packet<?> sendPacket(Packet<?> packet) {
//noinspection ConstantValue
if (((Object) this) instanceof ServerPlayNetworkHandler handler && packet instanceof BundleS2CPacket bundleS2CPacket) {
List<Packet<? super ClientPlayPacketListener>> packets = new ArrayList<>();
boolean modified = false;

for (Packet<? super ClientPlayPacketListener> child : bundleS2CPacket.getPackets()) {
if (BlockHunt.shouldCancel(child, handler)) {
modified = true;
} else {
packets.add(child);
}
}

if (modified) {
return new BundleS2CPacket(packets);
}
}

return packet;
}
}