2024-07-11 13:43:54 -05:00
|
|
|
package one.oth3r.sit.utl;
|
|
|
|
|
2025-03-10 10:12:10 -05:00
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
|
|
import com.mojang.brigadier.ParseResults;
|
|
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
2024-07-23 13:49:43 -05:00
|
|
|
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.ClientPlayConnectionEvents;
|
|
|
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
2024-07-11 13:43:54 -05:00
|
|
|
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
|
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
|
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
|
|
|
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
|
2024-07-23 13:49:43 -05:00
|
|
|
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
2024-07-11 13:43:54 -05:00
|
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
2024-07-23 13:49:43 -05:00
|
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
2024-09-29 11:36:31 -05:00
|
|
|
import net.minecraft.client.MinecraftClient;
|
2024-07-23 13:49:43 -05:00
|
|
|
import net.minecraft.client.network.ClientPlayerEntity;
|
|
|
|
import net.minecraft.client.option.KeyBinding;
|
2025-03-10 10:12:10 -05:00
|
|
|
import net.minecraft.server.command.ServerCommandSource;
|
2024-07-11 13:43:54 -05:00
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
|
|
import net.minecraft.util.ActionResult;
|
2025-05-04 17:43:37 -05:00
|
|
|
import one.oth3r.sit.SitClient;
|
2024-07-11 13:43:54 -05:00
|
|
|
import one.oth3r.sit.command.SitCommand;
|
2024-07-23 13:49:43 -05:00
|
|
|
import one.oth3r.sit.file.FileData;
|
2024-07-29 10:42:38 -05:00
|
|
|
import one.oth3r.sit.file.LangReader;
|
2024-07-23 13:49:43 -05:00
|
|
|
import one.oth3r.sit.file.SittingConfig;
|
|
|
|
import one.oth3r.sit.packet.SitPayloads;
|
|
|
|
import org.lwjgl.glfw.GLFW;
|
2024-07-11 13:43:54 -05:00
|
|
|
|
2025-05-03 15:41:16 -05:00
|
|
|
import java.awt.*;
|
|
|
|
|
2024-07-11 13:43:54 -05:00
|
|
|
public class Events {
|
|
|
|
|
2024-07-23 13:49:43 -05:00
|
|
|
private static class Keybindings {
|
|
|
|
private static KeyBinding toggle_key;
|
2024-08-06 11:10:45 -05:00
|
|
|
private static KeyBinding sit_key;
|
2024-09-29 11:36:31 -05:00
|
|
|
private static KeyBinding config__key;
|
2024-07-23 13:49:43 -05:00
|
|
|
|
|
|
|
private static void register() {
|
|
|
|
toggle_key = KeyBindingHelper.registerKeyBinding(new KeyBinding(
|
2024-10-03 15:42:51 -05:00
|
|
|
"key.sit!.toggle",
|
2024-07-23 13:49:43 -05:00
|
|
|
GLFW.GLFW_KEY_UNKNOWN,
|
2024-10-03 15:42:51 -05:00
|
|
|
"category.sit!"
|
2024-07-23 13:49:43 -05:00
|
|
|
));
|
2024-08-06 11:10:45 -05:00
|
|
|
sit_key = KeyBindingHelper.registerKeyBinding(new KeyBinding(
|
2024-10-03 15:42:51 -05:00
|
|
|
"key.sit!.sit",
|
2024-08-06 11:10:45 -05:00
|
|
|
GLFW.GLFW_KEY_UNKNOWN,
|
2024-10-03 15:42:51 -05:00
|
|
|
"category.sit!"
|
2024-08-06 11:10:45 -05:00
|
|
|
));
|
2024-09-29 11:36:31 -05:00
|
|
|
config__key = KeyBindingHelper.registerKeyBinding(new KeyBinding(
|
2024-10-03 15:42:51 -05:00
|
|
|
"key.sit!.config",
|
2024-09-29 11:36:31 -05:00
|
|
|
GLFW.GLFW_KEY_UNKNOWN,
|
2024-10-03 15:42:51 -05:00
|
|
|
"category.sit!"
|
2024-09-29 11:36:31 -05:00
|
|
|
));
|
2024-07-23 13:49:43 -05:00
|
|
|
}
|
|
|
|
|
2024-09-29 11:36:31 -05:00
|
|
|
private static void loopLogic(MinecraftClient client) {
|
|
|
|
ClientPlayerEntity player = client.player;
|
|
|
|
|
|
|
|
while (config__key.wasPressed()) {
|
2025-05-04 17:43:37 -05:00
|
|
|
client.setScreen(SitClient.getConfigScreen(client.currentScreen));
|
2024-09-29 11:36:31 -05:00
|
|
|
}
|
|
|
|
|
2024-10-19 17:54:35 -05:00
|
|
|
/// anything below uses the player object, make sure it's not null
|
2024-09-29 11:36:31 -05:00
|
|
|
if (player == null) return;
|
|
|
|
|
2024-07-27 14:57:59 -05:00
|
|
|
while (toggle_key.wasPressed()) {
|
2024-07-23 13:49:43 -05:00
|
|
|
if (Data.isInGame()) {
|
2024-09-29 11:36:18 -05:00
|
|
|
player.sendMessage(Logic.toggleSiting(), true);
|
2024-08-06 11:10:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sit_key.wasPressed()) {
|
|
|
|
// just send the sit command
|
|
|
|
if (Data.isInGame()) {
|
|
|
|
if (Data.isSupportedServer()) {
|
|
|
|
player.networkHandler.sendCommand("sit");
|
|
|
|
} else {
|
2024-07-26 09:42:05 -05:00
|
|
|
// unsupported server message if not in a Sit! server
|
2024-10-03 15:42:51 -05:00
|
|
|
player.sendMessage(Utl.lang("sit!.chat.unsupported")
|
2025-05-03 15:41:16 -05:00
|
|
|
.color(Color.RED).b(), true);
|
2024-07-23 13:49:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Packet {
|
|
|
|
private static void common() {
|
|
|
|
// register the data
|
|
|
|
PayloadTypeRegistry.playC2S().register(SitPayloads.SettingsPayload.ID, SitPayloads.SettingsPayload.CODEC);
|
|
|
|
|
|
|
|
PayloadTypeRegistry.playS2C().register(SitPayloads.ResponsePayload.ID, SitPayloads.ResponsePayload.CODEC);
|
|
|
|
|
|
|
|
// server receiver is common
|
2024-10-01 11:27:06 -05:00
|
|
|
|
|
|
|
/// receiving the sitting setting payload
|
2024-07-23 13:49:43 -05:00
|
|
|
ServerPlayNetworking.registerGlobalReceiver(SitPayloads.SettingsPayload.ID,((payload, context) -> Data.getServer().execute(() -> {
|
2024-10-01 11:27:06 -05:00
|
|
|
// save the setting on the server for that player
|
2024-07-23 13:49:43 -05:00
|
|
|
FileData.setPlayerSetting(context.player(),Utl.getGson().fromJson(payload.value(), SittingConfig.class));
|
2024-10-01 11:27:06 -05:00
|
|
|
|
|
|
|
// send the player back a response packet for confirmation
|
2024-07-23 13:49:43 -05:00
|
|
|
ServerPlayNetworking.send(context.player(),new SitPayloads.ResponsePayload(SitPayloads.ResponsePayload.VERSION));
|
2024-10-01 11:27:06 -05:00
|
|
|
|
|
|
|
// log the receiving of the packet from the player
|
2025-05-03 15:41:16 -05:00
|
|
|
Data.LOGGER.info(Utl.lang("sit!.console.player_settings",context.player().getName().getString()).toString());
|
2024-07-23 13:49:43 -05:00
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void client() {
|
2024-10-01 11:27:06 -05:00
|
|
|
/// receiving the response packet from the server
|
2024-07-23 13:49:43 -05:00
|
|
|
ClientPlayNetworking.registerGlobalReceiver(SitPayloads.ResponsePayload.ID, ((payload, context) -> {
|
|
|
|
// only update when needed
|
|
|
|
if (!Data.isSupportedServer()) {
|
|
|
|
Data.setSupportedServer(true);
|
2025-05-03 15:41:16 -05:00
|
|
|
Data.LOGGER.info(Utl.lang("sit!.console.connected",payload.value()).toString());
|
2024-07-23 13:49:43 -05:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void clientMisc() {
|
|
|
|
// client tick loop
|
|
|
|
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
|
|
|
assert client.player != null;
|
2024-09-29 11:36:31 -05:00
|
|
|
Keybindings.loopLogic(client);
|
2024-07-23 13:49:43 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* registers all client connection code
|
|
|
|
*/
|
|
|
|
private static void clientConnections() {
|
|
|
|
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
|
|
|
|
Data.setInGame(true);
|
|
|
|
if (client.isInSingleplayer()) Data.setSingleplayer(true);
|
|
|
|
// send a data packet whenever joining a server
|
|
|
|
Utl.sendSettingsPackets();
|
|
|
|
});
|
|
|
|
|
|
|
|
// reset cashed things on disconnect
|
|
|
|
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> {
|
|
|
|
Data.setInGame(false);
|
|
|
|
Data.setSingleplayer(false);
|
|
|
|
Data.setSupportedServer(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* registers all common server player connection code
|
|
|
|
*/
|
|
|
|
private static void playerConnections() {
|
2024-07-11 13:43:54 -05:00
|
|
|
// PLAYER JOIN
|
|
|
|
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
2024-07-23 13:49:43 -05:00
|
|
|
FileData.setPlayerSetting(handler.player, FileData.getSittingConfig());
|
2024-08-17 13:27:26 -05:00
|
|
|
Data.setCheckPlayer(handler.player, 5);
|
2024-07-11 13:43:54 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
|
|
|
// if keep is off, remove the entity
|
2024-07-23 13:49:43 -05:00
|
|
|
if (!FileData.getServerConfig().isKeepActive()) {
|
2024-07-11 13:43:54 -05:00
|
|
|
Logic.removeEntity(handler.player);
|
|
|
|
}
|
2024-07-23 13:49:43 -05:00
|
|
|
FileData.removePlayerSetting(handler.player);
|
2024-07-11 13:43:54 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-23 13:49:43 -05:00
|
|
|
/**
|
|
|
|
* registers all server lifecycle events
|
|
|
|
*/
|
|
|
|
private static void serverLifecycle() {
|
2024-07-11 13:43:54 -05:00
|
|
|
ServerLifecycleEvents.SERVER_STARTED.register(s -> {
|
2024-07-23 13:49:43 -05:00
|
|
|
Data.setServer(s);
|
2024-07-29 10:42:38 -05:00
|
|
|
LangReader.loadLanguageFile();
|
2024-07-11 13:43:54 -05:00
|
|
|
|
|
|
|
// right click on block event
|
|
|
|
UseBlockCallback.EVENT.register((pl, world, hand, hitResult) -> {
|
2024-08-03 15:15:57 -05:00
|
|
|
if (Data.isClient() && !Data.isSingleplayer()) return ActionResult.PASS;
|
2024-07-11 13:43:54 -05:00
|
|
|
// get the server player
|
2024-07-23 13:49:43 -05:00
|
|
|
ServerPlayerEntity player = Data.getServer().getPlayerManager().getPlayer(pl.getUuid());
|
2024-07-11 13:43:54 -05:00
|
|
|
|
|
|
|
// make sure the player isn't null, and make sure they aren't in spectator
|
|
|
|
if (player == null || player.isSpectator()) return ActionResult.PASS;
|
|
|
|
|
|
|
|
// consume if sitting, if not pass
|
2025-03-10 10:12:10 -05:00
|
|
|
ActionResult result = Logic.canSit(player,hitResult.getBlockPos(),hitResult) ? ActionResult.CONSUME : ActionResult.PASS;
|
|
|
|
// todo test
|
|
|
|
if (result.equals(ActionResult.CONSUME)) {
|
|
|
|
try {
|
|
|
|
CommandDispatcher<ServerCommandSource> dispatcher = Data.getServer().getCommandSource().getDispatcher();
|
2025-03-12 17:38:58 -05:00
|
|
|
ParseResults<ServerCommandSource> parse = dispatcher.parse("sit", player.getCommandSource());
|
2025-03-10 10:12:10 -05:00
|
|
|
dispatcher.execute(parse);
|
|
|
|
} catch (CommandSyntaxException e) {
|
|
|
|
Data.LOGGER.error("Error executing sit command for player {}", player.getName().getString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-07-11 13:43:54 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
ServerLifecycleEvents.SERVER_STOPPED.register(s -> {
|
2024-07-23 13:49:43 -05:00
|
|
|
// clear the server
|
|
|
|
Data.setServer(null);
|
2024-07-11 13:43:54 -05:00
|
|
|
// clear all player settings (singleplayer and such)
|
2024-07-23 13:49:43 -05:00
|
|
|
FileData.clearPlayerSettings();
|
2024-07-11 13:43:54 -05:00
|
|
|
});
|
|
|
|
|
2024-07-23 13:49:43 -05:00
|
|
|
// server loop setup
|
2024-07-11 13:43:54 -05:00
|
|
|
ServerTickEvents.END_SERVER_TICK.register(minecraftServer -> minecraftServer.execute(LoopManager::tick));
|
|
|
|
|
2024-07-23 13:49:43 -05:00
|
|
|
// server command setup
|
2024-07-11 13:43:54 -05:00
|
|
|
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> SitCommand.register(dispatcher));
|
|
|
|
}
|
|
|
|
|
2024-07-23 13:49:43 -05:00
|
|
|
// a one call method for the common and client
|
|
|
|
|
2024-07-11 13:43:54 -05:00
|
|
|
public static void registerCommon() {
|
|
|
|
playerConnections();
|
2024-07-23 13:49:43 -05:00
|
|
|
serverLifecycle();
|
|
|
|
Packet.common();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerClient() {
|
|
|
|
Keybindings.register();
|
|
|
|
clientConnections();
|
|
|
|
clientMisc();
|
|
|
|
Packet.client();
|
2024-07-11 13:43:54 -05:00
|
|
|
}
|
|
|
|
}
|