mirror of
https://gitlab.com/SamB440/rpgregions-2.git
synced 2025-12-27 18:59:10 +00:00
Add /rpgregions reset <player> [regionId], make materials in gui configurable
This commit is contained in:
@@ -108,6 +108,8 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu
|
||||
config.addDefault("settings.server.discoveries.discovered.sound.name", XSound.UI_TOAST_CHALLENGE_COMPLETE.parseSound().toString());
|
||||
config.addDefault("settings.server.discoveries.discovered.sound.pitch", 1);
|
||||
config.addDefault("settings.server.discoveries.discovered.title.animation_speed", 20);
|
||||
config.addDefault("settings.server.gui.forward", XMaterial.ARROW.parseMaterial());
|
||||
config.addDefault("settings.server.gui.back", XMaterial.ARROW.parseMaterial());
|
||||
config.options().copyDefaults(true);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
@@ -5,31 +5,21 @@ import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import com.github.stefvanschie.inventoryframework.Gui;
|
||||
import com.github.stefvanschie.inventoryframework.GuiItem;
|
||||
import com.github.stefvanschie.inventoryframework.pane.PaginatedPane;
|
||||
import com.github.stefvanschie.inventoryframework.pane.StaticPane;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.islandearth.rpgregions.RPGRegions;
|
||||
import net.islandearth.rpgregions.gui.DiscoveryGUI;
|
||||
import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion;
|
||||
import net.islandearth.rpgregions.rewards.ItemReward;
|
||||
import net.islandearth.rpgregions.translation.Translations;
|
||||
import net.islandearth.rpgregions.utils.ItemStackBuilder;
|
||||
import net.islandearth.rpgregions.utils.StringUtils;
|
||||
import net.islandearth.rpgregions.utils.XMaterial;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@CommandAlias("rpgregions")
|
||||
public class RPGRegionsCommand extends BaseCommand {
|
||||
@@ -123,103 +113,7 @@ public class RPGRegionsCommand extends BaseCommand {
|
||||
@Subcommand("list|discoveries")
|
||||
@CommandPermission("rpgregions.list")
|
||||
public void onList(Player player) {
|
||||
Gui gui = new Gui(plugin, 6, Translations.REGIONS.get(player));
|
||||
PaginatedPane pane = new PaginatedPane(0, 0, 9, 5);
|
||||
StaticPane back = new StaticPane(0, 5, 1, 1);
|
||||
StaticPane forward = new StaticPane(8, 5, 1, 1);
|
||||
|
||||
back.addItem(new GuiItem(new ItemStackBuilder(XMaterial.ARROW.parseMaterial())
|
||||
.withName(Translations.PREVIOUS_PAGE.get(player))
|
||||
.build(), event -> {
|
||||
event.setCancelled(true);
|
||||
if (pane.getPages() == 0) return;
|
||||
|
||||
pane.setPage(pane.getPage() - 1);
|
||||
|
||||
if (pane.getPage() == 0) {
|
||||
back.setVisible(false);
|
||||
}
|
||||
|
||||
forward.setVisible(true);
|
||||
gui.update();
|
||||
}), 0, 0);
|
||||
|
||||
back.setVisible(false);
|
||||
|
||||
forward.addItem(new GuiItem(new ItemStackBuilder(XMaterial.ARROW.parseMaterial())
|
||||
.withName(Translations.NEXT_PAGE.get(player))
|
||||
.build(), event -> {
|
||||
event.setCancelled(true);
|
||||
if (pane.getPages() == 0) return;
|
||||
|
||||
pane.setPage(pane.getPage() + 1);
|
||||
|
||||
if (pane.getPage() == pane.getPages() - 1) {
|
||||
forward.setVisible(false);
|
||||
}
|
||||
|
||||
back.setVisible(true);
|
||||
gui.update();
|
||||
}), 0, 0);
|
||||
|
||||
gui.addPane(back);
|
||||
gui.addPane(forward);
|
||||
|
||||
plugin.getManagers().getStorageManager().getAccount(player.getUniqueId()).thenAccept(account -> {
|
||||
List<GuiItem> items = new ArrayList<>();
|
||||
for (ConfiguredRegion configuredRegion : plugin.getManagers().getRegionsCache().getConfiguredRegions().values()) {
|
||||
boolean hasDiscovered = account.getDiscoveredRegions().containsKey(configuredRegion.getId());
|
||||
if (!hasDiscovered && !player.hasPermission("rpgregions.show")) continue;
|
||||
|
||||
ChatColor colour = hasDiscovered
|
||||
? ChatColor.GREEN
|
||||
: ChatColor.RED;
|
||||
String lore = account.getDiscoveredRegions().containsKey(configuredRegion.getId())
|
||||
? Translations.DISCOVERED_ON.get(player,
|
||||
account.getDiscoveredRegions().get(configuredRegion.getId()).getDate())
|
||||
: null;
|
||||
String lore2 = configuredRegion.isShowCoords()
|
||||
&& player.hasPermission("rpgregions.showloc")
|
||||
? ChatColor.GRAY + "" + configuredRegion.getX() + ", " + configuredRegion.getZ()
|
||||
: null;
|
||||
String hint = configuredRegion.isShowHint()
|
||||
&& player.hasPermission("rpgregions.showhint")
|
||||
&& !hasDiscovered
|
||||
? ChatColor.translateAlternateColorCodes('&', configuredRegion.getHint())
|
||||
: null;
|
||||
String teleport = configuredRegion.isTeleportable()
|
||||
&& player.hasPermission("rpgregions.teleport")
|
||||
&& hasDiscovered
|
||||
? Translations.TELEPORT.get(player)
|
||||
: null;
|
||||
items.add(new GuiItem(new ItemStackBuilder(configuredRegion.getIcon())
|
||||
.withName(colour + configuredRegion.getCustomName())
|
||||
.withLore(lore)
|
||||
.withLore(lore2)
|
||||
.withLore(hint)
|
||||
.withLore(" ")
|
||||
.withLore(teleport)
|
||||
.addFlags(ItemFlag.HIDE_ATTRIBUTES)
|
||||
.build(),
|
||||
event -> {
|
||||
if (configuredRegion.isTeleportable()
|
||||
&& player.hasPermission("rpgregions.teleport")
|
||||
&& hasDiscovered) {
|
||||
int x = configuredRegion.getX();
|
||||
int y = configuredRegion.getY();
|
||||
int z = configuredRegion.getZ();
|
||||
if (configuredRegion.getWorld() == null) Translations.CANNOT_TELEPORT.send(player);
|
||||
else PaperLib.teleportAsync(player, new Location(configuredRegion.getWorld(), x, y, z));
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
}
|
||||
|
||||
pane.populateWithGuiItems(items);
|
||||
gui.addPane(pane);
|
||||
gui.show(player);
|
||||
});
|
||||
new DiscoveryGUI(plugin, player).open();
|
||||
}
|
||||
|
||||
@Subcommand("additem")
|
||||
@@ -290,4 +184,40 @@ public class RPGRegionsCommand extends BaseCommand {
|
||||
long totalTime = endTime - startTime;
|
||||
sender.sendMessage(ChatColor.GREEN + "Done! (" + totalTime + "ms)");
|
||||
}
|
||||
|
||||
@Subcommand("reset")
|
||||
@CommandPermission("rpgregions.reset")
|
||||
public void onReset(CommandSender sender, String[] args) {
|
||||
switch (args.length) {
|
||||
case 1: {
|
||||
Player player = Bukkit.getPlayer(args[0]);
|
||||
if (player != null) {
|
||||
plugin.getManagers().getStorageManager().clearDiscoveries(player.getUniqueId());
|
||||
sender.sendMessage(ChatColor.GREEN + "Players discoveries has been cleared.");
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.RED + "That player cannot be found.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 2: {
|
||||
Player player = Bukkit.getPlayer(args[0]);
|
||||
if (player != null) {
|
||||
String regionId = args[1];
|
||||
if (plugin.getManagers().getRegionsCache().getConfiguredRegions().containsKey(regionId)) {
|
||||
plugin.getManagers().getStorageManager().clearDiscovery(player.getUniqueId(), regionId);
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.RED + "Region does not exist or is not configured.");
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.RED + "That player cannot be found.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
sender.sendMessage(ChatColor.RED + "Usage: /rpgregions reset <player> [region_id]");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
package net.islandearth.rpgregions.gui;
|
||||
|
||||
import com.github.stefvanschie.inventoryframework.Gui;
|
||||
import com.github.stefvanschie.inventoryframework.GuiItem;
|
||||
import com.github.stefvanschie.inventoryframework.pane.PaginatedPane;
|
||||
import com.github.stefvanschie.inventoryframework.pane.StaticPane;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.islandearth.rpgregions.RPGRegions;
|
||||
import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion;
|
||||
import net.islandearth.rpgregions.translation.Translations;
|
||||
import net.islandearth.rpgregions.utils.ItemStackBuilder;
|
||||
import net.islandearth.rpgregions.utils.XMaterial;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DiscoveryGUI extends RPGRegionsGUI {
|
||||
|
||||
private final Gui gui;
|
||||
|
||||
public DiscoveryGUI(RPGRegions plugin, Player player) {
|
||||
super(plugin, player);
|
||||
this.gui = new Gui(plugin, 6, Translations.REGIONS.get(player));
|
||||
PaginatedPane pane = new PaginatedPane(0, 0, 9, 5);
|
||||
StaticPane back = new StaticPane(0, 5, 1, 1);
|
||||
StaticPane forward = new StaticPane(8, 5, 1, 1);
|
||||
|
||||
back.addItem(new GuiItem(new ItemStackBuilder(XMaterial.valueOf(
|
||||
plugin.getConfig().getString("settings.server.gui.back")).parseMaterial())
|
||||
.withName(Translations.PREVIOUS_PAGE.get(player))
|
||||
.build(), event -> {
|
||||
event.setCancelled(true);
|
||||
if (pane.getPages() == 0) return;
|
||||
|
||||
pane.setPage(pane.getPage() - 1);
|
||||
|
||||
if (pane.getPage() == 0) {
|
||||
back.setVisible(false);
|
||||
}
|
||||
|
||||
forward.setVisible(true);
|
||||
gui.update();
|
||||
}), 0, 0);
|
||||
|
||||
back.setVisible(false);
|
||||
|
||||
forward.addItem(new GuiItem(new ItemStackBuilder(XMaterial.valueOf(
|
||||
plugin.getConfig().getString("settings.server.gui.back")).parseMaterial())
|
||||
.withName(Translations.NEXT_PAGE.get(player))
|
||||
.build(), event -> {
|
||||
event.setCancelled(true);
|
||||
if (pane.getPages() == 0) return;
|
||||
|
||||
pane.setPage(pane.getPage() + 1);
|
||||
|
||||
if (pane.getPage() == pane.getPages() - 1) {
|
||||
forward.setVisible(false);
|
||||
}
|
||||
|
||||
back.setVisible(true);
|
||||
gui.update();
|
||||
}), 0, 0);
|
||||
|
||||
gui.addPane(back);
|
||||
gui.addPane(forward);
|
||||
|
||||
plugin.getManagers().getStorageManager().getAccount(player.getUniqueId()).thenAccept(account -> {
|
||||
List<GuiItem> items = new ArrayList<>();
|
||||
for (ConfiguredRegion configuredRegion : plugin.getManagers().getRegionsCache().getConfiguredRegions().values()) {
|
||||
boolean hasDiscovered = account.getDiscoveredRegions().containsKey(configuredRegion.getId());
|
||||
if (!hasDiscovered && !player.hasPermission("rpgregions.show")) continue;
|
||||
|
||||
ChatColor colour = hasDiscovered
|
||||
? ChatColor.GREEN
|
||||
: ChatColor.RED;
|
||||
String lore = account.getDiscoveredRegions().containsKey(configuredRegion.getId())
|
||||
? Translations.DISCOVERED_ON.get(player,
|
||||
account.getDiscoveredRegions().get(configuredRegion.getId()).getDate())
|
||||
: null;
|
||||
String lore2 = configuredRegion.isShowCoords()
|
||||
&& player.hasPermission("rpgregions.showloc")
|
||||
? ChatColor.GRAY + "" + configuredRegion.getX() + ", " + configuredRegion.getZ()
|
||||
: null;
|
||||
String hint = configuredRegion.isShowHint()
|
||||
&& player.hasPermission("rpgregions.showhint")
|
||||
&& !hasDiscovered
|
||||
? ChatColor.translateAlternateColorCodes('&', configuredRegion.getHint())
|
||||
: null;
|
||||
String teleport = configuredRegion.isTeleportable()
|
||||
&& player.hasPermission("rpgregions.teleport")
|
||||
&& hasDiscovered
|
||||
? Translations.TELEPORT.get(player)
|
||||
: null;
|
||||
items.add(new GuiItem(new ItemStackBuilder(configuredRegion.getIcon())
|
||||
.withName(colour + configuredRegion.getCustomName())
|
||||
.withLore(lore)
|
||||
.withLore(lore2)
|
||||
.withLore(hint)
|
||||
.withLore(" ")
|
||||
.withLore(teleport)
|
||||
.addFlags(ItemFlag.HIDE_ATTRIBUTES)
|
||||
.build(),
|
||||
event -> {
|
||||
if (configuredRegion.isTeleportable()
|
||||
&& player.hasPermission("rpgregions.teleport")
|
||||
&& hasDiscovered) {
|
||||
int x = configuredRegion.getX();
|
||||
int y = configuredRegion.getY();
|
||||
int z = configuredRegion.getZ();
|
||||
if (configuredRegion.getWorld() == null) Translations.CANNOT_TELEPORT.send(player);
|
||||
else PaperLib.teleportAsync(player, new Location(configuredRegion.getWorld(), x, y, z));
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
pane.populateWithGuiItems(items);
|
||||
gui.addPane(pane);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() {
|
||||
gui.show(getPlayer());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.islandearth.rpgregions.gui;
|
||||
|
||||
import net.islandearth.rpgregions.RPGRegions;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class RPGRegionsGUI {
|
||||
|
||||
private final RPGRegions plugin;
|
||||
private final Player player;
|
||||
|
||||
public RPGRegionsGUI(RPGRegions plugin, Player player) {
|
||||
this.plugin = plugin;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public abstract void open();
|
||||
}
|
||||
@@ -22,7 +22,20 @@ public interface StorageManager {
|
||||
* @return map of cached accounts
|
||||
*/
|
||||
ConcurrentMap<UUID, RPGRegionsAccount> getCachedAccounts();
|
||||
|
||||
|
||||
/**
|
||||
* Removes all discoveries matching a player.
|
||||
* @param uuid player uuid
|
||||
*/
|
||||
void clearDiscoveries(UUID uuid);
|
||||
|
||||
/**
|
||||
* Removes a specific discovery matching a player.
|
||||
* @param uuid player uuid
|
||||
* @param regionId region id
|
||||
*/
|
||||
void clearDiscovery(UUID uuid, String regionId);
|
||||
|
||||
/**
|
||||
* Removes an account from the storage cache and saves its data.
|
||||
* @param uuid player's UUID
|
||||
|
||||
@@ -14,7 +14,7 @@ public class RPGRegionsAccount {
|
||||
this.uuid = uuid;
|
||||
this.discoveredRegions = discoveredRegions;
|
||||
}
|
||||
|
||||
|
||||
public Map<String, Discovery> getDiscoveredRegions() {
|
||||
return discoveredRegions;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ public class SqlStorage implements StorageManager {
|
||||
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS rpgregions_discoveries (uuid varchar(32) NOT NULL, region varchar(32) NOT NULL, time varchar(64) NOT NULL, PRIMARY KEY(uuid, region))";
|
||||
private static final String SELECT_REGION = "SELECT * FROM rpgregions_discoveries WHERE uuid = ?";
|
||||
private static final String INSERT_DISCOVERY = "INSERT INTO rpgregions_discoveries (uuid, region, time) VALUES (?, ?, ?)";
|
||||
|
||||
private static final String DELETE_DISCOVERIES = "DELETE * FROM rpgregions_discoveries WHERE uuid = ?";
|
||||
private static final String DELETE_DISCOVERY = "DELETE * FROM rpgregions_discoveries WHERE uuid = ? AND region = ?";
|
||||
|
||||
private ConcurrentMap<UUID, RPGRegionsAccount> cachedAccounts = new ConcurrentHashMap<>();
|
||||
|
||||
private final RPGRegions plugin;
|
||||
@@ -71,7 +73,25 @@ public class SqlStorage implements StorageManager {
|
||||
public ConcurrentMap<UUID, RPGRegionsAccount> getCachedAccounts() {
|
||||
return cachedAccounts;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void clearDiscoveries(UUID uuid) {
|
||||
getAccount(uuid).thenAccept(account -> {
|
||||
account.getDiscoveredRegions().clear();
|
||||
});
|
||||
|
||||
DB.executeUpdateAsync(DELETE_DISCOVERIES, getDatabaseUuid(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearDiscovery(UUID uuid, String regionId) {
|
||||
getAccount(uuid).thenAccept(account -> {
|
||||
account.getDiscoveredRegions().remove(regionId);
|
||||
});
|
||||
|
||||
DB.executeUpdateAsync(DELETE_DISCOVERY, getDatabaseUuid(uuid), regionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCachedAccount(UUID uuid) {
|
||||
RPGRegionsAccount account = cachedAccounts.get(uuid);
|
||||
|
||||
@@ -61,7 +61,55 @@ public class YamlStorage implements StorageManager {
|
||||
public ConcurrentMap<UUID, RPGRegionsAccount> getCachedAccounts() {
|
||||
return cachedAccounts;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void clearDiscoveries(UUID uuid) {
|
||||
getAccount(uuid).thenAccept(account -> {
|
||||
account.getDiscoveredRegions().clear();
|
||||
});
|
||||
|
||||
File file = new File(plugin.getDataFolder() + "/accounts/" + uuid.toString() + ".yml");
|
||||
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
config.set("Discoveries", null);
|
||||
try {
|
||||
config.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearDiscovery(UUID uuid, String regionId) {
|
||||
getAccount(uuid).thenAccept(account -> {
|
||||
account.getDiscoveredRegions().remove(regionId);
|
||||
});
|
||||
|
||||
File file = new File(plugin.getDataFolder() + "/accounts/" + uuid.toString() + ".yml");
|
||||
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
Map<String, Discovery> regions = new HashMap<>();
|
||||
for (String results : config.getStringList("Discoveries")) {
|
||||
String[] data = results.split(";");
|
||||
String time = data[0];
|
||||
String region = data[1];
|
||||
regions.put(region, new WorldDiscovery(time, region));
|
||||
}
|
||||
|
||||
regions.remove(regionId);
|
||||
|
||||
List<String> newData = config.getStringList("Discoveries");
|
||||
newData.clear();
|
||||
for (Discovery region : regions.values()) {
|
||||
newData.add(region.getDate() + ";" + region.getRegion());
|
||||
}
|
||||
|
||||
config.set("Discoveries", newData);
|
||||
try {
|
||||
config.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCachedAccount(UUID uuid) {
|
||||
RPGRegionsAccount account = cachedAccounts.get(uuid);
|
||||
|
||||
Reference in New Issue
Block a user