Search in sources :

Example 1 with IFortune

use of com.bewitchment.api.divination.IFortune in project Bewitchment by Um-Mitternacht.

the class TileEntityCrystalBall method readFortune.

@SuppressWarnings({ "deprecation", "null" })
private boolean readFortune(@Nonnull EntityPlayer endPlayer, @Nullable EntityPlayer externalReader) {
    // This is here to support reading to others in the future
    EntityPlayer messageRecpt = endPlayer;
    if (endPlayer.getDistanceSq(this.getPos()) > 25) {
        messageRecpt.sendStatusMessage(new TextComponentTranslation("crystal_ball.error.too_far"), true);
        return false;
    }
    IFortune fortune = endPlayer.getCapability(CapabilityDivination.CAPABILITY, null).getFortune();
    if (fortune != null) {
        messageRecpt.sendStatusMessage(new TextComponentTranslation("crystal_ball.error.already_told", new TextComponentTranslation(fortune.getUnlocalizedName())), false);
        return false;
    }
    List<IFortune> valid = Fortune.REGISTRY.getValuesCollection().parallelStream().filter(f -> f.canBeUsedFor(endPlayer)).collect(Collectors.toList());
    if (valid.size() == 0) {
        messageRecpt.sendStatusMessage(new TextComponentTranslation("crystal_ball.error.no_available_fortunes"), true);
        return false;
    }
    int totalEntries = valid.parallelStream().mapToInt(f -> f.getDrawingWeight()).sum();
    final int draw = endPlayer.getRNG().nextInt(totalEntries);
    int current = 0;
    for (IFortune f : valid) {
        int entries = f.getDrawingWeight();
        if (current < draw && draw < current + entries) {
            fortune = f;
            break;
        }
        current += entries;
    }
    endPlayer.getCapability(CapabilityDivination.CAPABILITY, null).setFortune(fortune);
    endPlayer.sendStatusMessage(new TextComponentTranslation(fortune.getUnlocalizedName()), true);
    return true;
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) List(java.util.List) IFortune(com.bewitchment.api.divination.IFortune) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Fortune(com.bewitchment.common.divination.Fortune) CapabilityDivination(com.bewitchment.common.core.capability.divination.CapabilityDivination) Nonnull(javax.annotation.Nonnull) Collectors(java.util.stream.Collectors) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) Nullable(javax.annotation.Nullable) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IFortune(com.bewitchment.api.divination.IFortune) EntityPlayer(net.minecraft.entity.player.EntityPlayer)

Example 2 with IFortune

use of com.bewitchment.api.divination.IFortune in project Bewitchment by Um-Mitternacht.

the class DivinationEvents method onLivingTick.

@SubscribeEvent
public void onLivingTick(PlayerTickEvent evt) {
    if (!evt.player.world.isRemote && evt.phase == Phase.END) {
        CapabilityDivination data = evt.player.getCapability(CapabilityDivination.CAPABILITY, null);
        IFortune f = data.getFortune();
        if (f != null) {
            if (data.isRemovable()) {
                data.setFortune(null);
            } else {
                if (f.canShouldBeAppliedNow(evt.player)) {
                    data.setActive();
                }
                if (data.isActive()) {
                    if (f.apply(evt.player))
                        data.setFortune(null);
                }
            }
        }
    }
}
Also used : CapabilityDivination(com.bewitchment.common.core.capability.divination.CapabilityDivination) IFortune(com.bewitchment.api.divination.IFortune) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with IFortune

use of com.bewitchment.api.divination.IFortune in project Bewitchment by Um-Mitternacht.

the class CommandForceFortune method execute.

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
    if (args.length == 0)
        throw new WrongUsageException("commands.set_fortune.usage");
    if (sender instanceof EntityPlayer) {
        CapabilityDivination dc = ((EntityPlayer) sender).getCapability(CapabilityDivination.CAPABILITY, null);
        IFortune add = Fortune.REGISTRY.getValue(new ResourceLocation(args[0]));
        if (add == null) {
            throw new CommandException("commands.set_fortune.error.no_fortune");
        }
        dc.setFortune(add);
        sender.sendMessage(new TextComponentTranslation("commands.set_fortune.success"));
    } else {
        throw new CommandException("commands.error.no_console");
    }
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) CapabilityDivination(com.bewitchment.common.core.capability.divination.CapabilityDivination) ResourceLocation(net.minecraft.util.ResourceLocation) IFortune(com.bewitchment.api.divination.IFortune) EntityPlayer(net.minecraft.entity.player.EntityPlayer)

Aggregations

IFortune (com.bewitchment.api.divination.IFortune)3 CapabilityDivination (com.bewitchment.common.core.capability.divination.CapabilityDivination)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)2 Fortune (com.bewitchment.common.divination.Fortune)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1