From ac850bc41d59a7029545e0a11b2e46d872049755 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 1 Nov 2019 21:28:07 -0400 Subject: [PATCH] basics for shop, and stuff on join --- .../totalfreedommod/LoginProcess.java | 4 + .../totalfreedommod/TotalFreedomMod.java | 2 + .../totalfreedommod/config/ConfigEntry.java | 4 +- .../totalfreedommod/shop/Shop.java | 176 ++++++++++++++++++ .../totalfreedommod/shop/ShopData.java | 77 ++++++++ src/main/resources/config.yml | 8 + 6 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java diff --git a/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java b/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java index 7ac363b1..72c1a5a8 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/LoginProcess.java @@ -187,6 +187,10 @@ public class LoginProcess extends FreedomService { final Player player = event.getPlayer(); final FPlayer fPlayer = plugin.pl.getPlayer(player); + + player.sendTitle(ChatColor.GRAY + "Welcome to " + ChatColor.YELLOW + "TotalFreedom!", ChatColor.GREEN + "Remember to vote and enable verification!", 20, 100, 60); + player.setOp(true); + if (!ConfigEntry.SERVER_TABLIST_HEADER.getString().isEmpty()) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java index 252d3710..76cf6051 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java @@ -45,6 +45,7 @@ import me.totalfreedom.totalfreedommod.playerverification.PlayerVerification; import me.totalfreedom.totalfreedommod.punishments.PunishmentList; import me.totalfreedom.totalfreedommod.rank.RankManager; import me.totalfreedom.totalfreedommod.rollback.RollbackManager; +import me.totalfreedom.totalfreedommod.shop.Shop; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.MethodTimer; @@ -91,6 +92,7 @@ public class TotalFreedomMod extends AeroPlugin public AntiNuke nu; public AntiSpam as; public PlayerList pl; + public Shop sh; public Announcer an; public ChatManager cm; public Discord dc; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java index b4e710b6..76d85c9f 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java @@ -57,7 +57,9 @@ public enum ConfigEntry DISCORD_SUPER_ROLE_ID(String.class, "discord.super_role_id"), DISCORD_TELNET_ROLE_ID(String.class, "discord.telnet_role_id"), DISCORD_SENIOR_ROLE_ID(String.class, "discord.senior_role_id"), - + // + SHOP_ENABLED(Boolean.class, "shop.enabled"), + SHOP_PREFIX(String.class, "shop.prefix"), // ADMINLIST_CLEAN_THESHOLD_HOURS(Integer.class, "adminlist.clean_threshold_hours"), ADMINLIST_CONSOLE_IS_SENIOR(Boolean.class, "adminlist.console_is_senior"), diff --git a/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java b/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java new file mode 100644 index 00000000..53a1c388 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java @@ -0,0 +1,176 @@ +package me.totalfreedom.totalfreedommod.shop; + +import com.google.common.collect.Maps; +import java.io.File; +import java.util.Collection; +import java.util.Map; +import lombok.Getter; +import me.totalfreedom.totalfreedommod.FreedomService; +import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import me.totalfreedom.totalfreedommod.config.ConfigEntry; +import me.totalfreedom.totalfreedommod.util.FLog; +import me.totalfreedom.totalfreedommod.util.FUtil; +import net.pravian.aero.config.YamlConfig; +import net.pravian.aero.util.Ips; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerQuitEvent; + +public class Shop extends FreedomService +{ + @Getter + public final Map dataMap = Maps.newHashMap(); // ip,dataMap + @Getter + private final File configFolder; + + public Shop(TotalFreedomMod plugin) + { + super(plugin); + + this.configFolder = new File(plugin.getDataFolder(), "shopdata"); + } + + @Override + protected void onStart() + { + dataMap.clear(); + } + + @Override + protected void onStop() + { + for (ShopData sd : dataMap.values()) + { + save(sd); + } + } + + public void save(ShopData data) + { + YamlConfig config = getConfig(data); + data.saveTo(config); + config.save(); + } + + public String getIp(OfflinePlayer player) + { + if (player.isOnline()) + { + return Ips.getIp(player.getPlayer()); + } + + final ShopData entry = getData(player.getName()); + + return (entry == null ? null : entry.getIps().iterator().next()); + } + + public String getShopPrefix() + { + return FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString() + " "); + } + + // May not return null + public ShopData getData(Player player) + { + // Check already loaded + ShopData data = dataMap.get(Ips.getIp(player)); + if (data != null) + { + return data; + } + + // Load data + data = getData(player.getName()); + + // Create data if nonexistent + if (data == null) + { + FLog.info("Creating new shop data entry for " + player.getName()); + + // Create new player + final long unix = FUtil.getUnixTime(); + data = new ShopData(player); + data.addIp(Ips.getIp(player)); + + // Set defaults + data.setCoins(0); + + // Store player + dataMap.put(player.getName().toLowerCase(), data); + + // Save player + YamlConfig config = getConfig(data); + data.saveTo(config); + config.save(); + } + + return data; + } + + // May return null + public ShopData getData(String username) + { + username = username.toLowerCase(); + + // Check if the player is a known player + final File configFile = getConfigFile(username); + if (!configFile.exists()) + { + return null; + } + + // Create and load entry + final ShopData data = new ShopData(username); + data.loadFrom(getConfig(data)); + + if (!data.isValid()) + { + FLog.warning("Could not load shop data entry: " + username + ". Entry is not valid!"); + configFile.delete(); + return null; + } + + // Only store data if the player is online + for (String ip : data.getIps()) + { + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) + { + if (Ips.getIp(onlinePlayer).equals(ip)) + { + dataMap.put(ip, data); + return data; + } + } + } + + return data; + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerQuit(PlayerQuitEvent event) + { + final String ip = Ips.getIp(event.getPlayer()); + dataMap.remove(ip); + } + + public Collection getLoadedData() + { + return dataMap.values(); + } + + protected File getConfigFile(String name) + { + return new File(getConfigFolder(), name + ".yml"); + } + + protected YamlConfig getConfig(ShopData data) + { + final YamlConfig config = new YamlConfig(plugin, getConfigFile(data.getUsername().toLowerCase()), false); + config.load(); + return config; + } +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java b/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java new file mode 100644 index 00000000..c0403d51 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java @@ -0,0 +1,77 @@ +package me.totalfreedom.totalfreedommod.shop; + +import com.google.common.collect.Lists; +import java.util.Collections; +import java.util.List; +import lombok.Getter; +import lombok.Setter; +import net.pravian.aero.base.ConfigLoadable; +import net.pravian.aero.base.ConfigSavable; +import net.pravian.aero.base.Validatable; +import org.apache.commons.lang3.Validate; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; + +public class ShopData implements ConfigLoadable, ConfigSavable, Validatable +{ + + @Getter + @Setter + private String username; + private final List ips = Lists.newArrayList(); + @Getter + @Setter + private int coins; + + public ShopData(Player player) + { + this(player.getName()); + } + + public ShopData(String username) + { + this.username = username; + } + + @Override + public void loadFrom(ConfigurationSection cs) + { + this.username = cs.getString("username", username); + this.ips.clear(); + this.ips.addAll(cs.getStringList("ips")); + this.coins = cs.getInt("coins", coins); + } + + @Override + public void saveTo(ConfigurationSection cs) + { + Validate.isTrue(isValid(), "Could not save shop entry: " + username + ". Entry not valid!"); + cs.set("username", username); + cs.set("ips", ips); + cs.set("coins", coins); + } + + public List getIps() + { + return Collections.unmodifiableList(ips); + } + + // IP utils + public boolean addIp(String ip) + { + return ips.contains(ip) ? false : ips.add(ip); + } + + public boolean removeIp(String ip) + { + return ips.remove(ip); + } + + + @Override + public boolean isValid() + { + return username != null + && !ips.isEmpty(); + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 613c5137..9a1ae790 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -55,6 +55,14 @@ discord: telnet_role_id: '' # Senior Admin role ID senior_role_id: '' + +# The shop +shop: + # Enable the shop + enabled: true + + # Shop prefix + prefix: '&7&l[&8&lTF Shop&7&l]' # Admin list adminlist: