diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/competition/Ranking.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/competition/Ranking.java index 4ecaf9ae..59772026 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/competition/Ranking.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/competition/Ranking.java @@ -37,6 +37,20 @@ public interface Ranking { */ CompetitionPlayer getCompetitionPlayer(String player); + /** + * Add a player to ranking + * + * @param competitionPlayer player + */ + void addPlayer(CompetitionPlayer competitionPlayer); + + /** + * Remove a player from ranking + * + * @param player player + */ + void removePlayer(String player); + /** * Returns an iterator for iterating over pairs of player names and scores. * diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java index 3132f568..b8db5073 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java @@ -29,8 +29,7 @@ import org.bukkit.inventory.PlayerInventory; public abstract class AbstractGamingPlayer implements GamingPlayer, Runnable { private final FishingManager manager; - private final long deadline; - + protected long deadline; protected boolean success; protected CancellableTask task; protected Player player; @@ -67,11 +66,6 @@ public abstract class AbstractGamingPlayer implements GamingPlayer, Runnable { return true; } - @Override - public boolean onLeftClick() { - return false; - } - @Override public boolean onChat(String message) { return false; diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GamingPlayer.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GamingPlayer.java index 4c1cd9f0..b199da8c 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GamingPlayer.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GamingPlayer.java @@ -35,11 +35,6 @@ public interface GamingPlayer { */ boolean onRightClick(); - /** - * @return whether to cancel the event - */ - boolean onLeftClick(); - /** * @return whether to cancel the event */ diff --git a/plugin/src/main/java/net/momirealms/customfishing/command/CommandManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/command/CommandManagerImpl.java index c559df68..7f73d51e 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/command/CommandManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/command/CommandManagerImpl.java @@ -47,7 +47,7 @@ public class CommandManagerImpl implements CommandManager { public void load() { new CommandAPICommand("customfishing") .withAliases("cfishing") - .withPermission(CommandPermission.OP) + .withPermission("customfishing.admin") .withSubcommands( getReloadCommand(), getMarketCommand(), diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/LocalRankingImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/LocalRankingImpl.java index f310cfdf..2fff1a47 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/LocalRankingImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/LocalRankingImpl.java @@ -39,10 +39,21 @@ public class LocalRankingImpl implements Ranking { * * @param competitionPlayer The CompetitionPlayer to add. */ + @Override public void addPlayer(CompetitionPlayer competitionPlayer) { competitionPlayers.add(competitionPlayer); } + /** + * Removes a competition player from the ranking. + * + * @param player player's name + */ + @Override + public void removePlayer(String player) { + competitionPlayers.removeIf(e -> e.getPlayer().equals(player)); + } + /** * Removes a competition player from the ranking. * diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/RedisRankingImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/RedisRankingImpl.java index b71ece98..acc80f8e 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/RedisRankingImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/competition/ranking/RedisRankingImpl.java @@ -54,6 +54,20 @@ public class RedisRankingImpl implements Ranking { } } + @Override + public void addPlayer(CompetitionPlayer competitionPlayer) { + try (Jedis jedis = RedisManager.getInstance().getJedis()) { + jedis.zincrby("cf_competition", competitionPlayer.getScore(), competitionPlayer.getPlayer()); + } + } + + @Override + public void removePlayer(String player) { + try (Jedis jedis = RedisManager.getInstance().getJedis()) { + jedis.del("cf_competition", player); + } + } + /** * Returns an iterator for iterating over pairs of player names and scores in descending order. * diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java index bb9d6e77..b724c63c 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java @@ -51,7 +51,9 @@ import net.momirealms.customfishing.util.ItemUtils; import org.bukkit.*; import org.bukkit.entity.*; import org.bukkit.event.*; +import org.bukkit.event.block.Action; import org.bukkit.event.player.*; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.Nullable; @@ -176,20 +178,6 @@ public class FishingManagerImpl implements Listener, FishingManager { } } - @EventHandler - public void onLeftClick(PlayerInteractEvent event) { - if (event.useItemInHand() == Event.Result.DENY) - return; - if (event.getAction() != org.bukkit.event.block.Action.LEFT_CLICK_AIR) - return; - GamingPlayer gamingPlayer = gamingPlayerMap.get(event.getPlayer().getUniqueId()); - if (gamingPlayer != null) { - if (gamingPlayer.onLeftClick()) { - event.setCancelled(true); - } - } - } - /** * Removes a fishing hook entity associated with a given UUID. * diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/game/GameManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/game/GameManagerImpl.java index d2501854..7a088c47 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/game/GameManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/game/GameManagerImpl.java @@ -28,6 +28,7 @@ import net.momirealms.customfishing.api.util.LogUtils; import net.momirealms.customfishing.api.util.OffsetUtils; import net.momirealms.customfishing.mechanic.requirement.RequirementManagerImpl; import net.momirealms.customfishing.util.ClassUtils; +import net.momirealms.customfishing.util.ConfigUtils; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.jetbrains.annotations.Nullable; @@ -57,6 +58,7 @@ public class GameManagerImpl implements GameManager { this.registerAccurateClickGame(); this.registerHoldGame(); this.registerTensionGame(); + this.registerClickGame(); } public void load() { @@ -497,6 +499,52 @@ public class GameManagerImpl implements GameManager { })); } + private void registerClickGame() { + this.registerGameType("click", (section -> { + + var title = section.getString("title","{click}"); + var subtitle = section.getString("subtitle", "Click {clicks} times to win. Time left {time}s"); + + return (player, fishHook, settings) -> new AbstractGamingPlayer(player, fishHook, settings) { + + private int clickedTimes; + private final int requiredTimes = settings.getDifficulty(); + + @Override + public void arrangeTask() { + this.task = CustomFishingPlugin.get().getScheduler().runTaskAsyncTimer(this, 50, 50, TimeUnit.MILLISECONDS); + } + + @Override + public void run() { + super.run(); + showUI(); + } + + @Override + public boolean onRightClick() { + clickedTimes++; + if (clickedTimes >= requiredTimes) { + setGameResult(true); + endGame(); + } + return true; + } + + public void showUI() { + AdventureManagerImpl.getInstance().sendTitle( + player, + title.replace("{click}", String.valueOf(clickedTimes)), + subtitle.replace("{clicks}", String.valueOf(requiredTimes)).replace("{time}", String.format("%.1f", ((double) deadline - System.currentTimeMillis())/1000)), + 0, + 500, + 0 + ); + } + }; + })); + } + /** * Loads minigame expansions from the expansion folder. */ diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java index 2eaeb9a9..3f3020b2 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java @@ -317,7 +317,7 @@ public class ItemManagerImpl implements ItemManager, Listener { itemCFBuilder .amount(section.getInt("amount", 1)) .stackable(section.getBoolean("stackable", true)) - .size(ConfigUtils.getSizePair(section.getString("size"))) + .size(ConfigUtils.getFloatPair(section.getString("size"))) .price((float) section.getDouble("price.base"), (float) section.getDouble("price.bonus")) .customModelData(section.getInt("custom-model-data")) .nbt(section.getConfigurationSection("nbt")) diff --git a/plugin/src/main/java/net/momirealms/customfishing/storage/method/database/sql/AbstractHikariDatabase.java b/plugin/src/main/java/net/momirealms/customfishing/storage/method/database/sql/AbstractHikariDatabase.java index d2d383d4..50ce5f6a 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/storage/method/database/sql/AbstractHikariDatabase.java +++ b/plugin/src/main/java/net/momirealms/customfishing/storage/method/database/sql/AbstractHikariDatabase.java @@ -17,6 +17,7 @@ package net.momirealms.customfishing.storage.method.database.sql; +import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.data.*; @@ -68,31 +69,33 @@ public abstract class AbstractHikariDatabase extends AbstractSQLDatabase impleme public void initialize() { YamlConfiguration config = plugin.getConfig("database.yml"); ConfigurationSection section = config.getConfigurationSection(sqlBrand); + if (section == null) { LogUtils.warn("Failed to load database config. It seems that your config is broken. Please regenerate a new one."); return; } super.tablePrefix = section.getString("table-prefix", "customfishing"); - dataSource = new HikariDataSource(); - dataSource.setDriverClassName(driverClass); - dataSource.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s%s", + HikariConfig hikariConfig = new HikariConfig(); + hikariConfig.setUsername(section.getString("user", "root")); + hikariConfig.setPassword(section.getString("password", "pa55w0rd")); + hikariConfig.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s%s", sqlBrand.toLowerCase(Locale.ENGLISH), section.getString("host", "localhost"), section.getString("port", "3306"), section.getString("database", "minecraft"), section.getString("connection-parameters") )); - - dataSource.setUsername(section.getString("user", "root")); - dataSource.setPassword(section.getString("password", "pa55w0rd")); - - dataSource.setMaximumPoolSize(section.getInt("Pool-Settings.max-pool-size", 10)); - dataSource.setMinimumIdle(section.getInt("Pool-Settings.min-idle", 10)); - dataSource.setMaxLifetime(section.getLong("Pool-Settings.max-lifetime", 180000L)); - dataSource.setKeepaliveTime(section.getLong("Pool-Settings.keep-alive-time", 60000L)); - dataSource.setConnectionTimeout(section.getLong("Pool-Settings.time-out", 20000L)); - dataSource.setPoolName("CustomFishingHikariPool"); + hikariConfig.setDriverClassName(driverClass); + hikariConfig.setMaximumPoolSize(section.getInt("Pool-Settings.max-pool-size", 10)); + hikariConfig.setMinimumIdle(section.getInt("Pool-Settings.min-idle", 10)); + hikariConfig.setMaxLifetime(section.getLong("Pool-Settings.max-lifetime", 180000L)); + hikariConfig.setConnectionTimeout(section.getLong("Pool-Settings.time-out", 20000L)); + hikariConfig.setPoolName("CustomFishingHikariPool"); + try { + hikariConfig.setKeepaliveTime(section.getLong("Pool-Settings.keep-alive-time", 60000L)); + } catch (NoSuchMethodError ignored) { + } final Properties properties = new Properties(); properties.putAll( @@ -111,7 +114,8 @@ public abstract class AbstractHikariDatabase extends AbstractSQLDatabase impleme "elideSetAutoCommits", "true", "maintainTimeStats", "false") ); - dataSource.setDataSourceProperties(properties); + hikariConfig.setDataSourceProperties(properties); + dataSource = new HikariDataSource(hikariConfig); super.createTableIfNotExist(); } diff --git a/plugin/src/main/java/net/momirealms/customfishing/util/ConfigUtils.java b/plugin/src/main/java/net/momirealms/customfishing/util/ConfigUtils.java index ef850914..75138cc3 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/util/ConfigUtils.java +++ b/plugin/src/main/java/net/momirealms/customfishing/util/ConfigUtils.java @@ -105,21 +105,39 @@ public class ConfigUtils { /** * Parses a string representing a size range and returns a pair of floats. * - * @param size The size string in the format "min~max". + * @param string The size string in the format "min~max". * @return A pair of floats representing the minimum and maximum size. */ @Nullable - public static Pair getSizePair(String size) { - if (size == null) return null; - String[] split = size.split("~", 2); + public static Pair getFloatPair(String string) { + if (string == null) return null; + String[] split = string.split("~", 2); if (split.length != 2) { - LogUtils.warn("Illegal size argument: " + size); + LogUtils.warn("Illegal size argument: " + string); LogUtils.warn("Correct usage example: 10.5~25.6"); - return null; + throw new IllegalArgumentException("Illegal float range"); } return Pair.of(Float.parseFloat(split[0]), Float.parseFloat(split[1])); } + /** + * Parses a string representing a size range and returns a pair of ints. + * + * @param string The size string in the format "min~max". + * @return A pair of ints representing the minimum and maximum size. + */ + @Nullable + public static Pair getIntegerPair(String string) { + if (string == null) return null; + String[] split = string.split("~", 2); + if (split.length != 2) { + LogUtils.warn("Illegal size argument: " + string); + LogUtils.warn("Correct usage example: 10~20"); + throw new IllegalArgumentException("Illegal int range"); + } + return Pair.of(Integer.parseInt(split[0]), Integer.parseInt(split[1])); + } + /** * Converts a list of strings in the format "key:value" into a list of Pairs with keys and WeightModifiers. *