Skip to content

Commit

Permalink
* Removed old deprecated StringUtils methods
Browse files Browse the repository at this point in the history
* Added new methods to FileList
* Added new method to Registry
* Added new Vault permission wrapper VaultPlayer
* Finalized UpdateChecker
* Cleaned up code.
  • Loading branch information
Hempfest committed May 29, 2021
1 parent 22c953a commit 7514822
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 98 deletions.
Binary file modified out/artifacts/Labyrinth_jar/Labyrinth.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/Labyrinth.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public void onEnable() {
}
}
Region.Loading region = new Region.Loading(o, t, d);
region.setPassthrough(Region.DATA.getConfig().getBoolean("Markers.region." + id + ".pass"));
region.setOwner(owner);
if (Region.DATA.getConfig().getString("Markers.region." + id + ".name") != null) {
region.setName(Region.DATA.getConfig().getString("Markers.region." + id + ".name"));
Expand All @@ -176,6 +177,7 @@ public void onEnable() {
run(() -> {
for (Region.Loading load : Region.loading().list()) {
Region.Standard result = new Region.Standard(load);
result.setPassthrough(load.isPassthrough());
if (!result.load()) {
getLogger().warning("- A pre-loaded region under the name '" + result.getId() + "' has failed to load properly.");
}
Expand All @@ -184,6 +186,7 @@ public void onEnable() {

for (Region.Spawning spawn : Region.spawning().list()) {
Region.Spawn result = new Region.Spawn(spawn);
result.setPassthrough(spawn.isPassthrough());
result.setLocation(spawn.location());
if (!result.load()) {
getLogger().warning("- A pre-loaded region under the name '" + result.getId() + "' has failed to load properly.");
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/data/FileList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.sanctum.labyrinth.data;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -39,6 +41,55 @@ public static FileList search(@NotNull final Plugin plugin) {
return list != null ? list : new FileList(plugin);
}

/**
* Get the list of all cached file managers for a specified plugin.
*
* @param plugin The plugin to retrieve file management for.
* @return A list of file managers if the plugin is cached otherwise an empty list.
*/
public static List<FileManager> getFiles(Plugin plugin) {
return CACHE.entrySet().stream().filter(e -> e.getKey().getName().equals(plugin.getName())).map(Map.Entry::getValue).findFirst().orElse(new ArrayList<>());
}

/**
* Get the plugin attached to the search.
*
* @return The plugin attached to this file search.
*/
public Plugin getPlugin() {
return plugin;
}

/**
* Copy a file of any type from this listings plugin.
*
* @param fileName The name of the file following the file type.
* @param fileMangager The filemanager instance to copy to.
*/
public void copy(String fileName, FileManager fileMangager) {
FileManager.copy(this.plugin.getResource(fileName), fileMangager);
}

/**
* Copy a yml file from this listings plugin to a manager of specification.
*
* @param fileName The file name only.
* @param fileMangager The filemanager instance to copy to.
*/
public void copyYML(String fileName, FileManager fileMangager) {
FileManager.copy(this.plugin.getResource(fileName + ".yml"), fileMangager);
}

/**
* Copy a yml file from this listings plugin to a file of specification.
*
* @param fileName The file name only.
* @param file The file instance to copy to.
*/
public void copyYML(String fileName, File file) {
FileManager.copy(this.plugin.getResource(fileName + ".yml"), file);
}

/**
* Retrieve a Config instance via its name and description.
* <p>
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/data/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.bukkit.plugin.Plugin;
Expand All @@ -27,6 +28,7 @@
public class Registry<T> {

private final Class<T> CLASS;
private Predicate<? super String> FILTER;
private Plugin PLUGIN = null;
private JarFile FILE;
private String PACKAGE;
Expand Down Expand Up @@ -59,6 +61,19 @@ public Registry<T> source(Plugin plugin) throws IOException {
return this;
}

/**
* Filter your search for locations targeted.
* <p>
* Alternative to {@link Registry#pick(String)}
*
* @param predicate The information to rely on before targeting a search.
* @return The same registry instance.
*/
public Registry<T> filter(Predicate<? super String> predicate) {
this.FILTER = predicate;
return this;
}

/**
* Source a file for the main jar file directory.
*
Expand Down Expand Up @@ -100,6 +115,62 @@ public RegistryData<T> operate(Consumer<T> operation) {
}
}

List<JarEntry> entries = Collections.list(jarFile.entries());

if (this.FILTER != null) {
entries.forEach(entry -> {
String className = entry.getName().replace("/", ".");
final String substring = className.substring(0, Math.max(className.length() - 6, 0));
if (this.FILTER.test(className)) {
Class<?> clazz = null;
try {
clazz = Class.forName(substring);
} catch (ClassNotFoundException ignored) {
}
if (clazz != null) {
if (CLASS.isAssignableFrom(clazz)) {
classes.add((Class<T>) clazz);
}
}
}
});
} else {
entries.forEach(entry -> {

String className = entry.getName().replace("/", ".");
final String substring = className.substring(0, Math.max(className.length() - 6, 0));
if (this.PACKAGE != null) {
if (className.startsWith(PACKAGE) && className.endsWith(".class")) {
Class<?> clazz = null;
try {
clazz = Class.forName(substring);
} catch (ClassNotFoundException ignored) {
}
if (clazz != null) {
if (CLASS.isAssignableFrom(clazz)) {
classes.add((Class<T>) clazz);
}
}
}
} else {

if (className.endsWith(".class")) {
Class<?> clazz = null;
try {
clazz = Class.forName(substring);
} catch (ClassNotFoundException ignored) {
}
if (clazz != null) {
if (CLASS.isAssignableFrom(clazz)) {
classes.add((Class<T>) clazz);
}
}
}

}
});
}

for (JarEntry jarEntry : Collections.list(jarFile.entries())) {
String className = jarEntry.getName().replace("/", ".");
if (this.PACKAGE != null) {
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/github/sanctum/labyrinth/library/Group.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.sanctum.labyrinth.library;

import java.util.LinkedList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.permissions.Permission;

public class Group {

private final String name;
private final String world;

protected Group(String name, String world) {
this.name = name;
this.world = world;
}

public String getName() {
return name;
}

public World getWorld() {
return Bukkit.getWorld(world);
}

public boolean has(Permission permission) {
return VaultPlayer.PERMS.groupHas(getWorld(), getName(), permission.getName());
}

public boolean has(String permission) {
return VaultPlayer.PERMS.groupHas(getWorld(), getName(), permission);
}

public boolean give(String permission) {
return VaultPlayer.PERMS.groupAdd(getWorld(), getName(), permission);
}

public boolean give(Permission permission) {
return VaultPlayer.PERMS.groupAdd(getWorld(), getName(), permission.getName());
}

public boolean take(String permission) {
return VaultPlayer.PERMS.groupRemove(getWorld(), getName(), permission);
}

public boolean take(Permission permission) {
return VaultPlayer.PERMS.groupRemove(getWorld(), getName(), permission.getName());
}

public Permission[] getKnownPermissions() {
List<Permission> perms = new LinkedList<>();
for (String cmd : CommandUtils.getServerCommandListing()) {
Command command = CommandUtils.getCommandByLabel(cmd);
if (command != null) {
if (command.getPermission() != null) {
if (VaultPlayer.PERMS.groupHas(getWorld(), getName(), command.getPermission())) {
perms.add(new Permission(command.getPermission(), command.getDescription()));
}
}
}
}
return perms.toArray(new Permission[0]);
}

}
60 changes: 52 additions & 8 deletions src/main/java/com/github/sanctum/labyrinth/library/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.github.sanctum.labyrinth.Labyrinth;
import com.github.sanctum.labyrinth.event.ItemRecipeProcessEvent;
import com.github.sanctum.labyrinth.event.ItemStackProcessEvent;
import com.google.common.base.Preconditions;
import com.google.common.collect.MapMaker;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -91,7 +93,7 @@ public Item setItem(char key, ItemStack item) {
public Item buildStack() {
ItemStack item = new ItemStack(mat);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(StringUtils.translate(name));
meta.setDisplayName(StringUtils.use(name).translate());
item.setItemMeta(meta);
ItemStackProcessEvent event = new ItemStackProcessEvent(name, item);
Bukkit.getPluginManager().callEvent(event);
Expand All @@ -101,7 +103,7 @@ public Item buildStack() {

public Item attachLore(List<String> lore) {
ItemMeta meta = item.getItemMeta();
meta.setLore(lore.stream().map(StringUtils::translate).collect(Collectors.toList()));
meta.setLore(lore.stream().map(s -> StringUtils.use(s).translate()).collect(Collectors.toList()));
item.setItemMeta(meta);
return this;
}
Expand All @@ -113,7 +115,6 @@ public Item addEnchant(Enchantment e, int level) {
return this;
}

@SuppressWarnings("deprecation")
public Item shapeRecipe(String... shape) {
ShapedRecipe recipe = new ShapedRecipe(key, item);
List<String> list = Arrays.asList(shape);
Expand Down Expand Up @@ -370,6 +371,15 @@ public static class Edit {
private Map<Enchantment, Integer> ENCHANTS;
private String TITLE;

public Edit(Collection<ItemStack> items) {
this.LISTED = true;
List<ItemStack> temp = new LinkedList<>();
for (ItemStack it : items) {
temp.add(new ItemStack(it));
}
this.LIST = temp;
}

public Edit(ItemStack... i) {
this.LISTED = true;
List<ItemStack> temp = new LinkedList<>();
Expand All @@ -380,10 +390,12 @@ public Edit(ItemStack... i) {
}

public Edit(ItemStack i) {
Preconditions.checkArgument(i != null, "ItemStack cannot be null");
this.ITEM = new ItemStack(i);
}

public Edit(Material mat) {
if (mat == null) mat = Material.PAPER;
this.ITEM = new ItemStack(mat);
}

Expand All @@ -392,6 +404,28 @@ public Edit setTitle(String text) {
return this;
}

public Edit setAmount(int amount) {
if (LISTED) {
for (ItemStack i : LIST) {
i.setAmount(amount);
}
} else {
this.ITEM.setAmount(amount);
}
return this;
}

public Edit setType(Material type) {
if (LISTED) {
for (ItemStack i : LIST) {
i.setType(type);
}
} else {
this.ITEM.setType(type);
}
return this;
}

public Edit setLore(String... text) {
List<String> list = new LinkedList<>();
for (String s : text) {
Expand Down Expand Up @@ -596,11 +630,11 @@ public ItemStack build() {

public static class CustomFirework {

private final Firework FIREWORK;
private final Location LOCATION;
private FireworkEffect.Builder EFFECTS;

protected CustomFirework(@NotNull Location location) {
this.FIREWORK = (Firework) location.getWorld().spawnEntity(location, Entities.getEntity("firework"));
this.LOCATION = location;
}

public static CustomFirework from(Location location) {
Expand All @@ -614,15 +648,25 @@ public CustomFirework addEffects(Consumer<FireworkEffect.Builder> operation) {
return this;
}

public Firework build() {
Firework f = (Firework) LOCATION.getWorld().spawnEntity(LOCATION, Entities.getEntity("firework"));
FireworkMeta meta = f.getFireworkMeta();
if (this.EFFECTS != null) {
meta.addEffect(this.EFFECTS.build());
}
f.setFireworkMeta(meta);
return f;
}

public Firework build(Consumer<FireworkMeta> operation) {
FireworkMeta meta = this.FIREWORK.getFireworkMeta();
Firework f = (Firework) LOCATION.getWorld().spawnEntity(LOCATION, Entities.getEntity("firework"));
FireworkMeta meta = f.getFireworkMeta();
operation.accept(meta);
if (this.EFFECTS != null) {
meta.addEffect(this.EFFECTS.build());
}
this.FIREWORK.setFireworkMeta(meta);
return this.FIREWORK;
f.setFireworkMeta(meta);
return f;
}


Expand Down

0 comments on commit 7514822

Please sign in to comment.