mcMMO/src/main/java/com/gmail/nossr50/runnables/skills/AlchemyBrewTask.java

104 lines
3.4 KiB
Java
Raw Normal View History

2013-11-16 00:21:00 +01:00
package com.gmail.nossr50.runnables.skills;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
2013-11-16 00:21:00 +01:00
import org.bukkit.block.BrewingStand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO;
2014-01-07 18:19:22 +01:00
import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
2013-11-16 00:21:00 +01:00
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.events.skills.alchemy.McMMOPlayerBrewEvent;
import com.gmail.nossr50.events.skills.alchemy.McMMOPlayerCatalysisEvent;
import com.gmail.nossr50.skills.alchemy.Alchemy;
import com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
public class AlchemyBrewTask extends BukkitRunnable {
private static double DEFAULT_BREW_SPEED = 1.0;
private static int DEFAULT_BREW_TICKS = 400;
private BlockState brewingStand;
2013-11-16 00:21:00 +01:00
private double brewSpeed;
private double brewTimer;
private Player player;
public AlchemyBrewTask(BlockState brewingStand, Player player) {
2013-11-16 00:21:00 +01:00
this.brewingStand = brewingStand;
this.player = player;
brewSpeed = DEFAULT_BREW_SPEED;
brewTimer = DEFAULT_BREW_TICKS;
if (player != null && !Misc.isNPCEntity(player) && Permissions.secondaryAbilityEnabled(player, SecondaryAbility.CATALYSIS)) {
2014-02-11 22:22:57 +01:00
double catalysis = UserManager.getPlayer(player).getAlchemyManager().calculateBrewSpeed(Permissions.lucky(player, SkillType.ALCHEMY));
2013-11-16 00:21:00 +01:00
McMMOPlayerCatalysisEvent event = new McMMOPlayerCatalysisEvent(player, catalysis);
2014-01-20 22:38:04 +01:00
mcMMO.p.getServer().getPluginManager().callEvent(event);
2014-02-11 22:22:57 +01:00
2013-11-16 00:21:00 +01:00
if (!event.isCancelled()) {
brewSpeed = catalysis;
}
}
2013-11-16 00:21:00 +01:00
if (Alchemy.brewingStandMap.containsKey(brewingStand)) {
Alchemy.brewingStandMap.get(brewingStand).cancel();
}
2013-11-16 00:21:00 +01:00
Alchemy.brewingStandMap.put(brewingStand, this);
this.runTaskTimer(mcMMO.p, 1, 1);
}
@Override
public void run() {
if (player == null || !player.isValid() || brewingStand == null || brewingStand.getType() != Material.BREWING_STAND) {
if (Alchemy.brewingStandMap.containsKey(brewingStand)) {
Alchemy.brewingStandMap.remove(brewingStand);
}
this.cancel();
return;
}
2013-11-16 00:21:00 +01:00
brewTimer -= brewSpeed;
// Vanilla potion brewing completes when BrewingTime == 1
if (brewTimer < Math.max(brewSpeed, 2)) {
this.cancel();
finish();
}
else {
((BrewingStand) brewingStand).setBrewingTime((int) brewTimer);
2013-11-16 00:21:00 +01:00
}
}
private void finish() {
McMMOPlayerBrewEvent event = new McMMOPlayerBrewEvent(player, brewingStand);
2014-01-20 22:38:04 +01:00
mcMMO.p.getServer().getPluginManager().callEvent(event);
2013-11-16 00:21:00 +01:00
if (!event.isCancelled()) {
AlchemyPotionBrewer.finishBrewing(brewingStand, player, false);
}
2013-11-16 00:21:00 +01:00
Alchemy.brewingStandMap.remove(brewingStand);
}
2013-11-16 00:21:00 +01:00
public void finishImmediately() {
this.cancel();
2013-11-16 00:21:00 +01:00
AlchemyPotionBrewer.finishBrewing(brewingStand, player, true);
Alchemy.brewingStandMap.remove(brewingStand);
}
2013-11-16 00:21:00 +01:00
public void cancelBrew() {
this.cancel();
((BrewingStand) brewingStand).setBrewingTime(-1);
2013-11-16 00:21:00 +01:00
Alchemy.brewingStandMap.remove(brewingStand);
}
}