Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package sugaku.rpg.framework;
package io.github.math0898.rpgframework;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import sugaku.rpg.factions.Faction;
import sugaku.rpg.factions.FactionData;
import sugaku.rpg.framework.classes.Classes;
import sugaku.rpg.framework.players.PlayerManager;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;
import java.util.UUID;

public class FileManager {
Expand Down Expand Up @@ -71,8 +66,7 @@ public static void init(Player p) {
try {
file.delete();
if(file.createNewFile()) {
FactionData[] norm = {new FactionData(0, Faction.ABYSS), new FactionData(0, Faction.ELEMENTAL)};
PlayerManager.addUserData(new UserData(norm, p.getUniqueId()));
PlayerManager.addUserData(new UserData(p.getUniqueId()));
save(p);
console("Created new file for " + p.getName() + "!", ChatColor.GREEN);
}
Expand All @@ -83,7 +77,7 @@ public static void init(Player p) {
* Unloads the data on the given player.
* @param p The player who's data should be unloaded.
*/
public static void unload(Player p) {
public static void unload (Player p) {
console("Unloading data on " + p.getName());

save(p);
Expand All @@ -97,89 +91,25 @@ public static void unload(Player p) {
* Reads the associated file for the given player. If no file is found it calls init(Player) to create one.
* @param p The player's file which should be read.
*/
public static void load(Player p) {
public static void load (Player p) {
console("Loading player data for " + p.getName() + ".");

File file = new File("./plugins/RPG/PlayerData/" + p.getUniqueId());
UserData data = new UserData(p.getUniqueId());
try {
Scanner scanner = new Scanner(file);

switch (scanner.nextLine()) {
case "1.0": reader1_0(p, scanner);
case "1.1": reader1_1(p, scanner);
case "1.2": reader1_2(p, scanner);
}

scanner.close();

YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
ConfigurationSection collections = config.getConfigurationSection("collections");
if (collections != null)
for (String s : collections.getKeys(false))
data.addCollection(s, new ItemCollection(collections.getConfigurationSection(s))); // TODO: should this be part of UserData?
PlayerManager.addUserData(data);
console("Loaded player data for " + p.getName() + ".", ChatColor.GREEN);
} catch (Exception e) {
console("File not found for " + p.getName() + ".", ChatColor.YELLOW);
init(p);
}
}

/**
* Translates the data in the scanner into a UserData object for Player p.
* @param p The player who we're making the UserData for
* @param s The scanner which holds the data dump
*/
private static void reader1_0 (Player p, Scanner s) {
ArrayList<FactionData> data = new ArrayList<>();

while(s.hasNextLine()) {
String faction = s.nextLine();
int rep = s.nextInt();
s.nextLine();
if (faction.equals("Abyss")) data.add(new FactionData(rep, Faction.ABYSS));
else if (faction.equals("Elemental")) data.add(new FactionData(rep, Faction.ELEMENTAL));
}

PlayerManager.addUserData(new UserData(data.toArray(new FactionData[0]), p.getUniqueId()));
}

/**
* Translates the data in the scanner into a UserData object for Player p.
* @param p The player who we're making the UserData for.
* @param s The scanner which holds the data dump.
*/
private static void reader1_1 (Player p, Scanner s) {
ArrayList<FactionData> data = new ArrayList<>();

s.nextLine();
data.add(new FactionData(Integer.parseInt(s.nextLine()), Faction.ABYSS));
s.nextLine();
data.add(new FactionData(Integer.parseInt(s.nextLine()), Faction.ELEMENTAL));
Objects.requireNonNull(PlayerManager.getPlayer(p.getUniqueId())).joinClass(Classes.fromString(s.nextLine()));

PlayerManager.addUserData(new UserData(data.toArray(new FactionData[0]), p.getUniqueId()));
}

/**
* Translate the data in the scanner into a UserData object for Player p. Also loads data like class levels.
*
* @param p The player who we're making the UserData for.
* @param s The scanner which holds the data dump.
*/
private static void reader1_2 (Player p, Scanner s) {
ArrayList<FactionData> data = new ArrayList<>();

s.nextLine();
data.add(new FactionData(Integer.parseInt(s.nextLine()), Faction.ABYSS));
s.nextLine();
data.add(new FactionData(Integer.parseInt(s.nextLine()), Faction.ELEMENTAL));
Objects.requireNonNull(PlayerManager.getPlayer(p.getUniqueId())).joinClass(Classes.fromString(s.nextLine()));

int[] a = new int[]{Integer.parseInt(s.nextLine()),
Integer.parseInt(s.nextLine()),
Integer.parseInt(s.nextLine()),
Integer.parseInt(s.nextLine()),
Integer.parseInt(s.nextLine()),
Integer.parseInt(s.nextLine()),
Integer.parseInt(s.nextLine())};

PlayerManager.addUserData(new UserData(data.toArray(new FactionData[0]), p.getUniqueId(), a));
}
/**
* Saves the player's data into their file.
* @param p The player who's data we're saving.
Expand All @@ -203,22 +133,11 @@ private static void save(String name, UUID uuid) {
console("Saving data for " + name + ".");

try {
FileWriter writer = new FileWriter("./plugins/RPG/PlayerData/" + uuid);
UserData save = null;
for (UserData d: PlayerManager.getUserData()) if (d.getUuid() == uuid) { save = d; break; }
if (save == null) { console("Unable to find " + name + "'s data among loaded data.", ChatColor.RED); return; }

writer.write("1.1\n");
writer.write("Abyss\n");
writer.write(save.getAbyssData().getReputation() + "\n");
writer.write("Elemental\n");
writer.write(save.getElementalData().getReputation() + "\n");
writer.write(Objects.requireNonNull(PlayerManager.getPlayer(uuid)).getCombatClassString() + "\n");

// int[] a = Objects.requireNonNull(PlayerManager.getUserData(uuid)).getXp();
// for (int i = 0; i < a.length; i++) writer.write(a[i]);
writer.close();

UserData data = PlayerManager.getUserData(uuid);
if (data == null) throw new IOException("Player does not have data!");
YamlConfiguration config = new YamlConfiguration();
data.toConfigurationSection(config);
config.save("./plugins/RPG/PlayerData/" + uuid);
console("Saved data for " + name + "!", ChatColor.GREEN);

} catch (IOException e) {
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/io/github/math0898/rpgframework/ItemCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.math0898.rpgframework;

import com.google.common.primitives.UnsignedLong;
import org.bukkit.configuration.ConfigurationSection;

/**
* An ItemCollection contains the score that a given player has for a specific resource.
*
* @author Sugaku
*/
public class ItemCollection {

/**
* The value of this collection.
*/
private UnsignedLong value;

/**
* Creates a new ItemCollection with the given value.
*
* @param val The value to assign to this ItemCollection.
*/
public ItemCollection (long val) {
value = UnsignedLong.valueOf(val);
}

/**
* Creates a new ItemCollection from the given configuration section.
*
* @param sec The configuration section to create this ItemCollection from.
*/
public ItemCollection (ConfigurationSection sec) {
if (sec != null)
if (sec.contains("value")) {
value = UnsignedLong.valueOf(sec.getLong("value", 0));
return;
}
value = UnsignedLong.ZERO;
}

/**
* Assigns the necessary information to the given ConfigurationSection to reconstruct this object.
*
* @param sec The configuration section to assign data to.
*/
public void toConfigurationSection (ConfigurationSection sec) {
sec.set("value", value.longValue());
}

/**
* Adds the given value to the ItemCollection.
*
* @param val The value to add onto this ItemCollection.
*/
public void add (long val) {
value = value.plus(UnsignedLong.valueOf(val));
}

/**
* Returns the value stored in this ItemCollection.
*
* @return The value stored in this ItemCollection.
*/
public long get () {
return value.longValue();
}
}
105 changes: 105 additions & 0 deletions src/main/java/io/github/math0898/rpgframework/UserData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package io.github.math0898.rpgframework;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import sugaku.rpg.factions.Faction;
import sugaku.rpg.factions.FactionData;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class UserData {

/**
* The UUID of the player who's data is loaded.
*/
private final UUID uuid;

/**
* The username of the player whom this data belongs to.
*/
private final String username;

/**
* A map of the collections that this player has. Some may be null.
*/
private final Map<String, ItemCollection> collections = new HashMap<>();

/**
* Creates a new empty UserData which data can be assigned to.
*
* @param uuid The uuid of the player this data is for.
*/
public UserData (UUID uuid) {
this.uuid = uuid;
Player p = Bukkit.getPlayer(uuid);
if (p != null) username = p.getName();
else username = "Unknown";
}

/**
* Adds a collection to this UserData if not present.
*
* @param name The name of the collection to add.
* @param collection The ItemCollection object to assign.
*/
public void addCollection (String name, ItemCollection collection) {
collections.put(name, collection);
}

/**
* Accessor method for collections if they are present in this UserData.
*
* @param name The name of the collection to grab.
* @return The ItemCollection that may or may not have been found.
*/
public ItemCollection getCollection (String name) {
return collections.get(name);
}

/**
* Accessor method for the list of collections that are present in this UserData.
*
* @return The String set of collections that exist.
*/
public Set<String> registeredCollections () {
return collections.keySet();
}

/**
* Assigns the important values of this UserData to the given ConfigurationSection.
*
* @param sec The configuration section to assign the important values to.
*/
public void toConfigurationSection (ConfigurationSection sec) {
sec.set("username", username);
if (!collections.isEmpty()) {
ConfigurationSection col = sec.createSection("collections");
for (String c : collections.keySet())
collections.get(c).toConfigurationSection(col.createSection(c));
}
}

/**
* Accesses the data on the Abyss faction.
* @return The data on the abyss faction.
*/
@Deprecated
public FactionData getAbyssData() { return new FactionData(0, Faction.ABYSS); }

/**
* Access the data on the Elementals for the player.
* @return The data on the elemental faction.
*/
@Deprecated
public FactionData getElementalData() { return new FactionData(0, Faction.ELEMENTAL); }

/**
* Access the uuid of the player this data is on and return it.
* @return The uuid of the player.
*/
public UUID getUuid() { return uuid; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.math0898.rpgframework.collections;

import org.bukkit.Material;

import java.util.UUID;

/**
* The CandidateCollectionEvent stores all the information the CollectionManager needs to consider a particular event in
* a single object, so we can store it in a stack easily.
*
* @param type The material involved.
* @param amount The number of items.
* @param player The uuid of the player who may be credited.
*/
public record CandidateCollectionEvent (Material type, long amount, UUID player) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.math0898.rpgframework.collections;

import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDropItemEvent;

/**
* The CollectionListener listens to events that could add to a player's collection. We catalog these events until they
* can be handled async by the CollectionManager.
*
* @author Sugaku
*/
public class CollectionListener implements Listener {

/**
* Called whenever a block is broken.
*
* @param event The block break event to pass onto the CollectionManager.
*/
@EventHandler
public void onBlockBreak (BlockDropItemEvent event) {
if (event.getItems().size() == 0) return;
for (Item i : event.getItems()) CollectionManager.getInstance().addEvent(new CandidateCollectionEvent(i.getItemStack().getType(), i.getItemStack().getAmount(), event.getPlayer().getUniqueId()));
}
}
Loading