-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Groups File, Model and Functions | Add missing backend Methods
- Loading branch information
1 parent
cbc1a0d
commit e46328b
Showing
10 changed files
with
283 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package de.tobias.spigotdash.utils; | ||
|
||
import com.google.gson.*; | ||
|
||
public class SerialUtils { | ||
|
||
// FROM: https://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java | ||
|
||
public static String serializeObject(Object o) { | ||
Gson gson = new Gson(); | ||
String serializedObject = gson.toJson(o); | ||
return serializedObject; | ||
} | ||
|
||
public static Object unserializeObject(String s, Object o){ | ||
Gson gson = new Gson(); | ||
Object object = gson.fromJson(s, o.getClass()); | ||
return object; | ||
} | ||
|
||
public static Object cloneObject(Object o){ | ||
String s = serializeObject(o); | ||
Object object = unserializeObject(s,o); | ||
return object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.tobias.spigotdash.utils.files; | ||
|
||
import de.tobias.spigotdash.utils.pluginConsole; | ||
import de.tobias.spigotdash.web.PermissionSet; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class Group implements Comparable<Group> { | ||
|
||
public String name; | ||
public String id; | ||
public String html_color; | ||
|
||
public Integer LEVEL; | ||
|
||
public PermissionSet permissions; | ||
|
||
public Group(String name, PermissionSet perms) { | ||
this.LEVEL = 0; | ||
this.name = name; | ||
this.id = UUID.randomUUID().toString(); | ||
this.permissions = perms; | ||
this.html_color = "#2965bd"; | ||
} | ||
|
||
public PermissionSet addGrantedPermissionsToSet(PermissionSet input) { | ||
PermissionSet output = input; | ||
for(Map.Entry<String, Boolean> permEntry : PermissionSet.getAsMap(permissions).entrySet()) { | ||
if(permEntry.getValue()) { | ||
PermissionSet.setPermissionByName(permEntry.getKey(), permEntry.getValue(), output); | ||
//pluginConsole.sendMessage("Inherited Permission from Group: " + permEntry.getKey() + " --> " + permEntry.getValue()); | ||
} | ||
} | ||
|
||
return output; | ||
} | ||
|
||
@Override | ||
public int compareTo(Group o) { | ||
return o.LEVEL.compareTo(this.LEVEL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/de/tobias/spigotdash/utils/files/groupsFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package de.tobias.spigotdash.utils.files; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import de.tobias.spigotdash.utils.errorCatcher; | ||
import de.tobias.spigotdash.utils.pluginConsole; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.lang.reflect.Array; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class groupsFile { | ||
|
||
public String FILE_VER = "0.1"; | ||
public ArrayList<Group> groups = new ArrayList<Group>(); | ||
|
||
public transient File f; | ||
|
||
public groupsFile(File pF) { | ||
f = pF; | ||
} | ||
|
||
public boolean save() { | ||
try { | ||
FileUtils.write(f, gson.toJson(this), StandardCharsets.UTF_8); | ||
return true; | ||
} catch (Exception ex) { | ||
pluginConsole.sendMessage("&cFailed to save Groups File: "); | ||
errorCatcher.catchException(ex, false); | ||
} | ||
return false; | ||
} | ||
|
||
public Group getGroupByName(String name) { | ||
for(Group g : groups) { | ||
if(g.name.equals(name)) return g; | ||
} | ||
return null; | ||
} | ||
|
||
public Group getGroupByID(String id) { | ||
for(Group g : groups) { | ||
if(g.id.equals(id)) return g; | ||
} | ||
return null; | ||
} | ||
|
||
public boolean groupExists(String value) { | ||
return (getGroupByName(value) != null || getGroupByID(value) != null); | ||
} | ||
|
||
public boolean add(Group g) { | ||
if(!groupExists(g.id) && !groupExists(g.name)) { | ||
groups.add(g); | ||
save(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public HashMap<String, HashMap<String, Object>> getGroupsSave() { | ||
HashMap<String, HashMap<String, Object>> safeGroups = new HashMap<>(); | ||
|
||
for(Group g : groups) { | ||
HashMap<String, Object> data = new HashMap<>(); | ||
data.put("name", g.name); | ||
data.put("perms", g.permissions); | ||
data.put("html_color", g.html_color); | ||
data.put("LEVEL", g.LEVEL); | ||
safeGroups.put(g.id, data); | ||
} | ||
return safeGroups; | ||
} | ||
|
||
public ArrayList<Group> getGroupOrder() { | ||
Collections.sort(groups); | ||
return groups; | ||
} | ||
|
||
// *** STATIC *** | ||
public static Gson gson = (new GsonBuilder()).disableHtmlEscaping().serializeNulls().setPrettyPrinting().create(); | ||
|
||
public static groupsFile getFromFile(File f) { | ||
try { | ||
groupsFile obj; | ||
|
||
if(f.exists()) { | ||
obj = gson.fromJson(new FileReader(f.getAbsolutePath()), groupsFile.class); | ||
} else { | ||
pluginConsole.sendMessage("&6Groups File was not found! Using empty Groups File Object..."); | ||
obj = new groupsFile(f); | ||
} | ||
|
||
if(obj == null) throw new NullPointerException(); | ||
obj.f = f; | ||
|
||
return obj; | ||
} catch (Exception ex) { | ||
pluginConsole.sendMessage("&cFailed to load Groups File: "); | ||
errorCatcher.catchException(ex, false); | ||
pluginConsole.sendMessage("&6Using empty Groups File Object..."); | ||
} | ||
return new groupsFile(f); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.