shops stuffs

This commit is contained in:
ZeroEpoch1969
2020-04-07 19:20:01 -07:00
parent 9485b62716
commit af935cb824
26 changed files with 849 additions and 809 deletions

View File

@ -1,9 +1,15 @@
package me.totalfreedom.totalfreedommod.shop;
import com.google.common.collect.Maps;
import com.vexsoftware.votifier.model.Vote;
import com.vexsoftware.votifier.model.VotifierEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import lombok.Getter;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
@ -12,18 +18,22 @@ 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.Material;
import org.bukkit.enchantments.Enchantment;
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.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class Shop extends FreedomService
{
@Getter
public final Map<String, ShopData> dataMap = Maps.newHashMap(); // ip,dataMap
public final Map<UUID, ShopData> dataMap = Maps.newHashMap(); // uuid, dataMap
@Getter
private final File configFolder;
@ -54,37 +64,32 @@ public class Shop extends FreedomService
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());
dataMap.remove(data.getUUID());
dataMap.put(data.getUUID(), data);
}
public String getShopPrefix()
{
return FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString() + " ");
return FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString());
}
public String getShopTitle()
{
return FUtil.colorize(ConfigEntry.SHOP_TITLE.getString());
}
// May not return null
public ShopData getData(Player player)
{
// Check already loaded
ShopData data = dataMap.get(Ips.getIp(player));
ShopData data = dataMap.get(player.getUniqueId());
if (data != null)
{
return data;
}
// Load data
data = getData(player.getName());
data = getData(player.getUniqueId());
// Create data if nonexistent
if (data == null)
@ -92,15 +97,14 @@ public class Shop extends FreedomService
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));
data.setUsername(player.getName());
// Set defaults
data.setCoins(0);
// Store player
dataMap.put(player.getName().toLowerCase(), data);
dataMap.put(player.getUniqueId(), data);
// Save player
YamlConfig config = getConfig(data);
@ -108,46 +112,231 @@ public class Shop extends FreedomService
config.save();
}
dataMap.put(player.getUniqueId(), data);
return data;
}
// May return null
public ShopData getData(String username)
{
username = username.toLowerCase();
UUID uuid = FUtil.nameToUUID(username);
if (uuid != null)
{
return getData(uuid);
}
return null;
}
public ShopData getData(UUID uuid)
{
// Check if the player is a known player
final File configFile = getConfigFile(username);
final File configFile = getConfigFile(uuid);
if (!configFile.exists())
{
return null;
}
// Create and load entry
final ShopData data = new ShopData(username);
// Load entry
final ShopData data = new ShopData(uuid);
data.loadFrom(getConfig(data));
return data;
}
if (!data.isValid())
public Inventory generateShopGUI(ShopData shopData)
{
Inventory gui = server.createInventory(null, 36, getShopTitle());
for (int slot = 0; slot < 36; slot++)
{
FLog.warning("Could not load shop data entry: " + username + ". Entry is not valid!");
configFile.delete();
return null;
ItemStack blank = new ItemStack(Material.WHITE_STAINED_GLASS_PANE);
ItemMeta meta = blank.getItemMeta();
meta.setDisplayName(" ");
blank.setItemMeta(meta);
gui.setItem(slot, blank);
}
for (ShopItem shopItem : ShopItem.values())
{
ItemStack item = shopGUIItem(shopItem, shopData);
gui.setItem(shopItem.getSlot(), item);
}
// Coins
ItemStack coins = new ItemStack(Material.GOLD_NUGGET);
ItemMeta meta = coins.getItemMeta();
meta.setDisplayName(FUtil.colorize("&c&lYou have &e&l" + shopData.getCoins() + "&c&l coins"));
coins.setItemMeta(meta);
gui.setItem(35, coins);
return gui;
}
public boolean isRealItem(ShopData data, ShopItem shopItem, ItemStack givenItem, ItemStack realItem)
{
if (!data.hasItem(shopItem) || !givenItem.getType().equals(realItem.getType()))
{
return false;
}
// Only store data if the player is online
for (String ip : data.getIps())
ItemMeta givenMeta = givenItem.getItemMeta();
ItemMeta realMeta = realItem.getItemMeta();
if (givenMeta.getDisplayName().equals(realMeta.getDisplayName()) && givenMeta.getLore().equals(realMeta.getLore()))
{
for (Player onlinePlayer : Bukkit.getOnlinePlayers())
return true;
}
return false;
}
public ItemStack getLightningRod()
{
ItemStack itemStack = new ItemStack(Material.BLAZE_ROD);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(FUtil.colorize("&bL&3i&bg&3h&bt&3i&bn&3g &b&bR&3o&bd"));
itemMeta.setLore(Arrays.asList(ChatColor.AQUA + "Strike others down with the power of lightning.", ChatColor.RED + ChatColor.ITALIC.toString() + "The classic way to exterminate annoyances."));
itemMeta.addEnchant(Enchantment.CHANNELING, 1, false);
itemStack.setItemMeta(itemMeta);
return itemStack;
}
public ItemStack getGrapplingHook()
{
ItemStack itemStack = new ItemStack(Material.FISHING_ROD);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(ChatColor.YELLOW + "Grappling Hook");
itemMeta.setLore(Arrays.asList(ChatColor.GREEN + "be spider-man but ghetto"));
itemStack.setItemMeta(itemMeta);
return itemStack;
}
public ItemStack getFireBall()
{
ItemStack itemStack = new ItemStack(Material.FIRE_CHARGE);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(ChatColor.RED + "Fire Ball");
itemMeta.setLore(Arrays.asList(ChatColor.GOLD+ "Yeet this at people"));
itemStack.setItemMeta(itemMeta);
return itemStack;
}
public ItemStack getRideablePearl()
{
ItemStack itemStack = new ItemStack(Material.ENDER_PEARL);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(ChatColor.DARK_PURPLE + "Rideable Ender Pearl");
itemMeta.setLore(Arrays.asList(ChatColor.LIGHT_PURPLE + "What the title says.", "", ChatColor.WHITE + ChatColor.ITALIC.toString() + "TotalFreedom is not responsible for any injuries", ChatColor.WHITE + ChatColor.ITALIC.toString() + "sustained while using this item."));
itemMeta.addEnchant(Enchantment.BINDING_CURSE, 1, false);
itemStack.setItemMeta(itemMeta);
return itemStack;
}
public boolean canAfford(int price, int coins)
{
if (coins >= price)
{
return true;
}
return false;
}
public int amountNeeded(int price, int coins)
{
return price - coins;
}
public ItemStack shopGUIItem(ShopItem item, ShopData data)
{
ItemStack itemStack = new ItemStack(item.getIcon());
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(item.getColoredName());
int price = item.getCost();
int coins = data.getCoins();
Boolean canAfford = canAfford(price, coins);
List<String> lore = new ArrayList();
if (!data.hasItem(item))
{
lore.add(ChatColor.GOLD + "Price: " + (canAfford ? ChatColor.DARK_GREEN : ChatColor.RED) + price);
if (!canAfford)
{
if (Ips.getIp(onlinePlayer).equals(ip))
{
dataMap.put(ip, data);
return data;
}
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
{
lore.add(ChatColor.RED + "You already purchased this item.");
}
itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta);
return itemStack;
}
return data;
@EventHandler(priority = EventPriority.HIGH)
public void onInventoryClick(InventoryClickEvent event)
{
if (!(event.getWhoClicked() instanceof Player))
{
return;
}
Inventory inventory = event.getInventory();
if (inventory.getSize() != 36 || !event.getView().getTitle().equals(plugin.sh.getShopTitle()))
{
return;
}
event.setCancelled(true);
ShopItem shopItem = getShopItem(event.getSlot());
if (shopItem == null)
{
return;
}
Player player = (Player) event.getWhoClicked();
ShopData shopData = plugin.sh.getData(player);
int price = shopItem.getCost();
int coins = shopData.getCoins();
if (shopData.hasItem(shopItem) || !plugin.sh.canAfford(price, coins))
{
return;
}
shopData.giveItem(shopItem);
shopData.setCoins(coins - price);
save(shopData);
player.closeInventory();
player.sendMessage(plugin.sh.getShopPrefix() + " " + ChatColor.GREEN + "Successfully purchased the \"" + shopItem.getColoredName() + ChatColor.GREEN + "\" for " + ChatColor.GOLD + price + ChatColor.GREEN + "!");
if (shopItem.equals(ShopItem.GRAPPLING_HOOK))
{
player.sendMessage(ChatColor.GREEN + "Run /grapplinghook to get one!");
}
else if (shopItem.equals(ShopItem.LIGHTNING_ROD))
{
player.sendMessage(ChatColor.GREEN + "Run /lightningrod to get one!");
}
else if (shopItem.equals(ShopItem.FIRE_BALL))
{
player.sendMessage(ChatColor.GREEN + "Run /fireball to get one!");
}
else if (shopItem.equals(ShopItem.RIDEABLE_PEARL))
{
player.sendMessage(ChatColor.GREEN + "Run /rideablepearl to get one!");
}
}
public ShopItem getShopItem(int slot)
{
for (ShopItem shopItem : ShopItem.values())
{
if (shopItem.getSlot() == slot)
{
return shopItem;
}
}
return null;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@ -157,19 +346,50 @@ public class Shop extends FreedomService
dataMap.remove(ip);
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerVote(VotifierEvent event)
{
Vote vote = event.getVote();
String name = vote.getUsername();
int coinsPerVote = ConfigEntry.SHOP_COINS_PER_VOTE.getInteger();
Player player = server.getPlayer(name);
ShopData data = null;
if (player != null)
{
data = plugin.sh.getData(player);
}
else
{
data = plugin.sh.getData(name);
}
if (data != null)
{
data.setCoins(data.getCoins() + coinsPerVote);
data.setTotalVotes(data.getTotalVotes() + 1);
save(data);
FUtil.bcastMsg(ChatColor.GREEN + name + ChatColor.AQUA + " has voted for us on " + ChatColor.GREEN + vote.getServiceName() + ChatColor.AQUA + "!");
}
if (player != null)
{
player.sendMessage(ChatColor.GREEN + "Thank you for voting for us! Here are " + coinsPerVote + " coins!");
}
}
public Collection<ShopData> getLoadedData()
{
return dataMap.values();
}
protected File getConfigFile(String name)
protected File getConfigFile(UUID uuid)
{
return new File(getConfigFolder(), name + ".yml");
return new File(getConfigFolder(), uuid + ".yml");
}
protected YamlConfig getConfig(ShopData data)
{
final YamlConfig config = new YamlConfig(plugin, getConfigFile(data.getUsername().toLowerCase()), false);
final YamlConfig config = new YamlConfig(plugin, getConfigFile(data.getUUID()), false);
config.load();
return config;
}

View File

@ -1,22 +1,17 @@
package me.totalfreedom.totalfreedommod.shop;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.totalfreedommod.util.FUtil;
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.ChatColor;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class ShopData implements ConfigLoadable, ConfigSavable, Validatable
{
@ -24,30 +19,33 @@ public class ShopData implements ConfigLoadable, ConfigSavable, Validatable
@Getter
@Setter
private String username;
private final List<String> ips = Lists.newArrayList();
private String uuid;
@Getter
@Setter
private int coins;
private List<String> items = Lists.newArrayList();
@Getter
@Setter
private int totalVotes;
public ShopData(Player player)
{
this(player.getName());
this(player.getUniqueId());
}
public ShopData(String username)
public ShopData(UUID uuid)
{
this.username = username;
this.uuid = uuid.toString();
}
@Override
public void loadFrom(ConfigurationSection cs)
{
this.username = cs.getString("username", username);
this.ips.clear();
this.ips.addAll(cs.getStringList("ips"));
this.uuid = cs.getString("uuid", uuid);
this.coins = cs.getInt("coins", coins);
this.items.addAll(cs.getStringList("items"));
this.totalVotes = cs.getInt("totalVotes");
}
@Override
@ -55,25 +53,10 @@ public class ShopData implements ConfigLoadable, ConfigSavable, Validatable
{
Validate.isTrue(isValid(), "Could not save shop entry: " + username + ". Entry not valid!");
cs.set("username", username);
cs.set("ips", ips);
cs.set("uuid", uuid);
cs.set("coins", coins);
cs.set("items", items);
}
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);
cs.set("totalVotes", totalVotes);
}
public List<String> getItems()
@ -81,165 +64,38 @@ public class ShopData implements ConfigLoadable, ConfigSavable, Validatable
return Collections.unmodifiableList(items);
}
public String giveItem(ShopItem item)
public void setUUID(UUID id)
{
String signature = FUtil.generateSignature(item);
items.add(signature);
return signature;
uuid = id.toString();
}
public void giveRawItem(String signature)
public UUID getUUID()
{
items.add(signature);
return UUID.fromString(uuid);
}
public void giveItem(ShopItem item)
{
items.add(item.getDataName());
}
public boolean hasItem(ShopItem item)
{
for (String i : items)
if (items.contains(item.getDataName()))
{
int id;
try
{
id = Integer.valueOf(i.substring(0, i.indexOf("A")));
}
catch (NumberFormatException ex)
{
continue;
}
if (item.ordinal() == id)
{
return true;
}
return true;
}
return false;
}
public ItemStack getItem(ShopItem item)
public void removeItem(ShopItem item)
{
String signature = "";
for (String i : items)
{
int id;
try
{
id = Integer.valueOf(i.substring(0, i.indexOf("A")));
}
catch (NumberFormatException ex)
{
continue;
}
if (item.ordinal() == id)
{
signature = i;
}
}
ItemStack stack = new ItemStack(item.getMaterial(), 1);
ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(item.getColoredName());
List<String> lore = new ArrayList<>();
lore.add(ChatColor.DARK_GRAY + signature);
meta.setLore(lore);
stack.setItemMeta(meta);
return stack;
}
public boolean validate(ItemStack stack, String nameSegment)
{
if (!stack.hasItemMeta())
{
return false;
}
if (!stack.getItemMeta().hasDisplayName())
{
return false;
}
if (!stack.getItemMeta().getDisplayName().contains(nameSegment))
{
return false;
}
if (!stack.getItemMeta().hasLore())
{
return false;
}
boolean loreValid = false;
for (String i : items)
{
if (stack.getItemMeta().getLore().contains(ChatColor.DARK_GRAY + i))
{
loreValid = true;
}
}
if (!loreValid)
{
return false;
}
return true;
}
public boolean validate(ItemStack stack, ShopItem item)
{
if (!stack.hasItemMeta())
{
return false;
}
if (!stack.getItemMeta().hasDisplayName())
{
return false;
}
if (!stack.getItemMeta().getDisplayName().contains(item.getName()))
{
return false;
}
if (!stack.getItemMeta().hasLore())
{
return false;
}
boolean loreValid = false;
for (String i : items)
{
if (stack.getItemMeta().getLore().contains(ChatColor.DARK_GRAY + i))
{
loreValid = true;
}
}
if (!loreValid)
{
return false;
}
return true;
}
public void takeItem(ShopItem item)
{
Iterator<String> it = items.iterator();
while (it.hasNext())
{
String i = it.next();
if (i.startsWith(item.ordinal() + ""))
{
it.remove();
}
}
items.remove(item.getDataName());
}
@Override
public boolean isValid()
{
return username != null
&& !ips.isEmpty();
return username != null;
}
}

