Fixed possible NPE in our events, and BrewingStands now remember who last interacted with them, this allows for hoppers to be used with Alchemy Fixes #5004 Fixes #4958 Fixes #4641

This commit is contained in:
nossr50
2024-05-12 14:09:00 -07:00
parent 17052861d1
commit 3ba6b93135
80 changed files with 1137 additions and 797 deletions

View File

@@ -1,32 +1,51 @@
package com.gmail.nossr50.runnables.skills;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
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 com.gmail.nossr50.util.ContainerMetadataUtils;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.BrewingStand;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import static com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer.isValidBrew;
import static com.gmail.nossr50.util.EventUtils.getMcMMOPlayer;
public class AlchemyBrewCheckTask extends CancellableRunnable {
private final Player player;
private final BrewingStand brewingStand;
private final ItemStack[] oldInventory;
public AlchemyBrewCheckTask(Player player, BrewingStand brewingStand) {
this.player = player;
@Deprecated(forRemoval = true, since = "2.2.010")
public AlchemyBrewCheckTask(@Nullable Player ignored, BrewingStand brewingStand) {
this(brewingStand);
}
public AlchemyBrewCheckTask(@NotNull BrewingStand brewingStand) {
this.brewingStand = brewingStand;
this.oldInventory = Arrays.copyOfRange(brewingStand.getInventory().getContents(), 0, 4);
}
@Override
public void run() {
Location location = brewingStand.getLocation();
ItemStack[] newInventory = Arrays.copyOfRange(brewingStand.getInventory().getContents(), 0, 4);
boolean validBrew = brewingStand.getFuelLevel() > 0 && AlchemyPotionBrewer.isValidBrew(player, newInventory);
OfflinePlayer offlinePlayer = ContainerMetadataUtils.getContainerOwner(brewingStand);
int ingredientLevel = 1;
if (offlinePlayer != null && offlinePlayer.isOnline()) {
final McMMOPlayer mmoPlayer = UserManager.getPlayer(offlinePlayer.getPlayer());
if (mmoPlayer != null) {
ingredientLevel = mmoPlayer.getAlchemyManager().getTier();
}
}
final Location location = brewingStand.getLocation();
final ItemStack[] newInventory = Arrays.copyOfRange(brewingStand.getInventory().getContents(), 0, 4);
boolean validBrew = brewingStand.getFuelLevel() > 0 && isValidBrew(ingredientLevel, newInventory);
if (Alchemy.brewingStandMap.containsKey(location)) {
if (oldInventory[Alchemy.INGREDIENT_SLOT] == null
@@ -36,7 +55,7 @@ public class AlchemyBrewCheckTask extends CancellableRunnable {
Alchemy.brewingStandMap.get(location).cancelBrew();
}
} else if (validBrew) {
Alchemy.brewingStandMap.put(location, new AlchemyBrewTask(brewingStand, player));
Alchemy.brewingStandMap.put(location, new AlchemyBrewTask(brewingStand));
}
}
}

View File

@@ -1,5 +1,6 @@
package com.gmail.nossr50.runnables.skills;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.events.skills.alchemy.McMMOPlayerBrewEvent;
@@ -8,44 +9,56 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.alchemy.Alchemy;
import com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer;
import com.gmail.nossr50.util.CancellableRunnable;
import com.gmail.nossr50.util.ContainerMetadataUtils;
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.OfflinePlayer;
import org.bukkit.block.BlockState;
import org.bukkit.block.BrewingStand;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class AlchemyBrewTask extends CancellableRunnable {
private static final double DEFAULT_BREW_SPEED = 1.0;
private static final int DEFAULT_BREW_TICKS = 400;
private final BlockState brewingStand;
private final Location location;
private final OfflinePlayer offlinePlayer;
private McMMOPlayer mmoPlayer;
private double brewSpeed;
private double brewTimer;
private final Player player;
private int fuel;
private boolean firstRun = true;
private int ingredientLevel = 1;
@Deprecated(forRemoval = true, since = "2.2.010")
public AlchemyBrewTask(@NotNull BlockState brewingStand, Player ignored) {
this(brewingStand);
}
public AlchemyBrewTask(@NotNull BlockState brewingStand) {
offlinePlayer = ContainerMetadataUtils.getContainerOwner(brewingStand);
McMMOPlayer mmoPlayer = null;
if (offlinePlayer != null && offlinePlayer.isOnline()) {
mmoPlayer = UserManager.getPlayer(offlinePlayer.getPlayer());
}
public AlchemyBrewTask(BlockState brewingStand, Player player) {
this.brewingStand = brewingStand;
this.location = brewingStand.getLocation();
this.player = player;
brewSpeed = DEFAULT_BREW_SPEED;
brewTimer = DEFAULT_BREW_TICKS;
if (player != null
&& !Misc.isNPCEntityExcludingVillagers(player)
&& Permissions.isSubSkillEnabled(player, SubSkillType.ALCHEMY_CATALYSIS)
&& UserManager.getPlayer(player) != null) {
if (mmoPlayer != null
&& !Misc.isNPCEntityExcludingVillagers(mmoPlayer.getPlayer())
&& Permissions.isSubSkillEnabled(mmoPlayer.getPlayer(), SubSkillType.ALCHEMY_CATALYSIS)) {
ingredientLevel = mmoPlayer.getAlchemyManager().getTier();
double catalysis = UserManager.getPlayer(player).getAlchemyManager().calculateBrewSpeed(Permissions.lucky(player, PrimarySkillType.ALCHEMY));
double catalysis = mmoPlayer.getAlchemyManager().calculateBrewSpeed(Permissions.lucky(mmoPlayer.getPlayer(),
PrimarySkillType.ALCHEMY));
McMMOPlayerCatalysisEvent event = new McMMOPlayerCatalysisEvent(player, catalysis);
McMMOPlayerCatalysisEvent event = new McMMOPlayerCatalysisEvent(mmoPlayer, catalysis);
mcMMO.p.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
@@ -53,8 +66,8 @@ public class AlchemyBrewTask extends CancellableRunnable {
}
}
if (Alchemy.brewingStandMap.containsKey(location)) {
Alchemy.brewingStandMap.get(location).cancel();
if (Alchemy.brewingStandMap.containsKey(brewingStand.getLocation())) {
Alchemy.brewingStandMap.get(brewingStand.getLocation()).cancel();
}
fuel = ((BrewingStand) brewingStand).getFuelLevel();
@@ -62,16 +75,16 @@ public class AlchemyBrewTask extends CancellableRunnable {
if (((BrewingStand) brewingStand).getBrewingTime() == -1) // Only decrement on our end if it isn't a vanilla ingredient.
fuel--;
Alchemy.brewingStandMap.put(location, this);
mcMMO.p.getFoliaLib().getImpl().runAtLocationTimer(location, this, 1, 1);
Alchemy.brewingStandMap.put(brewingStand.getLocation(), this);
mcMMO.p.getFoliaLib().getImpl().runAtLocationTimer(brewingStand.getLocation(), this, 1, 1);
}
@Override
public void run() {
// Check if preconditions for brewing are not met
if (shouldCancelBrewing()) {
if (Alchemy.brewingStandMap.containsKey(location)) {
Alchemy.brewingStandMap.remove(location);
if (Alchemy.brewingStandMap.containsKey(brewingStand.getLocation())) {
Alchemy.brewingStandMap.remove(brewingStand.getLocation());
}
this.cancel();
return;
@@ -93,10 +106,7 @@ public class AlchemyBrewTask extends CancellableRunnable {
}
private boolean shouldCancelBrewing() {
if (player == null) {
return true;
}
if (!player.isValid()) {
if (offlinePlayer == null) {
return true;
}
if (brewingStand == null) {
@@ -105,10 +115,24 @@ public class AlchemyBrewTask extends CancellableRunnable {
if (brewingStand.getType() != Material.BREWING_STAND) {
return true;
}
if (!AlchemyPotionBrewer.isValidIngredient(player, ((BrewingStand) brewingStand).getInventory().getContents()[Alchemy.INGREDIENT_SLOT])) {
return true;
return !AlchemyPotionBrewer.isValidIngredientByLevel(
getIngredientLevelUpdated(), ((BrewingStand) brewingStand).getInventory().getContents()[Alchemy.INGREDIENT_SLOT]);
}
private int getIngredientLevelUpdated() {
if (mmoPlayer != null) {
ingredientLevel = mmoPlayer.getAlchemyManager().getTier();
return ingredientLevel;
} else if (offlinePlayer.isOnline() && mmoPlayer == null) {
final McMMOPlayer fetchedMMOPlayer = UserManager.getPlayer(offlinePlayer.getPlayer());
if (fetchedMMOPlayer != null) {
this.mmoPlayer = fetchedMMOPlayer;
ingredientLevel = mmoPlayer.getAlchemyManager().getTier();
}
return ingredientLevel;
} else {
return ingredientLevel;
}
return false;
}
private void initializeBrewing() {
@@ -128,27 +152,27 @@ public class AlchemyBrewTask extends CancellableRunnable {
private void finish() {
McMMOPlayerBrewEvent event = new McMMOPlayerBrewEvent(player, brewingStand);
final McMMOPlayerBrewEvent event = new McMMOPlayerBrewEvent(mmoPlayer, brewingStand);
mcMMO.p.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
AlchemyPotionBrewer.finishBrewing(brewingStand, player, false);
AlchemyPotionBrewer.finishBrewing(brewingStand, mmoPlayer.getPlayer(), false);
}
Alchemy.brewingStandMap.remove(location);
Alchemy.brewingStandMap.remove(brewingStand.getLocation());
}
public void finishImmediately() {
this.cancel();
AlchemyPotionBrewer.finishBrewing(brewingStand, player, true);
Alchemy.brewingStandMap.remove(location);
AlchemyPotionBrewer.finishBrewing(brewingStand, mmoPlayer.getPlayer(), true);
Alchemy.brewingStandMap.remove(brewingStand.getLocation());
}
public void cancelBrew() {
this.cancel();
((BrewingStand) brewingStand).setBrewingTime(-1);
Alchemy.brewingStandMap.remove(location);
Alchemy.brewingStandMap.remove(brewingStand.getLocation());
}
}