2013-11-16 00:21:00 +01:00
|
|
|
package com.gmail.nossr50.runnables.skills;
|
|
|
|
|
2014-02-15 01:31:13 +01:00
|
|
|
import org.bukkit.Location;
|
2014-01-18 19:11:15 +01:00
|
|
|
import org.bukkit.Material;
|
2014-01-30 20:45:21 +01:00
|
|
|
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;
|
2018-12-29 14:24:55 +01:00
|
|
|
import com.gmail.nossr50.datatypes.skills.SubSkill;
|
|
|
|
import com.gmail.nossr50.datatypes.skills.PrimarySkill;
|
2013-11-16 00:21:00 +01:00
|
|
|
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 {
|
2014-01-19 11:49:30 +01:00
|
|
|
private static double DEFAULT_BREW_SPEED = 1.0;
|
|
|
|
private static int DEFAULT_BREW_TICKS = 400;
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2014-01-30 20:45:21 +01:00
|
|
|
private BlockState brewingStand;
|
2014-02-15 01:31:13 +01:00
|
|
|
private Location location;
|
2013-11-16 00:21:00 +01:00
|
|
|
private double brewSpeed;
|
|
|
|
private double brewTimer;
|
|
|
|
private Player player;
|
2016-03-16 14:16:48 +01:00
|
|
|
private int fuel;
|
2016-06-09 02:10:27 +02:00
|
|
|
private boolean firstRun = true;
|
2013-11-16 00:21:00 +01:00
|
|
|
|
2014-01-30 20:45:21 +01:00
|
|
|
public AlchemyBrewTask(BlockState brewingStand, Player player) {
|
2013-11-16 00:21:00 +01:00
|
|
|
this.brewingStand = brewingStand;
|
2014-02-15 01:31:13 +01:00
|
|
|
this.location = brewingStand.getLocation();
|
2013-11-16 00:21:00 +01:00
|
|
|
this.player = player;
|
|
|
|
|
|
|
|
brewSpeed = DEFAULT_BREW_SPEED;
|
|
|
|
brewTimer = DEFAULT_BREW_TICKS;
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2018-12-29 14:24:55 +01:00
|
|
|
if (player != null && !Misc.isNPCEntity(player) && Permissions.isSubSkillEnabled(player, SubSkill.ALCHEMY_CATALYSIS)) {
|
|
|
|
double catalysis = UserManager.getPlayer(player).getAlchemyManager().calculateBrewSpeed(Permissions.lucky(player, PrimarySkill.ALCHEMY));
|
2014-01-18 19:11:15 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2014-02-15 01:31:13 +01:00
|
|
|
if (Alchemy.brewingStandMap.containsKey(location)) {
|
|
|
|
Alchemy.brewingStandMap.get(location).cancel();
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
2016-11-13 17:26:29 +01:00
|
|
|
|
|
|
|
fuel = ((BrewingStand) brewingStand).getFuelLevel();
|
|
|
|
|
|
|
|
if (((BrewingStand) brewingStand).getBrewingTime() == -1) // Only decrement on our end if it isn't a vanilla ingredient.
|
|
|
|
fuel--;
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2014-02-15 01:31:13 +01:00
|
|
|
Alchemy.brewingStandMap.put(location, this);
|
2013-11-16 00:21:00 +01:00
|
|
|
this.runTaskTimer(mcMMO.p, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-03-16 14:16:48 +01:00
|
|
|
if (player == null || !player.isValid() || brewingStand == null || brewingStand.getType() != Material.BREWING_STAND || !AlchemyPotionBrewer.isValidIngredient(player, ((BrewingStand) brewingStand).getInventory().getContents()[Alchemy.INGREDIENT_SLOT])) {
|
2014-02-15 01:31:13 +01:00
|
|
|
if (Alchemy.brewingStandMap.containsKey(location)) {
|
|
|
|
Alchemy.brewingStandMap.remove(location);
|
2014-01-18 19:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.cancel();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-06-09 02:10:27 +02:00
|
|
|
|
|
|
|
if (firstRun) {
|
|
|
|
firstRun = false;
|
|
|
|
((BrewingStand) brewingStand).setFuelLevel(fuel);
|
|
|
|
}
|
2014-01-18 19:11:15 +01:00
|
|
|
|
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 {
|
2014-01-30 20:45:21 +01:00
|
|
|
((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);
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
if (!event.isCancelled()) {
|
|
|
|
AlchemyPotionBrewer.finishBrewing(brewingStand, player, false);
|
|
|
|
}
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2014-02-15 01:31:13 +01:00
|
|
|
Alchemy.brewingStandMap.remove(location);
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
public void finishImmediately() {
|
|
|
|
this.cancel();
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
AlchemyPotionBrewer.finishBrewing(brewingStand, player, true);
|
2014-02-15 01:31:13 +01:00
|
|
|
Alchemy.brewingStandMap.remove(location);
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
public void cancelBrew() {
|
|
|
|
this.cancel();
|
2014-01-18 19:11:15 +01:00
|
|
|
|
2014-01-30 20:45:21 +01:00
|
|
|
((BrewingStand) brewingStand).setBrewingTime(-1);
|
2014-02-15 01:31:13 +01:00
|
|
|
Alchemy.brewingStandMap.remove(location);
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
|
|
|
}
|