Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GameObjectHandler enhancement #156

Merged
merged 22 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import com.cleanroommc.groovyscript.documentation.Documentation;
import com.cleanroommc.groovyscript.documentation.linkgenerator.LinkGeneratorHooks;
import com.cleanroommc.groovyscript.event.EventHandler;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlerManager;
import com.cleanroommc.groovyscript.helper.JsonHelper;
import com.cleanroommc.groovyscript.mapper.ObjectMapper;
import com.cleanroommc.groovyscript.mapper.ObjectMapperManager;
import com.cleanroommc.groovyscript.network.CReload;
import com.cleanroommc.groovyscript.network.NetworkHandler;
import com.cleanroommc.groovyscript.network.NetworkUtils;
Expand Down Expand Up @@ -155,9 +156,12 @@ public static void initializeRunConfig(File minecraftHome) {
@ApiStatus.Internal
public static void initializeGroovyPreInit() {
// called via mixin in between construction and fml pre init
GameObjectHandlerManager.init();
ObjectMapperManager.init();
VanillaModule.initializeBinding();
ModSupport.init();
for (ObjectMapper<?> goh : ObjectMapperManager.getObjectMappers()) {
getSandbox().registerBinding(goh);
}
if (FMLLaunchHandler.isDeobfuscatedEnvironment()) Documentation.generate();
runGroovyScriptsInLoader(LoadStage.PRE_INIT);
}
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/com/cleanroommc/groovyscript/api/GroovyPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,4 @@ public interface GroovyPlugin extends IGroovyContainer {
default boolean isLoaded() {
return true;
}

/**
* Returns the override priority. Defines how this plugin should behave when another container with the same mod id exists.
* The return value should be as low as possible. Internal container always return {@link Priority#NONE}.
* @return the override priority
* @see Priority
*/
@NotNull
default Priority getOverridePriority() {
return Priority.NONE;
}

enum Priority {
/**
* Default. Can be overridden by anything and can't override anything.
*/
NONE,
/**
* Can override containers with priority NONE.
*/
OVERRIDE,
/**
* Can override containers with priority NONE, OVERRIDE.
*/
OVERRIDE_HIGH,
/**
* Can override containers with priority NONE, OVERRIDE, OVERRIDE_HIGH.
*/
OVERRIDE_HIGHEST
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/api/Hidden.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.cleanroommc.groovyscript.api;

public interface Hidden {

boolean isHidden();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.cleanroommc.groovyscript.api;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

/**
* When this is implemented on a class, {@link #getProperty(String)} will be called when groovy tries to get a field from this class
*/
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@Deprecated
public interface IDynamicGroovyProperty {

/**
Expand Down Expand Up @@ -34,5 +37,5 @@ default boolean setProperty(String name, @Nullable Object value) {
*
* @return all properties
*/
Map<String, Object> getProperties();
Map<String, ?> getProperties();
}
brachy84 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.cleanroommc.groovyscript.api;

import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlers;
import com.cleanroommc.groovyscript.mapper.ObjectMappers;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistry;
Expand Down Expand Up @@ -35,7 +35,7 @@ public interface IGameObjectParser<T> {

static <T extends IForgeRegistryEntry<T>> IGameObjectParser<T> wrapForgeRegistry(IForgeRegistry<T> forgeRegistry) {
return (s, args) -> {
Result<ResourceLocation> rl = GameObjectHandlers.parseResourceLocation(s, args);
Result<ResourceLocation> rl = ObjectMappers.parseResourceLocation(s, args);
if (rl.hasError()) return Result.error(rl.getError());
T value = forgeRegistry.getValue(rl.getValue());
return value == null ? Result.error() : Result.some(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,41 @@ default Collection<String> getAliases() {
/**
* Called before scripts are executed for the first time. Called right before {@link ModPropertyContainer#initialize()}.
* Used to initialize things like expansions with {@link com.cleanroommc.groovyscript.sandbox.expand.ExpansionHelper ExpansionHelper} and
* game object handlers with {@link com.cleanroommc.groovyscript.gameobjects.GameObjectHandlerManager GameObjectHandlerManager}.
* object mappers with {@link com.cleanroommc.groovyscript.mapper.ObjectMapperManager ObjectMapperManager}.
*
* @param container the created container for the compat mod
*/
@ApiStatus.OverrideOnly
void onCompatLoaded(GroovyContainer<?> container);

/**
* Returns the override priority. Defines how this plugin should behave when another container with the same mod id exists.
* The return value should be as low as possible. Internal container always return {@link GroovyPlugin.Priority#NONE}.
*
* @return the override priority
* @see GroovyPlugin.Priority
*/
@NotNull
default GroovyPlugin.Priority getOverridePriority() {
return GroovyPlugin.Priority.NONE;
brachy84 marked this conversation as resolved.
Show resolved Hide resolved
}

enum Priority {
/**
* Default. Can be overridden by anything and can't override anything.
*/
NONE,
/**
* Can override containers with priority NONE.
*/
OVERRIDE,
/**
* Can override containers with priority NONE, OVERRIDE.
*/
OVERRIDE_HIGH,
/**
* Can override containers with priority NONE, OVERRIDE, OVERRIDE_HIGH.
*/
OVERRIDE_HIGHEST
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
/**
* A helper interface to register {@link INamed registries} without having direct access to the
* {@link com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer ModPropertyContainer}.
* An instance can be obtained from {@link GroovyContainer#getVirtualizedRegistrar()}.
* An instance can be obtained from {@link GroovyContainer#getRegistrar()}.
*/
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@Deprecated
@ApiStatus.NonExtendable
public interface IRegistrar {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.cleanroommc.groovyscript.compat.mods;

import com.cleanroommc.groovyscript.api.GroovyPlugin;
import com.cleanroommc.groovyscript.api.IGroovyContainer;

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,23 +9,28 @@
import java.util.Objects;
import java.util.Set;

/**
* This is used for external mod compat. Don't use this directly. Instead, implement {@link GroovyPlugin} on any class.
* This class will then be automatically instanced.
*/
public class ExternalModContainer extends GroovyContainer<ModPropertyContainer> {

private final IGroovyContainer groovyContainer;
private final GroovyPlugin groovyContainer;
private final ModPropertyContainer container;
private final String modId;
private final String containerName;
private final Collection<String> aliases;
private final Priority priority;

ExternalModContainer(@NotNull GroovyPlugin groovyContainer, @NotNull ModPropertyContainer container) {
super(groovyContainer.getOverridePriority());
this.groovyContainer = Objects.requireNonNull(groovyContainer);
this.container = Objects.requireNonNull(container);
this.modId = groovyContainer.getModId();
this.containerName = groovyContainer.getContainerName();
Set<String> aliasSet = new ObjectOpenHashSet<>(groovyContainer.getAliases());
aliasSet.add(modId);
this.aliases = Collections.unmodifiableSet(aliasSet);
this.priority = groovyContainer.getOverridePriority();
}

@Override
Expand Down Expand Up @@ -60,4 +63,9 @@ public void onCompatLoaded(GroovyContainer<?> container) {
public ModPropertyContainer get() {
return container;
}

@Override
public @NotNull GroovyPlugin.Priority getOverridePriority() {
return priority;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.cleanroommc.groovyscript.compat.mods;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.GroovyPlugin;
import com.cleanroommc.groovyscript.api.IGroovyContainer;
import com.cleanroommc.groovyscript.api.INamed;
import com.cleanroommc.groovyscript.api.IRegistrar;
import com.cleanroommc.groovyscript.mapper.ObjectMapper;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.NonExtendable
public abstract class GroovyContainer<T extends ModPropertyContainer> implements IGroovyContainer {

private final GroovyPlugin.Priority overridePriority;

protected GroovyContainer(GroovyPlugin.Priority overridePriority) {
this.overridePriority = overridePriority;
}

public abstract T get();

@Override
Expand All @@ -23,20 +18,34 @@ public String toString() {
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "1.1.0")
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@GroovyBlacklist
public IRegistrar getVirtualizedRegistrar() {
return getRegistrar();
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@GroovyBlacklist
public IRegistrar getRegistrar() {
if (!isLoaded()) return null;
T t = get();
return t::addRegistry;
return t::addProperty;
}

public void addProperty(INamed property) {
if (isLoaded()) {
get().addProperty(property);
}
}

public void addPropertiesOfFields(Object o, boolean privateToo) {
if (isLoaded()) {
get().addPropertyFieldsOf(o, privateToo);
}
}

public GroovyPlugin.Priority getOverridePriority() {
return overridePriority;
public <V> ObjectMapper.Builder<V> objectMapper(String name, Class<V> returnType) {
return new ObjectMapper.Builder<>(name, returnType).mod(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import java.util.Set;

/**
* Will not be removed but made private and renamed to InternalContainer
* This class is only used for internal mod compat. Do not use this. Instead, refer to {@link ExternalModContainer} ans {@link GroovyPlugin}.
*
* @param <T> type of the mod property container.
*/
@SuppressWarnings("all")
public class InternalModContainer<T extends ModPropertyContainer> extends GroovyContainer<T> {

private final String modId, containerName;
Expand All @@ -28,7 +29,6 @@ public class InternalModContainer<T extends ModPropertyContainer> extends Groovy
}

InternalModContainer(String modId, String containerName, @NotNull Supplier<T> modProperty, String... aliases) {
super(GroovyPlugin.Priority.NONE);
if (ModSupport.isFrozen()) {
throw new RuntimeException("Groovy mod containers must be registered at construction event! Tried to register '" + containerName + "' too late.");
}
Expand All @@ -39,7 +39,11 @@ public class InternalModContainer<T extends ModPropertyContainer> extends Groovy
}
this.modId = modId;
this.containerName = containerName;
this.modProperty = Suppliers.memoize(modProperty);
this.modProperty = Suppliers.memoize(() -> {
T t = modProperty.get();
t.addPropertyFieldsOf(t, false);
return t;
});
this.loaded = Loader.isModLoaded(modId);
Set<String> aliasSet = new ObjectOpenHashSet<>(aliases);
aliasSet.add(modId);
Expand Down
Loading
Loading