Skip to content

Commit

Permalink
Done..?
Browse files Browse the repository at this point in the history
  • Loading branch information
Ale32bit committed Dec 22, 2024
1 parent 9219dda commit 6112d7d
Show file tree
Hide file tree
Showing 160 changed files with 1,208 additions and 1,392 deletions.
41 changes: 19 additions & 22 deletions src/main/java/me/alexdevs/solstice/Solstice.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package me.alexdevs.solstice;

import com.mojang.brigadier.CommandDispatcher;
import me.alexdevs.solstice.api.events.ModuleRegistrationCallback;
import me.alexdevs.solstice.api.events.SolsticeEvents;
import me.alexdevs.solstice.api.events.WorldSave;
import me.alexdevs.solstice.data.PlayerDataManager;
import me.alexdevs.solstice.data.ServerData;
import me.alexdevs.solstice.integrations.LuckPermsIntegration;
import me.alexdevs.solstice.locale.LocaleManager;
import me.alexdevs.solstice.modules.Modules;
import me.alexdevs.solstice.core.Modules;
import me.alexdevs.solstice.modules.ModuleProvider;
import me.alexdevs.solstice.util.data.HoconDataManager;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -38,35 +39,35 @@ public class Solstice implements ModInitializer {

public static final HoconDataManager configManager = new HoconDataManager(configDirectory.resolve("config.conf"));
public static final LocaleManager localeManager = new LocaleManager(configDirectory.resolve("locale.json"));

private static Solstice INSTANCE;

public static Solstice getInstance() {
return INSTANCE;
}

public static MinecraftServer server;

public static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public static final RegistryKey<MessageType> CHAT_TYPE = RegistryKey.of(RegistryKeys.MESSAGE_TYPE, new Identifier(MOD_ID, "chat"));

public static final ServerData serverData = new ServerData();
public static final PlayerDataManager playerData = new PlayerDataManager();

public static final Modules modules = new Modules();

private static final ConcurrentLinkedQueue<Runnable> nextTickRunnables = new ConcurrentLinkedQueue<>();
public static MinecraftServer server;
public static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private static Solstice INSTANCE;

public Solstice() {
INSTANCE = this;
}

public static Solstice getInstance() {
return INSTANCE;
}

public static void nextTick(Runnable runnable) {
nextTickRunnables.add(runnable);
}

@Override
public void onInitialize() {
var modMeta = FabricLoader.getInstance().getModContainer(MOD_ID).get().getMetadata();
LOGGER.info("Initializing Solstice v{}...", modMeta.getVersion());

ModuleProvider.register();
modules.register();

try {
configManager.prepareData();
configManager.save();
Expand All @@ -84,14 +85,14 @@ public void onInitialize() {
return;
}

if(FabricLoader.getInstance().isModLoaded("luckperms")) {
if (FabricLoader.getInstance().isModLoaded("luckperms")) {
LuckPermsIntegration.register();
}

ServerLifecycleEvents.SERVER_STARTING.register(server -> {
Solstice.server = server;
var path = server.getSavePath(WorldSavePath.ROOT).resolve("data").resolve(MOD_ID);
if(!path.toFile().exists()) {
if (!path.toFile().exists()) {
path.toFile().mkdirs();
}
serverData.setDataPath(path.resolve("server.json"));
Expand All @@ -116,8 +117,4 @@ public void onInitialize() {
public void broadcast(Text text) {
server.getPlayerManager().broadcast(text, false);
}

public static void nextTick(Runnable runnable) {
nextTickRunnables.add(runnable);
}
}
1 change: 0 additions & 1 deletion src/main/java/me/alexdevs/solstice/api/ServerPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.annotations.Expose;
import me.alexdevs.solstice.Solstice;
import me.alexdevs.solstice.modules.afk.PlayerPosition;
import me.alexdevs.solstice.modules.back.BackModule;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/me/alexdevs/solstice/api/command/TimeSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@

// https://github.com/NucleusPowered/Nucleus/blob/v3/nucleus-core/src/main/java/io/github/nucleuspowered/nucleus/core/scaffold/command/parameter/TimespanParameter.java
public class TimeSpan {
public static final SimpleCommandExceptionType INVALID_TIMESPAN = new SimpleCommandExceptionType(new LiteralMessage("Invalid timespan"));
private static final Pattern minorTimeString = Pattern.compile("^\\d+$");
private static final Pattern timeString = Pattern.compile("^((\\d+)w)?((\\d+)d)?((\\d+)h)?((\\d+)m)?((\\d+)s)?$");
private static final Pattern timeStringNoEnd = Pattern.compile("^((\\d+)w)?((\\d+)d)?((\\d+)h)?((\\d+)m)?((\\d+)s)?");
private static final Pattern lastDigits = Pattern.compile("\\d+$");

private static final int secondsInMinute = 60;
private static final int secondsInHour = 60 * secondsInMinute;
private static final int secondsInDay = 24 * secondsInHour;
private static final int secondsInWeek = 7 * secondsInDay;

public static final SimpleCommandExceptionType INVALID_TIMESPAN = new SimpleCommandExceptionType(new LiteralMessage("Invalid timespan"));

private static int amount(@Nullable final String g, final int multiplier) {
if (g != null && !g.isEmpty()) {
return multiplier * Integer.parseUnsignedInt(g);
Expand Down Expand Up @@ -113,7 +111,6 @@ public static CompletableFuture<Suggestions> suggest(CommandContext<ServerComman
}



return builder.buildFuture();
}

Expand Down
27 changes: 0 additions & 27 deletions src/main/java/me/alexdevs/solstice/api/events/ModuleEvents.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.alexdevs.solstice.api.events;

import me.alexdevs.solstice.api.module.ModuleBase;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

import java.util.HashSet;

public interface ModuleRegistrationCallback {
Event<ModuleRegistrationCallback> EVENT = EventFactory.createArrayBacked(ModuleRegistrationCallback.class,
(listeners) -> () -> {
var modules = new HashSet<ModuleBase>();
for (ModuleRegistrationCallback listener : listeners) {
modules.addAll(listener.register());
}

return modules;
});

HashSet<? extends ModuleBase> register();
}
18 changes: 1 addition & 17 deletions src/main/java/me/alexdevs/solstice/api/module/ModCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.alexdevs.solstice.Solstice;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
Expand All @@ -12,10 +11,10 @@
import java.util.function.Predicate;

public abstract class ModCommand<T extends ModuleBase> {
protected final T module;
protected CommandDispatcher<ServerCommandSource> dispatcher;
protected CommandRegistryAccess commandRegistry;
protected CommandManager.RegistrationEnvironment environment;
protected final T module;

public ModCommand(T module) {
this.commandRegistry = null;
Expand All @@ -25,21 +24,6 @@ public ModCommand(T module) {
this.module = module;
}

@Deprecated(forRemoval = true)
public ModCommand(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistry, CommandManager.RegistrationEnvironment environment) {
this.dispatcher = dispatcher;
this.commandRegistry = commandRegistry;
this.environment = environment;

this.module = null;

this.register();
}

public void register() {
this.register(dispatcher, commandRegistry, environment);
}

public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistry, CommandManager.RegistrationEnvironment environment) {
this.dispatcher = dispatcher;
this.commandRegistry = commandRegistry;
Expand Down
34 changes: 31 additions & 3 deletions src/main/java/me/alexdevs/solstice/api/module/ModuleBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@
import me.alexdevs.solstice.Solstice;
import me.alexdevs.solstice.locale.Locale;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public abstract class ModuleBase {
public abstract class ModuleBase implements Comparable<ModuleBase> {
protected final String id;
protected final List<ModCommand<?>> commands = new ArrayList<>();

public ModuleBase(String id) {
this.id = id;
}

public abstract Collection<? extends ModCommand<?>> getCommands();
public Collection<? extends ModCommand<?>> getCommands() {
return commands;
}

public String getId() {
return id;
}

public String getPermissionNode() {
return Solstice.MOD_ID + "." + id;
return Solstice.MOD_ID + "." + id.toLowerCase();
}

public String getPermissionNode(String sub) {
Expand All @@ -29,4 +35,26 @@ public String getPermissionNode(String sub) {
public Locale locale() {
return Solstice.localeManager.getLocale(id);
}

@Override
public String toString() {
return "Module [" + id + "]";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ModuleBase that)) return false;
return Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return Objects.hashCode(id);
}

@Override
public int compareTo(ModuleBase o) {
return id.compareTo(o.id);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.alexdevs.solstice.modules;
package me.alexdevs.solstice.api.module;

import com.mojang.brigadier.CommandDispatcher;

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/me/alexdevs/solstice/core/Modules.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.alexdevs.solstice.core;

import com.mojang.brigadier.CommandDispatcher;
import me.alexdevs.solstice.api.events.ModuleRegistrationCallback;
import me.alexdevs.solstice.api.module.ModuleBase;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;

import java.util.HashSet;

public class Modules {

private HashSet<? extends ModuleBase> modules = new HashSet<>();

public Modules() {
CommandRegistrationCallback.EVENT.register(this::registerCommands);
}

public <T> T getModule(Class<T> classOfModule) {
for (var module : modules) {
if (classOfModule.isInstance(module)) {
return classOfModule.cast(module);
}
}
return null;
}

public void register() {
modules = ModuleRegistrationCallback.EVENT.invoker().register();
}

private void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistry, CommandManager.RegistrationEnvironment environment) {
for (var module : modules) {
for(var command : module.getCommands()) {
command.register(dispatcher, commandRegistry, environment);
}
}
}
}
8 changes: 2 additions & 6 deletions src/main/java/me/alexdevs/solstice/data/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.google.gson.*;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.ConfigurateException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -21,15 +19,13 @@ public class PlayerData {
protected final Map<String, Class<?>> classMap = new HashMap<>();
protected final Map<Class<?>, Object> data = new HashMap<>();
protected final Map<Class<?>, Supplier<?>> providers = new HashMap<>();

protected JsonObject node;

protected final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
.serializeNulls()
.create();
protected JsonObject node;

public PlayerData(Path basePath, UUID uuid, Map<String, Class<?>> classMap, Map<Class<?>, Supplier<?>> providers) {
this.uuid = uuid;
Expand Down Expand Up @@ -88,7 +84,7 @@ public void loadData(boolean force) {
}

protected JsonObject loadNode() {
if(!this.filePath.toFile().exists())
if (!this.filePath.toFile().exists())
return new JsonObject();
try (var fr = new FileReader(this.filePath.toFile())) {
var reader = gson.newJsonReader(fr);
Expand Down
Loading

0 comments on commit 6112d7d

Please sign in to comment.