Skip to content

Commit

Permalink
Fix syncing of upgrades on dedicated servers
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n committed Mar 6, 2024
1 parent 65fbd5e commit 06b37d4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/main/java/dev/technici4n/moderndynamics/MdProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ public class MdProxy {
public boolean isShiftDown() {
return false;
}

/**
* Checks if the current client has a memory connection to the server.
*/
public boolean isMemoryConnection() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ public static void setup() {
});
NeoForge.EVENT_BUS.addListener(OnDatapackSyncEvent.class, e -> {
var server = ServerLifecycleHooks.getCurrentServer();
LoadedUpgrades.trySet(LOADED_UPGRADES.remove(server.getResourceManager()));

var player = e.getPlayer();
if (player != null) {
LoadedUpgrades.trySet(LOADED_UPGRADES.remove(server.getResourceManager()));
LoadedUpgrades.syncToClient(player);
} else {
LoadedUpgrades.trySet(LOADED_UPGRADES.remove(server.getResourceManager()));
// TODO: should maybe invalidate all cached filters?
LoadedUpgrades.syncToAllClients(e.getPlayerList());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.util.List;
import java.util.Map;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.item.Item;
import org.jetbrains.annotations.Nullable;

Expand All @@ -51,6 +53,10 @@ public static void syncToClient(ServerPlayer player) {
player.connection.send(new SetAttachmentUpgrades(holder));
}

public static void syncToAllClients(PlayerList playerList) {
playerList.broadcastAll(new ClientboundCustomPayloadPacket(new SetAttachmentUpgrades(holder)));
}

public final Map<Item, UpgradeType> map;
public final List<Item> list;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
package dev.technici4n.moderndynamics.client;

import dev.technici4n.moderndynamics.MdProxy;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;

public class ClientProxy extends MdProxy {
@Override
public boolean isShiftDown() {
return Screen.hasShiftDown();
}

@Override
public boolean isMemoryConnection() {
var listener = Minecraft.getInstance().getConnection();
return listener != null && listener.getConnection().isMemoryConnection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package dev.technici4n.moderndynamics.packets;

import dev.technici4n.moderndynamics.MdProxy;
import dev.technici4n.moderndynamics.attachment.upgrade.LoadedUpgrades;
import dev.technici4n.moderndynamics.util.MdId;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -44,14 +44,10 @@ public ResourceLocation id() {
}

public static final IPlayPayloadHandler<SetAttachmentUpgrades> HANDLER = (payload, context) -> {
context.player().ifPresent(player -> {
if (!(player instanceof LocalPlayer)) {
return;
}

context.workHandler().execute(() -> {
context.workHandler().execute(() -> {
if (!MdProxy.INSTANCE.isMemoryConnection()) {
LoadedUpgrades.trySet(payload.holder);
});
}
});
};

Expand Down

0 comments on commit 06b37d4

Please sign in to comment.