Search in sources :

Example 1 with Fortune

use of com.bewitchment.common.divination.Fortune 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)

Aggregations

IFortune (com.bewitchment.api.divination.IFortune)1 CapabilityDivination (com.bewitchment.common.core.capability.divination.CapabilityDivination)1 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 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1