Send int value from server to player during the loading screen. #1609
-
I'm currently working on a simple mod that features a config for user to play around with. I basically want the client to use the value received from the server through packets in the mixins, else use the value from the config. Below is the code that I've tried, but the only result from those is a crash log, despite the server not actually crashing. public class ForgingBeyond implements ModInitializer {
public static final String MOD_NAME = "Forging Beyond";
@Override
public void onInitialize() {
ContentProvider.getConfig();
ServerPlayNetworking.registerGlobalReceiver(new Identifier("forging-beyond", "anvil-limit"), (server, playerEntity, handler, buf, responseSender) -> {
PacketByteBuf sBuf = PacketByteBufs.create();
sBuf.writeInt(ContentProvider.getCustomIntLimit());
Identifier identifier = new Identifier("forging-beyond", "limit-int");
for (ServerPlayerEntity player : PlayerLookup.all(server)) {
if (ServerPlayNetworking.canSend(player, identifier)) {
ServerPlayNetworking.send(player, identifier, sBuf);
}
}
});
LogManager.getLogger(MOD_NAME).info("Starting " + MOD_NAME + " mod!");
LogManager.getLogger(MOD_NAME).info("This mod uses SimpleConfig by magistermaks @ GitHub!");
}
} public class ForgingBeyondClient implements ClientModInitializer {
private static int ANVIL_LIMIT_INT;
@Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(new Identifier("forging-beyond", "limit-int"), (client, handler, buf, responseSender) -> {
System.out.println(buf.readInt());
World world = client.world;
if (world != null) {
if (!world.isClient()) {
setAnvilLimitInt(
buf.readInt()
);
}
else {
setAnvilLimitInt(ContentProvider.getCustomIntLimit());
}
}
});
}
private static void setAnvilLimitInt(int limitInt) {
ANVIL_LIMIT_INT = limitInt;
}
public static int getAnvilLimitInt() {
return ANVIL_LIMIT_INT;
}
} What could I be doing wrong here? @Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(new Identifier("forging-beyond", "limit-int"), (client, handler, buf, responseSender) -> {
int packetInt = buf.readInt();
System.out.println(packetInt);
setAnvilLimitInt(packetInt);
});
} Here's also the mixin classes for context: @Mixin(AnvilScreen.class)
public class AnvilScreenMixin extends ForgingScreen<AnvilScreenHandler> {
public AnvilScreenMixin(AnvilScreenHandler handler, PlayerInventory playerInventory, Text title, Identifier texture) {
super(handler, playerInventory, title, texture);
}
@ModifyConstant(method = "drawForeground", constant = @Constant(intValue = 40))
private int customLimitInt(int i) {
return ForgingBeyondClient.getAnvilLimitInt();
}
} @Mixin(AnvilScreenHandler.class)
public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler {
public AnvilScreenHandlerMixin(@Nullable ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, ScreenHandlerContext context) {
super(type, syncId, playerInventory, context);
}
@ModifyConstant(method = "updateResult", constant = @Constant(intValue = 40))
private int customLimitInt(int i) {
int h = ForgingBeyondClient.getAnvilLimitInt();
return h;
}
@ModifyConstant(method = "updateResult", constant = @Constant(intValue = 39))
private int customMaxInt(int i) {
return ForgingBeyondClient.getAnvilLimitInt() - 1;
}
} Any tips or suggestions would be really appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
This method is being called on the server, but you are trying to use a class that looks like it is client specific from the name.
|
Beta Was this translation helpful? Give feedback.
-
Maybe you want to put your mixin in the "client" part of the mixin config?
|
Beta Was this translation helpful? Give feedback.
-
Do you mean it still shows the classloading exception? On the networking issue. From what I understand of what you posted you have: ServerSide:
ClientSide:
But what actually sends a message that would make either do anything in the first place? You also have:
Does this make sense? |
Beta Was this translation helpful? Give feedback.
-
Why are using a static buffer? You are supposed to create one for each request (its not thread safe). Also, the buffer is likely empty anyway since you don't show anything that would cause the receiver on the server to be called.
Why do you have a receiver on the server if the client doesn't send anything? |
Beta Was this translation helpful? Give feedback.
-
This is just registering a callback. The code in the lamba doesn't run until you get a message from the client.
Nope. |
Beta Was this translation helpful? Give feedback.
Why are using a static buffer? You are supposed to create one for each request (its not thread safe).
Also, the buffer is likely empty anyway since you don't show anything that would cause the receiver on the server to be called.
Why do you have a receiver on the server if the client doesn't send anything?