diff --git a/README.md b/README.md index 2dc0f0a..4f01e02 100644 --- a/README.md +++ b/README.md @@ -7,17 +7,23 @@ Sit on **stairs**, **slabs**, **carpets** by default, and sit on everything else using the config! You can also customize hand restrictions to stop accidentally sitting down! The mod also has full Geyser support! Enjoy sitting with everyone, weather they are on Bedrock or Java! + ### Where to install? -**Sit!** works on the server (singleplayer included), with support for clients that join in to use their own settings to sit. +**Sit!** works on the server (singleplayer included), also with support for clients that join in to use their own settings to sit. + ## Help localize Sit! on [Crowdin](https://crowdin.com/project/oth3r-sit)! ![overview](https://github.com/Oth3r/Sit/blob/master/media/overview.gif?raw=true) + ## Check out my other Projects! +[![Caligo badge](https://github.com/Oth3r/Caligo/blob/master/media/promo_badge.png?raw=true)](https://modrinth.com/mod/caligo) [![DirectionHUD badge](https://github.com/Oth3r/DirectionHUD/blob/master/media/mod-badge.png?raw=true)](https://modrinth.com/mod/directionhud) [![DirectionHUD Spigot badge](https://github.com/Oth3r/DirectionHUD/blob/master/media/plugin-badge.png?raw=true)](https://modrinth.com/plugin/directionhud-plugin) # Features + ### Hand Restrictions Don't want to accidentally sit down? Set custom restrictions for each hand in the config! + * Per player hand restrictions when connecting to a `Sit!` server on a `Sit!` Client! ![hand restrictions](https://github.com/Oth3r/Sit/blob/master/media/hand-restrictions.gif?raw=true) @@ -26,6 +32,7 @@ Don't want to accidentally sit down? Set custom restrictions for each hand in th Want to sit on _**EVERY**_ block? With the config you can add more options to sit on! Custom block states are also supported. ![custom blocks](https://github.com/Oth3r/Sit/blob/master/media/custom-blocks.gif?raw=true) + ### Customizable Config Configure to your hearts desire with the in-game config with **[ModMenu](https://modrinth.com/mod/modmenu)** & **[YetAnotherConfigLib](https://modrinth.com/mod/yacl)**, or use the provided config file for servers! @@ -35,3 +42,5 @@ Configure to your hearts desire with the in-game config with **[ModMenu](https:/ ## Future Goals * Forge Port (probably NeoForge 1.21) * Custom dismounting logic + * better config (coming soon!) + * keybindings (next update) diff --git a/gradle.properties b/gradle.properties index 2076609..15501fd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10 loader_version=0.14.24 # Mod Properties -mod_version=1.1.3+1.20-1.20.1 +mod_version=1.1.4+1.20-1.20.1 maven_group=one.oth3r archives_base_name=sit! diff --git a/src/main/java/one/oth3r/sit/Events.java b/src/main/java/one/oth3r/sit/Events.java index 444e023..a3f5afb 100644 --- a/src/main/java/one/oth3r/sit/Events.java +++ b/src/main/java/one/oth3r/sit/Events.java @@ -23,7 +23,8 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Box; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; -import one.oth3r.sit.Utl.HandType; +import one.oth3r.sit.Utl.HandSettings.HandType; +import one.oth3r.sit.file.Config; import java.util.*; @@ -38,25 +39,25 @@ public class Events { ArrayList notUsable = new ArrayList<>(food); notUsable.add(UseAction.NONE); HashMap itemMap = new HashMap<>(); - itemMap.put(HandType.main,player.getMainHandStack()); - itemMap.put(HandType.off,player.getOffHandStack()); + itemMap.put(Utl.HandSettings.HandType.main,player.getMainHandStack()); + itemMap.put(Utl.HandSettings.HandType.off,player.getOffHandStack()); // if sneaking cant sit if (player.isSneaking()) return false; // for both hands - for (HandType type:HandType.values()) { + for (HandType type: Utl.HandSettings.HandType.values()) { ItemStack targetStack = itemMap.get(type); // if req is empty and the item isn't empty, false - if (Utl.getReq(player,type).equals(config.HandRequirement.empty) && !targetStack.isEmpty()) return false; + if (Utl.HandSettings.getReq(player,type).equals(Config.HandRequirement.empty) && !targetStack.isEmpty()) return false; // if req is restrictive - if (Utl.getReq(player,type).equals(config.HandRequirement.restrictive)) { + if (Utl.HandSettings.getReq(player,type).equals(Config.HandRequirement.restrictive)) { // if item is in blacklist, false - if (checkList(Utl.getList(player,type,"blacklist"),targetStack)) return false; + if (checkList(Utl.HandSettings.getList(player,type,"blacklist"),targetStack)) return false; // if item is NOT in whitelist - if (!checkList(Utl.getList(player,type,"whitelist"),targetStack)) { + if (!checkList(Utl.HandSettings.getList(player,type,"whitelist"),targetStack)) { // if block is restricted and items is block, false, ect - if (Utl.getBool(player,type,"block") && (targetStack.getItem() instanceof BlockItem)) return false; - if (Utl.getBool(player,type,"food") && food.contains(targetStack.getUseAction())) return false; - if (Utl.getBool(player,type,"usable") && !notUsable.contains(targetStack.getUseAction())) return false; + if (Utl.HandSettings.getBool(player,type,"block") && (targetStack.getItem() instanceof BlockItem)) return false; + if (Utl.HandSettings.getBool(player,type,"food") && food.contains(targetStack.getUseAction())) return false; + if (Utl.HandSettings.getBool(player,type,"usable") && !notUsable.contains(targetStack.getUseAction())) return false; } } } @@ -72,7 +73,7 @@ public class Events { // get a hashmap of custom blocks HashMap> map = new HashMap<>(); int i = 1; - for (String s:config.customBlocks) { + for (String s: Config.customBlocks) { String[] split = s.split("\\|"); HashMap data = new HashMap<>(); data.put("block",split[0]); @@ -100,12 +101,12 @@ public class Events { for (Entity entity:entities.values()) if (entity.getBlockPos().equals(pos) || entity.getBlockPos().add(0,1,0).equals(pos)) return false; // return for the 4 default types - if (block instanceof StairsBlock && config.stairsOn) return blockState.get(StairsBlock.HALF) == BlockHalf.BOTTOM; - if (block instanceof SlabBlock && config.slabsOn) return blockState.get(SlabBlock.TYPE) == SlabType.BOTTOM; - if (block instanceof CarpetBlock && config.carpetsOn) return true; - if (blockState.isFullCube(world,pos.add(0,1,0)) && config.fullBlocksOn) return true; + if (block instanceof StairsBlock && Config.stairsOn) return blockState.get(StairsBlock.HALF) == BlockHalf.BOTTOM; + if (block instanceof SlabBlock && Config.slabsOn) return blockState.get(SlabBlock.TYPE) == SlabType.BOTTOM; + if (block instanceof CarpetBlock && Config.carpetsOn) return true; + if (blockState.isFullCube(world,pos.add(0,1,0)) && Config.fullBlocksOn) return true; // custom checker - if (config.customOn && config.customBlocks.size() != 0) { + if (Config.customOn && Config.customBlocks.size() != 0) { for (HashMap map:getCustomBlocks().values()) { String blockID = Registries.BLOCK.getId(block).toString(); if (map.get("block").equals(blockID)) { @@ -149,7 +150,7 @@ public class Events { entity.updatePositionAndAngles(pos.getX() + 0.5, pos.getY()+.78, pos.getZ() + 0.5, 0, 0); hitBoxY = 2; } - if (config.customOn && config.customBlocks.size() != 0) { + if (Config.customOn && !Config.customBlocks.isEmpty()) { for (HashMap map:getCustomBlocks().values()) { String blockID = Registries.BLOCK.getId(block).toString(); if (map.get("block").equals(blockID)) { @@ -165,6 +166,24 @@ public class Events { if (entity.getY() <= pos.getY()+.35) entity.setPitch(90); // below else entity.setPitch(-90); // above } + public static boolean sit(ServerPlayerEntity player, BlockPos pos) { + // todo interactions entity to make the sitting hitbox? + World world = player.getWorld(); + DisplayEntity.TextDisplayEntity entity = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY,player.getServerWorld()); + setEntity(pos,world,entity); + if (checkBlocks(pos,world,isAboveBlockheight(entity))) { + if (entities.containsKey(player)) { + if (!Config.sitWhileSeated) return false; + entities.get(player).setRemoved(Entity.RemovalReason.DISCARDED); + entities.remove(player); + } + player.getServerWorld().spawnEntity(entity); + player.startRiding(entity); + entities.put(player,entity); + return true; + } + return false; + } public static void register() { ServerTickEvents.END_SERVER_TICK.register(minecraftServer -> minecraftServer.execute(Events::cleanUp)); // PLAYER JOIN @@ -172,12 +191,12 @@ public class Events { ServerPlayerEntity player = handler.player; checkPlayers.put(player,2); // put server settings in the player settings - Sit.playerSettings.put(player,Utl.getHandSettings()); + Sit.playerSettings.put(player, Utl.HandSettings.getHandSettings()); }); ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { ServerPlayerEntity player = handler.player; if (entities.containsKey(player)) { - if (!config.keepActive) { + if (!Config.keepActive) { player.dismountVehicle(); entities.get(player).setRemoved(Entity.RemovalReason.DISCARDED); } @@ -194,22 +213,12 @@ public class Events { if (player == null) return ActionResult.PASS; if (hand == net.minecraft.util.Hand.MAIN_HAND && hitResult.getType() == HitResult.Type.BLOCK) { BlockPos pos = hitResult.getBlockPos(); + // check the players hands if (!checkLogic(player)) return ActionResult.PASS; - // todo interactions entity to make the hitbox? - // make the entity first before checking to make sure the blocks around are fine - DisplayEntity.TextDisplayEntity entity = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY,player.getServerWorld()); - setEntity(pos,world,entity); - if (checkBlocks(pos,world,isAboveBlockheight(entity))) { - if (entities.containsKey(player)) { - if (!config.sitWhileSeated) return ActionResult.PASS; - entities.get(player).setRemoved(Entity.RemovalReason.DISCARDED); - entities.remove(player); - } - player.getServerWorld().spawnEntity(entity); - player.startRiding(entity); - entities.put(player,entity); - return ActionResult.FAIL; - } + // make the player sit + boolean status = sit(player,pos); + // if sat, cancel / FAIL the use block event + if (status) return ActionResult.FAIL; } return ActionResult.PASS; }); diff --git a/src/main/java/one/oth3r/sit/LangReader.java b/src/main/java/one/oth3r/sit/LangReader.java index 0e3cdbd..2f46105 100644 --- a/src/main/java/one/oth3r/sit/LangReader.java +++ b/src/main/java/one/oth3r/sit/LangReader.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import net.minecraft.text.MutableText; import net.minecraft.text.Text; +import one.oth3r.sit.file.Config; import java.io.InputStream; import java.io.InputStreamReader; @@ -77,10 +78,10 @@ public class LangReader { public static void loadLanguageFile() { try { ClassLoader classLoader = Sit.class.getClassLoader(); - InputStream inputStream = classLoader.getResourceAsStream("assets/sit/lang/"+ config.lang+".json"); + InputStream inputStream = classLoader.getResourceAsStream("assets/sit/lang/"+ Config.lang+".json"); if (inputStream == null) { - inputStream = classLoader.getResourceAsStream("assets/sit/lang/"+config.defaults.lang+".json"); - config.lang = config.defaults.lang; + inputStream = classLoader.getResourceAsStream("assets/sit/lang/"+ Config.defaults.lang+".json"); + Config.lang = Config.defaults.lang; } if (inputStream == null) throw new IllegalArgumentException("CANT LOAD THE LANGUAGE FILE. DIRECTIONHUD WILL BREAK."); Type type = new TypeToken>(){}.getType(); diff --git a/src/main/java/one/oth3r/sit/ModMenu.java b/src/main/java/one/oth3r/sit/ModMenu.java index 7283b17..bba82ea 100644 --- a/src/main/java/one/oth3r/sit/ModMenu.java +++ b/src/main/java/one/oth3r/sit/ModMenu.java @@ -10,6 +10,7 @@ import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.text.TextColor; import net.minecraft.util.Formatting; +import one.oth3r.sit.file.Config; public class ModMenu implements ModMenuApi { private static MutableText lang(String key) { @@ -20,7 +21,9 @@ public class ModMenu implements ModMenuApi { } @Override public ConfigScreenFactory getModConfigScreenFactory() { - return parent -> YetAnotherConfigLib.createBuilder().save(config::save) + // return null if YACL isn't installed to not throw an error + if (!yaclCheck()) return screen -> null; + return parent -> YetAnotherConfigLib.createBuilder().save(Config::save) .title(Text.of("Sit!")) .category(ConfigCategory.createBuilder() .name(lang("category.general")) @@ -28,13 +31,13 @@ public class ModMenu implements ModMenuApi { .option(Option.createBuilder() .name(lang("general.keep_active")) .description(OptionDescription.of(lang("general.keep_active.description"))) - .binding(config.defaults.keepActive, () -> config.keepActive, n -> config.keepActive = n) + .binding(Config.defaults.keepActive, () -> Config.keepActive, n -> Config.keepActive = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .option(Option.createBuilder() .name(lang("general.sit_while_seated")) .description(OptionDescription.of(lang("general.sit_while_seated.description"))) - .binding(config.defaults.sitWhileSeated, () -> config.sitWhileSeated, n -> config.sitWhileSeated = n) + .binding(Config.defaults.sitWhileSeated, () -> Config.sitWhileSeated, n -> Config.sitWhileSeated = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .group(OptionGroup.createBuilder() @@ -42,28 +45,28 @@ public class ModMenu implements ModMenuApi { .description(OptionDescription.of(lang("general.sittable.description"))) .option(Option.createBuilder() .name(lang("general.sittable.stairs")) - .binding(config.defaults.stairsOn, () -> config.stairsOn, n -> config.stairsOn = n) + .binding(Config.defaults.stairsOn, () -> Config.stairsOn, n -> Config.stairsOn = n) .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter()) .build()) .option(Option.createBuilder() .name(lang("general.sittable.slabs")) - .binding(config.defaults.slabsOn, () -> config.slabsOn, n -> config.slabsOn = n) + .binding(Config.defaults.slabsOn, () -> Config.slabsOn, n -> Config.slabsOn = n) .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter()) .build()) .option(Option.createBuilder() .name(lang("general.sittable.carpets")) - .binding(config.defaults.carpetsOn, () -> config.carpetsOn, n -> config.carpetsOn = n) + .binding(Config.defaults.carpetsOn, () -> Config.carpetsOn, n -> Config.carpetsOn = n) .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter()) .build()) .option(Option.createBuilder() .name(lang("general.sittable.full_blocks")) - .binding(config.defaults.fullBlocksOn, () -> config.fullBlocksOn, n -> config.fullBlocksOn = n) + .binding(Config.defaults.fullBlocksOn, () -> Config.fullBlocksOn, n -> Config.fullBlocksOn = n) .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter()) .build()) .option(Option.createBuilder() .name(lang("general.sittable.custom")) .description(OptionDescription.of(lang("general.sittable.custom.description"))) - .binding(config.defaults.customOn, () -> config.customOn, n -> config.customOn = n) + .binding(Config.defaults.customOn, () -> Config.customOn, n -> Config.customOn = n) .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter()) .build()) .build()) @@ -86,7 +89,7 @@ public class ModMenu implements ModMenuApi { .append("\n").append(lang("general.sittable_blocks.description_6").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN)))) .append("\n").append(lang("general.sittable_blocks.description_7").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GOLD)))) .append("\n\n").append(lang("general.sittable_blocks.description_8").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.YELLOW)))))) - .binding(config.defaults.customBlocks, () -> config.customBlocks, n -> config.customBlocks = n) + .binding(Config.defaults.customBlocks, () -> Config.customBlocks, n -> Config.customBlocks = n) .controller(StringControllerBuilder::create) .initial("") .build()) @@ -94,14 +97,14 @@ public class ModMenu implements ModMenuApi { .category(ConfigCategory.createBuilder() .name(lang("category.main_hand")) .tooltip(lang("category.main_hand.tooltip")) - .option(Option.createBuilder() + .option(Option.createBuilder() .name(lang("hand.requirements")) .description(OptionDescription.of(lang("hand.requirements.description") .append("\n\n").append(lang("hand.requirements.description_2").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA)))) .append("\n").append(lang("hand.requirements.description_3").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN)))) .append("\n").append(lang("hand.requirements.description_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED)))))) - .binding(config.defaults.mainReq, () -> config.mainReq, n -> config.mainReq = n) - .controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirement.class) + .binding(Config.defaults.mainReq, () -> Config.mainReq, n -> Config.mainReq = n) + .controller(opt -> EnumControllerBuilder.create(opt).enumClass(Config.HandRequirement.class) .formatValue(v -> Text.translatable("config.sit."+v.name().toLowerCase()))) .build()) .group(OptionGroup.createBuilder() @@ -109,18 +112,18 @@ public class ModMenu implements ModMenuApi { .description(OptionDescription.of(lang("hand.restrictions.description"))) .option(Option.createBuilder() .name(lang("hand.restrictions.blocks")) - .binding(config.defaults.mainBlock,()-> config.mainBlock,n -> config.mainBlock = n) + .binding(Config.defaults.mainBlock,()-> Config.mainBlock, n -> Config.mainBlock = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .option(Option.createBuilder() .name(lang("hand.restrictions.food")) - .binding(config.defaults.mainFood,()-> config.mainFood,n -> config.mainFood = n) + .binding(Config.defaults.mainFood,()-> Config.mainFood, n -> Config.mainFood = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .option(Option.createBuilder() .name(lang("hand.restrictions.usable")) .description(OptionDescription.of(lang("hand.restrictions.usable.description"))) - .binding(config.defaults.mainUsable,()-> config.mainUsable,n -> config.mainUsable = n) + .binding(Config.defaults.mainUsable,()-> Config.mainUsable, n -> Config.mainUsable = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .build()) @@ -129,7 +132,7 @@ public class ModMenu implements ModMenuApi { .description(OptionDescription.of(lang("hand.whitelist.description") .append("\n\n").append(lang("hand.list.description")) .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY)))))) - .binding(config.defaults.mainWhitelist, () -> config.mainWhitelist, n -> config.mainWhitelist = n) + .binding(Config.defaults.mainWhitelist, () -> Config.mainWhitelist, n -> Config.mainWhitelist = n) .controller(StringControllerBuilder::create) .initial("") .build()) @@ -138,7 +141,7 @@ public class ModMenu implements ModMenuApi { .description(OptionDescription.of(lang("hand.blacklist.description") .append("\n\n").append(lang("hand.list.description")) .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY)))))) - .binding(config.defaults.mainBlacklist, () -> config.mainBlacklist, n -> config.mainBlacklist = n) + .binding(Config.defaults.mainBlacklist, () -> Config.mainBlacklist, n -> Config.mainBlacklist = n) .controller(StringControllerBuilder::create) .initial("") .build()) @@ -146,14 +149,14 @@ public class ModMenu implements ModMenuApi { .category(ConfigCategory.createBuilder() .name(lang("category.off_hand")) .tooltip(lang("category.off_hand.tooltip")) - .option(Option.createBuilder() + .option(Option.createBuilder() .name(lang("hand.requirements")) .description(OptionDescription.of(lang("hand.requirements.description") .append("\n\n").append(lang("hand.requirements.description_2").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA)))) .append("\n").append(lang("hand.requirements.description_3").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN)))) .append("\n").append(lang("hand.requirements.description_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED)))))) - .binding(config.defaults.offReq, () -> config.offReq, n -> config.offReq = n) - .controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirement.class) + .binding(Config.defaults.offReq, () -> Config.offReq, n -> Config.offReq = n) + .controller(opt -> EnumControllerBuilder.create(opt).enumClass(Config.HandRequirement.class) .formatValue(v -> Text.translatable("config.sit."+v.name().toLowerCase()))) .build()) .group(OptionGroup.createBuilder() @@ -161,18 +164,18 @@ public class ModMenu implements ModMenuApi { .description(OptionDescription.of(lang("hand.restrictions.description"))) .option(Option.createBuilder() .name(lang("hand.restrictions.blocks")) - .binding(config.defaults.offBlock,()-> config.offBlock,n -> config.offBlock = n) + .binding(Config.defaults.offBlock,()-> Config.offBlock, n -> Config.offBlock = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .option(Option.createBuilder() .name(lang("hand.restrictions.food")) - .binding(config.defaults.offFood,()-> config.offFood,n -> config.offFood = n) + .binding(Config.defaults.offFood,()-> Config.offFood, n -> Config.offFood = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .option(Option.createBuilder() .name(lang("hand.restrictions.usable")) .description(OptionDescription.of(lang("hand.restrictions.usable.description"))) - .binding(config.defaults.offUsable,()-> config.offUsable,n -> config.offUsable = n) + .binding(Config.defaults.offUsable,()-> Config.offUsable, n -> Config.offUsable = n) .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter()) .build()) .build()) @@ -181,7 +184,7 @@ public class ModMenu implements ModMenuApi { .description(OptionDescription.of(lang("hand.whitelist.description") .append("\n\n").append(lang("hand.list.description")) .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY)))))) - .binding(config.defaults.offWhitelist, () -> config.offWhitelist, n -> config.offWhitelist = n) + .binding(Config.defaults.offWhitelist, () -> Config.offWhitelist, n -> Config.offWhitelist = n) .controller(StringControllerBuilder::create) .initial("") .build()) @@ -190,11 +193,24 @@ public class ModMenu implements ModMenuApi { .description(OptionDescription.of(lang("hand.blacklist.description") .append("\n\n").append(lang("hand.list.description")) .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY)))))) - .binding(config.defaults.offBlacklist, () -> config.offBlacklist, n -> config.offBlacklist = n) + .binding(Config.defaults.offBlacklist, () -> Config.offBlacklist, n -> Config.offBlacklist = n) .controller(StringControllerBuilder::create) .initial("") .build()) .build()) .build().generateScreen(parent); } + + /** + * check if YACL is installed by getting a class and seeing if it throws + * @return if YACL is installed + */ + public static boolean yaclCheck() { + try { + Class.forName("dev.isxander.yacl3.platform.fabric.YACLPlatformImpl"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } } diff --git a/src/main/java/one/oth3r/sit/Sit.java b/src/main/java/one/oth3r/sit/Sit.java index 08abcc9..8b1f44a 100644 --- a/src/main/java/one/oth3r/sit/Sit.java +++ b/src/main/java/one/oth3r/sit/Sit.java @@ -3,23 +3,19 @@ package one.oth3r.sit; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; -import io.netty.buffer.ByteBuf; -import io.netty.util.ReferenceCountUtil; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.network.PacketByteBuf; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.CommandManager; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.MutableText; import net.minecraft.text.Text; +import one.oth3r.sit.file.Config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Type; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; import java.util.HashMap; public class Sit implements ModInitializer { @@ -36,7 +32,7 @@ public class Sit implements ModInitializer { //todo future: // make it so it updates the sitting height and pos based on the block so if it changed while offline it still works (or if stair changes shape) // inner stair offset & custom support for that ig - config.load(); + Config.load(); Events.register(); //PACKETS ServerPlayNetworking.registerGlobalReceiver(PacketBuilder.getIdentifier(), diff --git a/src/main/java/one/oth3r/sit/SitClient.java b/src/main/java/one/oth3r/sit/SitClient.java index 2a1d2e1..d8285fb 100644 --- a/src/main/java/one/oth3r/sit/SitClient.java +++ b/src/main/java/one/oth3r/sit/SitClient.java @@ -20,6 +20,6 @@ public class SitClient implements ClientModInitializer { } public static void sendPackets() { Gson gson = new GsonBuilder().disableHtmlEscaping().create(); - new PacketBuilder(gson.toJson(Utl.getHandSettings())).send(); + new PacketBuilder(gson.toJson(Utl.HandSettings.getHandSettings())).send(); } } diff --git a/src/main/java/one/oth3r/sit/SitCommand.java b/src/main/java/one/oth3r/sit/SitCommand.java index 0fee18c..d641d30 100644 --- a/src/main/java/one/oth3r/sit/SitCommand.java +++ b/src/main/java/one/oth3r/sit/SitCommand.java @@ -7,15 +7,13 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; -import net.minecraft.entity.EntityType; -import net.minecraft.entity.decoration.DisplayEntity; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.TextColor; import net.minecraft.util.Formatting; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; +import one.oth3r.sit.file.Config; import java.util.concurrent.CompletableFuture; @@ -48,31 +46,24 @@ public class SitCommand { // if console if (player == null) { if (args[0].equalsIgnoreCase("reload")) { - config.load(); + Config.load(); Sit.LOGGER.info(Sit.lang("key.sit.command.reloaded").getString()); } return 1; } if (args[0].equalsIgnoreCase("sit")) { BlockPos pos = player.getBlockPos(); - if (!(player.getY() -((int) player.getY()) > 0.00)) { + // get the block under the player if player on top of a solid + if (!(player.getY() - ((int) player.getY()) > 0.00)) { pos = pos.add(0,-1,0); } - World world = player.getWorld(); // if already sitting, ignore if (Events.entities.containsKey(player)) return 1; - // make entity first to check the blocks - DisplayEntity.TextDisplayEntity entity = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY,player.getServerWorld()); - Events.setEntity(pos,world,entity); - if (Events.checkBlocks(pos,world,Events.isAboveBlockheight(entity))) { - player.getServerWorld().spawnEntity(entity); - player.startRiding(entity); - Events.entities.put(player,entity); - return 1; - } + // sit + Events.sit(player,pos); } if (args[0].equalsIgnoreCase("reload")) { - config.load(); + Config.load(); player.sendMessage(Sit.lang("key.sit.command.reloaded").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN)))); } if (args[0].equalsIgnoreCase("purgeChairEntities")) { diff --git a/src/main/java/one/oth3r/sit/Utl.java b/src/main/java/one/oth3r/sit/Utl.java index 3045ec1..ed69d96 100644 --- a/src/main/java/one/oth3r/sit/Utl.java +++ b/src/main/java/one/oth3r/sit/Utl.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import net.minecraft.server.network.ServerPlayerEntity; +import one.oth3r.sit.file.Config; import java.lang.reflect.Type; import java.util.ArrayList; @@ -11,35 +12,59 @@ import java.util.HashMap; import java.util.List; public class Utl { - enum HandType { - main, - off + public static class HandSettings { + public static HashMap getHandSettings() { + Gson gson = new GsonBuilder().disableHtmlEscaping().create(); + HashMap settings = new HashMap<>(); + settings.put("hand.main.requirement",String.valueOf(Config.mainReq)); + settings.put("hand.main.block",String.valueOf(Config.mainBlock)); + settings.put("hand.main.food",String.valueOf(Config.mainFood)); + settings.put("hand.main.usable",String.valueOf(Config.mainUsable)); + settings.put("hand.main.whitelist",gson.toJson(Config.mainWhitelist)); + settings.put("hand.main.blacklist",gson.toJson(Config.mainBlacklist)); + settings.put("hand.off.requirement",String.valueOf(Config.offReq)); + settings.put("hand.off.block",String.valueOf(Config.offBlock)); + settings.put("hand.off.food",String.valueOf(Config.offFood)); + settings.put("hand.off.usable",String.valueOf(Config.offUsable)); + settings.put("hand.off.whitelist",gson.toJson(Config.offWhitelist)); + settings.put("hand.off.blacklist",gson.toJson(Config.offBlacklist)); + return settings; + } + + public static Config.HandRequirement getReq(ServerPlayerEntity player, HandType type) { + return Config.HandRequirement.get(Sit.playerSettings.get(player).get("hand."+type+".requirement")); + } + + public static List getList(ServerPlayerEntity player, HandType type, String setting) { + Type listType = new TypeToken>() {}.getType(); + return new Gson().fromJson(Sit.playerSettings.get(player).get("hand."+type+"."+setting),listType); + } + + public static boolean getBool(ServerPlayerEntity player, HandType type, String setting) { + return Boolean.parseBoolean(Sit.playerSettings.get(player).get("hand."+type+"."+setting)); + } + + public enum HandType { + main, + off + } } - public static HashMap getHandSettings() { - Gson gson = new GsonBuilder().disableHtmlEscaping().create(); - HashMap settings = new HashMap<>(); - settings.put("hand.main.requirement",String.valueOf(config.mainReq)); - settings.put("hand.main.block",String.valueOf(config.mainBlock)); - settings.put("hand.main.food",String.valueOf(config.mainFood)); - settings.put("hand.main.usable",String.valueOf(config.mainUsable)); - settings.put("hand.main.whitelist",gson.toJson(config.mainWhitelist)); - settings.put("hand.main.blacklist",gson.toJson(config.mainBlacklist)); - settings.put("hand.off.requirement",String.valueOf(config.offReq)); - settings.put("hand.off.block",String.valueOf(config.offBlock)); - settings.put("hand.off.food",String.valueOf(config.offFood)); - settings.put("hand.off.usable",String.valueOf(config.offUsable)); - settings.put("hand.off.whitelist",gson.toJson(config.offWhitelist)); - settings.put("hand.off.blacklist",gson.toJson(config.offBlacklist)); - return settings; - } - public static config.HandRequirement getReq(ServerPlayerEntity player, HandType type) { - return config.HandRequirement.get(Sit.playerSettings.get(player).get("hand."+type+".requirement")); - } - public static List getList(ServerPlayerEntity player, HandType type, String setting) { - Type listType = new TypeToken>() {}.getType(); - return new Gson().fromJson(Sit.playerSettings.get(player).get("hand."+type+"."+setting),listType); - } - public static boolean getBool(ServerPlayerEntity player, HandType type, String setting) { - return Boolean.parseBoolean(Sit.playerSettings.get(player).get("hand."+type+"."+setting)); + public static class Num { + public static boolean isInt(String string) { + try { + Integer.parseInt(string); + } catch (NumberFormatException nfe) { + return false; + } + return true; + } + public static boolean isFloat(String string) { + try { + Float.parseFloat(string); + } catch (NumberFormatException nfe) { + return false; + } + return true; + } } } diff --git a/src/main/java/one/oth3r/sit/config.java b/src/main/java/one/oth3r/sit/file/Config.java similarity index 77% rename from src/main/java/one/oth3r/sit/config.java rename to src/main/java/one/oth3r/sit/file/Config.java index 7e52df9..0910817 100644 --- a/src/main/java/one/oth3r/sit/config.java +++ b/src/main/java/one/oth3r/sit/file/Config.java @@ -1,9 +1,14 @@ -package one.oth3r.sit; +package one.oth3r.sit.file; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import net.fabricmc.loader.api.FabricLoader; +import one.oth3r.sit.LangReader; +import one.oth3r.sit.Sit; +import one.oth3r.sit.SitClient; +import one.oth3r.sit.Utl.*; import java.io.File; import java.io.FileInputStream; @@ -14,7 +19,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; -public class config { +public class Config { public static String lang = defaults.lang; public static boolean keepActive = defaults.keepActive; public static boolean sitWhileSeated = defaults.sitWhileSeated; @@ -24,7 +29,7 @@ public class config { public static boolean fullBlocksOn = defaults.fullBlocksOn; public static boolean customOn = defaults.customOn; public static List customBlocks = defaults.customBlocks; - enum HandRequirement { + public enum HandRequirement { empty, restrictive, none; @@ -62,17 +67,35 @@ public class config { Properties properties = new Properties(); properties.load(fileStream); String ver = (String) properties.computeIfAbsent("version", a -> String.valueOf(defaults.version)); - // if the old version system (v1.0) remove "v: + + // if the old version system (v1.0) remove "v" if (ver.contains("v")) ver = ver.substring(1); + loadVersion(properties,Double.parseDouble(ver)); LangReader.loadLanguageFile(); + save(); - } catch (Exception f) { + } catch (Exception e) { //read fail - f.printStackTrace(); + e.printStackTrace(); save(); } } + public static ArrayList validateCustomBlocks(ArrayList fix) { + ArrayList out = new ArrayList<>(); + for (String entry : fix) { + String[] split = entry.split("\\|"); + // skip if not the right size + if (split.length < 3 || split.length > 4) continue; + // keep going if that block exists +// if (Registries.BLOCK.stream().anyMatch(match -> Registries.BLOCK.getId(match).toString().equals(split[0]))) {} + // if the other entries aren't correct, skip + if (!Num.isFloat(split[1]) || !Num.isInt(split[2])) continue; + // add if everything is a okay + out.add(entry); + } + return out; + } public static void loadVersion(Properties properties, double version) { try { Gson gson = new GsonBuilder().disableHtmlEscaping().create(); @@ -86,17 +109,23 @@ public class config { carpetsOn = Boolean.parseBoolean((String) properties.computeIfAbsent("carpets", a -> String.valueOf(defaults.carpetsOn))); fullBlocksOn = Boolean.parseBoolean((String) properties.computeIfAbsent("full-blocks", a -> String.valueOf(defaults.fullBlocksOn))); customOn = Boolean.parseBoolean((String) properties.computeIfAbsent("custom", a -> String.valueOf(defaults.customOn))); - customBlocks = new Gson().fromJson((String) - properties.computeIfAbsent("custom-blocks", a -> gson.toJson(defaults.customBlocks)), listType); - mainReq = HandRequirement.valueOf((String) properties.computeIfAbsent("hand.main.requirement", a -> String.valueOf(defaults.mainReq))); + try { + customBlocks = validateCustomBlocks(new Gson().fromJson((String) + properties.computeIfAbsent("custom-blocks", a -> gson.toJson(defaults.customBlocks)), listType)); + } catch (JsonSyntaxException ignore) {} + mainReq = HandRequirement.get((String) properties.computeIfAbsent("hand.main.requirement", a -> String.valueOf(defaults.mainReq))); mainBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.block", a -> String.valueOf(defaults.mainBlock))); mainFood = Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.food", a -> String.valueOf(defaults.mainFood))); mainUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.usable", a -> String.valueOf(defaults.mainUsable))); - mainWhitelist = new Gson().fromJson((String) - properties.computeIfAbsent("hand.main.whitelist", a -> gson.toJson(defaults.mainWhitelist)), listType); - mainBlacklist = new Gson().fromJson((String) - properties.computeIfAbsent("hand.main.blacklist", a -> gson.toJson(defaults.mainBlacklist)), listType); - offReq = HandRequirement.valueOf((String) properties.computeIfAbsent("hand.off.requirement", a -> String.valueOf(defaults.offReq))); + try { + mainWhitelist = new Gson().fromJson((String) + properties.computeIfAbsent("hand.main.whitelist", a -> gson.toJson(defaults.mainWhitelist)), listType); + } catch (JsonSyntaxException ignore) {} + try { + mainBlacklist = new Gson().fromJson((String) + properties.computeIfAbsent("hand.main.blacklist", a -> gson.toJson(defaults.mainBlacklist)), listType); + } catch (JsonSyntaxException ignore) {} + offReq = HandRequirement.get((String) properties.computeIfAbsent("hand.off.requirement", a -> String.valueOf(defaults.offReq))); offBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.block", a -> String.valueOf(defaults.offBlock))); offFood = Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.food", a -> String.valueOf(defaults.offFood))); offUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.usable", a -> String.valueOf(defaults.offUsable))); @@ -105,22 +134,30 @@ public class config { offBlacklist = new Gson().fromJson((String) properties.computeIfAbsent("hand.off.blacklist", a -> gson.toJson(defaults.offBlacklist)), listType); if (version == 1.0) { - mainReq = HandRequirement.valueOf((String) properties.computeIfAbsent("main-hand-requirement", a -> String.valueOf(defaults.mainReq))); + mainReq = HandRequirement.get((String) properties.computeIfAbsent("main-hand-requirement", a -> String.valueOf(defaults.mainReq))); mainBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> String.valueOf(defaults.mainBlock))); mainFood = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> String.valueOf(defaults.mainFood))); mainUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> String.valueOf(defaults.mainUsable))); - mainWhitelist = new Gson().fromJson((String) - properties.computeIfAbsent("main-hand-whitelist", a -> gson.toJson(defaults.mainWhitelist)), listType); - mainBlacklist = new Gson().fromJson((String) - properties.computeIfAbsent("main-hand-blacklist", a -> gson.toJson(defaults.mainBlacklist)), listType); - offReq = HandRequirement.valueOf((String) properties.computeIfAbsent("off-hand-requirement", a -> String.valueOf(defaults.offReq))); + try { + mainWhitelist = new Gson().fromJson((String) + properties.computeIfAbsent("main-hand-whitelist", a -> gson.toJson(defaults.mainWhitelist)), listType); + } catch (JsonSyntaxException ignore) {} + try { + mainBlacklist = new Gson().fromJson((String) + properties.computeIfAbsent("main-hand-blacklist", a -> gson.toJson(defaults.mainBlacklist)), listType); + } catch (JsonSyntaxException ignore) {} + offReq = HandRequirement.get((String) properties.computeIfAbsent("off-hand-requirement", a -> String.valueOf(defaults.offReq))); offBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> String.valueOf(defaults.offBlock))); offFood = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> String.valueOf(defaults.offFood))); offUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> String.valueOf(defaults.offUsable))); - offWhitelist = new Gson().fromJson((String) - properties.computeIfAbsent("off-hand-whitelist", a -> gson.toJson(defaults.offWhitelist)), listType); - offBlacklist = new Gson().fromJson((String) - properties.computeIfAbsent("off-hand-blacklist", a -> gson.toJson(defaults.offBlacklist)), listType); + try { + offWhitelist = new Gson().fromJson((String) + properties.computeIfAbsent("off-hand-whitelist", a -> gson.toJson(defaults.offWhitelist)), listType); + } catch (JsonSyntaxException ignore) {} + try { + offBlacklist = new Gson().fromJson((String) + properties.computeIfAbsent("off-hand-blacklist", a -> gson.toJson(defaults.offBlacklist)), listType); + } catch (JsonSyntaxException ignore) {} } } catch (Exception e) { Sit.LOGGER.info("ERROR LOADING CONFIG - PLEASE REPORT WITH THE ERROR LOG"); @@ -191,7 +228,7 @@ public class config { public static boolean carpetsOn = true; public static boolean fullBlocksOn = false; public static boolean customOn = false; - public static List customBlocks = List.of("minecraft:campfire|.46|1|lit=false","minecraft:soul_campfire|.46|1|lit=false"); + public static List customBlocks = List.of("minecraft:campfire|.46|1|lit=false","minecraft:soul_campfire|.46|1|lit=false,waterlogged=false"); public static HandRequirement mainReq = HandRequirement.empty; public static boolean mainBlock = false; public static boolean mainFood = false;