Search in sources :

Example 1 with Advancement

use of net.minecraft.advancements.Advancement in project SpongeCommon by SpongePowered.

the class MixinCriterionProgress method grant.

@Override
public Instant grant() {
    if (this.obtained != null) {
        return this.obtained.toInstant();
    }
    final Advancement advancement = (Advancement) ((org.spongepowered.api.advancement.AdvancementProgress) this.advancementProgress).getAdvancement();
    ((IMixinAdvancementProgress) this.advancementProgress).getPlayerAdvancements().grantCriterion(advancement, getCriterion().getName());
    return this.obtained.toInstant();
}
Also used : Advancement(net.minecraft.advancements.Advancement)

Example 2 with Advancement

use of net.minecraft.advancements.Advancement in project SpongeCommon by SpongePowered.

the class MixinCriterionProgress method revoke.

@Override
public Optional<Instant> revoke() {
    if (this.obtained == null) {
        return Optional.empty();
    }
    final Instant instant = this.obtained.toInstant();
    final Advancement advancement = (Advancement) ((org.spongepowered.api.advancement.AdvancementProgress) this.advancementProgress).getAdvancement();
    ((IMixinAdvancementProgress) this.advancementProgress).getPlayerAdvancements().revokeCriterion(advancement, getCriterion().getName());
    return Optional.of(instant);
}
Also used : Instant(java.time.Instant) Advancement(net.minecraft.advancements.Advancement)

Example 3 with Advancement

use of net.minecraft.advancements.Advancement in project SpongeCommon by SpongePowered.

the class MixinICriterionTriggerListener method onGrantCriterion.

@SuppressWarnings("unchecked")
@Inject(method = "grantCriterion", at = @At("HEAD"), cancellable = true)
private void onGrantCriterion(PlayerAdvancements playerAdvancements, CallbackInfo ci) {
    final org.spongepowered.api.advancement.Advancement advancement = (org.spongepowered.api.advancement.Advancement) this.advancement;
    AdvancementCriterion advancementCriterion = (AdvancementCriterion) this.advancement.getCriteria().get(this.criterionName);
    final IMixinCriterion mixinCriterion = (IMixinCriterion) advancementCriterion;
    if (mixinCriterion.getScoreCriterion() != null) {
        advancementCriterion = mixinCriterion.getScoreCriterion();
    }
    // Sponge filters are always handled in the trigger method
    if (!(this.criterionInstance instanceof SpongeFilteredTrigger)) {
        final FilteredTrigger filteredTrigger = (FilteredTrigger) this.criterionInstance;
        if (filteredTrigger.getType() instanceof SpongeTrigger) {
            final Cause cause = Sponge.getCauseStackManager().getCurrentCause();
            final Player player = ((IMixinPlayerAdvancements) playerAdvancements).getPlayer();
            final TypeToken typeToken = TypeToken.of(filteredTrigger.getType().getConfigurationType());
            final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, filteredTrigger, true);
            SpongeImpl.postEvent(event);
            if (!event.getResult()) {
                ci.cancel();
                return;
            }
        }
    }
    SpongeImpl.getCauseStackManager().pushCause(this.criterionInstance);
    // the score be increased by one.
    if (advancementCriterion instanceof ScoreAdvancementCriterion) {
        ((IMixinPlayerAdvancements) playerAdvancements).getPlayer().getProgress(advancement).get((ScoreAdvancementCriterion) advancementCriterion).get().add(1);
        ci.cancel();
        SpongeImpl.getCauseStackManager().popCause();
    }
}
Also used : ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) Player(org.spongepowered.api.entity.living.player.Player) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeTrigger(org.spongepowered.common.advancement.SpongeTrigger) IMixinPlayerAdvancements(org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) FilteredTrigger(org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) TypeToken(com.google.common.reflect.TypeToken) Cause(org.spongepowered.api.event.cause.Cause) Advancement(net.minecraft.advancements.Advancement) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with Advancement

use of net.minecraft.advancements.Advancement in project Wizardry by TeamWizardry.

the class BlockFluidMana method onEntityUpdate.

