Skip to content

Commit

Permalink
Minor fixes everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Oct 25, 2021
1 parent f5833d9 commit eb429c3
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 96 deletions.
19 changes: 6 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@ archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group

def ENV = System.getenv()

repositories {
maven { url 'https://jitpack.io'}
maven { url "https://maven.architectury.dev/" }
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
maven {
url 'https://jitpack.io'
}
maven {
name = "TomTheGeek-repo"
url = "https://maven.tomthegeek.ml/releases/"
}
}

def ENV = System.getenv()

dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
Expand All @@ -41,10 +36,8 @@ dependencies {
// LuckPerms
modImplementation 'me.lucko:fabric-permissions-api:0.1-SNAPSHOT'

// WDMNF
modRuntime("com.github.tom_the_geek:wdmnf:1.0.0+mc.1.17")
//modRuntime "com.github.CaffeineMC:hydrogen-fabric:mc1.16.5-v0.2.0"
//modRuntime "com.github.astei:lazydfu:0.1.2"
// Allows in game config editing
modImplementation(include("com.github.samolego.Config2Brigadier:config2brigadier-fabric:${project.c2b_version}"))
}

processResources {
Expand Down
5 changes: 4 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ loader_version=0.11.6
fabric_version=0.40.0+1.17

# Mod Properties
mod_version = 1.0.8
mod_version = 1.0.9
maven_group = org.samo_lego
archives_base_name = healthcare

# Config editing lib
c2b_version = 1.0.0
4 changes: 3 additions & 1 deletion src/main/java/org/samo_lego/healthcare/HealthCare.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public class HealthCare implements ModInitializer {
public static final String MODID = "healthcare";

public static final File CONFIG_FILE = new File(FabricLoader.getInstance().getConfigDir() + "/health_config.json");

public static boolean LUCKPERMS_LOADED;

public static HealthConfig config;
Expand All @@ -26,7 +28,7 @@ public void onInitialize() {
EventHandler eventHandler = new EventHandler();
ServerPlayerEvents.COPY_FROM.register(eventHandler);

config = HealthConfig.loadConfigFile(new File(FabricLoader.getInstance().getConfigDir() + "/health_config.json"));
config = HealthConfig.loadConfigFile(CONFIG_FILE);
LUCKPERMS_LOADED = FabricLoader.getInstance().isModLoaded("fabric-permissions-api-v0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.command.suggestion.SuggestionProviders;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.samo_lego.healthcare.healthbar.HealthbarPreferences;
Expand Down Expand Up @@ -73,7 +74,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, b

private static int toggleEntityType(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
if(LUCKPERMS_LOADED && !PermissionHelper.checkPermission(context.getSource(), config.perms.healthbar_edit_showEntityType, 0)) {
context.getSource().sendError(new LiteralText(config.lang.noPermission).formatted(Formatting.RED));
context.getSource().sendError(new TranslatableText("commands.help.failed").formatted(Formatting.RED));
return -1;
}

Expand All @@ -94,7 +95,7 @@ private static int toggleEntityType(CommandContext<ServerCommandSource> context)

private static int editHealthbarLength(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
if(LUCKPERMS_LOADED && !PermissionHelper.checkPermission(context.getSource(), config.perms.healthbar_edit_custom_length, 0)) {
context.getSource().sendError(new LiteralText(config.lang.noPermission).formatted(Formatting.RED));
context.getSource().sendError(new TranslatableText("commands.help.failed").formatted(Formatting.RED));
return -1;
}

Expand All @@ -121,7 +122,7 @@ private static int editHealthbarLength(CommandContext<ServerCommandSource> conte

private static int setSymbol(CommandContext<ServerCommandSource> context, boolean full) throws CommandSyntaxException {
if(LUCKPERMS_LOADED && !PermissionHelper.checkPermission(context.getSource(), full ? config.perms.healthbar_edit_custom_symbols_full : config.perms.healthbar_edit_custom_symbols_empty, 0)) {
context.getSource().sendError(new LiteralText(config.lang.noPermission).formatted(Formatting.RED));
context.getSource().sendError(new TranslatableText("commands.help.failed").formatted(Formatting.RED));
return -1;
}

Expand Down Expand Up @@ -152,7 +153,7 @@ private static int setSymbol(CommandContext<ServerCommandSource> context, boolea

private static int changeVisibility(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
if(LUCKPERMS_LOADED && !PermissionHelper.checkPermission(context.getSource(), config.perms.healthbar_edit_visibility, 0)) {
context.getSource().sendError(new LiteralText(config.lang.noPermission).formatted(Formatting.RED));
context.getSource().sendError(new TranslatableText("commands.help.failed").formatted(Formatting.RED));
return -1;
}

Expand All @@ -173,12 +174,12 @@ private static int changeVisibility(CommandContext<ServerCommandSource> context)

private static int editHealthbarStyle(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
if(LUCKPERMS_LOADED && !PermissionHelper.checkPermission(context.getSource(), config.perms.healthbar_edit_style, 0)) {
context.getSource().sendError(new LiteralText(config.lang.noPermission).formatted(Formatting.RED));
context.getSource().sendError(new TranslatableText("commands.help.failed").formatted(Formatting.RED));
return -1;
}

HealthbarPreferences preferences = (HealthbarPreferences) context.getSource().getPlayer();
Enum<HealthbarPreferences.HealthbarStyle> style = HealthbarPreferences.HealthbarStyle.valueOf(StringArgumentType.getString(context, "style"));
HealthbarPreferences.HealthbarStyle style = HealthbarPreferences.HealthbarStyle.valueOf(StringArgumentType.getString(context, "style"));

preferences.setHealthbarStyle(style);

Expand All @@ -194,7 +195,7 @@ private static int editHealthbarStyle(CommandContext<ServerCommandSource> contex

private static int toggleHealthBar(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
if(LUCKPERMS_LOADED && !PermissionHelper.checkPermission(context.getSource(), config.perms.healthbar_toggle, 0)) {
context.getSource().sendError(new LiteralText(config.lang.noPermission).formatted(Formatting.RED));
context.getSource().sendError(new TranslatableText("commands.help.failed").formatted(Formatting.RED));
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,42 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.fabricmc.loader.api.FabricLoader;
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Formatting;
import org.samo_lego.healthcare.config.HealthConfig;
import org.samo_lego.healthcare.permission.PermissionHelper;

import java.io.File;

import static net.minecraft.server.command.CommandManager.literal;
import static org.samo_lego.healthcare.HealthCare.LUCKPERMS_LOADED;
import static org.samo_lego.healthcare.HealthCare.config;
import static org.samo_lego.healthcare.HealthCare.*;

public class HealthcareCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated) {
dispatcher.register(literal("healthcare")
.requires(src -> LUCKPERMS_LOADED ? PermissionHelper.checkPermission(src, config.perms.healthcare_reloadConfig, 4) : src.hasPermissionLevel(4))
.then(literal("reloadConfig").executes(HealthcareCommand::reloadConfig))
);
LiteralCommandNode<ServerCommandSource> healthcareNode = dispatcher.register(literal("healthcare")
.requires(src -> LUCKPERMS_LOADED ? PermissionHelper.checkPermission(src, config.perms.healthcare_config, 4) : src.hasPermissionLevel(4)));

LiteralCommandNode<ServerCommandSource> configNode = literal("config")
.then(literal("reload")
.requires(src -> LUCKPERMS_LOADED ? PermissionHelper.checkPermission(src, config.perms.healthcare_config_reload, 4) : src.hasPermissionLevel(4))
.executes(HealthcareCommand::reloadConfig)
)
.build();

LiteralCommandNode<ServerCommandSource> editNode = literal("edit")
.requires(src -> LUCKPERMS_LOADED ? PermissionHelper.checkPermission(src, config.perms.healthcare_config_edit, 4) : src.hasPermissionLevel(4))
.build();

config.generateCommand(editNode);

configNode.addChild(editNode);
healthcareNode.addChild(configNode);
}

private static int reloadConfig(CommandContext<ServerCommandSource> ctx) {
config = HealthConfig.loadConfigFile(new File(FabricLoader.getInstance().getConfigDir() + "/config.json"));
HealthConfig newConfig = HealthConfig.loadConfigFile(CONFIG_FILE);
config.reload(newConfig);

ctx.getSource().sendFeedback(
new LiteralText(config.lang.configReloaded).formatted(Formatting.GREEN),
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/org/samo_lego/healthcare/config/HealthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import org.samo_lego.config2brigadier.IBrigadierConfigurator;
import org.samo_lego.config2brigadier.annotation.BrigadierDescription;
import org.samo_lego.config2brigadier.annotation.BrigadierExcluded;
import org.samo_lego.healthcare.healthbar.HealthbarPreferences;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.apache.logging.log4j.LogManager.getLogger;
import static org.samo_lego.healthcare.HealthCare.CONFIG_FILE;
import static org.samo_lego.healthcare.HealthCare.MODID;

public class HealthConfig {
public class HealthConfig implements IBrigadierConfigurator {
private static final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
Expand All @@ -22,32 +27,57 @@ public class HealthConfig {

public final String _comment_blacklistedEntities = "// Which entities shouldn't have a healthbar above their name.";
@SerializedName("blacklisted_entities")
public ArrayList<String> blacklistedEntities = new ArrayList<>(Arrays.asList(
@BrigadierDescription(defaultOption = "[\"taterzens:npc\",\"specialmobs:mob_with_hidden_health\"]")
public List<String> blacklistedEntities = new ArrayList<>(Arrays.asList(
"taterzens:npc",
"specialmobs:mob_with_hidden_health"
));
@SerializedName("// When to activate the healthbar.")
public String _comment_activationRange = "";
@BrigadierDescription(defaultOption = "8.0")
public float activationRange = 8.0F;

@BrigadierExcluded
public final Permissions perms = new Permissions();

@SerializedName("// Max length of healthbar a player can use.")
public final String _comment_maxHealthbarLength = "";
@BrigadierDescription(defaultOption = "20")
public int maxHealthbarLength = 20;

@SerializedName("// Whether to show entity type next to health.")
public final String _comment_showType = "";
@BrigadierDescription(defaultOption = "true")
public boolean showType = true;

@SerializedName("// The default style of healthbar. The following are available")
public final String _comment_defaultStyle1 = Arrays.toString(HealthbarPreferences.HealthbarStyle.values());
@BrigadierDescription(defaultOption = "PERCENTAGE")
public HealthbarPreferences.HealthbarStyle defaultStyle = HealthbarPreferences.HealthbarStyle.PERCENTAGE;

@SerializedName("// Whether healthbar is enabled by default.")
public final String _comment_enabled = "";
@BrigadierDescription(defaultOption = "true")
@SerializedName("enabled_by_default")
public boolean enabledByDefault = true;

@SerializedName("// Whether healthbar should always be visible (not just on entity hover) by default.")
public final String _comment_alwaysVisibleDefault = "";
@SerializedName("always_visible_by_default")
@BrigadierDescription(defaultOption = "false")
public boolean alwaysVisibleDefault = false;

@Override
public void save() {
this.saveConfigFile(CONFIG_FILE);
}

public static final class Permissions {
@SerializedName("// Enabled only if LuckPerms is loaded.")
public final String _comment = "";
public final String healthcare_reloadConfig = "healthcare.reloadConfig";
public final String healthcare_config = "healthcare.config";
public final String healthcare_config_edit = "healthcare.config.edit";
public final String healthcare_config_reload = "healthcare.config.reload";

@SerializedName("// Player permissions")
public final String _comment_playerPermissions = "";
Expand All @@ -62,7 +92,6 @@ public static final class Permissions {

public Language lang = new Language();
public static class Language {
public String noPermission = "You don't have permission to run this command.";
public String configReloaded = "Config was reloaded successfully.";
public String customLengthSet = "Length of healthbar was set to %s.";
public String customSymbolSet = "%s healthbar symbol was set to %s.";
Expand All @@ -82,7 +111,7 @@ public static class Language {
* @return HealthConfig object
*/
public static HealthConfig loadConfigFile(File file) {
HealthConfig config;
HealthConfig config = null;
if (file.exists()) {
try (BufferedReader fileReader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)
Expand All @@ -92,10 +121,10 @@ public static HealthConfig loadConfigFile(File file) {
throw new RuntimeException(MODID + " Problem occurred when trying to load config: ", e);
}
}
else {
if (config == null) {
config = new HealthConfig();
}
config.saveConfigFile(file);
config.save();

return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void copyFromPlayer(ServerPlayerEntity oldPlayer, ServerPlayerEntity newP
((HealthbarPreferences) newPlayer).setHealthbarStyle(((HealthbarPreferences) oldPlayer).getHealthbarStyle());
((HealthbarPreferences) newPlayer).setEnabled(((HealthbarPreferences) oldPlayer).isEnabled());
((HealthbarPreferences) newPlayer).setAlwaysVisible(((HealthbarPreferences) oldPlayer).isAlwaysVisible());
((HealthbarPreferences) newPlayer).setShowEntityType(((HealthbarPreferences) oldPlayer).showEntityType());

((HealthbarPreferences) newPlayer).setCustomFullChar(((HealthbarPreferences) oldPlayer).getCustomFullChar());
((HealthbarPreferences) newPlayer).setCustomEmptyChar(((HealthbarPreferences) oldPlayer).getCustomEmptyChar());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public interface HealthbarPreferences {
* Gets style of the healthbar.
* @return healthbar style
*/
Enum<HealthbarStyle> getHealthbarStyle();
void setHealthbarStyle(Enum<HealthbarStyle> healthbarStyle);
HealthbarStyle getHealthbarStyle();
void setHealthbarStyle(HealthbarStyle healthbarStyle);

/**
* Gets the health text from current health and max health depending on HealthbarStyle.
Expand Down
Loading

0 comments on commit eb429c3

Please sign in to comment.