Search in sources :

Example 1 with DelayedRegistration

use of org.spongepowered.api.registry.util.DelayedRegistration in project LanternServer by LanternPowered.

the class AdvancementTreeLayoutModule method registerDefaults.

@DelayedRegistration(RegistrationPhase.INIT)
@Override
public void registerDefaults() {
    for (AdvancementTree tree : AdvancementTreeRegistryModule.get().getAll()) {
        final LanternTreeLayout layout = new LanternTreeLayout(tree);
        layout.generate();
    }
}
Also used : AdvancementTree(org.spongepowered.api.advancement.AdvancementTree) LanternTreeLayout(org.lanternpowered.server.advancement.layout.LanternTreeLayout) DelayedRegistration(org.spongepowered.api.registry.util.DelayedRegistration)

Example 2 with DelayedRegistration

use of org.spongepowered.api.registry.util.DelayedRegistration in project LanternServer by LanternPowered.

the class GeneratorTypeRegistryModule method postInit.

/**
 * Post initialize the {@link GeneratorType}s. All the default world generators
 * here be selected by scanning for 'default-world-gen.json' files.
 */
@CustomCatalogRegistration
@DelayedRegistration(RegistrationPhase.POST_INIT)
public void postInit() {
    final Multimap<String, DefaultEntry> entries = HashMultimap.create();
    final Gson gson = new Gson();
    // Scan every plugin
    for (PluginContainer pluginContainer : Sponge.getPluginManager().getPlugins()) {
        final Optional<Asset> optAsset = pluginContainer.getAsset("default-world-gen.json");
        if (optAsset.isPresent()) {
            try {
                final InputStream is = optAsset.get().getUrl().openStream();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
                    final JsonObject json = gson.fromJson(reader, JsonObject.class);
                    for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
                        entries.put(entry.getKey(), new DefaultEntry(pluginContainer, entry.getValue().getAsString()));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    for (Map.Entry<String, Collection<DefaultEntry>> entry : entries.asMap().entrySet()) {
        final String id = entry.getKey();
        if (!getById(id).map(type -> type instanceof DelegateGeneratorType).orElse(false)) {
            Lantern.getLogger().warn("The plugin(s) ({}) attempted to map an unknown id: {}", Arrays.toString(entry.getValue().stream().map(e -> e.pluginContainer.getId()).toArray()), id);
            continue;
        }
        final List<DefaultEntry> possibleEntries = new ArrayList<>();
        for (DefaultEntry entry1 : entry.getValue()) {
            final Optional<GeneratorType> generatorType = getById(entry1.type);
            if (generatorType.isPresent()) {
                possibleEntries.add(entry1);
            } else {
                Lantern.getLogger().warn("The plugin {} attempted to map a missing generator type {} for {}", entry1.pluginContainer.getId(), entry1.type, id);
            }
        }
        if (!possibleEntries.isEmpty()) {
            final DefaultEntry defaultEntry = possibleEntries.get(0);
            if (possibleEntries.size() > 1) {
                Lantern.getLogger().warn("Multiple plugins are mapping {}: {}", id, Arrays.toString(entry.getValue().stream().map(e -> "\n" + e.pluginContainer.getId() + ": " + e.type).toArray()));
                Lantern.getLogger().warn("The first one will be used.");
            }
            ((DelegateGeneratorType) getById(id).get()).setGeneratorType(getById(defaultEntry.type).get());
            Lantern.getLogger().warn("Successfully registered a generator type mapping: {} from {} for {}", defaultEntry.type, defaultEntry.pluginContainer.getId(), id);
        }
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) FlatTheEndGeneratorType(org.lanternpowered.server.world.gen.flat.FlatTheEndGeneratorType) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) HashMultimap(com.google.common.collect.HashMultimap) FlatOverworldGeneratorType(org.lanternpowered.server.world.gen.flat.FlatOverworldGeneratorType) CustomCatalogRegistration(org.spongepowered.api.registry.util.CustomCatalogRegistration) TheVoidGeneratorType(org.lanternpowered.server.world.gen.thevoid.TheVoidGeneratorType) Gson(com.google.gson.Gson) Map(java.util.Map) BlockRegistryModule(org.lanternpowered.server.game.registry.type.block.BlockRegistryModule) RegistrationDependency(org.spongepowered.api.registry.util.RegistrationDependency) PluginContainer(org.spongepowered.api.plugin.PluginContainer) FlatNetherGeneratorType(org.lanternpowered.server.world.gen.flat.FlatNetherGeneratorType) GeneratorTypes(org.spongepowered.api.world.GeneratorTypes) DebugGeneratorType(org.lanternpowered.server.world.gen.debug.DebugGeneratorType) GeneratorType(org.spongepowered.api.world.GeneratorType) Collection(java.util.Collection) RegistrationPhase(org.spongepowered.api.registry.RegistrationPhase) Sponge(org.spongepowered.api.Sponge) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) DelegateGeneratorType(org.lanternpowered.server.world.gen.DelegateGeneratorType) List(java.util.List) DelayedRegistration(org.spongepowered.api.registry.util.DelayedRegistration) Lantern(org.lanternpowered.server.game.Lantern) BlockStateRegistryModule(org.lanternpowered.server.game.registry.type.block.BlockStateRegistryModule) Optional(java.util.Optional) BufferedReader(java.io.BufferedReader) AdditionalPluginCatalogRegistryModule(org.lanternpowered.server.game.registry.AdditionalPluginCatalogRegistryModule) Asset(org.spongepowered.api.asset.Asset) InputStream(java.io.InputStream) PluginContainer(org.spongepowered.api.plugin.PluginContainer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) FlatTheEndGeneratorType(org.lanternpowered.server.world.gen.flat.FlatTheEndGeneratorType) FlatOverworldGeneratorType(org.lanternpowered.server.world.gen.flat.FlatOverworldGeneratorType) TheVoidGeneratorType(org.lanternpowered.server.world.gen.thevoid.TheVoidGeneratorType) FlatNetherGeneratorType(org.lanternpowered.server.world.gen.flat.FlatNetherGeneratorType) DebugGeneratorType(org.lanternpowered.server.world.gen.debug.DebugGeneratorType) GeneratorType(org.spongepowered.api.world.GeneratorType) DelegateGeneratorType(org.lanternpowered.server.world.gen.DelegateGeneratorType) JsonElement(com.google.gson.JsonElement) BufferedReader(java.io.BufferedReader) Asset(org.spongepowered.api.asset.Asset) Collection(java.util.Collection) Map(java.util.Map) DelegateGeneratorType(org.lanternpowered.server.world.gen.DelegateGeneratorType) CustomCatalogRegistration(org.spongepowered.api.registry.util.CustomCatalogRegistration) DelayedRegistration(org.spongepowered.api.registry.util.DelayedRegistration)

Example 3 with DelayedRegistration

use of org.spongepowered.api.registry.util.DelayedRegistration in project LanternServer by LanternPowered.

the class LanternSmeltingRecipeRegistryModule method registerDefaults.

@DelayedRegistration(RegistrationPhase.POST_INIT)
@Override
public void registerDefaults() {
    final PluginContainer plugin = Lantern.getMinecraftPlugin();
    register(ISmeltingRecipe.builder().ingredient(ItemTypes.COBBLESTONE).result(ItemStack.of(ItemTypes.STONE, 1)).build("stone", plugin));
    final ItemStack result = ItemStack.of(ItemTypes.IRON_INGOT, 1);
    result.offer(Keys.DISPLAY_NAME, Text.of(TextColors.RESET, "Steel Ingot"));
    register(ISmeltingRecipe.builder().ingredient(IIngredient.builder().with(ItemTypes.IRON_INGOT).withQuantity(2).build(), ItemStack.of(ItemTypes.IRON_INGOT, 1).createSnapshot()).result(result).build("steel_ingot", plugin));
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DelayedRegistration(org.spongepowered.api.registry.util.DelayedRegistration)

Example 4 with DelayedRegistration

use of org.spongepowered.api.registry.util.DelayedRegistration in project LanternServer by LanternPowered.

the class LanternFuelRegistryModule method registerDefaults.

@DelayedRegistration(RegistrationPhase.POST_INIT)
@Override
public void registerDefaults() {
    final PluginContainer plugin = Lantern.getMinecraftPlugin();
    register(IFuel.builder().ingredient(IIngredient.builder().with(ItemTypes.LAVA_BUCKET).withRemaining(ItemTypes.BUCKET).build()).burnTime(20000).build("lava_bucket", plugin));
    register(IFuel.builder().ingredient(Ingredient.of(ItemTypes.COAL)).burnTime(1600).build("coal", plugin));
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) DelayedRegistration(org.spongepowered.api.registry.util.DelayedRegistration)

Example 5 with DelayedRegistration

use of org.spongepowered.api.registry.util.DelayedRegistration in project SpongeCommon by SpongePowered.

the class RegistryModuleLoader method isCustomProperPhase.

private static boolean isCustomProperPhase(RegistryModule module) {
    for (Method method : module.getClass().getMethods()) {
        CustomCatalogRegistration registration = method.getDeclaredAnnotation(CustomCatalogRegistration.class);
        DelayedRegistration delay = method.getDeclaredAnnotation(DelayedRegistration.class);
        if (registration != null) {
            if (delay == null) {
                return SpongeImpl.getRegistry().getPhase() == RegistrationPhase.PRE_REGISTRY;
            }
            return SpongeImpl.getRegistry().getPhase() == delay.value();
        }
    }
    return false;
}
Also used : CustomCatalogRegistration(org.spongepowered.api.registry.util.CustomCatalogRegistration) DelayedRegistration(org.spongepowered.api.registry.util.DelayedRegistration) Method(java.lang.reflect.Method)

Aggregations

DelayedRegistration (org.spongepowered.api.registry.util.DelayedRegistration)7 PluginContainer (org.spongepowered.api.plugin.PluginContainer)4 Method (java.lang.reflect.Method)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 CustomCatalogRegistration (org.spongepowered.api.registry.util.CustomCatalogRegistration)2 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1