fix issues with Alchemy and older MC versions

This commit is contained in:
nossr50
2024-05-03 13:53:53 -07:00
parent 5b822bc626
commit 75561350c1
14 changed files with 165 additions and 128 deletions

View File

@@ -3,6 +3,7 @@ package com.gmail.nossr50.runnables.skills;
import com.gmail.nossr50.skills.alchemy.Alchemy;
import com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer;
import com.gmail.nossr50.util.CancellableRunnable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.BrewingStand;
import org.bukkit.entity.Player;
@@ -31,8 +32,7 @@ public class AlchemyBrewCheckTask extends CancellableRunnable {
if (oldInventory[Alchemy.INGREDIENT_SLOT] == null || newInventory[Alchemy.INGREDIENT_SLOT] == null || !oldInventory[Alchemy.INGREDIENT_SLOT].isSimilar(newInventory[Alchemy.INGREDIENT_SLOT]) || !validBrew) {
Alchemy.brewingStandMap.get(location).cancelBrew();
}
}
else if (validBrew) {
} else if (validBrew) {
Alchemy.brewingStandMap.put(location, new AlchemyBrewTask(brewingStand, player));
}
}

View File

@@ -11,6 +11,7 @@ import com.gmail.nossr50.util.CancellableRunnable;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
@@ -67,33 +68,65 @@ public class AlchemyBrewTask extends CancellableRunnable {
@Override
public void run() {
if (player == null || !player.isValid() || brewingStand == null || brewingStand.getType() != Material.BREWING_STAND || !AlchemyPotionBrewer.isValidIngredient(player, ((BrewingStand) brewingStand).getInventory().getContents()[Alchemy.INGREDIENT_SLOT])) {
// Check if preconditions for brewing are not met
if (shouldCancelBrewing()) {
if (Alchemy.brewingStandMap.containsKey(location)) {
Alchemy.brewingStandMap.remove(location);
}
this.cancel();
return;
}
// Initialize the brewing stand on the first run
initializeBrewing();
// Update the brewing process timer
brewTimer -= brewSpeed;
// Check if the brewing process should finish
if (isBrewingComplete()) {
this.cancel();
finish();
} else {
updateBrewingTime();
}
}
private boolean shouldCancelBrewing() {
if (player == null) {
return true;
}
if (!player.isValid()) {
return true;
}
if (brewingStand == null) {
return true;
}
if (brewingStand.getType() != Material.BREWING_STAND) {
return true;
}
if (!AlchemyPotionBrewer.isValidIngredient(player, ((BrewingStand) brewingStand).getInventory().getContents()[Alchemy.INGREDIENT_SLOT])) {
return true;
}
return false;
}
private void initializeBrewing() {
if (firstRun) {
firstRun = false;
((BrewingStand) brewingStand).setFuelLevel(fuel);
}
brewTimer -= brewSpeed;
// Vanilla potion brewing completes when BrewingTime == 1
if (brewTimer < Math.max(brewSpeed, 2)) {
this.cancel();
finish();
}
else {
((BrewingStand) brewingStand).setBrewingTime((int) brewTimer);
}
}
private boolean isBrewingComplete() {
return brewTimer < Math.max(brewSpeed, 2);
}
private void updateBrewingTime() {
((BrewingStand) brewingStand).setBrewingTime((int) brewTimer);
}
private void finish() {
McMMOPlayerBrewEvent event = new McMMOPlayerBrewEvent(player, brewingStand);
mcMMO.p.getServer().getPluginManager().callEvent(event);