Skip to content

Commit

Permalink
Merge pull request #141 from celestialfault/fix/sync
Browse files Browse the repository at this point in the history
Fix syncing on dedicated servers
  • Loading branch information
WildfireRomeo authored Sep 18, 2023
2 parents 1e30d09 + f5de384 commit aec4fac
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 250 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/wildfire/main/GenderPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class GenderPlayer {
private float bounceMultiplier = Configuration.BOUNCE_MULTIPLIER.getDefault();
private float floppyMultiplier = Configuration.FLOPPY_MULTIPLIER.getDefault();

public boolean lockSettings = false;

public SyncStatus syncStatus = SyncStatus.UNKNOWN;
private boolean showBreastsInArmor = Configuration.SHOW_IN_ARMOR.getDefault();
private boolean armorPhysOverride = Configuration.ARMOR_PHYSICS_OVERRIDE.getDefault();
Expand Down Expand Up @@ -191,7 +189,6 @@ public static JsonObject toJsonObject(GenderPlayer plr) {
public static GenderPlayer loadCachedPlayer(UUID uuid, boolean markForSync) {
GenderPlayer plr = WildfireGender.getPlayerById(uuid);
if (plr != null) {
plr.lockSettings = false;
plr.syncStatus = SyncStatus.CACHED;
Configuration config = plr.getConfig();
plr.updateGender(config.get(Configuration.GENDER));
Expand All @@ -218,7 +215,7 @@ public static GenderPlayer loadCachedPlayer(UUID uuid, boolean markForSync) {
}
return null;
}

public static void saveGenderInfo(GenderPlayer plr) {
Configuration config = plr.getConfig();
config.set(Configuration.USERNAME, plr.uuid);
Expand Down
120 changes: 43 additions & 77 deletions src/main/java/com/wildfire/main/WildfireEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,98 +19,64 @@
package com.wildfire.main;

import com.wildfire.gui.screen.WardrobeBrowserScreen;
import com.wildfire.main.networking.PacketSendGenderInfo;
import com.wildfire.main.networking.PacketSync;

import java.util.UUID;

import com.wildfire.main.networking.WildfireSync;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.Identifier;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import org.lwjgl.glfw.GLFW;

public class WildfireEventHandler {

public static final KeyBinding toggleEditGUI = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.wildfire_gender.gender_menu", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_G, "category.wildfire_gender.generic"));
import java.util.UUID;

private static int timer = 0;
@Environment(EnvType.CLIENT)
public class WildfireEventHandler {
public static final KeyBinding toggleEditGUI = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.wildfire_gender.gender_menu", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_G, "category.wildfire_gender.generic"));
private static long timer = 0;

public static void registerClientEvents() {
ClientEntityEvents.ENTITY_LOAD.register(WildfireEventHandler::onEntityLoad);
ClientTickEvents.END_CLIENT_TICK.register(WildfireEventHandler::onClientTick);
ClientPlayNetworking.registerGlobalReceiver(WildfireSync.SYNC_IDENTIFIER, WildfireSync::handle);
}

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
if (!handler.getPlayer().getWorld().isClient()) {
//Send all other players to the player who joined. Note: We don't send the player to
// other players as that will happen once the player finishes sending themselves to the server
PacketSync.sendTo(handler.getPlayer());
}
});

ClientEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if(!world.isClient) return;
if(entity instanceof AbstractClientPlayerEntity plr) {
UUID uuid = plr.getUuid();
GenderPlayer aPlr = WildfireGender.getPlayerById(plr.getUuid());
if (aPlr == null) {
aPlr = new GenderPlayer(uuid);
WildfireGender.CLOTHING_PLAYERS.put(uuid, aPlr);
WildfireGender.loadGenderInfoAsync(uuid, uuid.equals(MinecraftClient.getInstance().player.getUuid()));
return;
}
}
/*if(!world.isClient) return;
if(entity instanceof AbstractClientPlayerEntity plr) {
UUID uuid = plr.getUuid();
GenderPlayer aPlr = WildfireGender.getPlayerById(plr.getUuid());
if(aPlr == null) {
aPlr = new GenderPlayer(uuid);
WildfireGender.CLOTHING_PLAYERS.put(uuid, aPlr);
WildfireGender.loadGenderInfoAsync(uuid, uuid.equals(MinecraftClient.getInstance().player.getUuid()));
return;
}
}*/
});

ClientTickEvents.END_CLIENT_TICK.register(client -> {
if(client.world == null) WildfireGender.CLOTHING_PLAYERS.clear();

boolean isVanillaServer = !ClientPlayNetworking.canSend(new Identifier(WildfireGender.MODID, "send_gender_info"));


if(!isVanillaServer) {
//20 ticks per second / 5 = 4 times per second

timer++;
if (timer >= 5) {
try {
GenderPlayer aPlr = WildfireGender.getPlayerById(MinecraftClient.getInstance().player.getUuid());
if(aPlr == null /*|| !aPlr.needsSync*/) return;
PacketSendGenderInfo.send(aPlr);
} catch (Exception e) {
//e.printStackTrace();
}
timer = 0;
}
}

while (toggleEditGUI.wasPressed()) {
//client.setScreen(new WildfirePlayerListScreen(client)); //old screen
try {
MinecraftClient.getInstance().setScreen(new WardrobeBrowserScreen(null, MinecraftClient.getInstance().player.getUuid()));
} catch(Exception ignored) {}
private static void onEntityLoad(Entity entity, World world) {
if(!world.isClient() || MinecraftClient.getInstance().player == null) return;
if(entity instanceof AbstractClientPlayerEntity plr) {
UUID uuid = plr.getUuid();
GenderPlayer aPlr = WildfireGender.getPlayerById(plr.getUuid());
if(aPlr == null) {
aPlr = new GenderPlayer(uuid);
WildfireGender.CLOTHING_PLAYERS.put(uuid, aPlr);
WildfireGender.loadGenderInfoAsync(uuid, uuid.equals(MinecraftClient.getInstance().player.getUuid()));
}
});
}
}

ClientPlayNetworking.registerGlobalReceiver(new Identifier(WildfireGender.MODID, "sync"),
(client, handler, buf, responseSender) -> {
PacketSync.handle(client, handler, buf, responseSender);
});
private static void onClientTick(MinecraftClient client) {
if(client.world == null || client.player == null) {
WildfireGender.CLOTHING_PLAYERS.clear();
return;
}

// Only attempt to sync if the server will accept the packet, and only once every 5 ticks, or around 4 times a second
if(ClientPlayNetworking.canSend(WildfireSync.SEND_GENDER_IDENTIFIER) && timer++ % 5 == 0) {
GenderPlayer aPlr = WildfireGender.getPlayerById(client.player.getUuid());
// sendToServer will only actually send a packet if any changes have been made that need to be synced,
// or if we haven't synced before.
if(aPlr != null) WildfireSync.sendToServer(aPlr);
}

if(toggleEditGUI.wasPressed() && client.currentScreen == null) {
client.setScreen(new WardrobeBrowserScreen(null, client.player.getUuid()));
}
}
}
32 changes: 22 additions & 10 deletions src/main/java/com/wildfire/main/WildfireGenderServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,34 @@
*/

package com.wildfire.main;
import com.wildfire.main.networking.PacketSendGenderInfo;

import com.wildfire.main.networking.WildfireSync;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.EntityTrackingEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.util.Identifier;

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

public class WildfireGenderServer implements ModInitializer {

@Override
public void onInitialize() {
// while this class is named 'Server', this is actually a common code path,
// so we can safely register here for both sides.
WildfireSounds.register();
ServerPlayNetworking.registerGlobalReceiver(new Identifier(WildfireGender.MODID, "send_gender_info"),
(server, playerEntity, handler, buf, responseSender) -> {
PacketSendGenderInfo.handle(server, playerEntity, handler, buf, responseSender);
});
ServerPlayNetworking.registerGlobalReceiver(WildfireSync.SEND_GENDER_IDENTIFIER, WildfireSync::handle);
EntityTrackingEvents.START_TRACKING.register(this::onBeginTracking);
}

private void onBeginTracking(Entity tracked, ServerPlayerEntity syncTo) {
if(tracked instanceof PlayerEntity toSync) {
GenderPlayer genderToSync = WildfireGender.getPlayerById(toSync.getUuid());
if(genderToSync == null) return;
// Note that we intentionally don't check if we've previously synced a player with this code path;
// because we use entity tracking to sync, it's entirely possible that one player would leave the
// tracking distance of another, change their settings, and then re-enter their tracking distance;
// we wouldn't sync while they're out of tracking distance, and as such, their settings would be out
// of sync until they relog.
WildfireSync.sendToClient(syncTo, genderToSync);
}
}
}
}

This file was deleted.

88 changes: 0 additions & 88 deletions src/main/java/com/wildfire/main/networking/PacketSync.java

This file was deleted.

Loading

0 comments on commit aec4fac

Please sign in to comment.