@SubscribeEvent
public static void onEntityUpdate(EntityUpdateEvent event) {
    Entity entityIn = event.getEntity();
    BlockPos pos = entityIn.getPosition();
    World world = entityIn.world;
    IBlockState state = world.getBlockState(pos);
    if (state.getBlock() == ModFluids.MANA.getActualBlock()) {
        // Fizz all entities in the pool
        if (world.isRemote)
            run(world, pos, state.getBlock(), entityIn, entity -> true, entity -> LibParticles.FIZZING_AMBIENT(world, entityIn.getPositionVector()));
        // Nullify gravity of player
        if (!world.isRemote)
            run(world, pos, state.getBlock(), entityIn, entity -> entity instanceof EntityLivingBase, entity -> {
                ((EntityLivingBase) entityIn).addPotionEffect(new PotionEffect(ModPotions.NULLIFY_GRAVITY, 100, 0, true, false));
                if (RandUtil.nextInt(50) == 0)
                    entity.attackEntityFrom(DamageSourceMana.INSTANCE, 0.1f);
            });
        // Subtract player food
        run(world, pos, state.getBlock(), entityIn, entity -> entity instanceof EntityPlayer, entity -> {
            if (!world.isRemote) {
                MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
                Advancement advancement = server.getAdvancementManager().getAdvancement(new ResourceLocation(Wizardry.MODID, "advancements/advancement_crunch.json"));
                if (advancement == null)
                    return;
                AdvancementProgress progress = ((EntityPlayerMP) entity).getAdvancements().getProgress(advancement);
                for (String s : progress.getRemaningCriteria()) {
                    ((EntityPlayerMP) entity).getAdvancements().grantCriterion(advancement, s);
                }
            }
            if (!((EntityPlayer) entity).capabilities.isCreativeMode && RandUtil.nextInt(50) == 0)
                ((EntityPlayer) entity).getFoodStats().addExhaustion(1f);
        });
        // Explode explodable items
        run(world, pos, state.getBlock(), entityIn, entity -> entity instanceof EntityItem && ((EntityItem) entity).getItem().getItem() instanceof IExplodable, entity -> FluidTracker.INSTANCE.addManaCraft(entity.world, entity.getPosition(), new ManaRecipes.ExplodableCrafter()));
    }
    run(world, pos, state.getBlock(), entityIn, entity -> entity instanceof EntityItem && ManaRecipes.RECIPES.keySet().stream().anyMatch(item -> item.apply(((EntityItem) entity).getItem())), entity -> {
        List<Map.Entry<Ingredient, FluidRecipeLoader.FluidCrafter>> allEntries = ManaRecipes.RECIPES.entries().stream().filter(entry -> entry.getValue().getFluid().getBlock() == state.getBlock() && entry.getKey().apply(((EntityItem) entity).getItem())).collect(Collectors.toList());
        allEntries.forEach(crafter -> FluidTracker.INSTANCE.addManaCraft(entity.world, entity.getPosition(), crafter.getValue().build()));
    });
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ManaRecipes(com.teamwizardry.wizardry.crafting.mana.ManaRecipes) FMLCommonHandler(net.minecraftforge.fml.common.FMLCommonHandler) Random(java.util.Random) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) PotionEffect(net.minecraft.potion.PotionEffect) BlockModFluid(com.teamwizardry.librarianlib.features.base.fluid.BlockModFluid) ModFluid(com.teamwizardry.librarianlib.features.base.fluid.ModFluid) MinecraftServer(net.minecraft.server.MinecraftServer) Block(net.minecraft.block.Block) Vec3d(net.minecraft.util.math.Vec3d) Map(java.util.Map) FluidRecipeLoader(com.teamwizardry.wizardry.crafting.mana.FluidRecipeLoader) Nonnull(javax.annotation.Nonnull) EntityItem(net.minecraft.entity.item.EntityItem) Entity(net.minecraft.entity.Entity) AdvancementProgress(net.minecraft.advancements.AdvancementProgress) World(net.minecraft.world.World) Predicate(java.util.function.Predicate) Wizardry(com.teamwizardry.wizardry.Wizardry) ModPotions(com.teamwizardry.wizardry.init.ModPotions) Advancement(net.minecraft.advancements.Advancement) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) DamageSourceMana(com.teamwizardry.wizardry.common.core.DamageSourceMana) EntityUpdateEvent(com.teamwizardry.librarianlib.features.forgeevents.EntityUpdateEvent) FluidTracker(com.teamwizardry.wizardry.api.block.FluidTracker) Consumer(java.util.function.Consumer) IBlockState(net.minecraft.block.state.IBlockState) List(java.util.List) IFluidBlock(net.minecraftforge.fluids.IFluidBlock) IExplodable(com.teamwizardry.wizardry.api.item.IExplodable) Material(net.minecraft.block.material.Material) EntityLivingBase(net.minecraft.entity.EntityLivingBase) LibParticles(com.teamwizardry.wizardry.client.fx.LibParticles) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ResourceLocation(net.minecraft.util.ResourceLocation) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent) RandUtil(com.teamwizardry.wizardry.api.util.RandUtil) Entity(net.minecraft.entity.Entity) AdvancementProgress(net.minecraft.advancements.AdvancementProgress) IBlockState(net.minecraft.block.state.IBlockState) PotionEffect(net.minecraft.potion.PotionEffect) World(net.minecraft.world.World) MinecraftServer(net.minecraft.server.MinecraftServer) IExplodable(com.teamwizardry.wizardry.api.item.IExplodable) ResourceLocation(net.minecraft.util.ResourceLocation) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) FluidRecipeLoader(com.teamwizardry.wizardry.crafting.mana.FluidRecipeLoader) Advancement(net.minecraft.advancements.Advancement) EntityItem(net.minecraft.entity.item.EntityItem) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 5 with Advancement

