Added favorites menu

Dieser Commit ist enthalten in:
Silent 2020-11-07 00:45:44 +01:00
Ursprung ddf0e10c19
Commit abc90629ea
10 geänderte Dateien mit 133 neuen und 26 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@
<groupId>tsp.headdb</groupId> <groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId> <artifactId>HeadDB</artifactId>
<version>1.1</version> <version>1.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HeadDB</name> <name>HeadDB</name>

Datei anzeigen

@ -15,6 +15,7 @@ public class HeadDB extends JavaPlugin {
private static HeadDB instance; private static HeadDB instance;
private static Config config; private static Config config;
private static Config playerdata;
@Override @Override
public void onEnable() { public void onEnable() {
@ -22,6 +23,8 @@ public class HeadDB extends JavaPlugin {
Log.info("Loading HeadDB - " + getDescription().getVersion()); Log.info("Loading HeadDB - " + getDescription().getVersion());
saveDefaultConfig(); saveDefaultConfig();
config = new Config("plugins/HeadDB/config.yml"); config = new Config("plugins/HeadDB/config.yml");
playerdata = new Config("plugins/HeadDB/playerdata.yml");
playerdata.create();
Log.debug("Starting metrics..."); Log.debug("Starting metrics...");
new Metrics(this, Utils.METRICS_ID); new Metrics(this, Utils.METRICS_ID);
@ -43,6 +46,10 @@ public class HeadDB extends JavaPlugin {
return config; return config;
} }
public static Config getPlayerdata() {
return playerdata;
}
public static HeadDB getInstance() { public static HeadDB getInstance() {
return instance; return instance;
} }

Datei anzeigen

@ -11,7 +11,7 @@ import tsp.headdb.util.Log;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Collections; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
public class Head { public class Head {
@ -29,7 +29,6 @@ public class Head {
Validate.notNull(category, "category must not be null!"); Validate.notNull(category, "category must not be null!");
ItemStack item = new ItemStack(Material.PLAYER_HEAD); ItemStack item = new ItemStack(Material.PLAYER_HEAD);
if (item != null) {
SkullMeta meta = (SkullMeta) item.getItemMeta(); SkullMeta meta = (SkullMeta) item.getItemMeta();
meta.setDisplayName(Utils.colorize(category.getColor() + name)); meta.setDisplayName(Utils.colorize(category.getColor() + name));
// set skull owner // set skull owner
@ -44,9 +43,11 @@ public class Head {
Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:"); Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:");
e1.printStackTrace(); e1.printStackTrace();
} }
meta.setLore(Collections.singletonList(Utils.colorize("&cID: " + id))); meta.setLore(Arrays.asList(
Utils.colorize("&cID: " + id),
Utils.colorize("&8Right-Click to add/remove from favorites.")
));
item.setItemMeta(meta); item.setItemMeta(meta);
}
return item; return item;
} }

Datei anzeigen

@ -1,10 +1,12 @@
package tsp.headdb.api; package tsp.headdb.api;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import tsp.headdb.HeadDB;
import tsp.headdb.database.Category; import tsp.headdb.database.Category;
import tsp.headdb.database.HeadDatabase; import tsp.headdb.database.HeadDatabase;
import tsp.headdb.inventory.InventoryUtils; import tsp.headdb.inventory.InventoryUtils;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -50,6 +52,38 @@ public class HeadAPI {
return HeadDatabase.getHeads(); return HeadDatabase.getHeads();
} }
public static void addFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites");
if (!favs.contains(id)) {
favs.add(id);
}
HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs);
HeadDB.getPlayerdata().save();
}
public static void removeFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites");
for (int i = 0; i < favs.size(); i++) {
if (favs.get(i) == id) {
favs.remove(i);
break;
}
}
HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs);
HeadDB.getPlayerdata().save();
}
public static List<Head> getFavoriteHeads(UUID uuid) {
List<Head> heads = new ArrayList<>();
List<Integer> ids = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites");
for (int id : ids) {
Head head = getHeadByID(id);
heads.add(head);
}
return heads;
}
public static void updateDatabase() { public static void updateDatabase() {
HeadDatabase.update(); HeadDatabase.update();
} }

Datei anzeigen

