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