Search in sources :

Example 1 with DeserializationContext

use of net.minecraft.advancements.critereon.DeserializationContext in project Mohist by MohistMC.

the class CraftMagicNumbers method loadAdvancement.

@Override
public Advancement loadAdvancement(NamespacedKey key, String advancement) {
    if (Bukkit.getAdvancement(key) != null) {
        throw new IllegalArgumentException("Advancement " + key + " already exists.");
    }
    ResourceLocation minecraftkey = CraftNamespacedKey.toMinecraft(key);
    JsonElement jsonelement = ServerAdvancementManager.GSON.fromJson(advancement, JsonElement.class);
    JsonObject jsonobject = GsonHelper.convertToJsonObject(jsonelement, "advancement");
    net.minecraft.advancements.Advancement.Builder nms = net.minecraft.advancements.Advancement.Builder.fromJson(jsonobject, new DeserializationContext(minecraftkey, MinecraftServer.getServer().getPredicateManager()));
    if (nms != null) {
        MinecraftServer.getServer().getAdvancements().advancements.add(Maps.newHashMap(Collections.singletonMap(minecraftkey, nms)));
        Advancement bukkit = Bukkit.getAdvancement(key);
        if (bukkit != null) {
            File file = new File(getBukkitDataPackFolder(), "data" + File.separator + key.getNamespace() + File.separator + "advancements" + File.separator + key.getKey() + ".json");
            file.getParentFile().mkdirs();
            try {
                Files.write(advancement, file, Charsets.UTF_8);
            } catch (IOException ex) {
                Bukkit.getLogger().log(Level.SEVERE, "Error saving advancement " + key, ex);
            }
            MinecraftServer.getServer().getPlayerList().reloadResources();
            return bukkit;
        }
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) JsonObject(com.google.gson.JsonObject) DeserializationContext(net.minecraft.advancements.critereon.DeserializationContext) IOException(java.io.IOException) File(java.io.File) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) Advancement(org.bukkit.advancement.Advancement)

Example 2 with DeserializationContext

use of net.minecraft.advancements.critereon.DeserializationContext in project blueprint by team-abnormals.

the class AdvancementModificationManager method apply.

@Override
protected void apply(Map<ResourceLocation, JsonElement> map, ResourceManager resourceManager, ProfilerFiller profiler) {
    this.reset();
    SelectionSpace unmodifiedAdvancements = this.getUnmodifiedEntries();
    for (Map.Entry<ResourceLocation, JsonElement> entry : map.entrySet()) {
        ResourceLocation resourcelocation = entry.getKey();
        if (resourcelocation.getPath().startsWith("_"))
            continue;
        try {
            TargetedModifier<Builder, Void, DeserializationContext> targetedAdvancementModifier = TargetedModifier.deserialize(entry.getValue().getAsJsonObject(), "advancement", new DeserializationContext(resourcelocation, this.lootPredicateManager), AdvancementModifiers.REGISTRY, true);
            this.addModifiers(targetedAdvancementModifier.getTargetSelector().getTargetNames(unmodifiedAdvancements), targetedAdvancementModifier.getPriority(), targetedAdvancementModifier.getConfiguredModifiers());
        } catch (IllegalArgumentException | JsonParseException jsonparseexception) {
            Blueprint.LOGGER.error("Parsing error loading Advancement Modifier: {}", resourcelocation, jsonparseexception);
        }
    }
    Blueprint.LOGGER.info("Advancement Modification Manager has assigned {} sets of modifiers", this.size());
}
Also used : SelectionSpace(com.teamabnormals.blueprint.core.util.modification.targeting.SelectionSpace) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) GsonBuilder(com.google.gson.GsonBuilder) Builder(net.minecraft.advancements.Advancement.Builder) DeserializationContext(net.minecraft.advancements.critereon.DeserializationContext) JsonParseException(com.google.gson.JsonParseException) Map(java.util.Map)

Aggregations

JsonElement (com.google.gson.JsonElement)2 DeserializationContext (net.minecraft.advancements.critereon.DeserializationContext)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 SelectionSpace (com.teamabnormals.blueprint.core.util.modification.targeting.SelectionSpace)1 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Builder (net.minecraft.advancements.Advancement.Builder)1 Advancement (org.bukkit.advancement.Advancement)1 PluginDescriptionFile (org.bukkit.plugin.PluginDescriptionFile)1