Skip to content

Commit

Permalink
Merge pull request #12 from wiviw5/main
Browse files Browse the repository at this point in the history
Updating Skyblock Joining & Leaving Logic, Fixed Location updates, Added Logging.
  • Loading branch information
CraftedFury authored May 29, 2023
2 parents b325a98 + 0d60e54 commit 47fb002
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 87 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modRuntimeOnly("me.djtheredstoner:DevAuth-fabric:1.1.2")
Expand Down
81 changes: 72 additions & 9 deletions src/main/java/io/github/skyblockcore/SkyblockCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,59 @@

package io.github.skyblockcore;

import io.github.skyblockcore.event.JoinSkyblockCallback;
import io.github.skyblockcore.event.LeaveSkyblockCallback;
import io.github.skyblockcore.event.LocationChangedCallback;
import com.mojang.brigadier.CommandDispatcher;
import io.github.skyblockcore.command.SkyblockCoreCommand;
import io.github.skyblockcore.event.*;
import io.github.skyblockcore.mixin.HandledScreenFocusedSlotAccessor;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.util.InputUtil;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.ActionResult;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.github.skyblockcore.command.SkyblockCoreCommand.NBTCOPYING;
import static io.github.skyblockcore.event.ConfigManager.loadConfig;

public class SkyblockCore implements ClientModInitializer {
public static final String ModID = "skyblockcore";

public static final String ModID = "skyblockcore";
public static final String TITLE = "[SkyblockCore]";
public static final String SKYBLOCK_SCOREBOARD = "SBScoreboard";
public static final String HEALTH_SCOREBOARD = "health";
private static boolean ON_SKYBLOCK = false;
public static boolean isOnSkyblock() { return ON_SKYBLOCK; }
public static String getLocation() { return LOCATION; }
private static String LOCATION;

public static boolean isOnSkyblock() {
return ON_SKYBLOCK;
}

public static String getLocation() {
return LOCATION;
}

private static String LOCATION;
public static final Logger LOGGER = LoggerFactory.getLogger(ModID);


@Override
public void onInitializeClient() {
// example of events, TODO; move to better class
// We Must load the config first. Otherwise, Events relying on the config such as Location do not work.
loadConfig();
// Install log filter

ConsoleLogFilter.installFilter();

ClientCommandRegistrationCallback.EVENT.register(SkyblockCore::registerCommands);
//ModConfig config = SkyblockCore.getConfig();
// example of events, TODO; move to a better class
JoinSkyblockCallback.EVENT.register(() -> {
ON_SKYBLOCK = true;
return ActionResult.PASS;
Expand All @@ -47,8 +79,39 @@ public void onInitializeClient() {
return ActionResult.PASS;
});
LocationChangedCallback.EVENT.register(((oldLocation, newLocation) -> {
// Simple Logging Statement for mod developers to debug locations affecting their code.
if (ConfigManager.getConfig() != null && ConfigManager.getConfig().isLocation()) {
LOGGER.info(TITLE + " Detected Location Change on Scoreboard! [Dev Old Location] > " + oldLocation);
LOGGER.info(TITLE + " Detected Location Change on Scoreboard! [Dev New Location] > " + newLocation);
}
LOCATION = newLocation;
return ActionResult.PASS;
}));
// Source from https://github.com/apace100/show-me-what-you-got for implementation of the mixin.
ClientTickEvents.START_CLIENT_TICK.register(tick -> {
if (!NBTCOPYING) return;
MinecraftClient client = MinecraftClient.getInstance();
if (client.player != null && client.currentScreen instanceof HandledScreen) {
HandledScreenFocusedSlotAccessor focusedSlotAccessor = (HandledScreenFocusedSlotAccessor) client.currentScreen;
Slot focusedSlot = focusedSlotAccessor.getFocusedSlot();
boolean isCtrlPressed = InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), GLFW.GLFW_KEY_RIGHT_CONTROL);
if (isCtrlPressed) {
if (client.player.currentScreenHandler.getCursorStack().isEmpty() && focusedSlot != null && focusedSlot.hasStack()) {
ItemStack itemToCopyNBT = focusedSlot.getStack();
if (itemToCopyNBT.getNbt() == null) return;
String itemNBT = "minecraft:" + itemToCopyNBT.getItem().getName().getString() + " " + itemToCopyNBT.getNbt();
if (ConfigManager.getConfig() != null && ConfigManager.getConfig().isDev()) {
LOGGER.info(TITLE + " [Dev NBT] > " + itemNBT);
}
MinecraftClient.getInstance().keyboard.setClipboard(itemNBT);
}
}
}
});

}

