Cyclic Dependency Patch

- Fixed cyclic dependencies
- Added a TFShoppe class for effective plugin initialization
- Registered discord commands, did not before
- Added SLF4J loggers for module logging.
This commit is contained in:
Paul Reilly
2023-03-10 19:21:03 -06:00
parent b5804c7eb1
commit 3a96c853d2
38 changed files with 721 additions and 418 deletions

View File

@ -1,12 +1,9 @@
package me.totalfreedom.shop;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import io.papermc.paper.event.player.AsyncChatEvent;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.api.ShopItem;
import me.totalfreedom.totalfreedommod.api.ShoppeCommons;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.util.FLog;
@ -21,7 +18,6 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
@ -29,10 +25,12 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
public class Shop extends FreedomService
import java.util.*;
public class Shop extends FreedomService implements ShoppeCommons
{
public final int coinsPerReactionWin = ConfigEntry.SHOP_REACTIONS_COINS_PER_WIN.getInteger();
public final String prefix = ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Reaction" + ChatColor.DARK_GRAY + "] ";
private final int coinsPerReactionWin = ConfigEntry.SHOP_REACTIONS_COINS_PER_WIN.getInteger();
private final String prefix = ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Reaction" + ChatColor.DARK_GRAY + "] ";
private final String LOGIN_MESSAGE_GUI_TITLE = ChatColor.DARK_GREEN + ChatColor.BOLD.toString() + "Login Messages";
public String reactionString = "";
public Date reactionStartTime;
@ -49,6 +47,13 @@ public class Shop extends FreedomService
}
}
@Override
public int getCoinsPerReactionWin()
{
return coinsPerReactionWin;
}
@Override
public void startReactionTimer()
{
long interval = ConfigEntry.SHOP_REACTIONS_INTERVAL.getInteger() * 20L;
@ -64,12 +69,14 @@ public class Shop extends FreedomService
}.runTaskLater(plugin, interval);
}
@Override
public void forceStartReaction()
{
reactions.cancel();
startReaction();
}
@Override
public void startReaction()
{
if (!ConfigEntry.SHOP_ENABLED.getBoolean())
@ -101,8 +108,7 @@ public class Shop extends FreedomService
if ((seconds -= 1) == 0)
{
endReaction(null);
}
else
} else
{
countdownBar.setProgress(seconds / max);
if (!countdownBar.getColor().equals(BarColor.YELLOW) && seconds / max <= 0.25)
@ -114,6 +120,7 @@ public class Shop extends FreedomService
}.runTaskTimer(plugin, 0, 20);
}
@Override
public void endReaction(String winner)
{
countdownTask.cancel();
@ -143,16 +150,19 @@ public class Shop extends FreedomService
}
}
@Override
public String getShopPrefix()
{
return FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString());
}
@Override
public String getShopTitle()
{
return FUtil.colorize(ConfigEntry.SHOP_TITLE.getString());
}
@Override
public Inventory generateShopGUI(PlayerData playerData)
{
Inventory gui = server.createInventory(null, 36, getShopTitle());
@ -180,6 +190,7 @@ public class Shop extends FreedomService
return gui;
}
@Override
public Inventory generateLoginMessageGUI(Player player)
{
Inventory gui = server.createInventory(null, 36, LOGIN_MESSAGE_GUI_TITLE);
@ -203,11 +214,13 @@ public class Shop extends FreedomService
return gui;
}
@Override
public boolean isRealItem(PlayerData data, ShopItem shopItem, PlayerInventory inventory, ItemStack realItem)
{
return isRealItem(data, shopItem, inventory.getItemInMainHand(), realItem) || isRealItem(data, shopItem, inventory.getItemInOffHand(), realItem);
}
@Override
public boolean isRealItem(PlayerData data, ShopItem shopItem, ItemStack givenItem, ItemStack realItem)
{
if (!data.hasItem(shopItem) || !givenItem.getType().equals(realItem.getType()))
@ -223,6 +236,7 @@ public class Shop extends FreedomService
return givenMeta.getDisplayName().equals(realMeta.getDisplayName()) && Objects.equals(givenMeta.getLore(), realMeta.getLore());
}
@Override
public ItemStack getLightningRod()
{
ItemStack itemStack = new ItemStack(Material.BLAZE_ROD);
@ -235,6 +249,7 @@ public class Shop extends FreedomService
return itemStack;
}
@Override
public ItemStack getGrapplingHook()
{
ItemStack itemStack = new ItemStack(Material.FISHING_ROD);
@ -246,6 +261,7 @@ public class Shop extends FreedomService
return itemStack;
}
@Override
public ItemStack getFireBall()
{
ItemStack itemStack = new ItemStack(Material.FIRE_CHARGE);
@ -257,6 +273,7 @@ public class Shop extends FreedomService
return itemStack;
}
@Override
public ItemStack getRideablePearl()
{
ItemStack itemStack = new ItemStack(Material.ENDER_PEARL);
@ -269,6 +286,7 @@ public class Shop extends FreedomService
return itemStack;
}
@Override
public ItemStack getStackingPotato()
{
ItemStack itemStack = new ItemStack(Material.POTATO);
@ -280,6 +298,7 @@ public class Shop extends FreedomService
return itemStack;
}
@Override
public ItemStack getClownFish()
{
ItemStack itemStack = new ItemStack(Material.TROPICAL_FISH);
@ -291,16 +310,19 @@ public class Shop extends FreedomService
return itemStack;
}
@Override
public boolean canAfford(int price, int coins)
{
return coins >= price;
}
@Override
public int amountNeeded(int price, int coins)
{
return price - coins;
}
@Override
public ItemStack shopGUIItem(ShopItem item, PlayerData data)
{
ItemStack itemStack = new ItemStack(item.getIcon());
@ -319,8 +341,7 @@ public class Shop extends FreedomService
lore.add(ChatColor.RED + "You can not afford this item!");
lore.add(ChatColor.RED + "You need " + amountNeeded(price, coins) + " more coins to buy this item.");
}
}
else
} else
{
lore.add(ChatColor.RED + "You already purchased this item.");
}
@ -332,7 +353,7 @@ public class Shop extends FreedomService
@EventHandler(priority = EventPriority.HIGH)
public void onShopGUIClick(InventoryClickEvent event)
{
if (!(event.getWhoClicked() instanceof Player))
if (!(event.getWhoClicked() instanceof Player player))
{
return;
}
@ -350,7 +371,6 @@ public class Shop extends FreedomService
return;
}
Player player = (Player)event.getWhoClicked();
PlayerData playerData = plugin.pl.getData(player);
int price = shopItem.getCost();
int coins = playerData.getCoins();
@ -377,7 +397,7 @@ public class Shop extends FreedomService
@EventHandler(priority = EventPriority.HIGH)
public void onLoginMessageGUIClick(InventoryClickEvent event)
{
if (!(event.getWhoClicked() instanceof Player))
if (!(event.getWhoClicked() instanceof Player player))
{
return;
}
@ -391,7 +411,6 @@ public class Shop extends FreedomService
int slot = event.getSlot();
Player player = (Player)event.getWhoClicked();
PlayerData data = plugin.pl.getData(player);
if (slot == 35)
@ -399,8 +418,7 @@ public class Shop extends FreedomService
data.setLoginMessage(null);
plugin.pl.save(data);
player.sendMessage(ChatColor.GREEN + "Removed your login message");
}
else
} else
{
String message = ConfigEntry.SHOP_LOGIN_MESSAGES.getStringList().get(slot);
data.setLoginMessage(message);
@ -413,24 +431,25 @@ public class Shop extends FreedomService
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event)
public void onPlayerChat(AsyncChatEvent event)
{
String message = event.getMessage();
String message = event.message().toString();
Player player = event.getPlayer();
if (ConfigEntry.SHOP_ENABLED.getBoolean() && ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean()
&& !plugin.sh.reactionString.isEmpty() && message.equals(plugin.sh.reactionString))
&& !plugin.sh.getReactionString().isEmpty() && message.equals(plugin.sh.getReactionString()))
{
event.setCancelled(true);
PlayerData data = plugin.pl.getData(player);
data.setCoins(data.getCoins() + plugin.sh.coinsPerReactionWin);
data.setCoins(data.getCoins() + plugin.sh.getCoinsPerReactionWin());
plugin.pl.save(data);
plugin.sh.endReaction(player.getName());
player.sendMessage(ChatColor.GREEN + "You have been given " + ChatColor.GOLD
+ plugin.sh.coinsPerReactionWin + ChatColor.GREEN + " coins!");
+ plugin.sh.getCoinsPerReactionWin() + ChatColor.GREEN + " coins!");
}
}
@Override
public ShopItem getShopItem(int slot)
{
for (ShopItem shopItem : ShopItem.values())
@ -442,4 +461,10 @@ public class Shop extends FreedomService
}
return null;
}
@Override
public String getReactionString()
{
return reactionString;
}
}