View File

@ -1,34 +1,49 @@
package me.totalfreedom.totalfreedommod.shop;
import lombok.Getter;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import org.bukkit.ChatColor;
import org.bukkit.Material;
public enum ShopItem
{
GRAPPLING_HOOK("Grappling Hook", Material.FISHING_ROD, 100, ChatColor.GREEN, true),
THOR_STAR("Thor's Star", Material.NETHER_STAR, 10000, ChatColor.LIGHT_PURPLE, true),
ELECTRICAL_DIAMOND_SWORD("Electrical Diamond Sword", Material.DIAMOND_SWORD, 0, ChatColor.YELLOW, false),
SUPERIOR_SWORD("Superior Sword", Material.GOLDEN_SWORD, 0, ChatColor.GOLD, false);
GRAPPLING_HOOK("Grappling Hook", Material.FISHING_ROD, 10, ConfigEntry.SHOP_PRICES_GRAPPLING_HOOK, ChatColor.GREEN, "grapplingHook"),
LIGHTNING_ROD("Lightning Rod", Material.BLAZE_ROD, 12, ConfigEntry.SHOP_PRICES_LIGHTNING_ROD, ChatColor.LIGHT_PURPLE, "lightningRod"),
FIRE_BALL("Fire Ball", Material.FIRE_CHARGE, 14, ConfigEntry.SHOP_PRICES_FIRE_BALL, ChatColor.RED, "fireBall"),
RIDEABLE_PEARL("Rideable Ender Pearl", Material.ENDER_PEARL, 16, ConfigEntry.SHOP_PRICES_RIDEABLE_PEARL, ChatColor.DARK_PURPLE, "rideablePearl");
/*
Shop GUI Layout:
Dimensions: 9x4 = 36
Key: g = Grappling Hook, l = Lightning Rod, f = Fire Ball, r = Rideable Ender Pearl, $ = Coins}
---------
-g-l-f-r-
---------
--------$
*/
@Getter
private final String name;
@Getter
private final Material material;
private final Material icon;
@Getter
private final int cost;
private final int slot;
private final ConfigEntry cost;
@Getter
private final ChatColor color;
@Getter
private final boolean purchaseable;
private final String dataName;
ShopItem(String name, Material material, int cost, ChatColor color, boolean purchaseable)
ShopItem(String name, Material icon, int slot, ConfigEntry cost, ChatColor color, String dataName)
{
this.name = name;
this.material = material;
this.icon = icon;
this.slot = slot;
this.cost = cost;
this.color = color;
this.purchaseable = purchaseable;
this.dataName = dataName;
}
public String getColoredName()
@ -36,6 +51,11 @@ public enum ShopItem
return color + name;
}
public int getCost()
{
return cost.getInteger();
}
public static ShopItem findItem(String string)
{
try