Skip to content

Commit

Permalink
Modify Ketting.java for more clarity, add BukkitAdapter, moved some f…
Browse files Browse the repository at this point in the history
…unctions to SharedConstants, add NOOP versions for some adapters
  • Loading branch information
JustRed23 committed Nov 28, 2023
1 parent 182a08a commit 6619503
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.kettingpowered.ketting.adapter;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kettingpowered.ketting.types.Plugin;

import java.io.File;
import java.util.List;

public interface BukkitAdapter extends SharedConstants {

@Nullable Plugin getPlugin(String name);
@Nullable List<Plugin> getPlugins();
@Nullable String getPluginName(String name);
@Nullable String getPluginName(@NotNull Plugin plugin);

void loadPlugin(@NotNull File pluginFile) throws Exception;
void loadPlugins(@NotNull File pluginDirectory) throws Exception;

void enablePlugin(@NotNull Plugin plugin);
void disablePlugin(@NotNull Plugin plugin);
void disablePlugins();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.kettingpowered.ketting.adapter;

public interface DimensionRegistry<R, K, V> {
public interface DimensionRegistry<R, K, V> extends SharedConstants {

String getMcVersion();
void createDefaults();
void register(R registry);
K getKey(V value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

import java.util.List;

public interface ForgeAdapter {

String getMcVersion();
public interface ForgeAdapter extends SharedConstants {

@Nullable Mod getMod(String modID);
@Nullable List<Mod> getMods();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.kettingpowered.ketting.adapter;

interface SharedConstants {

String getMcVersion();

default boolean isValidAdapter() {
return getMcVersion() != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.kettingpowered.ketting.adapter.noop;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kettingpowered.ketting.adapter.BukkitAdapter;
import org.kettingpowered.ketting.types.Plugin;

import java.io.File;
import java.util.List;

public class NOOPBukkitAdapter implements BukkitAdapter {

public static final NOOPBukkitAdapter INSTANCE = new NOOPBukkitAdapter();

public String getMcVersion() {
return null;
}

public @Nullable Plugin getPlugin(String name) {
return null;
}

public @Nullable List<Plugin> getPlugins() {
return null;
}

public @Nullable String getPluginName(String name) {
return null;
}

public @Nullable String getPluginName(@NotNull Plugin plugin) {
return null;
}

public void loadPlugin(@NotNull File pluginFile) throws Exception {}

public void loadPlugins(@NotNull File pluginDirectory) throws Exception {}

public void enablePlugin(@NotNull Plugin plugin) {}

public void disablePlugin(@NotNull Plugin plugin) {}

public void disablePlugins() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.kettingpowered.ketting.adapter.noop;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kettingpowered.ketting.adapter.ForgeAdapter;
import org.kettingpowered.ketting.types.Mod;

import java.util.List;

public class NOOPForgeAdapter implements ForgeAdapter {

public static final NOOPForgeAdapter INSTANCE = new NOOPForgeAdapter();

public String getMcVersion() {
return null;
}

public @Nullable Mod getMod(String modID) {
return null;
}

public @Nullable List<Mod> getMods() {
return null;
}

public @Nullable String getModName(String modID) {
return null;
}

public @Nullable String getModName(@NotNull Mod mod) {
return null;
}
}
46 changes: 37 additions & 9 deletions src/main/java/org/kettingpowered/ketting/core/Ketting.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.kettingpowered.ketting.core;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.kettingpowered.ketting.adapter.BukkitAdapter;
import org.kettingpowered.ketting.adapter.DimensionRegistry;
import org.kettingpowered.ketting.adapter.ForgeAdapter;
import org.kettingpowered.ketting.adapter.noop.NOOPBukkitAdapter;
import org.kettingpowered.ketting.adapter.noop.NOOPForgeAdapter;
import org.kettingpowered.ketting.core.injectprotect.InjectProtect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -17,11 +21,15 @@ public final class Ketting {
private static Ketting INSTANCE;
private static String mcVersion;

private final List<ForgeAdapter> AVAILABLE_ADAPTERS = new ArrayList<>();
private final List<ForgeAdapter> AVAILABLE_FORGE_ADAPTERS = new ArrayList<>();
private final List<BukkitAdapter> AVAILABLE_BUKKIT_ADAPTERS = new ArrayList<>();
private final List<DimensionRegistry> AVAILABLE_DIMENSION_REGISTRIES = new ArrayList<>();

/**
* Should only be called by the server implementation
*/
public static Ketting init(String mcVersion) {
if (Ketting.mcVersion != null)
if (isInitialized())
throw new RuntimeException("Ketting is already initialized");

Ketting.mcVersion = mcVersion;
Expand All @@ -34,6 +42,10 @@ public static Ketting getInstance() {
return INSTANCE;
}

public static boolean isInitialized() {
return mcVersion != null;
}

public static String getCoreVersion() {
return coreVersion;
}
Expand All @@ -42,22 +54,38 @@ public static String getMcVersion() {
return mcVersion;
}

public Ketting() {
private Ketting() {
LOGGER.info("KettingCore " + coreVersion + " for Minecraft " + mcVersion + " initializing...");
InjectProtect.init();
}

public void registerAdapter(ForgeAdapter adapter) {
AVAILABLE_ADAPTERS.add(adapter);
public void registerAdapter(ForgeAdapter adapter, BukkitAdapter bukkitAdapter) {
AVAILABLE_FORGE_ADAPTERS.add(adapter);
AVAILABLE_BUKKIT_ADAPTERS.add(bukkitAdapter);
}

public @NotNull ForgeAdapter getForgeAdapter() {
ForgeAdapter adapter = AVAILABLE_FORGE_ADAPTERS.stream()
.filter(adptr -> adptr.getMcVersion().equals(mcVersion))
.findFirst().orElse(null);

if (adapter == null) {
LOGGER.error("Could not find a valid Forge adapter for Minecraft version " + mcVersion);
return NOOPForgeAdapter.INSTANCE;
}

return adapter;
}

public @NotNull ForgeAdapter getAdapter() {
ForgeAdapter adapter = AVAILABLE_ADAPTERS.stream()
public @NotNull BukkitAdapter getBukkitAdapter() {
BukkitAdapter adapter = AVAILABLE_BUKKIT_ADAPTERS.stream()
.filter(adptr -> adptr.getMcVersion().equals(mcVersion))
.findFirst().orElse(null);

if (adapter == null)
throw new RuntimeException("Could not find an adapter for Minecraft version " + mcVersion);
if (adapter == null) {
LOGGER.error("Could not find a valid Bukkit adapter for Minecraft version " + mcVersion);
return NOOPBukkitAdapter.INSTANCE;
}

return adapter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ private static Throwable findCause(Throwable error) {
if (modid == null)
return null;

return Ketting.getInstance().getAdapter().getModName(modid);
return Ketting.getInstance().getForgeAdapter().getModName(modid);
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/kettingpowered/ketting/types/Plugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.kettingpowered.ketting.types;

import java.io.File;
import java.util.logging.Logger;

public record Plugin(
PluginInfo info,
boolean enabled,
File dataFolder,
Logger logger
) {}
29 changes: 29 additions & 0 deletions src/main/java/org/kettingpowered/ketting/types/PluginInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.kettingpowered.ketting.types;

import java.util.List;
import java.util.Map;

public record PluginInfo(
String name,
List<String> provides,
String main,
String classLoaderOf,
List<String> depends,
List<String> softDepends,
List<String> loadBefore,
String version,
Map<String, Map<String, Object>> commands,
String description,
List<String> authors,
List<String> contributors,
String website,
String prefix,
PluginLoadOrder order,
String apiVersion,
List<String> libraries
) {
public enum PluginLoadOrder {
STARTUP,
POSTWORLD
}
}

0 comments on commit 6619503

Please sign in to comment.