Skip to content

Commit

Permalink
PacketHandlerCodec for entire login and config process
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Dec 3, 2023
1 parent 8dca3dc commit b868546
Show file tree
Hide file tree
Showing 50 changed files with 1,170 additions and 796 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ dependencies {
shade("com.github.rfresh2.Discord4j:discord4j-core:33c208f6ac") {
exclude group: 'io.netty'
}
shade("com.github.rfresh2:MCProtocolLib:82ec73716d") {
shade("com.github.rfresh2:MCProtocolLib:4d1be8669e") {
exclude group: 'io.netty.incubator'
exclude group: 'io.netty'
}
Expand Down
209 changes: 0 additions & 209 deletions src/main/java/com/zenith/Shared.java

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/main/java/com/zenith/cache/data/PlayerCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.github.steveice10.mc.protocol.data.game.inventory.CreativeGrabAction;
import com.github.steveice10.mc.protocol.data.game.level.notify.GameEvent;
import com.github.steveice10.mc.protocol.data.game.setting.Difficulty;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundUpdateTagsPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundChangeDifficultyPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundEntityEventPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerAbilitiesPacket;
Expand All @@ -33,7 +32,10 @@
import lombok.experimental.Accessors;
import org.cloudburstmc.math.vector.Vector3i;

import java.util.*;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
Expand Down Expand Up @@ -71,7 +73,6 @@ public class PlayerCache implements CachedData {
protected float walkSpeed;
protected boolean isSneaking = false;
protected boolean isSprinting = false;
protected Map<String, Map<String, int[]>> tags = new HashMap<>();
protected EntityEvent opLevel = EntityEvent.PLAYER_OP_PERMISSION_LEVEL_0;
protected AtomicInteger actionId = new AtomicInteger(0);
private static final MutableVec3i DEFAULT_SPAWN_POSITION = new MutableVec3i(0, 0, 0);
Expand All @@ -86,7 +87,6 @@ public void getPackets(@NonNull Consumer<Packet> consumer) {
// todo: may need to move this out so spectators don't get sent wrong abilities
consumer.accept(new ClientboundPlayerAbilitiesPacket(this.invincible, this.canFly, this.flying, this.creative, this.flySpeed, this.walkSpeed));
consumer.accept(new ClientboundChangeDifficultyPacket(this.difficulty, this.isDifficultyLocked));
consumer.accept(new ClientboundUpdateTagsPacket(this.tags));
consumer.accept(new ClientboundGameEventPacket(GameEvent.CHANGE_GAMEMODE, this.gameMode));
consumer.accept(new ClientboundEntityEventPacket(this.thePlayer.getEntityId(), this.opLevel));
consumer.accept(new ClientboundContainerSetContentPacket(0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.zenith.cache.data.config;

import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundResourcePackPushPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundUpdateEnabledFeaturesPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundUpdateTagsPacket;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundRegistryDataPacket;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundUpdateEnabledFeaturesPacket;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.packetlib.packet.Packet;
import com.zenith.cache.CachedData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public static void initSpectator(ServerConnection session, Supplier<Collection<C
.setFlying(true)
.setFlySpeed(0.05f)
.setWalkSpeed(0.1f)
.setTags(CACHE.getPlayerCache().getTags())
.setOpLevel(CACHE.getPlayerCache().getOpLevel())
.setMaxPlayers(CACHE.getPlayerCache().getMaxPlayers());
session.setAllowSpectatorServerPlayerPosRotate(true);
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/com/zenith/module/impl/ActionLimiter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zenith.module.impl;

import com.github.steveice10.mc.protocol.data.ProtocolState;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundMoveVehiclePacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket;
Expand All @@ -17,7 +18,8 @@
import com.zenith.feature.actionlimiter.handlers.outbound.ALLoginHandler;
import com.zenith.feature.actionlimiter.handlers.outbound.ALPlayerPositionHandler;
import com.zenith.module.Module;
import com.zenith.network.registry.HandlerRegistry;
import com.zenith.network.registry.PacketHandlerCodec;
import com.zenith.network.registry.PacketHandlerStateCodec;
import com.zenith.network.server.ServerConnection;
import lombok.Getter;

Expand All @@ -29,31 +31,33 @@

public class ActionLimiter extends Module {
@Getter
private HandlerRegistry<ServerConnection> handlerRegistry;
private PacketHandlerCodec codec;
private final HashSet<ServerConnection> limitedConnections = new HashSet<>();

public ActionLimiter() {
initializeHandlers();
}

private void initializeHandlers() {
handlerRegistry = new HandlerRegistry.Builder<ServerConnection>()
codec = PacketHandlerCodec.builder()
.setLogger(MODULE_LOG)
.allowUnhandled(true)
.registerInbound(ServerboundChatCommandPacket.class, new ALChatCommandHandler())
.registerInbound(ServerboundChatPacket.class, new ALChatHandler())
.registerInbound(ServerboundClientCommandPacket.class, new ALClientCommandHandler())
.registerInbound(ServerboundContainerClickPacket.class, new ALContainerClickHandler())
.registerInbound(ServerboundInteractPacket.class, new ALInteractHandler())
.registerInbound(ServerboundMovePlayerPosPacket.class, new ALMovePlayerPosHandler())
.registerInbound(ServerboundMovePlayerPosRotPacket.class, new ALMovePlayerPosRotHandler())
.registerInbound(ServerboundMoveVehiclePacket.class, new ALMoveVehicleHandler())
.registerInbound(ServerboundPlayerActionPacket.class, new ALPlayerActionHandler())
.registerInbound(ServerboundUseItemOnPacket.class, new ALUseItemOnHandler())
.registerInbound(ServerboundUseItemPacket.class, new ALUseItemHandler())
.registerOutbound(ClientboundMoveVehiclePacket.class, new ALCMoveVehicleHandler())
.registerOutbound(ClientboundLoginPacket.class, new ALLoginHandler())
.registerOutbound(ClientboundPlayerPositionPacket.class, new ALPlayerPositionHandler())
.state(ProtocolState.GAME, PacketHandlerStateCodec.<ServerConnection>builder()
.allowUnhandled(true)
.registerInbound(ServerboundChatCommandPacket.class, new ALChatCommandHandler())
.registerInbound(ServerboundChatPacket.class, new ALChatHandler())
.registerInbound(ServerboundClientCommandPacket.class, new ALClientCommandHandler())
.registerInbound(ServerboundContainerClickPacket.class, new ALContainerClickHandler())
.registerInbound(ServerboundInteractPacket.class, new ALInteractHandler())
.registerInbound(ServerboundMovePlayerPosPacket.class, new ALMovePlayerPosHandler())
.registerInbound(ServerboundMovePlayerPosRotPacket.class, new ALMovePlayerPosRotHandler())
.registerInbound(ServerboundMoveVehiclePacket.class, new ALMoveVehicleHandler())
.registerInbound(ServerboundPlayerActionPacket.class, new ALPlayerActionHandler())
.registerInbound(ServerboundUseItemOnPacket.class, new ALUseItemOnHandler())
.registerInbound(ServerboundUseItemPacket.class, new ALUseItemHandler())
.registerOutbound(ClientboundMoveVehiclePacket.class, new ALCMoveVehicleHandler())
.registerOutbound(ClientboundLoginPacket.class, new ALLoginHandler())
.registerOutbound(ClientboundPlayerPositionPacket.class, new ALPlayerPositionHandler())
.build())
.build();
}

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/zenith/network/KeepAliveTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.zenith.network;

import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundKeepAlivePacket;
import com.zenith.network.server.ServerConnection;

import java.util.concurrent.TimeUnit;

import static com.zenith.Shared.SCHEDULED_EXECUTOR_SERVICE;

public class KeepAliveTask implements Runnable {
private final ServerConnection session;

public KeepAliveTask(ServerConnection session) {
this.session = session;
}

@Override
public void run() {
if (this.session.isConnected()) {
session.setLastPingTime(System.currentTimeMillis());
session.setLastPingId((int) session.getLastPingTime());
this.session.sendAsync(new ClientboundKeepAlivePacket(session.getLastPingId()));
SCHEDULED_EXECUTOR_SERVICE.schedule(this, 2L, TimeUnit.SECONDS);
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/zenith/network/UserAuthTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.zenith.network;

import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.auth.service.SessionService;
import com.github.steveice10.mc.protocol.MinecraftConstants;
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginCompressionPacket;
import com.zenith.network.server.ServerConnection;

import javax.crypto.SecretKey;
import java.util.UUID;

public class UserAuthTask implements Runnable {
private ServerConnection session;
private SecretKey key;

public UserAuthTask(ServerConnection session, SecretKey key) {
this.key = key;
this.session = session;
}

@Override
public void run() {
GameProfile profile;
if (this.key != null) {
SessionService sessionService = this.session.getFlag(MinecraftConstants.SESSION_SERVICE_KEY,
new SessionService());
try {
profile = sessionService.getProfileByServer(session.getUsername(),
sessionService.getServerId(session.getServerId(),
session.getKeyPair().getPublic(),
this.key));
} catch (RequestException e) {
this.session.disconnect("Failed to make session service request.", e);
return;
}

if (profile == null) {
this.session.disconnect("Failed to verify username.");
}
} else {
profile = new GameProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + session.getUsername()).getBytes()),
session.getUsername());
}

this.session.setFlag(MinecraftConstants.PROFILE_KEY, profile);

int threshold = session.getFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD,
ServerConnection.DEFAULT_COMPRESSION_THRESHOLD);
this.session.send(new ClientboundLoginCompressionPacket(threshold));
}
}
Loading

0 comments on commit b868546

Please sign in to comment.