use of net.minecraft.advancements.Advancement in project SpongeVanilla by SpongePowered.

the class MixinAdvancementManager method onLoadAdvancements.

@Redirect(method = "reload", at = @At(value = "INVOKE", target = "Lnet/minecraft/advancements/AdvancementList;loadAdvancements(Ljava/util/Map;)V"))
private void onLoadAdvancements(AdvancementList advancementList, Map<ResourceLocation, Advancement.Builder> advancements) {
    if (!SpongeImpl.isInitialized()) {
        return;
    }
    for (PluginContainer pluginContainer : Sponge.getPluginManager().getPlugins()) {
        // Skip the minecraft advancements
        if (pluginContainer.getId().equals("minecraft")) {
            continue;
        }
        final Optional<Path> optSource = pluginContainer.getSource();
        if (optSource.isPresent()) {
            final Path source = optSource.get();
            final String base = "assets/" + pluginContainer.getId() + "/advancements";
            Path root;
            if (Files.isDirectory(source)) {
                root = source.resolve(base);
            } else {
                try {
                    final FileSystem fileSystem = FileSystems.newFileSystem(source, null);
                    root = fileSystem.getPath("/" + base);
                } catch (IOException e) {
                    SpongeImpl.getLogger().error("Error loading FileSystem from jar: ", e);
                    continue;
                }
            }
            if (!Files.exists(root)) {
                continue;
            }
            try {
                Files.walk(root).forEach(path -> {
                    if (!FilenameUtils.getExtension(path.getFileName().toString()).equals("json")) {
                        return;
                    }
                    final Path relPath = root.relativize(path);
                    final String id = FilenameUtils.removeExtension(relPath.toString()).replaceAll("\\\\", "/");
                    final ResourceLocation resourceLocation = new ResourceLocation(pluginContainer.getId(), id);
                    if (!advancements.containsKey(resourceLocation)) {
                        try (BufferedReader reader = Files.newBufferedReader(path)) {
                            final Advancement.Builder advancementBuilder = JsonUtils.fromJson(GSON, reader, Advancement.Builder.class);
                            advancements.put(resourceLocation, advancementBuilder);
                        } catch (IOException e) {
                            SpongeImpl.getLogger().error("Failed to read advancement " + resourceLocation + " from path " + path, e);
                        }
                    }
                });
            } catch (IOException e) {
                SpongeImpl.getLogger().error("Failed to walk path: " + root, e);
            }
        }
    }
    advancementList.loadAdvancements(advancements);
}
Also used : Path(java.nio.file.Path) PluginContainer(org.spongepowered.api.plugin.PluginContainer) FileSystem(java.nio.file.FileSystem) ResourceLocation(net.minecraft.util.ResourceLocation) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Advancement(net.minecraft.advancements.Advancement) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Advancement (net.minecraft.advancements.Advancement)12 AdvancementProgress (net.minecraft.advancements.AdvancementProgress)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 ResourceLocation (net.minecraft.util.ResourceLocation)3 Map (java.util.Map)2 AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)2 IMixinCriterion (org.spongepowered.common.interfaces.advancement.IMixinCriterion)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 TypeToken (com.google.common.reflect.TypeToken)1 BlockModFluid (com.teamwizardry.librarianlib.features.base.fluid.BlockModFluid)1 ModFluid (com.teamwizardry.librarianlib.features.base.fluid.ModFluid)1 EntityUpdateEvent (com.teamwizardry.librarianlib.features.forgeevents.EntityUpdateEvent)1 Wizardry (com.teamwizardry.wizardry.Wizardry)1 FluidTracker (com.teamwizardry.wizardry.api.block.FluidTracker)1 IExplodable (com.teamwizardry.wizardry.api.item.IExplodable)1 RandUtil (com.teamwizardry.wizardry.api.util.RandUtil)1 LibParticles (com.teamwizardry.wizardry.client.fx.LibParticles)1 DamageSourceMana (com.teamwizardry.wizardry.common.core.DamageSourceMana)1 FluidRecipeLoader (com.teamwizardry.wizardry.crafting.mana.FluidRecipeLoader)1