Skip to content

Commit

Permalink
Cleanup and prevent sending packets if the server hasn't consented
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Jul 22, 2024
1 parent 38f29c0 commit ac105db
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public float getMaxMultiplier() {
public long getLastSettingsUpdateTime() {
return 0;
}

public boolean isNetworkConnected() {
return true;
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/minelittlepony/bigpony/Scaling.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ public void tick(PlayerEntity entity) {
if (dirty) {
dirty = false;
entity.calculateDimensions();
if (entity instanceof ServerPlayerEntity) {
Network.OTHER_PLAYER_SIZE.sendToSurroundingPlayers(toUpdatePacket(entity), entity);
} else if (entity.getWorld().isClient && BigPonyClient.isClientPlayer(entity)) {
Network.PLAYER_SIZE.sendToServer(toUpdatePacket(entity));
if (InteractionManager.getInstance().isNetworkConnected()) {
if (entity instanceof ServerPlayerEntity) {
Network.OTHER_PLAYER_SIZE.sendToSurroundingPlayers(toUpdatePacket(entity), entity);
} else if (entity.getWorld().isClient && BigPonyClient.isClientPlayer(entity)) {
Network.PLAYER_SIZE.sendToServer(toUpdatePacket(entity));
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/minelittlepony/bigpony/mixin/MixinEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.minelittlepony.bigpony.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.minelittlepony.bigpony.Scaling;
import com.minelittlepony.bigpony.network.Network;

import net.minecraft.entity.Entity;
import net.minecraft.server.network.ServerPlayerEntity;

@Mixin(Entity.class)
abstract class MixinEntity {
@Inject(method = "onStartedTrackingBy", at = @At("HEAD"))
private void sendScalingOnStartedTrackingBy(ServerPlayerEntity player, CallbackInfo info) {
if (this instanceof Scaling.Holder holder) {
Network.OTHER_PLAYER_SIZE.sendToPlayer(holder.getScaling().toUpdatePacket((Entity)(Object)this), player);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.minelittlepony.bigpony.network.client;

import java.util.Optional;

import org.jetbrains.annotations.Nullable;

import com.minelittlepony.bigpony.BigPony;
import com.minelittlepony.bigpony.InteractionManager;
import com.minelittlepony.bigpony.Permissions;
import com.minelittlepony.bigpony.Scaling;
import com.minelittlepony.bigpony.network.ConsentPacket;
import com.minelittlepony.bigpony.network.MsgPlayerSize;
import com.minelittlepony.bigpony.network.Network;

import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;

public class ClientNetworkHandlerImpl extends InteractionManager {
private final MinecraftClient client = MinecraftClient.getInstance();

private long permissions = Permissions.DEFAULT;
private float maxScalingMultiplier;
private long lastSettingsUpdate = 0;
private Optional<ConsentPacket> serverConsent = Optional.empty();

public ClientNetworkHandlerImpl() {
Network.SERVER_CONSENT.receiver().addPersistentListener(this::handleConsent);
Network.OTHER_PLAYER_SIZE.receiver().addPersistentListener(this::handleSizeUpdate);
ClientLoginConnectionEvents.INIT.register((handler, client) -> {
permissions = Permissions.DEFAULT;
maxScalingMultiplier = 2;
BigPony.LOGGER.info("Resetting registered flag");
Network.SERVER_CONSENT.receiver().addPersistentListener((sender, packet) -> {
updateConsent(packet);
});
Network.OTHER_PLAYER_SIZE.receiver().addPersistentListener((sender, packet) -> {
if (sender.getWorld().getEntityById(packet.entityId()) instanceof Scaling.Holder holder) {
holder.getScaling().setDimensions(packet.dimensions());
}
});
ClientLoginConnectionEvents.INIT.register((handler, client) -> updateConsent(null));
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
if (client.player instanceof Scaling.Holder holder) {
Scaling scaling = holder.getScaling();
Expand All @@ -38,22 +38,26 @@ public ClientNetworkHandlerImpl() {

@Override
public long getPermissions() {
return permissions;
return serverConsent.map(ConsentPacket::permissions).orElseGet(super::getPermissions);
}

@Override
public float getMaxMultiplier() {
return maxScalingMultiplier;
return serverConsent.map(ConsentPacket::maxMultiplier).orElseGet(super::getMaxMultiplier);
}

private void handleConsent(PlayerEntity sender, ConsentPacket packet) {
permissions = packet.permissions();
maxScalingMultiplier = packet.maxMultiplier();
private void updateConsent(@Nullable ConsentPacket consent) {
lastSettingsUpdate = System.currentTimeMillis();
serverConsent = Optional.ofNullable(consent);
}

private void handleSizeUpdate(PlayerEntity sender, MsgPlayerSize packet) {
if (client.world.getEntityById(packet.entityId()) instanceof Scaling.Holder holder) {
holder.getScaling().setDimensions(packet.dimensions());
}
@Override
public long getLastSettingsUpdateTime() {
return lastSettingsUpdate;
}

@Override
public boolean isNetworkConnected() {
return serverConsent.isPresent();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/bigpony.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"compatibilityLevel": "JAVA_16",
"mixins": [
"MixinPlayerEntity",
"MixinServerPlayerEntity"
"MixinEntity"
],
"client": [
"client.MixinCamera",
Expand Down

0 comments on commit ac105db

Please sign in to comment.