Skip to content

Commit

Permalink
Fake mod registering
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcat101 committed Sep 14, 2024
1 parent 6df414d commit 6f542a2
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ logs
eclipse
run
run_server
/log
.vscode
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dev.latvian.kubejs.registry.types;

import org.jetbrains.annotations.Contract;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.latvian.mods.rhino.util.HideFromJS;

/**
* @author G_cat101
*/
public class FakeModBuilder {
public String modId;
public String displayName;
public String namespace;
public String description = "";
public String version = "1.0";

public FakeModBuilder(String modId) {
this.modId = modId;
this.namespace = modId;
this.displayName = modId;

addFakeMod(this);
}

@ExpectPlatform
@HideFromJS
@Contract(value = "_ -> null")
static void addFakeMod(FakeModBuilder builder) {
throw new AssertionError("Not Implemented");
}

public FakeModBuilder displayName(String displayName) {
this.displayName = displayName;
return this;
}
public FakeModBuilder namespace(String namespace) {
this.namespace = namespace;
return this;
}
public FakeModBuilder description(String description) {
this.description = description;
return this;
}
public FakeModBuilder version(String version) {
this.version = version;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.architectury.injectables.targets.ArchitecturyTarget;
import dev.latvian.kubejs.KubeJS;
import dev.latvian.kubejs.registry.types.FakeModBuilder;
import dev.latvian.mods.rhino.util.HideFromJS;
import me.shedaniel.architectury.platform.Mod;
import me.shedaniel.architectury.platform.Platform;
Expand Down Expand Up @@ -104,6 +105,13 @@ public static Map<String, ModInfo> getMods() {
return MOD_MAP;
}

public static FakeModBuilder registerFakeMod(String modId) {
return new FakeModBuilder(modId);
}
public static FakeModBuilder createFakeMod(String modId) {
return registerFakeMod(modId);
}

public static boolean isDevelopmentEnvironment() {
return Platform.isDevelopmentEnvironment();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.latvian.kubejs.fabric.fakemods;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import com.google.common.collect.Lists;

import dev.latvian.kubejs.registry.types.FakeModBuilder;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.fabricmc.loader.api.metadata.ModOrigin;
import net.fabricmc.loader.impl.metadata.ModOriginImpl;

public class FakeModContainer implements ModContainer {
private FakeModMetadata metadata;

public FakeModContainer(FakeModBuilder builder) {
this.metadata = new FakeModMetadata(builder);
}

@Override
public ModMetadata getMetadata() {
return metadata;
}

@Override
public List<Path> getRootPaths() {
return Lists.newArrayList();
}
@Override
public ModOrigin getOrigin() {
return new ModOriginImpl();
}
@Override
public Optional<ModContainer> getContainingMod() {
return Optional.empty();
}
@Override
public Collection<ModContainer> getContainedMods() {
return Lists.newArrayList();
}
@Override
public Path getRootPath() {
return null;
}
@Override
public Path getPath(String file) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package dev.latvian.kubejs.fabric.fakemods;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import dev.latvian.kubejs.registry.types.FakeModBuilder;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.metadata.ContactInformation;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.api.metadata.ModDependency;
import net.fabricmc.loader.api.metadata.ModEnvironment;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.fabricmc.loader.api.metadata.Person;
import net.fabricmc.loader.impl.metadata.ContactInformationImpl;
import net.fabricmc.loader.impl.util.version.StringVersion;

public class FakeModMetadata implements ModMetadata {
private FakeModBuilder builder;

public FakeModMetadata(FakeModBuilder builder) {
this.builder = builder;
}

@Override
public String getId() {
return builder.modId;
}
@Override
public Version getVersion() {
return new StringVersion(builder.version);
}
@Override
public String getName() {
return builder.displayName;
}
@Override
public String getDescription() {
return builder.description;
}

@Override
public String getType() {
return "fabric";
}
@Override
public Collection<String> getProvides() {
return Lists.newArrayList();
}
@Override
public ModEnvironment getEnvironment() {
return ModEnvironment.UNIVERSAL;
}
@Override
public Collection<ModDependency> getDependencies() {
return Lists.newArrayList();
}

@Override
public Collection<Person> getAuthors() {
return Lists.newArrayList();
}
@Override
public Collection<Person> getContributors() {
return Lists.newArrayList();
}
@Override
public ContactInformation getContact() {
return new ContactInformationImpl(Maps.newHashMap());
}
@Override
public Collection<String> getLicense() {
return Lists.newArrayList();
}
@Override
public Optional<String> getIconPath(int size) {
return Optional.empty();
}
@Override
public boolean containsCustomValue(String key) {
return false;
}
@Override
public CustomValue getCustomValue(String key) {
return null;
}
@Override
public Map<String, CustomValue> getCustomValues() {
return Maps.newHashMap();
}
@Override
public boolean containsCustomElement(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.latvian.kubejs.mixin.fabric;

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

import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.impl.FabricLoaderImpl;
import net.fabricmc.loader.impl.ModContainerImpl;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import dev.latvian.kubejs.registry.types.fabric.FakeModBuilderImpl;

@Mixin(FabricLoaderImpl.class)
public class FabricLoaderImplMixin {
@Shadow(remap = false) protected List<ModContainerImpl> mods;

@Inject(method = "getAllMods", at = @At(value = "HEAD"), remap = false)
private void getCategory(CallbackInfoReturnable<Collection<ModContainer>> cir) {
ArrayList<ModContainer> modsWithFake = new ArrayList<>();

modsWithFake.addAll(mods);
modsWithFake.addAll(FakeModBuilderImpl.fakeMods);

cir.setReturnValue(modsWithFake);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.latvian.kubejs.registry.types.fabric;

import java.util.ArrayList;
import dev.latvian.kubejs.fabric.fakemods.FakeModContainer;
import dev.latvian.kubejs.registry.types.FakeModBuilder;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.impl.FabricLoaderImpl;

/**
* @author G_cat101
*/
public class FakeModBuilderImpl {
public static ArrayList<ModContainer> fakeMods = new ArrayList<>();

private static ArrayList<String> getModIds() {
ArrayList<String> modIds = new ArrayList<>();
FabricLoaderImpl.INSTANCE.getAllMods().forEach(m -> {
modIds.add(m.getMetadata().getId());
});
return modIds;
}

public static void addFakeMod(FakeModBuilder builder) throws Exception {
if (getModIds().contains(builder.modId)) {
throw new IllegalArgumentException("Tried to create a fake mod with id '" + builder.modId + "', but a mod with that id already exists.");
}

fakeMods.add(new FakeModContainer(builder));
}
}
3 changes: 2 additions & 1 deletion fabric/src/main/resources/kubejs.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"mixins": [
],
"client": [
"ClientRecipeBookMixin"
"ClientRecipeBookMixin",
"FabricLoaderImplMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
44 changes: 44 additions & 0 deletions forge/src/main/java/dev/latvian/kubejs/forge/FakeModInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dev.latvian.kubejs.forge;

import java.net.URL;
import java.util.List;
import java.util.Map;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import dev.latvian.kubejs.registry.types.FakeModBuilder;
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;
import net.minecraftforge.forgespi.language.IModFileInfo;
import net.minecraftforge.forgespi.language.IModInfo;

public class FakeModInfo implements IModInfo {
private FakeModBuilder builder;

public FakeModInfo(FakeModBuilder builder) {
this.builder = builder;
}

@Override
public String getModId() {return builder.modId;}
@Override
public String getDisplayName() {return builder.displayName;}
@Override
public String getNamespace() {return builder.namespace;}
@Override
public String getDescription() {return builder.description;}
@Override
public ArtifactVersion getVersion() {return new DefaultArtifactVersion(builder.version);}

@Override
public IModFileInfo getOwningFile() {return null;}
@Override
public List<? extends ModVersion> getDependencies() {return Lists.newArrayList();}
@Override
public Map<String, Object> getModProperties() {return Maps.newHashMap();}
@Override
public URL getUpdateURL() {return null;}
}
Loading

0 comments on commit 6f542a2

Please sign in to comment.