1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Allow Talismans to use the Actionbar

This commit is contained in:
TheBusyBiscuit 2021-01-29 03:02:50 +01:00
parent 3c0afeb943
commit ee7786b8e4
6 changed files with 64 additions and 37 deletions

View File

@ -29,6 +29,7 @@
* Added Talisman of the Wise
* Added Book Binder
* Added Tier 3 Electric Ore Grinder
* Added an option to allow Talismans to send their notifications via the Actionbar
#### Changes
* Massive performance improvements to holograms/armorstands

View File

@ -64,12 +64,14 @@ public final class SlimefunRegistry {
private final List<String> researchRanks = new ArrayList<>();
private final Set<UUID> researchingPlayers = Collections.synchronizedSet(new HashSet<>());
// TODO: Move this all into a proper "config cache" class
private boolean backwardsCompatibility;
private boolean automaticallyLoadItems;
private boolean enableResearches;
private boolean freeCreativeResearches;
private boolean researchFireworks;
private boolean logDuplicateBlockEntries;
private boolean talismanActionBarMessages;
private final Set<String> tickers = new HashSet<>();
private final Set<SlimefunItem> radioactive = new HashSet<>();
@ -110,6 +112,7 @@ public final class SlimefunRegistry {
freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode");
researchFireworks = cfg.getBoolean("researches.enable-fireworks");
logDuplicateBlockEntries = cfg.getBoolean("options.log-duplicate-block-entries");
talismanActionBarMessages = cfg.getBoolean("talismans.use-actionbar");
}
/**
@ -353,6 +356,10 @@ public final class SlimefunRegistry {
public boolean logDuplicateBlockEntries() {
return logDuplicateBlockEntries;
}
public boolean useActionbarForTalismans() {
return talismanActionBarMessages;
}
@Nonnull
public NamespacedKey getSoulboundDataKey() {

View File

@ -26,6 +26,9 @@ import io.github.thebusybiscuit.slimefun4.api.SlimefunBranch;
import io.github.thebusybiscuit.slimefun4.core.services.LocalizationService;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
/**
* This is an abstract parent class of {@link LocalizationService}.
@ -225,6 +228,14 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
}
}
public void sendActionbarMessage(@Nonnull Player player, @Nonnull String key, boolean addPrefix) {
String prefix = addPrefix ? getPrefix() : "";
String message = ChatColors.color(prefix + getMessage(player, key));
BaseComponent[] components = TextComponent.fromLegacyText(message);
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, components);
}
@Override
public void sendMessage(CommandSender sender, String key) {
sendMessage(sender, key, true);

View File

@ -619,6 +619,8 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
new ButcherAndroidListener(this);
new NetworkListener(this, networkManager);
new HopperListener(this);
new TalismanListener(this);
new SoulboundListener(this);
// Bees were added in 1.15
if (minecraftVersion.isAtLeast(MinecraftVersion.MINECRAFT_1_15)) {
@ -640,15 +642,6 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
bowListener.register(this);
backpackListener.register(this);
// Toggleable Listeners for performance reasons
if (config.getBoolean("items.talismans")) {
new TalismanListener(this);
}
if (config.getBoolean("items.soulbound")) {
new SoulboundListener(this);
}
// Handle Slimefun Guide being given on Join
new SlimefunGuideListener(this, config.getBoolean("guide.receive-on-first-join"));

View File

@ -113,10 +113,6 @@ public class Talisman extends SlimefunItem {
return effects;
}
protected String getMessageSuffix() {
return suffix;
}
protected boolean isEventCancelled() {
return cancel;
}
@ -147,10 +143,6 @@ public class Talisman extends SlimefunItem {
}
}
private static boolean hasMessage(@Nonnull Talisman talisman) {
return talisman.getMessageSuffix() != null;
}
@ParametersAreNonnullByDefault
public static boolean checkFor(Event e, SlimefunItemStack stack) {
return checkFor(e, stack.getItem());
@ -202,7 +194,23 @@ public class Talisman extends SlimefunItem {
consumeItem(inv, talisman, talismanItem);
applyTalismanEffects(p, talisman);
cancelEvent(e, talisman);
sendMessage(p, talisman);
talisman.sendMessage(p);
}
@ParametersAreNonnullByDefault
private static void consumeItem(Inventory inv, Talisman talisman, ItemStack talismanItem) {
if (talisman.isConsumable()) {
ItemStack[] contents = inv.getContents();
for (int i = 0; i < contents.length; i++) {
ItemStack item = contents[i];
if (SlimefunUtils.isItemSimilar(item, talismanItem, true, false)) {
ItemUtils.consumeItem(item, false);
return;
}
}
}
}
@ParametersAreNonnullByDefault
@ -219,29 +227,37 @@ public class Talisman extends SlimefunItem {
}
}
@ParametersAreNonnullByDefault
private static void sendMessage(Player p, Talisman talisman) {
if (hasMessage(talisman)) {
SlimefunPlugin.getLocalization().sendMessage(p, "messages.talisman." + talisman.getMessageSuffix(), true);
}
/**
* This returns whether the {@link Talisman} is silent.
* A silent {@link Talisman} will not send a message to a {@link Player}
* when activated.
*
* @return Whether this {@link Talisman} is silent
*/
public boolean isSilent() {
return getMessageSuffix() == null;
}
@Nullable
protected final String getMessageSuffix() {
return suffix;
}
@ParametersAreNonnullByDefault
private static void consumeItem(Inventory inv, Talisman talisman, ItemStack talismanItem) {
if (talisman.isConsumable()) {
ItemStack[] contents = inv.getContents();
for (int i = 0; i < contents.length; i++) {
ItemStack item = contents[i];
private void sendMessage(Player p) {
if (!isSilent()) {
String messageKey = "messages.talisman." + getMessageSuffix();
if (SlimefunUtils.isItemSimilar(item, talismanItem, true, false)) {
ItemUtils.consumeItem(item, false);
return;
}
if (SlimefunPlugin.getRegistry().useActionbarForTalismans()) {
SlimefunPlugin.getLocalization().sendActionbarMessage(p, messageKey, false);
} else {
SlimefunPlugin.getLocalization().sendMessage(p, messageKey, true);
}
}
}
private static Player getPlayerByEventType(Event e) {
@Nullable
private static Player getPlayerByEventType(@Nonnull Event e) {
if (e instanceof EntityDeathEvent) {
return ((EntityDeathEvent) e).getEntity().getKiller();
} else if (e instanceof BlockBreakEvent) {
@ -259,7 +275,7 @@ public class Talisman extends SlimefunItem {
return null;
}
private static boolean pass(Player p, SlimefunItem talisman) {
private static boolean pass(@Nonnull Player p, @Nonnull SlimefunItem talisman) {
for (PotionEffect effect : ((Talisman) talisman).getEffects()) {
if (effect != null && p.hasPotionEffect(effect.getType())) {
return false;

View File

@ -38,9 +38,8 @@ networks:
enable-visualizer: true
delete-excess-items: false
items:
talismans: true
soulbound: true
talismans:
use-actionbar: true
metrics:
auto-update: true