View File

@ -1,116 +0,0 @@
package me.totalfreedom.shop;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import org.bukkit.ChatColor;
import org.bukkit.Material;
public enum ShopItem
{
GRAPPLING_HOOK("Grappling Hook", Material.FISHING_ROD, 10, ConfigEntry.SHOP_PRICES_GRAPPLING_HOOK, ChatColor.GREEN, "grapplingHook", "/grapplinghook"),
LIGHTNING_ROD("Lightning Rod", Material.BLAZE_ROD, 12, ConfigEntry.SHOP_PRICES_LIGHTNING_ROD, ChatColor.LIGHT_PURPLE, "lightningRod", "/lightningrod"),
FIRE_BALL("Fire Ball", Material.FIRE_CHARGE, 14, ConfigEntry.SHOP_PRICES_FIRE_BALL, ChatColor.RED, "fireBall", "/fireball"),
RIDEABLE_PEARL("Rideable Ender Pearl", Material.ENDER_PEARL, 16, ConfigEntry.SHOP_PRICES_RIDEABLE_PEARL, ChatColor.DARK_PURPLE, "rideablePearl", "/rideablepearl"),
STACKING_POTATO("Stacking Potato", Material.POTATO, 19, ConfigEntry.SHOP_PRICES_STACKING_POTATO, ChatColor.YELLOW, "stackingPotato", "/stackingpotato"),
CLOWN_FISH("Clown Fish", Material.TROPICAL_FISH, 21, ConfigEntry.SHOP_PRICES_CLOWN_FISH, ChatColor.GOLD, "clownFish", "/clownfish"),
LOGIN_MESSAGES("Login Messages", Material.NAME_TAG, 23, ConfigEntry.SHOP_PRICES_LOGIN_MESSAGES, ChatColor.DARK_GREEN, "loginMessages", "/loginmessage"),
RAINBOW_TRAIL("Rainbow Trail", Material.RED_WOOL, 25, ConfigEntry.SHOP_PRICES_RAINBOW_TRAIL, ChatColor.DARK_RED, "rainbowTrail", "/trail");
/*
Shop GUI Layout:
Dimensions: 9x4 = 36
Key:
g = Grappling Hook,
l = Lightning Rod
f = Fire Ball
r = Rideable Ender Pearl
s = Stacking Potato
c = Clown Fish
x = Login Messages
t = Rainbow Trail
$ = Coins
---------
-g-l-f-r-
-s-c-x-t-
--------$
*/
private final String name;
private final Material icon;
private final int slot;
private final ConfigEntry cost;
private final ChatColor color;
private final String dataName;
private final String command;
ShopItem(String name, Material icon, int slot, ConfigEntry cost, ChatColor color, String dataName, String command)
{
this.name = name;
this.icon = icon;
this.slot = slot;
this.cost = cost;
this.color = color;
this.dataName = dataName;
this.command = command;
}
public static ShopItem findItem(String string)
{
try
{
return ShopItem.valueOf(string.toUpperCase());
}
catch (Exception ignored)
{
}
return null;
}
public String getColoredName()
{
return color + name;
}
public int getCost()
{
return cost.getInteger();
}
public String getName()
{
return name;
}
public Material getIcon()
{
return icon;
}
public int getSlot()
{
return slot;
}
public ChatColor getColor()
{
return color;
}
public String getDataName()
{
return dataName;
}
public String getCommand()
{
return command;
}
}

