From cbc1d997ecaad741c8de2041a77e631eb299f964 Mon Sep 17 00:00:00 2001 From: ZeroEpoch1969 <13510767+ZeroEpoch1969@users.noreply.github.com> Date: Wed, 8 Apr 2020 01:34:08 -0700 Subject: [PATCH] chat reactions --- .../totalfreedommod/ChatManager.java | 20 ++++++++++ .../totalfreedommod/LoginProcess.java | 4 +- .../command/Command_manageshop.java | 7 ++++ .../command/Command_tprandom.java | 4 +- .../totalfreedommod/config/ConfigEntry.java | 4 ++ .../totalfreedommod/shop/Shop.java | 32 ++++++++++++++++ .../totalfreedommod/util/FUtil.java | 37 +++++++++++++++++-- src/main/resources/config.yml | 17 ++++++++- 8 files changed, 116 insertions(+), 9 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java index a9fc3708..7b6c15eb 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java @@ -1,10 +1,14 @@ package me.totalfreedom.totalfreedommod; import com.google.common.base.Strings; +import java.time.LocalDate; +import java.time.Period; +import java.util.Date; import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.rank.Displayable; +import me.totalfreedom.totalfreedommod.shop.ShopData; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FSync; import me.totalfreedom.totalfreedommod.util.FUtil; @@ -59,6 +63,22 @@ public class ChatManager extends FreedomService message = FUtil.colorize(message); message = message.replaceAll(ChatColor.MAGIC.toString(), "&k"); + if (ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean() && !plugin.sh.reactionString.isEmpty() && message.equals(plugin.sh.reactionString)) + { + event.setCancelled(true); + ShopData data = plugin.sh.getData(player); + data.setCoins(data.getCoins() + plugin.sh.coinsPerReactionWin); + plugin.sh.save(data); + plugin.sh.reactionString = ""; + Date currentTime = new Date(); + long seconds = (currentTime.getTime() - plugin.sh.reactionStartTime.getTime()) / 1000; + String reactionMessage = ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Reaction" + ChatColor.DARK_GRAY + "] " + + ChatColor.GREEN + player.getName() + ChatColor.AQUA + " won in " + seconds + " seconds!"; + FUtil.bcastMsg(reactionMessage, false); + player.sendMessage(ChatColor.GREEN + "You have been given " + ChatColor.GOLD + plugin.sh.coinsPerReactionWin + ChatColor.GREEN + " coins!"); + return; + } + if (!ConfigEntry.TOGGLE_CHAT.getBoolean() && !plugin.al.isAdmin(player)) { event.setCancelled(true); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java b/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java index f071de86..13404a57 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java @@ -201,8 +201,8 @@ public class LoginProcess extends FreedomService if (TELEPORT_ON_JOIN.contains(player.getName())) { - int x = FUtil.random(-10000, 10000); - int z = FUtil.random(-10000, 10000); + int x = FUtil.randomInteger(-10000, 10000); + int z = FUtil.randomInteger(-10000, 10000); int y = player.getWorld().getHighestBlockYAt(x, z); Location location = new Location(player.getLocation().getWorld(), x, y, z); player.teleport(location); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java index 8251bf82..a7b65792 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java @@ -3,6 +3,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.shop.ShopData; import me.totalfreedom.totalfreedommod.shop.ShopItem; +import me.totalfreedom.totalfreedommod.util.FUtil; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -16,6 +17,12 @@ public class Command_manageshop extends FreedomCommand @Override public boolean run(final CommandSender sender, final Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { + + if (!FUtil.isExecutive(sender.getName())) + { + return noPerms(); + } + if (args.length < 2) { return false; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tprandom.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tprandom.java index a4e52222..88917974 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tprandom.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tprandom.java @@ -15,8 +15,8 @@ public class Command_tprandom extends FreedomCommand @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - int x = FUtil.random(-10000, 10000); - int z = FUtil.random(-10000, 10000); + int x = FUtil.randomInteger(-10000, 10000); + int z = FUtil.randomInteger(-10000, 10000); int y = playerSender.getWorld().getHighestBlockYAt(x, z); Location location = new Location(playerSender.getLocation().getWorld(), x, y, z); playerSender.teleport(location); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java index b7fc533a..7e298663 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java @@ -84,6 +84,10 @@ public enum ConfigEntry SHOP_TITLE(String.class, "shop.title"), SHOP_PREFIX(String.class, "shop.prefix"), SHOP_COINS_PER_VOTE(Integer.class, "shop.coins_per_vote"), + SHOP_REACTIONS_ENABLED(Boolean.class, "shop.reactions.enabled"), + SHOP_REACTIONS_INTERVAL(Integer.class, "shop.reactions.interval"), + SHOP_REACTIONS_COINS_PER_WIN(Integer.class, "shop.reactions.coins_per_win"), + SHOP_REACTIONS_STRING_LENGTH(Integer.class, "shop.reactions.string_length"), SHOP_PRICES_GRAPPLING_HOOK(Integer.class, "shop.prices.grappling_hook"), SHOP_PRICES_LIGHTNING_ROD(Integer.class, "shop.prices.lightning_rod"), SHOP_PRICES_FIRE_BALL(Integer.class, "shop.prices.fire_ball"), diff --git a/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java b/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java index c31f53e5..1e76a316 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java @@ -7,6 +7,8 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.time.LocalDate; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; @@ -29,6 +31,8 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; public class Shop extends FreedomService { @@ -36,6 +40,10 @@ public class Shop extends FreedomService public final Map dataMap = Maps.newHashMap(); // uuid, dataMap @Getter private final File configFolder; + private BukkitTask reactions; + public String reactionString = ""; + public Date reactionStartTime; + public final int coinsPerReactionWin = ConfigEntry.SHOP_REACTIONS_COINS_PER_WIN.getInteger(); public Shop(TotalFreedomMod plugin) { @@ -48,6 +56,25 @@ public class Shop extends FreedomService protected void onStart() { dataMap.clear(); + if (ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean()) + { + long interval = ConfigEntry.SHOP_REACTIONS_INTERVAL.getInteger() * 20L; + + reactions = new BukkitRunnable() + { + + @Override + public void run() + { + reactionString = FUtil.randomString(ConfigEntry.SHOP_REACTIONS_STRING_LENGTH.getInteger()); + String reactionMessage = ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Reaction" + ChatColor.DARK_GRAY + "] " + + ChatColor.AQUA + "Type the string " + ChatColor.DARK_AQUA + reactionString + + ChatColor.AQUA + " to win " + ChatColor.GOLD + plugin.sh.coinsPerReactionWin + ChatColor.AQUA + " coins!"; + FUtil.bcastMsg(reactionMessage, false); + reactionStartTime = new Date(); + } + }.runTaskTimer(plugin, interval, interval); + } } @Override @@ -57,6 +84,11 @@ public class Shop extends FreedomService { save(sd); } + + if (ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean()) + { + reactions.cancel(); + } } public void save(ShopData data) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java index cab4d758..d929ee0e 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java @@ -212,7 +212,15 @@ public class FUtil public static void bcastMsg(String message, ChatColor color) { - FLog.info(message, true); + bcastMsg(message, color, true); + } + + public static void bcastMsg(String message, ChatColor color, Boolean toConsole) + { + if (toConsole) + { + FLog.info(message, true); + } for (Player player : Bukkit.getOnlinePlayers()) { @@ -220,9 +228,14 @@ public class FUtil } } + public static void bcastMsg(String message, Boolean toConsole) + { + bcastMsg(message, null, toConsole); + } + public static void bcastMsg(String message) { - FUtil.bcastMsg(message, null); + FUtil.bcastMsg(message, null, true); } // Still in use by listeners @@ -549,13 +562,29 @@ public class FUtil return packageName.substring(packageName.lastIndexOf('.') + 1); } - public static int random(int min, int max) + public static int randomInteger(int min, int max) { int range = max - min + 1; int value = (int)(Math.random() * range) + min; return value; } + public static String randomString(int length) + { + String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"; + String randomString = ""; + for (int i = 0; i < length; i++) + { + + int selectedCharacter = randomInteger(1, characters.length()) - 1; + + randomString += characters.charAt(selectedCharacter); + } + + return randomString; + + } + public static boolean isPaper() { try @@ -606,7 +635,7 @@ public class FUtil public static Player getRandomPlayer() { List players = new ArrayList<>(Bukkit.getOnlinePlayers()); - return players.get(random(0, players.size() - 1)); + return players.get(randomInteger(0, players.size() - 1)); } // convert the current time diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index fbe229d5..892cea5b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -67,7 +67,7 @@ discord: chat_channel_id: '' # Do you want roles to sync with the server? role_sync: false - # Master Admin role ID + # Master Builder role ID master_builder_role_id: '' # Super Admin role ID super_role_id: '' @@ -98,6 +98,21 @@ shop: # How many coins to voters get per vote? coins_per_vote: 20 + # Chat reactions + reactions: + + # Enable chat reactions? + enabled: true + + # How much time (in seconds) before a new reaction prompt is made + interval: 300 + + # How many coins do winners get? + coins_per_win: 5 + + # How long is the random string (in characters)? + string_length: 10 + # Item prices prices: grappling_hook: 100