public static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
SkyblockCoreCommand.register(dispatcher);
}
}
}
111 changes: 111 additions & 0 deletions src/main/java/io/github/skyblockcore/command/SkyblockCoreCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.github.skyblockcore.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.skyblockcore.event.ConfigManager;
import io.github.skyblockcore.event.ModConfig;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import static io.github.skyblockcore.SkyblockCore.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;


public class SkyblockCoreCommand {
public static boolean NBTCOPYING = false;

public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
LiteralArgumentBuilder<FabricClientCommandSource> skyblockcore = literal("skyblockcore");
skyblockcore.then(literal("reload").executes(ctx -> reloadConfig()));
skyblockcore.then(literal("nbt").executes(ctx -> nbt()));
skyblockcore.then(literal("dev").executes(ctx -> dev()));
skyblockcore.then(literal("test").executes(ctx -> test()));
skyblockcore.then(literal("LogSpam").executes(ctx -> player()));
skyblockcore.then(literal("location").executes(ctx -> location()));
dispatcher.register(skyblockcore);
}

private static int test() {
// example command
if (ConfigManager.getConfig() != null && ConfigManager.getConfig().isDev()) {
LOGGER.info(TITLE + " woah!! [dev]");
}
if (MinecraftClient.getInstance().player == null) return 0;
MinecraftClient.getInstance().player.sendMessage(Text.literal(TITLE + " Test command, cool that you found this, easter egg ig! well anyways, hi from Axle!").formatted(Formatting.AQUA), false);
return Command.SINGLE_SUCCESS;
}

private static int reloadConfig() {
ConfigManager.reloadConfig(); // Reload the config
LOGGER.info(TITLE + " Reloaded Config!");
if (MinecraftClient.getInstance().player == null) return 0;
MinecraftClient.getInstance().player.sendMessage(Text.literal(TITLE + " Config file Reloaded in: " + System.currentTimeMillis() + " ms").formatted(Formatting.WHITE), false);


return Command.SINGLE_SUCCESS;
}

private static int nbt() {
// Practically done. Needs help in the main class for copying.
// TODO Implement NBT Handling Logic here
// This will be similar to "/sba dev" or "/skytils dev nbt".
if (MinecraftClient.getInstance().player == null) return 0;
NBTCOPYING = !NBTCOPYING;
MinecraftClient.getInstance().player.sendMessage(Text.literal(TITLE + " NBT copying has been " + (NBTCOPYING ? "enabled! (Tip, use Right Control to copy items NBT data to clipboard.)" : "disabled!")).formatted(Formatting.WHITE), false);
return Command.SINGLE_SUCCESS;
}

private static int dev() {
ModConfig config = ConfigManager.getConfig();
if (config != null) {
boolean currentDevSetting = config.isDev();
config.setDev(!currentDevSetting); // Toggle the dev mode setting
ConfigManager.saveConfig(); // Save the updated config
LOGGER.info(TITLE + " Successfully " + (currentDevSetting ? "disabled" : "enabled") + " Developer mode!");
if (MinecraftClient.getInstance().player == null) return 0;
MinecraftClient.getInstance().player.sendMessage(Text.literal(TITLE + " Dev mode " + (currentDevSetting ? "Disabled" : "Enabled")).formatted(Formatting.WHITE), false);
}
return Command.SINGLE_SUCCESS;
}

private static int location() {
ModConfig config = ConfigManager.getConfig();
if (config != null) {
boolean currentLocationSetting = config.isLocation();
config.setLocation(!currentLocationSetting); // Toggle the location setting
ConfigManager.saveConfig(); // Save the updated config
LOGGER.info(TITLE + " Successfully " + (currentLocationSetting ? "disabled" : "enabled") + " location logging!");
if (MinecraftClient.getInstance().player == null) return 0;
MinecraftClient.getInstance().player.sendMessage(Text.literal(TITLE + " Location data " + (currentLocationSetting ? "Disabled" : "Enabled")).formatted(Formatting.WHITE), false);
}


return Command.SINGLE_SUCCESS;
}


