I am writing a mod for Minecraft (in Java, duh) and want to apply a random potion effect like this:
private static Effect[] poss_effects = new Effect[]{
Effects.SPEED,
Effects.SLOWNESS,
Effects.HASTE,
Effects.MINING_FATIGUE,
Effects.STRENGTH,
Effects.INSTANT_HEALTH,
Effects.INSTANT_DAMAGE,
Effects.JUMP_BOOST,
Effects.NAUSEA,
Effects.REGENERATION,
Effects.RESISTANCE,
Effects.FIRE_RESISTANCE,
Effects.WATER_BREATHING,
Effects.INVISIBILITY,
Effects.BLINDNESS,
Effects.NIGHT_VISION,
Effects.HUNGER,
Effects.WEAKNESS,
Effects.POISON,
Effects.WITHER,
Effects.HEALTH_BOOST,
Effects.ABSORPTION,
Effects.SATURATION,
Effects.GLOWING,
Effects.LEVITATION,
Effects.LUCK,
Effects.UNLUCK,
Effects.SLOW_FALLING,
Effects.CONDUIT_POWER,
Effects.DOLPHINS_GRACE,
Effects.BAD_OMEN,
Effects.HERO_OF_THE_VILLAGE
};
private long prev_milli = System.currentTimeMillis();
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
if (System.currentTimeMillis() - this.prev_milli >= 60000) {
Effect to_apply = poss_effects[(int) (Math.random() * poss_effects.length)];
int duration = (int) (Math.random() * 15 * 20);
int amplifier = (int) (Math.random() * 2);
playerIn.addPotionEffect(new EffectInstance(to_apply, duration, amplifier));
this.prev_milli = System.currentTimeMillis();
}
return super.onItemRightClick(worldIn, playerIn, handIn);
}
However, when the timer ticks down to zero on the potion effect, the effect stays there with the timer at 0:00
. The first time I right-click it, I get two potion effects: one that works properly and only lasts for the given amount of time and one that has the problem described previously. All subsequent right-clicks produce the latter.
When I do this:
@Override
public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker) {
target.addPotionEffect(new EffectInstance(Effects.WITHER, 60, 2));
target.setHeadRotation((float) (Math.random() * 360), (int) (Math.random() * 180 - 90));
return super.hitEntity(stack, target, attacker);
}
the effect applies normally and runs out normally every time I hit an entity.
What's wrong?