basics for shop, and stuff on join

This commit is contained in:
Ivan 2019-11-01 21:28:07 -04:00
parent d6dbdf15bc
commit ac850bc41d
6 changed files with 270 additions and 1 deletions

View File

@ -187,6 +187,10 @@ public class LoginProcess extends FreedomService
{ {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
final FPlayer fPlayer = plugin.pl.getPlayer(player); 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()) if (!ConfigEntry.SERVER_TABLIST_HEADER.getString().isEmpty())
{ {

View File

@ -45,6 +45,7 @@ import me.totalfreedom.totalfreedommod.playerverification.PlayerVerification;
import me.totalfreedom.totalfreedommod.punishments.PunishmentList; import me.totalfreedom.totalfreedommod.punishments.PunishmentList;
import me.totalfreedom.totalfreedommod.rank.RankManager; import me.totalfreedom.totalfreedommod.rank.RankManager;
import me.totalfreedom.totalfreedommod.rollback.RollbackManager; import me.totalfreedom.totalfreedommod.rollback.RollbackManager;
import me.totalfreedom.totalfreedommod.shop.Shop;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import me.totalfreedom.totalfreedommod.util.MethodTimer; import me.totalfreedom.totalfreedommod.util.MethodTimer;
@ -91,6 +92,7 @@ public class TotalFreedomMod extends AeroPlugin<TotalFreedomMod>
public AntiNuke nu; public AntiNuke nu;
public AntiSpam as; public AntiSpam as;
public PlayerList pl; public PlayerList pl;
public Shop sh;
public Announcer an; public Announcer an;
public ChatManager cm; public ChatManager cm;
public Discord dc; public Discord dc;

View File

@ -57,7 +57,9 @@ public enum ConfigEntry
DISCORD_SUPER_ROLE_ID(String.class, "discord.super_role_id"), DISCORD_SUPER_ROLE_ID(String.class, "discord.super_role_id"),
DISCORD_TELNET_ROLE_ID(String.class, "discord.telnet_role_id"), DISCORD_TELNET_ROLE_ID(String.class, "discord.telnet_role_id"),
DISCORD_SENIOR_ROLE_ID(String.class, "discord.senior_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_CLEAN_THESHOLD_HOURS(Integer.class, "adminlist.clean_threshold_hours"),
ADMINLIST_CONSOLE_IS_SENIOR(Boolean.class, "adminlist.console_is_senior"), ADMINLIST_CONSOLE_IS_SENIOR(Boolean.class, "adminlist.console_is_senior"),

View File

@ -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<String, ShopData> 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<ShopData> 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;
}
}

View File

@ -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<String> 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<String> 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();
}
}

View File

@ -55,6 +55,14 @@ discord:
telnet_role_id: '' telnet_role_id: ''
# Senior Admin role ID # Senior Admin role ID
senior_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 # Admin list
adminlist: adminlist: