mirror of
https://github.com/Oth3r/Sit.git
synced 2025-09-20 00:13:21 +02:00
toggle keybinding
sit data to Data.java client events to Events.java
This commit is contained in:
parent
7d06735b3c
commit
ddf7703693
13 changed files with 294 additions and 124 deletions
|
@ -1,49 +1,15 @@
|
||||||
package one.oth3r.sit;
|
package one.oth3r.sit;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
import one.oth3r.sit.file.FileData;
|
||||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.server.command.CommandManager;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import one.oth3r.sit.file.Data;
|
|
||||||
import one.oth3r.sit.packet.SitPayloads;
|
|
||||||
import one.oth3r.sit.utl.Events;
|
import one.oth3r.sit.utl.Events;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class Sit implements ModInitializer {
|
public class Sit implements ModInitializer {
|
||||||
public static final String MOD_ID = "oth3r-sit";
|
|
||||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
|
||||||
public static String CONFIG_DIR = FabricLoader.getInstance().getConfigDir().toFile()+"/sit!/";
|
|
||||||
|
|
||||||
public static HashMap<ServerPlayerEntity, HashMap<String,String>> playerSettings = new HashMap<>();
|
|
||||||
public static final String ENTITY_NAME = "-sit!-entity-";
|
|
||||||
public static MinecraftServer server;
|
|
||||||
public static CommandManager commandManager;
|
|
||||||
|
|
||||||
public static boolean client = false;
|
|
||||||
public static boolean singleplayer = false;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
Data.loadFiles(true);
|
FileData.loadFiles(true);
|
||||||
Events.registerCommon();
|
Events.registerCommon();
|
||||||
|
|
||||||
//PACKETS
|
|
||||||
PayloadTypeRegistry.playC2S().register(SitPayloads.SettingsPayload.ID, SitPayloads.SettingsPayload.CODEC);
|
|
||||||
ServerPlayNetworking.registerGlobalReceiver(SitPayloads.SettingsPayload.ID,((payload, context) -> server.execute(() -> {
|
|
||||||
Type hashMapToken = new TypeToken<HashMap<String, Object>>() {}.getType();
|
|
||||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
|
||||||
playerSettings.put(context.player(),gson.fromJson(payload.value(),hashMapToken));
|
|
||||||
})));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,40 +1,16 @@
|
||||||
package one.oth3r.sit;
|
package one.oth3r.sit;
|
||||||
|
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
import one.oth3r.sit.utl.Data;
|
||||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
import one.oth3r.sit.utl.Events;
|
||||||
import one.oth3r.sit.file.Data;
|
|
||||||
import one.oth3r.sit.packet.SitPayloads;
|
|
||||||
import one.oth3r.sit.utl.Utl;
|
|
||||||
|
|
||||||
public class SitClient implements ClientModInitializer {
|
public class SitClient implements ClientModInitializer {
|
||||||
private static boolean IN_GAME = false;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitializeClient() {
|
public void onInitializeClient() {
|
||||||
Sit.client = true;
|
Data.setClient(true);
|
||||||
|
Events.registerClient();
|
||||||
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
|
|
||||||
IN_GAME = true;
|
|
||||||
if (client.isInSingleplayer()) Sit.singleplayer = true;
|
|
||||||
// send a data packet whenever joining a server
|
|
||||||
sendSettingsPackets();
|
|
||||||
});
|
|
||||||
|
|
||||||
// reset cashed things
|
|
||||||
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> {
|
|
||||||
IN_GAME = false;
|
|
||||||
if (Sit.singleplayer) {
|
|
||||||
// flip the single player switch
|
|
||||||
Sit.singleplayer = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sends the settings packets to the server
|
|
||||||
*/
|
|
||||||
public static void sendSettingsPackets() {
|
|
||||||
if (IN_GAME) ClientPlayNetworking.send(new SitPayloads.SettingsPayload(Utl.getGson().toJson(Data.getHandConfig())));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,8 @@ import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.text.TextColor;
|
import net.minecraft.text.TextColor;
|
||||||
import net.minecraft.util.Formatting;
|
import net.minecraft.util.Formatting;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import one.oth3r.sit.utl.Data;
|
||||||
import one.oth3r.sit.utl.Logic;
|
import one.oth3r.sit.utl.Logic;
|
||||||
import one.oth3r.sit.Sit;
|
|
||||||
import one.oth3r.sit.utl.Utl;
|
import one.oth3r.sit.utl.Utl;
|
||||||
import one.oth3r.sit.file.FileData;
|
import one.oth3r.sit.file.FileData;
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ public class SitCommand {
|
||||||
if (player == null) {
|
if (player == null) {
|
||||||
if (args[0].equalsIgnoreCase("reload")) {
|
if (args[0].equalsIgnoreCase("reload")) {
|
||||||
Logic.reload();
|
Logic.reload();
|
||||||
Sit.LOGGER.info(Utl.lang("msg.reloaded").getString());
|
Data.LOGGER.info(Utl.lang("msg.reloaded").getString());
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.google.gson.reflect.TypeToken;
|
||||||
import net.minecraft.text.MutableText;
|
import net.minecraft.text.MutableText;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import one.oth3r.sit.Sit;
|
import one.oth3r.sit.Sit;
|
||||||
|
import one.oth3r.sit.utl.Data;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
@ -98,7 +99,7 @@ public class LangReader {
|
||||||
// close the input stream
|
// close the input stream
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error(e.getMessage());
|
Data.LOGGER.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static String getLanguageValue(String key) {
|
public static String getLanguageValue(String key) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package one.oth3r.sit.file;
|
package one.oth3r.sit.file;
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
import one.oth3r.sit.Sit;
|
import one.oth3r.sit.utl.Data;
|
||||||
import one.oth3r.sit.utl.Utl;
|
import one.oth3r.sit.utl.Utl;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -127,7 +127,7 @@ public class ServerConfig {
|
||||||
|
|
||||||
|
|
||||||
public static File getFile() {
|
public static File getFile() {
|
||||||
return new File(Sit.CONFIG_DIR+"server-config.json");
|
return new File(Data.CONFIG_DIR+"server-config.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -139,14 +139,14 @@ public class ServerConfig {
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
// try to make the config directory
|
// try to make the config directory
|
||||||
try {
|
try {
|
||||||
Files.createDirectories(Paths.get(Sit.CONFIG_DIR));
|
Files.createDirectories(Paths.get(Data.CONFIG_DIR));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error("Failed to create config directory. Canceling all config loading...");
|
Data.LOGGER.error("Failed to create config directory. Canceling all config loading...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// if loading from legacy, try checking the old config directory for the file
|
// if loading from legacy, try checking the old config directory for the file
|
||||||
if (tryLegacy && Updater.ServerConfigFile.Legacy.getLegacyFile().exists()) {
|
if (tryLegacy && Updater.ServerConfigFile.Legacy.getLegacyFile().exists()) {
|
||||||
Sit.LOGGER.info("Updating Sit!.properties to sit!/config.json");
|
Data.LOGGER.info("Updating Sit!.properties to sit!/config.json");
|
||||||
Updater.ServerConfigFile.Legacy.run();
|
Updater.ServerConfigFile.Legacy.run();
|
||||||
}
|
}
|
||||||
save();
|
save();
|
||||||
|
@ -155,7 +155,7 @@ public class ServerConfig {
|
||||||
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
||||||
Updater.ServerConfigFile.run(reader);
|
Updater.ServerConfigFile.run(reader);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
|
Data.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
|
||||||
}
|
}
|
||||||
// save after loading
|
// save after loading
|
||||||
save();
|
save();
|
||||||
|
@ -166,12 +166,12 @@ public class ServerConfig {
|
||||||
*/
|
*/
|
||||||
public static void save() {
|
public static void save() {
|
||||||
if (!getFile().exists()) {
|
if (!getFile().exists()) {
|
||||||
Sit.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
|
Data.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
|
||||||
}
|
}
|
||||||
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
|
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
|
||||||
writer.write(Utl.getGson().toJson(FileData.getServerConfig()));
|
writer.write(Utl.getGson().toJson(FileData.getServerConfig()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.info(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
|
Data.LOGGER.info(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package one.oth3r.sit.file;
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
import net.minecraft.util.Hand;
|
import net.minecraft.util.Hand;
|
||||||
import one.oth3r.sit.Sit;
|
import one.oth3r.sit.utl.Data;
|
||||||
import one.oth3r.sit.utl.Utl;
|
import one.oth3r.sit.utl.Utl;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -48,10 +48,22 @@ public class SittingConfig {
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(Boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean canSitWithHand() {
|
public boolean canSitWithHand() {
|
||||||
return handSitting;
|
return handSitting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setHandSitting(Boolean handSitting) {
|
||||||
|
this.handSitting = handSitting;
|
||||||
|
}
|
||||||
|
|
||||||
public HandSetting getHand(Hand handType) {
|
public HandSetting getHand(Hand handType) {
|
||||||
return handType.equals(Hand.MAIN_HAND) ? mainHand : offHand;
|
return handType.equals(Hand.MAIN_HAND) ? mainHand : offHand;
|
||||||
}
|
}
|
||||||
|
@ -65,7 +77,7 @@ public class SittingConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File getFile() {
|
public static File getFile() {
|
||||||
return new File(Sit.CONFIG_DIR+"sitting-config.json");
|
return new File(Data.CONFIG_DIR+"sitting-config.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,7 +91,7 @@ public class SittingConfig {
|
||||||
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
||||||
Updater.SittingConfigFile.run(reader);
|
Updater.SittingConfigFile.run(reader);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
|
Data.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
|
||||||
}
|
}
|
||||||
// save after loading
|
// save after loading
|
||||||
save();
|
save();
|
||||||
|
@ -90,12 +102,12 @@ public class SittingConfig {
|
||||||
*/
|
*/
|
||||||
public static void save() {
|
public static void save() {
|
||||||
if (!getFile().exists()) {
|
if (!getFile().exists()) {
|
||||||
Sit.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
|
Data.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
|
||||||
}
|
}
|
||||||
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
|
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
|
||||||
writer.write(Utl.getGson().toJson(FileData.getSittingConfig()));
|
writer.write(Utl.getGson().toJson(FileData.getSittingConfig()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
|
Data.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import net.minecraft.util.Hand;
|
import net.minecraft.util.Hand;
|
||||||
import one.oth3r.sit.Sit;
|
import one.oth3r.sit.utl.Data;
|
||||||
import one.oth3r.sit.utl.Utl;
|
import one.oth3r.sit.utl.Utl;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -108,7 +108,7 @@ public class Updater {
|
||||||
*/
|
*/
|
||||||
public static File getLegacyFile() {
|
public static File getLegacyFile() {
|
||||||
// strip the new directory
|
// strip the new directory
|
||||||
return new File(Sit.CONFIG_DIR.substring(0,Sit.CONFIG_DIR.length()-5)+"Sit!.properties");
|
return new File(Data.CONFIG_DIR.substring(0, Data.CONFIG_DIR.length()-5)+"Sit!.properties");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -131,15 +131,15 @@ public class Updater {
|
||||||
loadVersion(properties,Double.parseDouble(ver));
|
loadVersion(properties,Double.parseDouble(ver));
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error("Error loading legacy config file: {}", e.getMessage());
|
Data.LOGGER.error("Error loading legacy config file: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete the old file
|
// delete the old file
|
||||||
try {
|
try {
|
||||||
Files.delete(file.toPath());
|
Files.delete(file.toPath());
|
||||||
Sit.LOGGER.info("Deleted " + file.getName());
|
Data.LOGGER.info("Deleted " + file.getName());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error("Failed to delete the old Sit! config.");
|
Data.LOGGER.error("Failed to delete the old Sit! config.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +278,7 @@ public class Updater {
|
||||||
ServerConfig.save();
|
ServerConfig.save();
|
||||||
SittingConfig.save();
|
SittingConfig.save();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Sit.LOGGER.error("Error loading legacy config: {}", e.getMessage());
|
Data.LOGGER.error("Error loading legacy config: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,16 @@ import net.minecraft.network.codec.PacketCodec;
|
||||||
import net.minecraft.network.codec.PacketCodecs;
|
import net.minecraft.network.codec.PacketCodecs;
|
||||||
import net.minecraft.network.packet.CustomPayload;
|
import net.minecraft.network.packet.CustomPayload;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import one.oth3r.sit.Sit;
|
import one.oth3r.sit.utl.Data;
|
||||||
|
|
||||||
public class SitPayloads {
|
public class SitPayloads {
|
||||||
|
/**
|
||||||
|
* the packet that the client sends to the server
|
||||||
|
* @param value the sitting settings for the client
|
||||||
|
*/
|
||||||
public record SettingsPayload(String value) implements CustomPayload {
|
public record SettingsPayload(String value) implements CustomPayload {
|
||||||
|
|
||||||
public static final Id<SettingsPayload> ID = new Id<>(Identifier.of(Sit.MOD_ID,"settings_v2.0"));
|
public static final Id<SettingsPayload> ID = new Id<>(Identifier.of(Data.MOD_ID,"settings_v2.0"));
|
||||||
|
|
||||||
public static final PacketCodec<RegistryByteBuf, SettingsPayload> CODEC = PacketCodecs.STRING.xmap(SettingsPayload::new, SettingsPayload::value).cast();
|
public static final PacketCodec<RegistryByteBuf, SettingsPayload> CODEC = PacketCodecs.STRING.xmap(SettingsPayload::new, SettingsPayload::value).cast();
|
||||||
|
|
||||||
|
@ -19,4 +23,21 @@ public class SitPayloads {
|
||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the packet that the server sends to the client when responding to the settings payload
|
||||||
|
*/
|
||||||
|
public record ResponsePayload(String value) implements CustomPayload {
|
||||||
|
|
||||||
|
public static final String VERSION = "response_v1.0";
|
||||||
|
|
||||||
|
public static final Id<ResponsePayload> ID = new Id<>(Identifier.of(Data.MOD_ID,VERSION));
|
||||||
|
|
||||||
|
public static final PacketCodec<RegistryByteBuf, ResponsePayload> CODEC = PacketCodecs.STRING.xmap(ResponsePayload::new, ResponsePayload::value).cast();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Id<ResponsePayload> getId() {
|
||||||
|
return ID;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
65
src/main/java/one/oth3r/sit/utl/Data.java
Normal file
65
src/main/java/one/oth3r/sit/utl/Data.java
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package one.oth3r.sit.utl;
|
||||||
|
|
||||||
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.server.command.CommandManager;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class Data {
|
||||||
|
public static final String MOD_ID = "sit-oth3r";
|
||||||
|
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||||
|
|
||||||
|
public static final String CONFIG_DIR = FabricLoader.getInstance().getConfigDir().toFile()+"/sit!/";
|
||||||
|
|
||||||
|
public static final String ENTITY_NAME = "-sit!-entity-";
|
||||||
|
|
||||||
|
// init on server load
|
||||||
|
private static MinecraftServer server;
|
||||||
|
|
||||||
|
public static MinecraftServer getServer() {
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setServer(MinecraftServer server) {
|
||||||
|
Data.server = server;
|
||||||
|
}
|
||||||
|
|
||||||
|
// client booleans
|
||||||
|
private static boolean client = false;
|
||||||
|
private static boolean inGame = false;
|
||||||
|
private static boolean singleplayer = false;
|
||||||
|
private static boolean supportedServer = false;
|
||||||
|
|
||||||
|
public static boolean isClient() {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setClient(boolean client) {
|
||||||
|
Data.client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isInGame() {
|
||||||
|
return inGame;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setInGame(boolean inGame) {
|
||||||
|
Data.inGame = inGame;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isSingleplayer() {
|
||||||
|
return singleplayer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setSingleplayer(boolean singleplayer) {
|
||||||
|
Data.singleplayer = singleplayer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isSupportedServer() {
|
||||||
|
return supportedServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setSupportedServer(boolean supportedServer) {
|
||||||
|
Data.supportedServer = supportedServer;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,43 +1,157 @@
|
||||||
package one.oth3r.sit.utl;
|
package one.oth3r.sit.utl;
|
||||||
|
|
||||||
|
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;
|
||||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
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.ServerLifecycleEvents;
|
||||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||||
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
|
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
|
||||||
|
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
||||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||||
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||||
|
import net.minecraft.client.network.ClientPlayerEntity;
|
||||||
|
import net.minecraft.client.option.KeyBinding;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.util.ActionResult;
|
||||||
import one.oth3r.sit.Sit;
|
import net.minecraft.util.Formatting;
|
||||||
import one.oth3r.sit.command.SitCommand;
|
import one.oth3r.sit.command.SitCommand;
|
||||||
import one.oth3r.sit.file.Data;
|
import one.oth3r.sit.file.FileData;
|
||||||
|
import one.oth3r.sit.file.SittingConfig;
|
||||||
|
import one.oth3r.sit.packet.SitPayloads;
|
||||||
|
import org.lwjgl.glfw.GLFW;
|
||||||
|
|
||||||
public class Events {
|
public class Events {
|
||||||
|
|
||||||
public static void playerConnections() {
|
private static class Keybindings {
|
||||||
|
private static KeyBinding toggle_key;
|
||||||
|
|
||||||
|
private static void register() {
|
||||||
|
toggle_key = KeyBindingHelper.registerKeyBinding(new KeyBinding(
|
||||||
|
"key.toggle",
|
||||||
|
GLFW.GLFW_KEY_UNKNOWN,
|
||||||
|
"category.sit"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void loopLogic(ClientPlayerEntity player) {
|
||||||
|
while (toggle_key.isPressed()) {
|
||||||
|
toggle_key.setPressed(false);
|
||||||
|
if (Data.isInGame()) {
|
||||||
|
if (Data.isSupportedServer()) {
|
||||||
|
// get the sitting config
|
||||||
|
SittingConfig config = FileData.getSittingConfig();
|
||||||
|
// toggle the setting
|
||||||
|
config.setEnabled(!config.getEnabled());
|
||||||
|
|
||||||
|
// set the sitting config to the new value
|
||||||
|
FileData.setSittingConfig(config);
|
||||||
|
// save the changes to the file
|
||||||
|
SittingConfig.save();
|
||||||
|
// send the changes to the server
|
||||||
|
Utl.sendSettingsPackets();
|
||||||
|
|
||||||
|
|
||||||
|
// get the message settings
|
||||||
|
String messageKey = "msg.sit_toggle."+(config.getEnabled()?"on":"off");
|
||||||
|
Formatting messageColor = config.getEnabled()?Formatting.GREEN:Formatting.RED;
|
||||||
|
|
||||||
|
// send the player the actionbar message
|
||||||
|
player.sendMessage(Utl.lang("msg.sit_toggle",
|
||||||
|
Utl.lang(messageKey).formatted(messageColor)), true);
|
||||||
|
} else {
|
||||||
|
player.sendMessage(Utl.lang("msg.sit_toggle.unsupported")
|
||||||
|
.formatted(Formatting.RED), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
ServerPlayNetworking.registerGlobalReceiver(SitPayloads.SettingsPayload.ID,((payload, context) -> Data.getServer().execute(() -> {
|
||||||
|
FileData.setPlayerSetting(context.player(),Utl.getGson().fromJson(payload.value(), SittingConfig.class));
|
||||||
|
// send the player back a packet for conformation
|
||||||
|
ServerPlayNetworking.send(context.player(),new SitPayloads.ResponsePayload(SitPayloads.ResponsePayload.VERSION));
|
||||||
|
})));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void client() {
|
||||||
|
ClientPlayNetworking.registerGlobalReceiver(SitPayloads.ResponsePayload.ID, ((payload, context) -> {
|
||||||
|
// only update when needed
|
||||||
|
if (!Data.isSupportedServer()) {
|
||||||
|
Data.setSupportedServer(true);
|
||||||
|
Data.LOGGER.info("Connected to Sit! server! packet: {}", payload.value());
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void clientMisc() {
|
||||||
|
// client tick loop
|
||||||
|
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||||
|
assert client.player != null;
|
||||||
|
Keybindings.loopLogic(client.player);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
||||||
// PLAYER JOIN
|
// PLAYER JOIN
|
||||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
||||||
Data.setPlayerSetting(handler.player,Data.getHandConfig());
|
FileData.setPlayerSetting(handler.player, FileData.getSittingConfig());
|
||||||
Data.setCheckPlayer(handler.player, 5);
|
FileData.setCheckPlayer(handler.player, 5);
|
||||||
});
|
});
|
||||||
|
|
||||||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
||||||
// if keep is off, remove the entity
|
// if keep is off, remove the entity
|
||||||
if (!Data.getServerConfig().isKeepActive()) {
|
if (!FileData.getServerConfig().isKeepActive()) {
|
||||||
Logic.removeEntity(handler.player);
|
Logic.removeEntity(handler.player);
|
||||||
}
|
}
|
||||||
Data.removePlayerSetting(handler.player);
|
FileData.removePlayerSetting(handler.player);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void server() {
|
/**
|
||||||
|
* registers all server lifecycle events
|
||||||
|
*/
|
||||||
|
private static void serverLifecycle() {
|
||||||
ServerLifecycleEvents.SERVER_STARTED.register(s -> {
|
ServerLifecycleEvents.SERVER_STARTED.register(s -> {
|
||||||
Sit.server = s;
|
Data.setServer(s);
|
||||||
Sit.commandManager = s.getCommandManager();
|
|
||||||
|
|
||||||
// right click on block event
|
// right click on block event
|
||||||
UseBlockCallback.EVENT.register((pl, world, hand, hitResult) -> {
|
UseBlockCallback.EVENT.register((pl, world, hand, hitResult) -> {
|
||||||
// get the server player
|
// get the server player
|
||||||
ServerPlayerEntity player = Sit.server.getPlayerManager().getPlayer(pl.getUuid());
|
ServerPlayerEntity player = Data.getServer().getPlayerManager().getPlayer(pl.getUuid());
|
||||||
|
|
||||||
// make sure the player isn't null, and make sure they aren't in spectator
|
// make sure the player isn't null, and make sure they aren't in spectator
|
||||||
if (player == null || player.isSpectator()) return ActionResult.PASS;
|
if (player == null || player.isSpectator()) return ActionResult.PASS;
|
||||||
|
@ -49,22 +163,31 @@ public class Events {
|
||||||
});
|
});
|
||||||
|
|
||||||
ServerLifecycleEvents.SERVER_STOPPED.register(s -> {
|
ServerLifecycleEvents.SERVER_STOPPED.register(s -> {
|
||||||
|
// clear the server
|
||||||
|
Data.setServer(null);
|
||||||
// clear all player settings (singleplayer and such)
|
// clear all player settings (singleplayer and such)
|
||||||
Data.clearPlayerSettings();
|
FileData.clearPlayerSettings();
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
public static void misc() {
|
// server loop setup
|
||||||
// loop setup
|
|
||||||
ServerTickEvents.END_SERVER_TICK.register(minecraftServer -> minecraftServer.execute(LoopManager::tick));
|
ServerTickEvents.END_SERVER_TICK.register(minecraftServer -> minecraftServer.execute(LoopManager::tick));
|
||||||
|
|
||||||
// command setup
|
// server command setup
|
||||||
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> SitCommand.register(dispatcher));
|
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> SitCommand.register(dispatcher));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// a one call method for the common and client
|
||||||
|
|
||||||
public static void registerCommon() {
|
public static void registerCommon() {
|
||||||
playerConnections();
|
playerConnections();
|
||||||
server();
|
serverLifecycle();
|
||||||
misc();
|
Packet.common();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void registerClient() {
|
||||||
|
Keybindings.register();
|
||||||
|
clientConnections();
|
||||||
|
clientMisc();
|
||||||
|
Packet.client();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package one.oth3r.sit.utl;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.decoration.DisplayEntity;
|
import net.minecraft.entity.decoration.DisplayEntity;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import one.oth3r.sit.Sit;
|
|
||||||
import one.oth3r.sit.file.FileData;
|
import one.oth3r.sit.file.FileData;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -41,7 +40,7 @@ public class LoopManager {
|
||||||
|
|
||||||
if (player.getVehicle() != null) {
|
if (player.getVehicle() != null) {
|
||||||
Entity entity = player.getVehicle();
|
Entity entity = player.getVehicle();
|
||||||
if (entity instanceof DisplayEntity.TextDisplayEntity tde && entity.getName().getString().equals(Sit.ENTITY_NAME)) {
|
if (entity instanceof DisplayEntity.TextDisplayEntity tde && entity.getName().getString().equals(Data.ENTITY_NAME)) {
|
||||||
// bind the entity to the player
|
// bind the entity to the player
|
||||||
FileData.addSitEntity(player, tde);
|
FileData.addSitEntity(player, tde);
|
||||||
// check if the player is still allowed to sit
|
// check if the player is still allowed to sit
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.google.gson.reflect.TypeToken;
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
import com.google.gson.stream.MalformedJsonException;
|
import com.google.gson.stream.MalformedJsonException;
|
||||||
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||||
import net.minecraft.block.*;
|
import net.minecraft.block.*;
|
||||||
import net.minecraft.block.enums.BlockHalf;
|
import net.minecraft.block.enums.BlockHalf;
|
||||||
import net.minecraft.block.enums.SlabType;
|
import net.minecraft.block.enums.SlabType;
|
||||||
|
@ -23,8 +24,8 @@ import net.minecraft.util.UseAction;
|
||||||
import net.minecraft.util.hit.BlockHitResult;
|
import net.minecraft.util.hit.BlockHitResult;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import one.oth3r.sit.Sit;
|
|
||||||
import one.oth3r.sit.file.*;
|
import one.oth3r.sit.file.*;
|
||||||
|
import one.oth3r.sit.packet.SitPayloads;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
@ -227,7 +228,7 @@ public class Utl {
|
||||||
public static DisplayEntity.TextDisplayEntity create(World world, BlockPos blockPos, double sitHeight) {
|
public static DisplayEntity.TextDisplayEntity create(World world, BlockPos blockPos, double sitHeight) {
|
||||||
DisplayEntity.TextDisplayEntity entity = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY,world);
|
DisplayEntity.TextDisplayEntity entity = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY,world);
|
||||||
|
|
||||||
entity.setCustomName(Text.of(Sit.ENTITY_NAME));
|
entity.setCustomName(Text.of(Data.ENTITY_NAME));
|
||||||
entity.setCustomNameVisible(false);
|
entity.setCustomNameVisible(false);
|
||||||
entity.setInvulnerable(true);
|
entity.setInvulnerable(true);
|
||||||
entity.setInvisible(true);
|
entity.setInvisible(true);
|
||||||
|
@ -275,7 +276,7 @@ public class Utl {
|
||||||
// get a list of sit entities
|
// get a list of sit entities
|
||||||
List<? extends DisplayEntity.TextDisplayEntity> list = player.getServerWorld()
|
List<? extends DisplayEntity.TextDisplayEntity> list = player.getServerWorld()
|
||||||
.getEntitiesByType(TypeFilter.instanceOf(DisplayEntity.TextDisplayEntity.class),
|
.getEntitiesByType(TypeFilter.instanceOf(DisplayEntity.TextDisplayEntity.class),
|
||||||
entity -> entity.getName().getString().equals(Sit.ENTITY_NAME));
|
entity -> entity.getName().getString().equals(Data.ENTITY_NAME));
|
||||||
|
|
||||||
// remove each one
|
// remove each one
|
||||||
for (DisplayEntity.TextDisplayEntity entity : list) {
|
for (DisplayEntity.TextDisplayEntity entity : list) {
|
||||||
|
@ -290,20 +291,14 @@ public class Utl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets a MutableText using the language key, if on server, using the custom lang reader
|
||||||
|
*/
|
||||||
public static MutableText lang(String key, Object... args) {
|
public static MutableText lang(String key, Object... args) {
|
||||||
if (Sit.client) return Text.translatable(key, args);
|
if (Data.isClient()) return Text.translatable(key, args);
|
||||||
else return LangReader.of(key, args).getTxT();
|
else return LangReader.of(key, args).getTxT();
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static class ConfigExamples {
|
|
||||||
// public static final String CUSTOM_BLOCKS = "\"minecraft:campfire|0.255|1|lit=false\"";
|
|
||||||
// public static final String REQUIREMENT_OPTIONS = String.format("%s, %s, %s",
|
|
||||||
// configFile.HandRequirement.empty,configFile.HandRequirement.restrictive,configFile.HandRequirement.none);
|
|
||||||
// public static final String LIST = "\"minecraft:torch\"";
|
|
||||||
// }
|
|
||||||
|
|
||||||
public static class Enum {
|
public static class Enum {
|
||||||
|
|
||||||
public static <T extends java.lang.Enum<T>> T get(Object enumString, Class<T> enumType) {
|
public static <T extends java.lang.Enum<T>> T get(Object enumString, Class<T> enumType) {
|
||||||
|
@ -327,6 +322,16 @@ public class Utl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo call when editing a config on the client
|
||||||
|
/**
|
||||||
|
* sends the settings packets to the server, if client & in game
|
||||||
|
*/
|
||||||
|
public static void sendSettingsPackets() {
|
||||||
|
if (Data.isClient() && Data.isInGame()) {
|
||||||
|
ClientPlayNetworking.send(new SitPayloads.SettingsPayload(Utl.getGson().toJson(FileData.getSittingConfig())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets a Gson with the LenientTypeAdapter
|
* gets a Gson with the LenientTypeAdapter
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -45,8 +45,10 @@
|
||||||
"config.hand.restriction.blacklist": "Blacklist",
|
"config.hand.restriction.blacklist": "Blacklist",
|
||||||
"config.example": "Example: %s",
|
"config.example": "Example: %s",
|
||||||
|
|
||||||
"msg.sit_toggle.on": "Enabled Sitting!",
|
"msg.sit_toggle": "%s Sitting!",
|
||||||
"msg.sit_toggle.off": "Disabled Sitting!",
|
"msg.sit_toggle.on": "Enabled",
|
||||||
|
"msg.sit_toggle.off": "Disabled",
|
||||||
|
"msg.sit_toggle.unsupported": "Sitting is not available on this server.",
|
||||||
"msg.reloaded": "Reloaded the config!",
|
"msg.reloaded": "Reloaded the config!",
|
||||||
"msg.purged": "Purged all active chair entities!",
|
"msg.purged": "Purged all active chair entities!",
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue