code optimization

This commit is contained in:
Oth3r 2023-08-02 23:05:02 -05:00
commit ccab3bb0e2
4 changed files with 36 additions and 44 deletions

View file

@ -39,8 +39,8 @@ public class Events {
Item mainItem = player.getMainHandStack().getItem(); Item mainItem = player.getMainHandStack().getItem();
Item offItem = player.getOffHandStack().getItem(); Item offItem = player.getOffHandStack().getItem();
if (player.isSneaking()) return false; if (player.isSneaking()) return false;
if (config.mainReq.equals(config.MainReq.empty) && !player.getMainHandStack().isEmpty()) return false; if (config.mainReq.equals(config.HandRequirements.empty) && !player.getMainHandStack().isEmpty()) return false;
if (config.mainReq.equals(config.MainReq.restrictive)) { if (config.mainReq.equals(config.HandRequirements.restrictive)) {
if (checkList(config.mainBlacklist,mainItem)) return false; if (checkList(config.mainBlacklist,mainItem)) return false;
if (!checkList(config.mainWhitelist,mainItem)) { if (!checkList(config.mainWhitelist,mainItem)) {
if (config.mainBlock && (mainItem instanceof BlockItem)) return false; if (config.mainBlock && (mainItem instanceof BlockItem)) return false;
@ -48,8 +48,8 @@ public class Events {
if (config.mainUsable && !notUsable.contains(player.getMainHandStack().getUseAction())) return false; if (config.mainUsable && !notUsable.contains(player.getMainHandStack().getUseAction())) return false;
} }
} }
if (config.offReq.equals(config.OffReq.empty) && !player.getOffHandStack().isEmpty()) return false; if (config.offReq.equals(config.HandRequirements.empty) && !player.getOffHandStack().isEmpty()) return false;
if (config.offReq.equals(config.OffReq.restrictive)) { if (config.offReq.equals(config.HandRequirements.restrictive)) {
if (checkList(config.offBlacklist,offItem)) return false; if (checkList(config.offBlacklist,offItem)) return false;
if (!checkList(config.offWhitelist,offItem)) { if (!checkList(config.offWhitelist,offItem)) {
if (config.offBlock && (offItem instanceof BlockItem)) return false; if (config.offBlock && (offItem instanceof BlockItem)) return false;
@ -73,7 +73,7 @@ public class Events {
data.put("height",split[1]); data.put("height",split[1]);
data.put("hitbox",split[2]); data.put("hitbox",split[2]);
if (split.length==4) data.put("state",split[3]); if (split.length==4) data.put("state",split[3]);
map.put(i+"",data); map.put(String.valueOf(i),data);
i++; i++;
} }
return map; return map;

View file

@ -90,14 +90,14 @@ public class ModMenu implements ModMenuApi {
.category(ConfigCategory.createBuilder() .category(ConfigCategory.createBuilder()
.name(lang("category.main_hand")) .name(lang("category.main_hand"))
.tooltip(lang("category.main_hand.tooltip")) .tooltip(lang("category.main_hand.tooltip"))
.option(Option.<config.MainReq>createBuilder() .option(Option.<config.HandRequirements>createBuilder()
.name(lang("hand.requirements")) .name(lang("hand.requirements"))
.description(OptionDescription.of(lang("hand.requirements.description") .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\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_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)))))) .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) .binding(config.defaults.mainReq, () -> config.mainReq, n -> config.mainReq = n)
.controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.MainReq.class) .controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirements.class)
.valueFormatter(v -> Text.translatable("config.sit."+v.name().toLowerCase()))) .valueFormatter(v -> Text.translatable("config.sit."+v.name().toLowerCase())))
.build()) .build())
.group(OptionGroup.createBuilder() .group(OptionGroup.createBuilder()
@ -142,14 +142,14 @@ public class ModMenu implements ModMenuApi {
.category(ConfigCategory.createBuilder() .category(ConfigCategory.createBuilder()
.name(lang("category.off_hand")) .name(lang("category.off_hand"))
.tooltip(lang("category.off_hand.tooltip")) .tooltip(lang("category.off_hand.tooltip"))
.option(Option.<config.OffReq>createBuilder() .option(Option.<config.HandRequirements>createBuilder()
.name(lang("hand.requirements")) .name(lang("hand.requirements"))
.description(OptionDescription.of(lang("hand.requirements.description") .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\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_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)))))) .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) .binding(config.defaults.offReq, () -> config.offReq, n -> config.offReq = n)
.controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.OffReq.class) .controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirements.class)
.valueFormatter(v -> Text.translatable("config.sit."+v.name().toLowerCase()))) .valueFormatter(v -> Text.translatable("config.sit."+v.name().toLowerCase())))
.build()) .build())
.group(OptionGroup.createBuilder() .group(OptionGroup.createBuilder()

View file

@ -17,7 +17,6 @@ import net.minecraft.util.Formatting;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class SitCommand { public class SitCommand {
@ -31,8 +30,6 @@ public class SitCommand {
.executes((context2) -> command(context2.getSource(), context2.getInput())))); .executes((context2) -> command(context2.getSource(), context2.getInput()))));
} }
public static CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) { public static CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
ServerPlayerEntity player = Objects.requireNonNull(context.getSource().getPlayer());
String[] args = context.getInput().split(" ");
builder.suggest("reload"); builder.suggest("reload");
builder.suggest("purgeChairEntities"); builder.suggest("purgeChairEntities");
return builder.buildFuture(); return builder.buildFuture();

View file

@ -24,23 +24,18 @@ public class config {
public static boolean fullBlocksOn = defaults.fullBlocksOn; public static boolean fullBlocksOn = defaults.fullBlocksOn;
public static boolean customOn = defaults.customOn; public static boolean customOn = defaults.customOn;
public static List<String> customBlocks = defaults.customBlocks; public static List<String> customBlocks = defaults.customBlocks;
enum MainReq { enum HandRequirements {
empty, empty,
restrictive, restrictive,
none none
} }
public static MainReq mainReq = defaults.mainReq; public static HandRequirements mainReq = defaults.mainReq;
public static boolean mainBlock = defaults.mainBlock; public static boolean mainBlock = defaults.mainBlock;
public static boolean mainFood = defaults.mainFood; public static boolean mainFood = defaults.mainFood;
public static boolean mainUsable = defaults.mainUsable; public static boolean mainUsable = defaults.mainUsable;
public static List<String> mainWhitelist = defaults.mainWhitelist; public static List<String> mainWhitelist = defaults.mainWhitelist;
public static List<String> mainBlacklist = defaults.mainBlacklist; public static List<String> mainBlacklist = defaults.mainBlacklist;
enum OffReq { public static HandRequirements offReq = defaults.offReq;
empty,
restrictive,
none
}
public static OffReq offReq = defaults.offReq;
public static boolean offBlock = defaults.offBlock; public static boolean offBlock = defaults.offBlock;
public static boolean offFood = defaults.offFood; public static boolean offFood = defaults.offFood;
public static boolean offUsable = defaults.offUsable; public static boolean offUsable = defaults.offUsable;
@ -82,7 +77,7 @@ public class config {
try (FileInputStream fileStream = new FileInputStream(configFile())) { try (FileInputStream fileStream = new FileInputStream(configFile())) {
Properties properties = new Properties(); Properties properties = new Properties();
properties.load(fileStream); properties.load(fileStream);
loadVersion(properties,(String) properties.computeIfAbsent("version", a -> defaults.version+"")); loadVersion(properties,(String) properties.computeIfAbsent("version", a -> defaults.version));
save(); save();
} catch (Exception f) { } catch (Exception f) {
//read fail //read fail
@ -92,33 +87,33 @@ public class config {
} }
public static void loadVersion(Properties properties, String version) { public static void loadVersion(Properties properties, String version) {
Type mapType = new TypeToken<ArrayList<String>>() {}.getType(); Type mapType = new TypeToken<ArrayList<String>>() {}.getType();
lang = (String) properties.computeIfAbsent("lang", a -> defaults.lang+""); lang = (String) properties.computeIfAbsent("lang", a -> defaults.lang);
//CONFIG //CONFIG
keepActive = Boolean.parseBoolean((String) properties.computeIfAbsent("keep-active", a -> defaults.keepActive+"")); keepActive = Boolean.parseBoolean((String) properties.computeIfAbsent("keep-active", a -> String.valueOf(defaults.keepActive)));
sitWhileSeated = Boolean.parseBoolean((String) properties.computeIfAbsent("sit-while-seated", a -> defaults.sitWhileSeated+"")); sitWhileSeated = Boolean.parseBoolean((String) properties.computeIfAbsent("sit-while-seated", a -> String.valueOf(defaults.sitWhileSeated)));
stairsOn = Boolean.parseBoolean((String) properties.computeIfAbsent("stairs", a -> defaults.stairsOn+"")); stairsOn = Boolean.parseBoolean((String) properties.computeIfAbsent("stairs", a -> String.valueOf(defaults.stairsOn)));
slabsOn = Boolean.parseBoolean((String) properties.computeIfAbsent("slabs", a -> defaults.slabsOn+"")); slabsOn = Boolean.parseBoolean((String) properties.computeIfAbsent("slabs", a -> String.valueOf(defaults.slabsOn)));
carpetsOn = Boolean.parseBoolean((String) properties.computeIfAbsent("carpets", a -> defaults.carpetsOn+"")); carpetsOn = Boolean.parseBoolean((String) properties.computeIfAbsent("carpets", a -> String.valueOf(defaults.carpetsOn)));
fullBlocksOn = Boolean.parseBoolean((String) properties.computeIfAbsent("full-blocks", a -> defaults.fullBlocksOn+"")); fullBlocksOn = Boolean.parseBoolean((String) properties.computeIfAbsent("full-blocks", a -> String.valueOf(defaults.fullBlocksOn)));
customOn = Boolean.parseBoolean((String) properties.computeIfAbsent("custom", a -> defaults.customOn+"")); customOn = Boolean.parseBoolean((String) properties.computeIfAbsent("custom", a -> String.valueOf(defaults.customOn)));
customBlocks = new Gson().fromJson((String) customBlocks = new Gson().fromJson((String)
properties.computeIfAbsent("custom-blocks", a -> defaults.customBlocks+""),mapType); properties.computeIfAbsent("custom-blocks", a -> String.valueOf(defaults.customBlocks)),mapType);
mainReq = MainReq.valueOf((String) properties.computeIfAbsent("main-hand-requirement", a -> defaults.mainReq+"")); mainReq = HandRequirements.valueOf((String) properties.computeIfAbsent("main-hand-requirement", a -> String.valueOf(defaults.mainReq)));
mainBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> defaults.mainBlock+"")); mainBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> String.valueOf(defaults.mainBlock)));
mainFood = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> defaults.mainFood+"")); mainFood = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> String.valueOf(defaults.mainFood)));
mainUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> defaults.mainUsable +"")); mainUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> String.valueOf(defaults.mainUsable)));
mainWhitelist = new Gson().fromJson((String) mainWhitelist = new Gson().fromJson((String)
properties.computeIfAbsent("main-hand-whitelist", a -> defaults.mainWhitelist+""),mapType); properties.computeIfAbsent("main-hand-whitelist", a -> String.valueOf(defaults.mainWhitelist)),mapType);
mainBlacklist = new Gson().fromJson((String) mainBlacklist = new Gson().fromJson((String)
properties.computeIfAbsent("main-hand-blacklist", a -> defaults.mainBlacklist+""),mapType); properties.computeIfAbsent("main-hand-blacklist", a -> String.valueOf(defaults.mainBlacklist)),mapType);
offReq = OffReq.valueOf((String) properties.computeIfAbsent("off-hand-requirement", a -> defaults.offReq+"")); offReq = HandRequirements.valueOf((String) properties.computeIfAbsent("off-hand-requirement", a -> String.valueOf(defaults.offReq)));
offBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> defaults.offBlock+"")); offBlock = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> String.valueOf(defaults.offBlock)));
offFood = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> defaults.offFood+"")); offFood = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> String.valueOf(defaults.offFood)));
offUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> defaults.offUsable +"")); offUsable = Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> String.valueOf(defaults.offUsable)));
offWhitelist = new Gson().fromJson((String) offWhitelist = new Gson().fromJson((String)
properties.computeIfAbsent("off-hand-whitelist", a -> defaults.offWhitelist+""),mapType); properties.computeIfAbsent("off-hand-whitelist", a -> String.valueOf(defaults.offWhitelist)),mapType);
offBlacklist = new Gson().fromJson((String) offBlacklist = new Gson().fromJson((String)
properties.computeIfAbsent("off-hand-blacklist", a -> defaults.offBlacklist+""),mapType); properties.computeIfAbsent("off-hand-blacklist", a -> String.valueOf(defaults.offBlacklist)),mapType);
} }
public static MutableText lang(String key, Object... args) { public static MutableText lang(String key, Object... args) {
LangReader.loadLanguageFile(); LangReader.loadLanguageFile();
@ -185,13 +180,13 @@ public class config {
public static boolean fullBlocksOn = false; public static boolean fullBlocksOn = false;
public static boolean customOn = false; public static boolean customOn = false;
public static List<String> customBlocks = List.of("minecraft:campfire|.46|1|lit=false","minecraft:soul_campfire|.46|1|lit=false"); public static List<String> customBlocks = List.of("minecraft:campfire|.46|1|lit=false","minecraft:soul_campfire|.46|1|lit=false");
public static MainReq mainReq = MainReq.empty; public static HandRequirements mainReq = HandRequirements.empty;
public static boolean mainBlock = false; public static boolean mainBlock = false;
public static boolean mainFood = false; public static boolean mainFood = false;
public static boolean mainUsable = false; public static boolean mainUsable = false;
public static List<String> mainWhitelist = new ArrayList<>(); public static List<String> mainWhitelist = new ArrayList<>();
public static List<String> mainBlacklist = new ArrayList<>(); public static List<String> mainBlacklist = new ArrayList<>();
public static OffReq offReq = OffReq.restrictive; public static HandRequirements offReq = HandRequirements.restrictive;
public static boolean offBlock = true; public static boolean offBlock = true;
public static boolean offFood = false; public static boolean offFood = false;
public static boolean offUsable = true; public static boolean offUsable = true;