fix fabric tree injection issue

This commit is contained in:
dfsek 2021-07-15 14:45:26 -07:00
parent d94ddb3e76
commit 9d2ae0a828
6 changed files with 48 additions and 29 deletions

View File

@ -48,6 +48,10 @@ public class ConfigurationLoadEvent implements PackEvent {
return type;
}
public boolean is(Class<?> clazz) {
return clazz.isAssignableFrom(type.getTypeClass());
}
@SuppressWarnings("unchecked")
public <T> T getLoadedObject(Class<T> clazz) {
if(!clazz.isAssignableFrom(type.getTypeClass()))

View File

@ -9,14 +9,21 @@ public abstract class ConfigTypeLoadEvent implements PackEvent {
private final ConfigType<?, ?> type;
private final CheckedRegistry<?> registry;
public ConfigTypeLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry) {
private final ConfigPack pack;
public ConfigTypeLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
this.type = type;
this.registry = registry;
this.pack = pack;
}
@Override
public ConfigPack getPack() {
return null;
return pack;
}
public boolean is(Class<?> clazz) {
return clazz.isAssignableFrom(type.getTypeClass());
}
@SuppressWarnings("unchecked")

View File

@ -1,10 +1,11 @@
package com.dfsek.terra.api.event.events.config.type;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.CheckedRegistry;
public class ConfigTypePostLoadEvent extends ConfigTypeLoadEvent{
public ConfigTypePostLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry) {
super(type, registry);
public ConfigTypePostLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
super(type, registry, pack);
}
}

View File

@ -1,10 +1,11 @@
package com.dfsek.terra.api.event.events.config.type;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.CheckedRegistry;
public class ConfigTypePreLoadEvent extends ConfigTypeLoadEvent{
public ConfigTypePreLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry) {
super(type, registry);
public ConfigTypePreLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
super(type, registry, pack);
}
}

View File

@ -246,7 +246,7 @@ public class ConfigPackImpl implements ConfigPack {
for(ConfigType<?, ?> configType : configTypeRegistry.entries()) { // Load the configs
CheckedRegistry registry = getCheckedRegistry(configType.getTypeClass());
main.getEventManager().callEvent(new ConfigTypePreLoadEvent(configType, registry));
main.getEventManager().callEvent(new ConfigTypePreLoadEvent(configType, registry, this));
for(AbstractConfiguration config : abstractConfigLoader.loadConfigs(configs.getOrDefault(configType, Collections.emptyList()))) {
try {
Object loaded = ((ConfigFactory) configType.getFactory()).build(selfLoader.load(configType.getTemplate(this, main), config), main);
@ -256,7 +256,7 @@ public class ConfigPackImpl implements ConfigPack {
throw new LoadException("Duplicate registry entry: ", e);
}
}
main.getEventManager().callEvent(new ConfigTypePostLoadEvent(configType, registry));
main.getEventManager().callEvent(new ConfigTypePostLoadEvent(configType, registry, this));
}
main.getEventManager().callEvent(new ConfigPackPostLoadEvent(this, template -> selfLoader.load(template, configuration)));

View File

@ -20,6 +20,7 @@ import com.dfsek.terra.api.event.annotations.Global;
import com.dfsek.terra.api.event.annotations.Priority;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPostLoadEvent;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.events.config.type.ConfigTypePostLoadEvent;
import com.dfsek.terra.api.handle.ItemHandle;
import com.dfsek.terra.api.handle.WorldHandle;
import com.dfsek.terra.api.lang.Language;
@ -295,9 +296,34 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
eventManager.registerListener(this, this);
}
@Global
public void onPackLoad(ConfigPackPreLoadEvent event) {
PreLoadCompatibilityOptions template = new PreLoadCompatibilityOptions();
try {
event.loadTemplate(template);
} catch(ConfigException e) {
e.printStackTrace();
}
if(template.doRegistryInjection()) {
BuiltinRegistries.CONFIGURED_FEATURE.getEntries().forEach(entry -> {
if(!template.getExcludedRegistryFeatures().contains(entry.getKey().getValue())) {
try {
event.getPack().getCheckedRegistry(Tree.class).register(entry.getKey().getValue().toString(), (Tree) entry.getValue());
debugLogger.info("Injected ConfiguredFeature " + entry.getKey().getValue() + " as Tree.");
} catch(DuplicateEntryException ignored) {
}
}
});
}
templates.put(event.getPack(), Pair.of(template, null));
}
@Priority(Priority.HIGHEST)
@Global
public void injectTrees(ConfigPackPreLoadEvent event) {
public void injectTrees(ConfigTypePostLoadEvent event) {
if(!event.is(Tree.class)) return;
CheckedRegistry<Tree> treeRegistry = event.getPack().getOrCreateRegistry(Tree.class);
injectTree(treeRegistry, "BROWN_MUSHROOM", ConfiguredFeatures.HUGE_BROWN_MUSHROOM);
injectTree(treeRegistry, "RED_MUSHROOM", ConfiguredFeatures.HUGE_RED_MUSHROOM);
@ -318,26 +344,6 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
injectTree(treeRegistry, "MEGA_SPRUCE", ConfiguredFeatures.MEGA_SPRUCE);
injectTree(treeRegistry, "CRIMSON_FUNGUS", ConfiguredFeatures.CRIMSON_FUNGI);
injectTree(treeRegistry, "WARPED_FUNGUS", ConfiguredFeatures.WARPED_FUNGI);
PreLoadCompatibilityOptions template = new PreLoadCompatibilityOptions();
try {
event.loadTemplate(template);
} catch(ConfigException e) {
e.printStackTrace();
}
if(template.doRegistryInjection()) {
BuiltinRegistries.CONFIGURED_FEATURE.getEntries().forEach(entry -> {
if(!template.getExcludedRegistryFeatures().contains(entry.getKey().getValue())) {
try {
event.getPack().getCheckedRegistry(Tree.class).register(entry.getKey().getValue().toString(), (Tree) entry.getValue());
debugLogger.info("Injected ConfiguredFeature " + entry.getKey().getValue() + " as Tree.");
} catch(DuplicateEntryException ignored) {
}
}
});
}
templates.put(event.getPack(), Pair.of(template, null));
}
@Priority(Priority.HIGHEST)