@ -1,6 +1,7 @@
package tsp.headdb.inventory; package tsp.headdb.inventory;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -17,6 +18,32 @@ import java.util.List;
public class InventoryUtils { public class InventoryUtils {
public static void openFavoritesMenu(Player player) {
PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &eFavorites: " + player.getName()));
List<Head> heads = HeadAPI.getFavoriteHeads(player.getUniqueId());
for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) {
ItemStack item = head.getItemStack();
item.setAmount(64);
player.getInventory().addItem(item);
return;
}
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(head.getItemStack());
}
if (e.getClick() == ClickType.RIGHT) {
HeadAPI.removeFavoriteHead(player.getUniqueId(), head.getId());
openFavoritesMenu(player);
Utils.sendMessage(player, "Removed &e" + head.getName() + " &8from favorites.");
}
}));
}
pane.open(player);
}
public static void openSearchDatabase(Player player, String search) { public static void openSearchDatabase(Player player, String search) {
PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &eSearch: " + search)); PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &eSearch: " + search));
@ -29,7 +56,13 @@ public class InventoryUtils {
player.getInventory().addItem(item); player.getInventory().addItem(item);
return; return;
} }
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(head.getItemStack()); player.getInventory().addItem(head.getItemStack());
}
if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
Utils.sendMessage(player, "Added &e" + head.getName() + " &8to favorites.");
}
})); }));
} }
@ -48,7 +81,13 @@ public class InventoryUtils {
player.getInventory().addItem(item); player.getInventory().addItem(item);
return; return;
} }
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(head.getItemStack()); player.getInventory().addItem(head.getItemStack());
}
if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
Utils.sendMessage(player, "Added &e" + head.getName() + " &8to favorites.");
}
})); }));
} }
@ -56,7 +95,7 @@ public class InventoryUtils {
} }
public static void openDatabase(Player player) { public static void openDatabase(Player player) {
Inventory inventory = Bukkit.createInventory(null, 54, Utils.colorize("&c&lHeadDB")); Inventory inventory = Bukkit.createInventory(null, 54, Utils.colorize("&c&lHeadDB &8(" + HeadAPI.getHeads().size() + ")"));
fillBorder(inventory, XMaterial.BLACK_STAINED_GLASS_PANE.parseItem()); fillBorder(inventory, XMaterial.BLACK_STAINED_GLASS_PANE.parseItem());
for (Category category : Category.getCategories()) { for (Category category : Category.getCategories()) {
@ -70,6 +109,12 @@ public class InventoryUtils {
inventory.addItem(item); inventory.addItem(item);
} }
ItemStack fav = XMaterial.BOOK.parseItem();
ItemMeta meta = fav.getItemMeta();
meta.setDisplayName(Utils.colorize("&eFavorites"));
fav.setItemMeta(meta);
inventory.setItem(37, fav);
player.openInventory(inventory); player.openInventory(inventory);
} }

Datei anzeigen

@ -24,6 +24,7 @@ public class MenuListener implements Listener {
if (!(e.getWhoClicked() instanceof Player)) { if (!(e.getWhoClicked() instanceof Player)) {
return; return;
} }
Player player = (Player) e.getWhoClicked();
if (e.getView().getTitle().startsWith(Utils.colorize("&c&lHeadDB"))) { if (e.getView().getTitle().startsWith(Utils.colorize("&c&lHeadDB"))) {
e.setCancelled(true); e.setCancelled(true);
Inventory inventory = e.getClickedInventory(); Inventory inventory = e.getClickedInventory();
@ -34,11 +35,19 @@ public class MenuListener implements Listener {
if (item != null && item.getType() != XMaterial.AIR.parseMaterial()) { if (item != null && item.getType() != XMaterial.AIR.parseMaterial()) {
String name = ChatColor.stripColor(item.getItemMeta().getDisplayName().toLowerCase()); String name = ChatColor.stripColor(item.getItemMeta().getDisplayName().toLowerCase());
if (name.equalsIgnoreCase("favorites")) {
if (!player.hasPermission("headdb.favorites")) {
Utils.sendMessage(player, "&cYou do not have permission for favorites!");
player.closeInventory();
return;
}
InventoryUtils.openFavoritesMenu(player);
return;
}
Category category = Category.getByName(name); Category category = Category.getByName(name);
if (category != null) { if (category != null) {
InventoryUtils.openCategoryDatabase((Player) e.getWhoClicked(), category); InventoryUtils.openCategoryDatabase(player, category);
} }
} }
} }

Datei anzeigen

@ -48,6 +48,10 @@ public class Config {
return config.getInt(path); return config.getInt(path);
} }
public List<Integer> getIntList(String path) {
return config.getIntegerList(path);
}
public long getLong(String path) { public long getLong(String path) {
return config.getLong(path); return config.getLong(path);
} }

Datei anzeigen

@ -2,6 +2,7 @@ package tsp.headdb.util;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import tsp.headdb.HeadDB;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
@ -34,6 +35,9 @@ public class Log {
} }
public static void log(LogLevel level, String message) { public static void log(LogLevel level, String message) {
if (level == LogLevel.DEBUG && !HeadDB.getCfg().getBoolean("debug")) {
return;
}
Bukkit.getConsoleSender().sendMessage(Utils.colorize("&7[&9&l" + name + "&7] " + level.getColor() + "[" + level.name() + "]: " + message)); Bukkit.getConsoleSender().sendMessage(Utils.colorize("&7[&9&l" + name + "&7] " + level.getColor() + "[" + level.name() + "]: " + message));
} }

Datei anzeigen

@ -2,4 +2,4 @@
refresh: 3600 refresh: 3600
# Debug Mode # Debug Mode
debug: true debug: false

Datei anzeigen

@ -2,7 +2,7 @@ name: HeadDB
description: Head Database description: Head Database
main: tsp.headdb.HeadDB main: tsp.headdb.HeadDB
version: 1.1 version: 1.2
api-version: 1.16 api-version: 1.16
author: Silent author: Silent
@ -19,9 +19,12 @@ permissions:
headdb.open: true headdb.open: true
headdb.search: true headdb.search: true
headdb.give: true headdb.give: true
headdb.favorites: true
headdb.open: headdb.open:
default: op default: op
headdb.search: headdb.search:
default: op default: op
headdb.give: headdb.give:
default: op default: op
headdb.favorites:
default: op