Start implementing PlayerSettings.java and PlayerSettingsGui.java
This commit is contained in:
parent
bd2f71d8c5
commit
0019f27636
|
|
@ -70,12 +70,14 @@ public class MessageConfig extends AbstractConfiguration {
|
||||||
public static String GUI_LIST_SHOPS_TITLE = "<green>Players shops by <playername> <page>/<pages>";
|
public static String GUI_LIST_SHOPS_TITLE = "<green>Players shops by <playername> <page>/<pages>";
|
||||||
public static String GUI_LIST_TRANSACTIONS_TITLE = "<green>Shop Transactions <page>/<pages>";
|
public static String GUI_LIST_TRANSACTIONS_TITLE = "<green>Shop Transactions <page>/<pages>";
|
||||||
public static String GUI_MANAGE_TITLE = "<green>Shop Manager";
|
public static String GUI_MANAGE_TITLE = "<green>Shop Manager";
|
||||||
|
public static String GUI_PLAYER_SETTINGS_TITLE = "<green>Shop Settings Manager";
|
||||||
private static void guiTitles() {
|
private static void guiTitles() {
|
||||||
GUI_HOME_TITLE = config.getString("gui.home-title", GUI_HOME_TITLE);
|
GUI_HOME_TITLE = config.getString("gui.home-title", GUI_HOME_TITLE);
|
||||||
GUI_LIST_PLAYERS_TITLE = config.getString("gui.list-players-title", GUI_LIST_PLAYERS_TITLE);
|
GUI_LIST_PLAYERS_TITLE = config.getString("gui.list-players-title", GUI_LIST_PLAYERS_TITLE);
|
||||||
GUI_LIST_SHOPS_TITLE = config.getString("gui.list-shops-title", GUI_LIST_SHOPS_TITLE);
|
GUI_LIST_SHOPS_TITLE = config.getString("gui.list-shops-title", GUI_LIST_SHOPS_TITLE);
|
||||||
GUI_LIST_TRANSACTIONS_TITLE = config.getString("gui.list-transactions-title", GUI_LIST_TRANSACTIONS_TITLE);
|
GUI_LIST_TRANSACTIONS_TITLE = config.getString("gui.list-transactions-title", GUI_LIST_TRANSACTIONS_TITLE);
|
||||||
GUI_MANAGE_TITLE = config.getString("gui.manage-title", GUI_MANAGE_TITLE);
|
GUI_MANAGE_TITLE = config.getString("gui.manage-title", GUI_MANAGE_TITLE);
|
||||||
|
GUI_PLAYER_SETTINGS_TITLE = config.getString("gui.player-settings-title", GUI_PLAYER_SETTINGS_TITLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,10 @@ public enum GuiIcon {
|
||||||
LIST_SHOP,
|
LIST_SHOP,
|
||||||
LIST_PLAYER,
|
LIST_PLAYER,
|
||||||
LIST_PLAYER_ADMIN,
|
LIST_PLAYER_ADMIN,
|
||||||
EMPTY_SHOP;
|
EMPTY_SHOP,
|
||||||
|
|
||||||
|
SETTING_ON,
|
||||||
|
SETTING_OFF;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final int slot;
|
private final int slot;
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,10 @@ public class HomeGui extends AbstractGui {
|
||||||
ListPlayersGui listPlayersGui = new ListPlayersGui(uuid);
|
ListPlayersGui listPlayersGui = new ListPlayersGui(uuid);
|
||||||
listPlayersGui.setLastGui(this);
|
listPlayersGui.setLastGui(this);
|
||||||
listPlayersGui.open();
|
listPlayersGui.open();
|
||||||
} else if (slot == GuiIcon.HOME_SETTINGS.getSlot() && GuiIcon.HOME_SETTINGS.getItemStack().equals(item)) {
|
}/* else if (slot == GuiIcon.HOME_SETTINGS.getSlot() && GuiIcon.HOME_SETTINGS.getItemStack().equals(item)) {
|
||||||
|
PlayerSettingsGui playerSettingsGui = new PlayerSettingsGui(uuid);
|
||||||
}
|
playerSettingsGui.setLastGui(this);
|
||||||
|
playerSettingsGui.open();
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.alttd.playershops.gui;
|
||||||
|
|
||||||
|
import com.alttd.playershops.PlayerShops;
|
||||||
|
import com.alttd.playershops.config.MessageConfig;
|
||||||
|
import com.alttd.playershops.shop.PlayerSettings;
|
||||||
|
import com.alttd.playershops.utils.ShopUtil;
|
||||||
|
import com.alttd.playershops.utils.Util;
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.TextReplacementConfig;
|
||||||
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||||
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.inventory.ItemFlag;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class PlayerSettingsGui extends AbstractGui {
|
||||||
|
|
||||||
|
public PlayerSettingsGui(UUID uuid) {
|
||||||
|
super(uuid);
|
||||||
|
this.inventory = Bukkit.createInventory(this, INV_SIZE, Util.parseMiniMessage(MessageConfig.GUI_PLAYER_SETTINGS_TITLE));
|
||||||
|
initInvContents();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void initInvContents() {
|
||||||
|
super.initInvContents();
|
||||||
|
int startIndex = pageIndex * 45;
|
||||||
|
ItemStack item;
|
||||||
|
|
||||||
|
for(Object2BooleanMap.Entry<PlayerSettings.Option> entry : PlayerShops.getInstance().getShopHandler().getPlayerSettings(uuid).optionsMap.object2BooleanEntrySet()) {
|
||||||
|
item = entry.getBooleanValue() ? GuiIcon.SETTING_ON.getItemStack() : GuiIcon.SETTING_OFF.getItemStack();
|
||||||
|
ItemMeta itemMeta = item.getItemMeta();
|
||||||
|
if (itemMeta.hasDisplayName()) {
|
||||||
|
itemMeta.displayName(
|
||||||
|
itemMeta.displayName().replaceText(
|
||||||
|
TextReplacementConfig.builder()
|
||||||
|
.match("<setting>")
|
||||||
|
.replacement(entry.getKey().toString().toLowerCase().replace("_", " ")).build()));
|
||||||
|
}
|
||||||
|
item.setItemMeta(itemMeta);
|
||||||
|
|
||||||
|
if (!addItem(item)) {
|
||||||
|
addNextPageItem();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pageIndex > 0) {
|
||||||
|
addPrevPageItem();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(int slot, ItemStack item) {
|
||||||
|
super.onClick(slot, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package com.alttd.playershops.handler;
|
||||||
|
|
||||||
import com.alttd.playershops.PlayerShops;
|
import com.alttd.playershops.PlayerShops;
|
||||||
import com.alttd.playershops.config.Config;
|
import com.alttd.playershops.config.Config;
|
||||||
|
import com.alttd.playershops.shop.PlayerSettings;
|
||||||
import com.alttd.playershops.shop.PlayerShop;
|
import com.alttd.playershops.shop.PlayerShop;
|
||||||
import com.alttd.playershops.storage.database.DatabaseHelper;
|
import com.alttd.playershops.storage.database.DatabaseHelper;
|
||||||
import com.alttd.playershops.utils.Logger;
|
import com.alttd.playershops.utils.Logger;
|
||||||
|
|
@ -9,8 +10,6 @@ import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.Tag;
|
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.block.DoubleChest;
|
import org.bukkit.block.DoubleChest;
|
||||||
|
|
@ -18,7 +17,6 @@ import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.InventoryHolder;
|
import org.bukkit.inventory.InventoryHolder;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
|
@ -31,12 +29,15 @@ public class ShopHandler {
|
||||||
private final Map<Location, PlayerShop> shopLocation;
|
private final Map<Location, PlayerShop> shopLocation;
|
||||||
private final Map<Location, PlayerShop> shopSignLocation;
|
private final Map<Location, PlayerShop> shopSignLocation;
|
||||||
|
|
||||||
|
private HashMap<UUID, PlayerSettings> playerSettings;
|
||||||
|
|
||||||
public ShopHandler(PlayerShops instance) {
|
public ShopHandler(PlayerShops instance) {
|
||||||
plugin = instance;
|
plugin = instance;
|
||||||
shopLocation = new ConcurrentHashMap<>();
|
shopLocation = new ConcurrentHashMap<>();
|
||||||
shopSignLocation = new ConcurrentHashMap<>();
|
shopSignLocation = new ConcurrentHashMap<>();
|
||||||
shopBuildLimits = new Object2IntOpenHashMap<>();
|
shopBuildLimits = new Object2IntOpenHashMap<>();
|
||||||
shopBuildLimits.defaultReturnValue(Config.shopLimit);
|
shopBuildLimits.defaultReturnValue(Config.shopLimit);
|
||||||
|
playerSettings = new HashMap<>();
|
||||||
|
|
||||||
loadShops();
|
loadShops();
|
||||||
}
|
}
|
||||||
|
|
@ -169,4 +170,9 @@ public class ShopHandler {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PlayerSettings getPlayerSettings(UUID uuid) { // TODO a manager for this, extracted and edited from unished PlayerSettingsPlugin
|
||||||
|
return playerSettings.computeIfAbsent(uuid, PlayerSettings::loadFromFile);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
103
src/main/java/com/alttd/playershops/shop/PlayerSettings.java
Normal file
103
src/main/java/com/alttd/playershops/shop/PlayerSettings.java
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
package com.alttd.playershops.shop;
|
||||||
|
|
||||||
|
import com.alttd.playershops.config.Config;
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class PlayerSettings {
|
||||||
|
public enum Option {
|
||||||
|
SALE_OWNER_NOTIFICATIONS, SALE_USER_NOTIFICATIONS,
|
||||||
|
STOCK_NOTIFICATIONS, DISCORD_STOCK_NOTIFICATIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
private final UUID uuid;
|
||||||
|
public final Object2BooleanMap<Option> optionsMap;
|
||||||
|
|
||||||
|
public PlayerSettings(UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
|
||||||
|
this.optionsMap = new Object2BooleanOpenHashMap<>();
|
||||||
|
optionsMap.defaultReturnValue(false);
|
||||||
|
this.optionsMap.put(Option.SALE_OWNER_NOTIFICATIONS, true);
|
||||||
|
this.optionsMap.put(Option.SALE_USER_NOTIFICATIONS, true);
|
||||||
|
this.optionsMap.put(Option.STOCK_NOTIFICATIONS, true);
|
||||||
|
this.optionsMap.put(Option.DISCORD_STOCK_NOTIFICATIONS, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PlayerSettings(UUID uuid, Object2BooleanMap<Option> optionsMap) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
this.optionsMap = optionsMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOption(Option option, boolean set) {
|
||||||
|
optionsMap.put(option, set);
|
||||||
|
saveToFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getOption(Option option) {
|
||||||
|
if (optionsMap.containsKey(option))
|
||||||
|
return optionsMap.get(option);
|
||||||
|
optionsMap.put(option, true);
|
||||||
|
saveToFile();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveToFile() {
|
||||||
|
try {
|
||||||
|
File settingsDirectory = new File(Config.configPath, "PlayerSettings");
|
||||||
|
if (!settingsDirectory.exists() && !settingsDirectory.mkdir()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File playerSettingsFile = new File(settingsDirectory, this.uuid.toString() + ".yml");
|
||||||
|
if (!playerSettingsFile.exists()) {
|
||||||
|
//noinspection ResultOfMethodCallIgnored
|
||||||
|
playerSettingsFile.createNewFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
YamlConfiguration config = YamlConfiguration.loadConfiguration(playerSettingsFile);
|
||||||
|
|
||||||
|
config.set("player.UUID", this.uuid.toString());
|
||||||
|
for (Map.Entry<Option, Boolean> entry : optionsMap.entrySet()) {
|
||||||
|
config.set("player." + entry.getKey().toString(), entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
config.save(playerSettingsFile);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PlayerSettings loadFromFile(UUID uuid) {
|
||||||
|
File settingsDirectory = new File(Config.configPath, "PlayerSettings");
|
||||||
|
if (!settingsDirectory.exists() && !settingsDirectory.mkdir()) {
|
||||||
|
return null; // could not create directory
|
||||||
|
}
|
||||||
|
|
||||||
|
File playerSettingsFile = new File(settingsDirectory, uuid + ".yml");
|
||||||
|
|
||||||
|
if (playerSettingsFile.exists()) {
|
||||||
|
|
||||||
|
YamlConfiguration config = YamlConfiguration.loadConfiguration(playerSettingsFile);
|
||||||
|
|
||||||
|
//noinspection ConstantConditions
|
||||||
|
Object2BooleanMap<Option> optionsMap = new Object2BooleanOpenHashMap<>();
|
||||||
|
|
||||||
|
for (Option option : Option.values()) {
|
||||||
|
boolean value = config.getBoolean("player." + option.toString());
|
||||||
|
optionsMap.put(option, value);
|
||||||
|
//System.out.println(""+value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PlayerSettings(uuid, optionsMap);
|
||||||
|
}
|
||||||
|
return new PlayerSettings(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user