Fix potions in ItemExchange

Also deleted PotionUtils and made ItemExchange work without JukeAlert
This commit is contained in:
okx-code
2024-10-25 20:34:55 +01:00
parent 6717fec0ce
commit 6c99e5f5bb
4 changed files with 33 additions and 86 deletions

View File

@@ -1,75 +0,0 @@
package vg.civcraft.mc.civmodcore.inventory.items;
import it.unimi.dsi.fastutil.objects.Object2ObjectAVLTreeMap;
import java.util.Map;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.translation.Translatable;
import org.apache.commons.lang3.tuple.Pair;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import vg.civcraft.mc.civmodcore.chat.ChatUtils;
public final class PotionUtils {
private static final Map<Pair<Material, PotionType>, TranslatableComponent> POTION_TRANSLATIONS = new Object2ObjectAVLTreeMap<>();
/**
* @param type The potion type to get a translatable component for.
* @return Returns a translatable component for the given potion type.
*/
@NotNull
public static TranslatableComponent asTranslatable(@NotNull final PotionType type) {
return asTranslatable(Material.POTION, type);
}
/**
* @param material The potion kind. Must comply with {@link MoreTags#POTIONS}.
* @param type The potion type to get a translatable component for.
* @return Returns a translatable component for the given potion type.
*/
@NotNull
public static TranslatableComponent asTranslatable(@NotNull final Material material,
@NotNull final PotionType type) {
if (!MoreTags.POTIONS.isTagged(material)) {
throw new IllegalArgumentException("That is not a recognised potion material! [" + material.name() + "]");
}
return POTION_TRANSLATIONS.computeIfAbsent(Pair.of(material, type), (_key) -> {
final var item = new ItemStack(material);
ItemUtils.handleItemMeta(item, (PotionMeta meta) -> {
meta.setBasePotionData(new PotionData(type, false, false));
return true;
});
return Component.translatable(item.translationKey());
});
}
/**
* @param potion The potion type to get the name of.
* @return Returns the name of the potion, or null.
* @deprecated Use {@link #asTranslatable(PotionType)} or
* {@link #asTranslatable(Material, PotionType)} instead.
*/
@Deprecated
@Nullable
public static String getPotionNiceName(@Nullable final PotionType potion) {
return potion == null ? null : ChatUtils.stringify(asTranslatable(potion));
}
/**
* @param effect The potion effect to get the name of.
* @return Returns the name of the potion effect, or null.
* @deprecated Use {@link Component#translatable(Translatable)} instead.
*/
@Deprecated
@Nullable
public static String getEffectNiceName(@Nullable final PotionEffectType effect) {
return effect == null ? null : ChatUtils.stringify(Component.translatable(effect));
}
}

View File

@@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
@@ -20,8 +21,8 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import vg.civcraft.mc.civmodcore.chat.ChatUtils;
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;
import vg.civcraft.mc.civmodcore.inventory.items.PotionUtils;
import vg.civcraft.mc.civmodcore.players.scoreboard.bottom.BottomLine;
import vg.civcraft.mc.civmodcore.players.scoreboard.bottom.BottomLineAPI;
import vg.civcraft.mc.civmodcore.players.scoreboard.side.CivScoreBoard;
@@ -170,7 +171,7 @@ public class ScoreboardHUD implements Listener {
}
//TODO check deprecated methods
String name = PotionUtils.getEffectNiceName(pot.getType());
String name = ChatUtils.stringify(Component.translatable(pot.getType()));
String formatted = String.format("%s %s%s %d | %d:%s", sortingPrefix, effectColor, name, level, minutes,
seconds);
scoreBoards.get(boardIndex).set(p, formatted);

View File

@@ -4,6 +4,8 @@ import com.untamedears.itemexchange.ItemExchangePlugin;
import com.untamedears.itemexchange.events.SuccessfulPurchaseEvent;
import com.untamedears.jukealert.JukeAlert;
import com.untamedears.jukealert.model.Snitch;
import com.untamedears.jukealert.model.actions.abstr.LoggableAction;
import com.untamedears.jukealert.model.actions.abstr.SnitchAction;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -12,21 +14,25 @@ import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import vg.civcraft.mc.civmodcore.utilities.DependencyGlue;
import java.lang.reflect.InvocationTargetException;
public final class JukeAlertGlue extends DependencyGlue {
private Class<?> shopPurchaseAction;
public JukeAlertGlue(final @NotNull ItemExchangePlugin plugin) {
super(plugin, "JukeAlert");
}
private final Listener listener = new Listener() {
@EventHandler(ignoreCancelled = true)
public void triggerNearbySnitches(final SuccessfulPurchaseEvent event) {
public void triggerNearbySnitches(final SuccessfulPurchaseEvent event) throws InvocationTargetException, InstantiationException, IllegalAccessException {
final Player purchaser = event.getPurchaser();
final Location location = event.getTrade().getInventory().getLocation();
final long now = System.currentTimeMillis();
for (final Snitch snitch : JukeAlert.getInstance().getSnitchManager().getSnitchesCovering(location)) {
if (!purchaser.hasPermission("jukealert.vanish")) {
snitch.processAction(new ShopPurchaseAction(snitch, purchaser.getUniqueId(), location, now));
snitch.processAction((SnitchAction) shopPurchaseAction.getConstructors()[0].newInstance(snitch, purchaser.getUniqueId(), location, now));
}
}
}
@@ -34,11 +40,22 @@ public final class JukeAlertGlue extends DependencyGlue {
@Override
protected void onDependencyEnabled() {
JukeAlert.getInstance().getLoggedActionFactory().registerProvider(
ShopPurchaseAction.IDENTIFIER,
(snitch, player, location, timestamp, extra) ->
new ShopPurchaseAction(snitch, player, location, timestamp));
ItemExchangePlugin.getInstance().registerListener(this.listener);
try {
this.shopPurchaseAction = Class.forName("com.untamedears.itemexchange.glues.jukealert.ShopPurchaseAction");
JukeAlert.getInstance().getLoggedActionFactory().registerProvider(
(String) shopPurchaseAction.getDeclaredField("IDENTIFIER").get(null),
(snitch, player, location, timestamp, extra) ->
{
try {
return (LoggableAction) shopPurchaseAction.getConstructors()[0].newInstance(snitch, player, location, timestamp);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
ItemExchangePlugin.getInstance().registerListener(this.listener);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override

View File

@@ -13,13 +13,14 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.WordUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull;
import vg.civcraft.mc.civmodcore.inventory.items.PotionUtils;
import vg.civcraft.mc.civmodcore.nbt.wrappers.NBTCompound;
import vg.civcraft.mc.civmodcore.utilities.NullUtils;
@@ -34,6 +35,7 @@ public final class PotionModifier extends ModifierData {
private PotionType base;
private List<PotionEffect> effects;
private boolean splash;
@Override
public PotionModifier construct(ItemStack item) {
@@ -43,6 +45,7 @@ public final class PotionModifier extends ModifierData {
PotionModifier modifier = new PotionModifier();
modifier.base = meta.getBasePotionType();
modifier.effects = meta.getCustomEffects();
modifier.splash = item.getType() == Material.SPLASH_POTION;
return modifier;
}
@@ -125,7 +128,8 @@ public final class PotionModifier extends ModifierData {
if (this.base == null) {
return null;
}
return PotionUtils.getPotionNiceName(this.base);
return (splash ? "Splash " : "")
+ WordUtils.capitalize(this.base.getKey().getKey().replace("_", " "));
}
public PotionType getPotionType() {