View File

@ -0,0 +1,53 @@
package me.totalfreedom.shop;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.api.Context;
import me.totalfreedom.totalfreedommod.api.ShoppeCommons;
import org.bukkit.plugin.java.JavaPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TFShoppe extends JavaPlugin
{
private final Logger slf4j = LoggerFactory.getLogger("TF-Shoppe");
private Shop shop;
private Votifier votifier;
public Logger slf4j()
{
return slf4j;
}
@Override
public void onEnable()
{
slf4j().info("Hello from the TF-Shoppe! Attempting to resolve TotalFreedomMod...");
TotalFreedomMod plugin = (TotalFreedomMod) getServer().getPluginManager().getPlugin("TotalFreedomMod");
if (plugin == null)
{
slf4j().error("TotalFreedomMod not found! Disabling...");
onDisable();
return;
}
slf4j().info("TotalFreedomMod found! Registering the shop...");
shop = new Shop();
slf4j().info("Shop registered! Registering the Votifier listener...");
votifier = new Votifier();
slf4j().info("Votifier listener registered! Providing context to TFM...");
Context<ShoppeCommons> context = new Context<>(shop);
plugin.ag.setShoppeContext(context);
plugin.registerShoppe();
slf4j().info("Context provided! TF-Shoppe is now ready to go!");
}
@Override
public void onDisable()
{
slf4j().info("Goodbye from the TF-Shoppe!");
}
}

View File

@ -16,11 +16,13 @@ public class Votifier extends FreedomService
@Override
public void onStart()
{
// This is here to please SonarLint.
}
@Override
public void onStop()
{
// This is here to please SonarLint.
}
@EventHandler(priority = EventPriority.NORMAL)