private static int player() {
// example command
ModConfig config = ConfigManager.getConfig();
if (config != null) {
boolean currentPlayerSetting = config.isUnknownPlayer();
config.setUnknownPlayer(!currentPlayerSetting); // Toggle the location setting
ConfigManager.saveConfig(); // Save the updated config
if (MinecraftClient.getInstance().player == null) return 0;
if (ConfigManager.getConfig() != null && ConfigManager.getConfig().isDev()) {
LOGGER.info(TITLE + " Warning! You have " + (currentPlayerSetting ? "disabled" : "enabled") + " \"Ignoring player info update\"");
}
MinecraftClient.getInstance().player.sendMessage(
Text.literal(TITLE + " Warning! You have " + (currentPlayerSetting ? "disabled" : "enabled") + " \"Ignoring player info update\"")
.setStyle(Style.EMPTY.withColor(Formatting.WHITE)),
false
);
}
return Command.SINGLE_SUCCESS;
}

}
73 changes: 73 additions & 0 deletions src/main/java/io/github/skyblockcore/event/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.github.skyblockcore.event;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;

import static io.github.skyblockcore.SkyblockCore.LOGGER;
import static io.github.skyblockcore.SkyblockCore.TITLE;

public class ConfigManager {



private static ModConfig config;

public static void loadConfig() {
File configFile = getConfigFile();
if (configFile.exists()) {
try (FileReader reader = new FileReader(configFile)) {
Gson gson = new GsonBuilder().create();
config = gson.fromJson(reader, ModConfig.class);
if (config != null && config.isDev()) {
LOGGER.info(TITLE + " Config file loaded. [Dev]");
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
createConfig();
LOGGER.info(TITLE + " Config successfully created! Welcome to SkyblockCore, a Skyblock mod designed for modern Minecraft.");
}
}
/*
* Config Guide ~~ Please add to this if you plan on adding config values!
* setDev: False = do not show // True = show
* setUnknownPlayer: True = hide spam // False = show spam
* setLocation: False = do not show // True = show
*/
public static void createConfig() {
config = new ModConfig(); // Below toggles will only change every time you wipe a config!
config.setDev(false); // This will set all development features and logs to "TRUE". this is used for debugging errors with leaving/joining skyblock.
config.setUnknownPlayer(true); // Don't use unless you want random UUID strings its annoying and I hated it. so now its gone!
config.setLocation(false); // this is used for location output (see SkyblockCore file > location stuff) this is used for debugging new locations.
saveConfig();
}

public static void reloadConfig() {
loadConfig();
}

public static ModConfig getConfig() {
return config; // see io.github.skyblockcore/event/ModConfig
}

private static File getConfigFile() {
return FabricLoader.getInstance().getConfigDir().resolve("SkyblockCoreConfig.json").toFile();
}

public static void saveConfig() {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(config);
File configFile = getConfigFile();
try (FileWriter writer = new FileWriter(configFile)) {
writer.write(json);
} catch (IOException e) {
e.printStackTrace();
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/io/github/skyblockcore/event/ConsoleLogFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.skyblockcore.event;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.filter.AbstractFilter;

public class ConsoleLogFilter extends AbstractFilter {

private static final String UNWANTED_LOG_MESSAGE = "Ignoring player info update for unknown player";

public ConsoleLogFilter() {
super(Result.NEUTRAL, Result.DENY);
}

@Override
public Result filter(LogEvent event) {
if (ConfigManager.getConfig() != null && ConfigManager.getConfig().isUnknownPlayer() &&
event.getMessage().getFormattedMessage().contains(UNWANTED_LOG_MESSAGE)) {
return Result.DENY; // Ignore the log event
}
return Result.NEUTRAL; // Allow the log event to proceed
}

public static void installFilter() {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.addFilter(new ConsoleLogFilter());
loggerContext.updateLoggers();
}
}





// if you're reading this you're a femboy now
// Meow! >.<
Loading

0 comments on commit 47fb002

Please sign in to comment.