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;
}
Aggregations