Config and logging | I dont even know anymore
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package de.winniepat.parrotmod;
|
||||||
|
|
||||||
|
import de.winniepat.parrotmod.config.ConfigManager;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class ParrotLogger {
|
||||||
|
public static void logInfo(Class<?> className, String message) {
|
||||||
|
Logger logger = LoggerFactory.getLogger(ConfigManager.class);
|
||||||
|
if (Parrotmod.DEBUG) {
|
||||||
|
logger.info(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void logError(Class<?> className, String message, Exception e) {
|
||||||
|
Logger logger = LoggerFactory.getLogger(className);
|
||||||
|
if (Parrotmod.DEBUG) {
|
||||||
|
logger.error(message, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
package de.winniepat.parrotmod;
|
package de.winniepat.parrotmod;
|
||||||
|
|
||||||
|
import de.winniepat.parrotmod.config.ConfigManager;
|
||||||
import de.winniepat.parrotmod.discord.DiscordRPCManager;
|
import de.winniepat.parrotmod.discord.DiscordRPCManager;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
|
||||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class Parrotmod implements ModInitializer {
|
public class Parrotmod implements ModInitializer {
|
||||||
|
|
||||||
|
private static final Logger DEBUG_LOGGER = LoggerFactory.getLogger("ParrotMod-Debug");
|
||||||
|
|
||||||
|
public static final boolean DEBUG = true;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
|
ConfigManager.load();
|
||||||
DiscordRPCManager.init();
|
DiscordRPCManager.init();
|
||||||
|
|
||||||
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> DiscordRPCManager.shutdown());
|
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> DiscordRPCManager.shutdown());
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.jagrosh.discordipc.entities.Packet;
|
|||||||
import com.jagrosh.discordipc.entities.RichPresence;
|
import com.jagrosh.discordipc.entities.RichPresence;
|
||||||
import com.jagrosh.discordipc.entities.User;
|
import com.jagrosh.discordipc.entities.User;
|
||||||
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
|
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
|
||||||
|
import de.winniepat.parrotmod.config.ConfigManager;
|
||||||
import net.minecraft.client.player.LocalPlayer;
|
import net.minecraft.client.player.LocalPlayer;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
|
||||||
@@ -16,7 +17,6 @@ import java.util.List;
|
|||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class DiscordRPCManager {
|
public class DiscordRPCManager {
|
||||||
private static final long APPLICATION_ID = 1517985621396820039L;
|
|
||||||
private static final long UPDATE_COOLDOWN_MS = 15_000;
|
private static final long UPDATE_COOLDOWN_MS = 15_000;
|
||||||
|
|
||||||
private static IPCClient client;
|
private static IPCClient client;
|
||||||
@@ -47,7 +47,27 @@ public class DiscordRPCManager {
|
|||||||
private DiscordRPCManager() {}
|
private DiscordRPCManager() {}
|
||||||
|
|
||||||
public static void init() {
|
public static void init() {
|
||||||
client = new IPCClient(APPLICATION_ID);
|
ConfigManager.registerListener(cfg -> {
|
||||||
|
if (cfg.enableDiscordRPC) {
|
||||||
|
if (!connected && client == null) {
|
||||||
|
startClient();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (client != null) {
|
||||||
|
shutdown();
|
||||||
|
client = null;
|
||||||
|
connected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (ConfigManager.getInstance().enableDiscordRPC) {
|
||||||
|
startClient();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void startClient() {
|
||||||
|
client = new IPCClient(ConfigManager.getInstance().discordAppId);
|
||||||
client.setListener(new IPCListener() {
|
client.setListener(new IPCListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onPacketSent(IPCClient client, Packet packet) {
|
public void onPacketSent(IPCClient client, Packet packet) {
|
||||||
@@ -109,6 +129,7 @@ public class DiscordRPCManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void updateFromPlayer(LocalPlayer player) {
|
private static void updateFromPlayer(LocalPlayer player) {
|
||||||
|
var cfg = ConfigManager.getInstance();
|
||||||
String dimension = player.level().dimension().registry().getPath();
|
String dimension = player.level().dimension().registry().getPath();
|
||||||
String biomeName = player.level().getBiome(player.blockPosition())
|
String biomeName = player.level().getBiome(player.blockPosition())
|
||||||
.unwrapKey()
|
.unwrapKey()
|
||||||
@@ -117,12 +138,27 @@ public class DiscordRPCManager {
|
|||||||
ItemStack held = player.getMainHandItem();
|
ItemStack held = player.getMainHandItem();
|
||||||
String heldName = held.isEmpty() ? "empty hands" : held.getHoverName().getString();
|
String heldName = held.isEmpty() ? "empty hands" : held.getHoverName().getString();
|
||||||
|
|
||||||
String details = switch (dimension) {
|
String details;
|
||||||
case "the_nether" -> "Surviving the Nether";
|
if (cfg.showBiomeInRPC) {
|
||||||
case "the_end" -> "Facing the End";
|
details = switch (dimension) {
|
||||||
default -> "Exploring the " + prettify(biomeName);
|
case "the_nether" -> "Surviving the Nether";
|
||||||
};
|
case "the_end" -> "Facing the End";
|
||||||
String state = "Wielding " + heldName + " · " + (int) player.getHealth() + " HP";
|
default -> "Exploring the " + prettify(biomeName);
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
details = "Playing Minecraft";
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder stateBuilder = new StringBuilder();
|
||||||
|
if (cfg.showHeldItemInRPC) {
|
||||||
|
stateBuilder.append("Wielding ").append(heldName);
|
||||||
|
}
|
||||||
|
if (cfg.showHealthInRPC) {
|
||||||
|
if (stateBuilder.length() > 0) stateBuilder.append(" · ");
|
||||||
|
stateBuilder.append((int) player.getHealth()).append(" HP");
|
||||||
|
}
|
||||||
|
String state = stateBuilder.toString();
|
||||||
|
if (state.isEmpty()) state = "In-game";
|
||||||
|
|
||||||
updatePresence(details, state);
|
updatePresence(details, state);
|
||||||
}
|
}
|
||||||
@@ -132,7 +168,7 @@ public class DiscordRPCManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void updatePresence(String details, String state) {
|
public static void updatePresence(String details, String state) {
|
||||||
if (!connected) return;
|
if (!connected || !ConfigManager.getInstance().enableDiscordRPC) return;
|
||||||
RichPresence presence = new RichPresence.Builder()
|
RichPresence presence = new RichPresence.Builder()
|
||||||
.setActivityType(ActivityType.Playing)
|
.setActivityType(ActivityType.Playing)
|
||||||
.setDetails(details)
|
.setDetails(details)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.winniepat.parrotmod.ui;
|
package de.winniepat.parrotmod.ui;
|
||||||
|
|
||||||
|
import de.winniepat.parrotmod.config.ConfigManager;
|
||||||
import icyllis.modernui.fragment.Fragment;
|
import icyllis.modernui.fragment.Fragment;
|
||||||
import icyllis.modernui.util.DataSet;
|
import icyllis.modernui.util.DataSet;
|
||||||
import icyllis.modernui.view.LayoutInflater;
|
import icyllis.modernui.view.LayoutInflater;
|
||||||
@@ -8,7 +9,6 @@ import icyllis.modernui.view.ViewGroup;
|
|||||||
import icyllis.modernui.widget.LinearLayout;
|
import icyllis.modernui.widget.LinearLayout;
|
||||||
import icyllis.modernui.widget.Switch;
|
import icyllis.modernui.widget.Switch;
|
||||||
import icyllis.modernui.widget.TextView;
|
import icyllis.modernui.widget.TextView;
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
|
|
||||||
public class DiscordTabFragment extends Fragment {
|
public class DiscordTabFragment extends Fragment {
|
||||||
|
|
||||||
@@ -20,13 +20,40 @@ public class DiscordTabFragment extends Fragment {
|
|||||||
|
|
||||||
layout.addView(makeLabel("Enable DiscordRPC"));
|
layout.addView(makeLabel("Enable DiscordRPC"));
|
||||||
Switch toggleDiscordRPC = new Switch(getContext());
|
Switch toggleDiscordRPC = new Switch(getContext());
|
||||||
toggleDiscordRPC.setChecked(true); // TODO: load from config
|
toggleDiscordRPC.setChecked(ConfigManager.getInstance().enableDiscordRPC);
|
||||||
toggleDiscordRPC.setOnCheckedChangeListener((v, checked) -> {
|
toggleDiscordRPC.setOnCheckedChangeListener((v, checked) -> {
|
||||||
// TODO: save to config
|
ConfigManager.getInstance().enableDiscordRPC = checked;
|
||||||
// MyConfig.enableDiscordRPC = checked;
|
ConfigManager.save();
|
||||||
});
|
});
|
||||||
layout.addView(toggleDiscordRPC);
|
layout.addView(toggleDiscordRPC);
|
||||||
|
|
||||||
|
layout.addView(makeLabel("Show Biome in RPC"));
|
||||||
|
Switch toggleBiome = new Switch(getContext());
|
||||||
|
toggleBiome.setChecked(ConfigManager.getInstance().showBiomeInRPC);
|
||||||
|
toggleBiome.setOnCheckedChangeListener((v, checked) -> {
|
||||||
|
ConfigManager.getInstance().showBiomeInRPC = checked;
|
||||||
|
ConfigManager.save();
|
||||||
|
});
|
||||||
|
layout.addView(toggleBiome);
|
||||||
|
|
||||||
|
layout.addView(makeLabel("Show Held Item in RPC"));
|
||||||
|
Switch toggleHeldItem = new Switch(getContext());
|
||||||
|
toggleHeldItem.setChecked(ConfigManager.getInstance().showHeldItemInRPC);
|
||||||
|
toggleHeldItem.setOnCheckedChangeListener((v, checked) -> {
|
||||||
|
ConfigManager.getInstance().showHeldItemInRPC = checked;
|
||||||
|
ConfigManager.save();
|
||||||
|
});
|
||||||
|
layout.addView(toggleHeldItem);
|
||||||
|
|
||||||
|
layout.addView(makeLabel("Show Health in RPC"));
|
||||||
|
Switch toggleHealth = new Switch(getContext());
|
||||||
|
toggleHealth.setChecked(ConfigManager.getInstance().showHealthInRPC);
|
||||||
|
toggleHealth.setOnCheckedChangeListener((v, checked) -> {
|
||||||
|
ConfigManager.getInstance().showHealthInRPC = checked;
|
||||||
|
ConfigManager.save();
|
||||||
|
});
|
||||||
|
layout.addView(toggleHealth);
|
||||||
|
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.winniepat.parrotmod.ui;
|
package de.winniepat.parrotmod.ui;
|
||||||
|
|
||||||
import de.winniepat.parrotmod.Parrotmod;
|
import de.winniepat.parrotmod.Parrotmod;
|
||||||
|
import de.winniepat.parrotmod.config.ConfigManager;
|
||||||
import icyllis.modernui.fragment.Fragment;
|
import icyllis.modernui.fragment.Fragment;
|
||||||
import icyllis.modernui.util.DataSet;
|
import icyllis.modernui.util.DataSet;
|
||||||
import icyllis.modernui.view.*;
|
import icyllis.modernui.view.*;
|
||||||
@@ -17,6 +18,18 @@ public class GeneralTabFragment extends Fragment {
|
|||||||
|
|
||||||
layout.addView(makeLabel("Minecraft Version: " + Minecraft.getInstance().getLaunchedVersion()));
|
layout.addView(makeLabel("Minecraft Version: " + Minecraft.getInstance().getLaunchedVersion()));
|
||||||
|
|
||||||
|
Button reloadBtn = new Button(getContext());
|
||||||
|
reloadBtn.setText("Reload Config");
|
||||||
|
reloadBtn.setOnClickListener(v -> ConfigManager.load());
|
||||||
|
var p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||||
|
p.topMargin = dp(16);
|
||||||
|
layout.addView(reloadBtn, p);
|
||||||
|
|
||||||
|
Button saveBtn = new Button(getContext());
|
||||||
|
saveBtn.setText("Save Config");
|
||||||
|
saveBtn.setOnClickListener(v -> ConfigManager.save());
|
||||||
|
layout.addView(saveBtn, p);
|
||||||
|
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,10 +34,11 @@ public class SettingsFragment extends Fragment {
|
|||||||
contentParams.leftMargin = dp(6);
|
contentParams.leftMargin = dp(6);
|
||||||
root.addView(contentArea, contentParams);
|
root.addView(contentArea, contentParams);
|
||||||
|
|
||||||
String[] tabNames = {"Info", "General"};
|
String[] tabNames = {"Info", "General", "Discord"};
|
||||||
Fragment[] tabFragments = {
|
Fragment[] tabFragments = {
|
||||||
new InfoTabFragment(),
|
new InfoTabFragment(),
|
||||||
new GeneralTabFragment()
|
new GeneralTabFragment(),
|
||||||
|
new DiscordTabFragment()
|
||||||
};
|
};
|
||||||
|
|
||||||
getChildFragmentManager().beginTransaction()
|
getChildFragmentManager().beginTransaction()
|
||||||
|
|||||||
Reference in New Issue
Block a user