Toggle group permission, start edit language and configuration

This commit is contained in:
Ethanell 2020-04-08 10:58:58 +02:00
parent 18919c4a20
commit 2a5d79db93
6 changed files with 109 additions and 41 deletions

View file

@ -1,13 +1,18 @@
package fr.flifloo.StaffToggle.Commands;
import com.mysql.fabric.xmlrpc.base.Array;
import fr.flifloo.StaffToggle.Configurations.PlayerConfig;
import org.bukkit.ChatColor;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.group.GroupManager;
import net.luckperms.api.model.user.User;
import net.luckperms.api.model.user.UserManager;
import net.luckperms.api.node.NodeType;
import net.luckperms.api.node.types.InheritanceNode;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import fr.flifloo.StaffToggle.Main;
@ -23,35 +28,64 @@ public class Staff implements CommandExecutor {
public Staff(Main plugin) {
this.plugin = plugin;
language = plugin.language.config;
staffConf = plugin.staffConf.config;
language = plugin.language.getConfig();
staffConf = plugin.staffConf.getConfig();
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Command only for players !");
sender.sendMessage(language.getString("notPlayer"));
return false;
}
Player player = (Player) sender;
String state = "players." + player.getUniqueId() + ".state";
GroupManager groupManager = plugin.luckPerms.getGroupManager();
String[] groups = groupManager.getLoadedGroups().stream()
.map(Group::getName)
.filter(t->player.hasPermission("staff.group."+t))
.toArray(String[]::new);
if (groups.length == 0) {
player.sendMessage(language.getString("premissionDenide"));
return false;
} else if (groups.length > 1)
Arrays.sort(groups, Comparator.comparingInt((String s) -> groupManager.getGroup(s).getWeight().isEmpty() ? 0 : groupManager.getGroup(s).getWeight().getAsInt()).reversed());
if (!staffConf.isSet(state)) {
String userGroup = groups[0];
UserManager userManager = plugin.luckPerms.getUserManager();
User user = userManager.getUser(player.getUniqueId());
if (user == null)
player.sendMessage(language.getString("userNull"));
String state = "players." + player.getUniqueId() + PlayerConfig.STATE.getValue();
if (!staffConf.isSet(state))
staffConf.set(state, false);
}
if (!staffConf.getBoolean(state)){
staffConf.set(state, true);
save(player, "normal");
restore(player, "staff");
player.sendMessage("Staff mod enabled !");
if (user != null) {
Group group = groupManager.getGroup(userGroup);
if (group != null)
user.data().add(InheritanceNode.builder(group).build());
else
player.sendMessage(language.getString("unknonGroup"));
}
player.sendMessage(language.getString("enable"));
} else {
staffConf.set(state, false);
save(player, "staff");
restore(player, "normal");
player.sendMessage("Staff mod disabled !");
if (user != null)
user.getNodes().stream()
.filter(NodeType.INHERITANCE::matches)
.map(NodeType.INHERITANCE::cast)
.filter(t->t.getGroupName().equals(userGroup))
.forEach(t->user.data().remove(t));
player.sendMessage(language.getString("disable"));
}
if (user != null)
userManager.saveUser(user);
return true;
}
@ -62,26 +96,45 @@ public class Staff implements CommandExecutor {
staffConf.set(playerConf + PlayerConfig.ARMOR.getValue(), playerInventory.getArmorContents());
staffConf.set(playerConf + PlayerConfig.LEVEL.getValue(), player.getLevel());
staffConf.set(playerConf + PlayerConfig.EXP.getValue(), player.getExp());
staffConf.set(playerConf + PlayerConfig.GAMEMODE.getValue(), player.getGameMode());
staffConf.set(playerConf + PlayerConfig.GAMEMODE.getValue(), player.getGameMode().getValue());
}
private void restore(Player player, String mode) {
String playerConf = "players." + player.getUniqueId() + "." + mode;
ItemStack[] inventory = (ItemStack[]) staffConf.get(playerConf + PlayerConfig.INVENTORY.getValue());
ItemStack[] armor = (ItemStack[]) staffConf.get(playerConf + PlayerConfig.ARMOR.getValue());
ItemStack[] inventory = getItemStackConfig(playerConf + PlayerConfig.INVENTORY.getValue());
ItemStack[] armor = getItemStackConfig(playerConf + PlayerConfig.ARMOR.getValue());
int level = staffConf.getInt(playerConf + PlayerConfig.LEVEL.getValue());
Object exp = staffConf.get(playerConf + PlayerConfig.EXP.getValue());
GameMode gameMode = (GameMode) staffConf.get(playerConf + PlayerConfig.GAMEMODE.getValue());
GameMode gameMode = GameMode.getByValue(staffConf.getInt(playerConf + PlayerConfig.GAMEMODE.getValue()));
PlayerInventory playerInventory = player.getInventory();
if (inventory != null)
playerInventory.setContents(inventory);
if (armor != null)
playerInventory.setArmorContents(armor);
playerInventory.setContents(inventory);
playerInventory.setArmorContents(armor);
if (exp != null) {
if (exp.getClass() == Double.class)
player.setExp(((Double) exp).floatValue());
else
player.setExp((float) exp);
}
player.setLevel(level);
if (gameMode == null)
gameMode = GameMode.SURVIVAL;
if (exp != null)
player.setExp((float) exp);
player.setLevel(level);
player.setGameMode(gameMode);
}
private ItemStack[] getItemStackConfig(String config) {
Object itemStackRaw = staffConf.get(config);
ArrayList<ItemStack> itemStackArray;
ItemStack[] itemStacks = null;
if (itemStackRaw != null) {
if (itemStackRaw.getClass() == ArrayList.class) {
itemStackArray = (ArrayList<ItemStack>) itemStackRaw;
itemStacks = new ItemStack[itemStackArray.size()];
for (int i = 0; i<itemStackArray.size(); i++)
itemStacks[i] = itemStackArray.get(i);
} else {
itemStacks = (ItemStack[]) staffConf.get(config);
}
}
return itemStacks;
}
}

View file

@ -12,10 +12,10 @@ import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class Configuration {
public String fileName;
public FileConfiguration config;
public File configFile;
InputStream resourceFile;
private String fileName;
private FileConfiguration config;
private File configFile;
private InputStream resourceFile;
private Main plugin;
public Configuration (Main main, String name, String resourcePath) {
@ -49,8 +49,15 @@ public class Configuration {
}
}
public void set(String path, Object value) {
this.config.set(path, value);
this.save();
public File getConfigFile() {
return configFile;
}
public FileConfiguration getConfig() {
return config;
}
public InputStream getResourceFile() {
return resourceFile;
}
}

View file

@ -13,10 +13,10 @@ public class Language extends Configuration{
public Language (Main main) {
super(main, "language", "languages/" + main.getConfig().getString("language") + ".yml");
if (!main.getConfig().getString("language").equalsIgnoreCase(config.getString("language"))) {
if (!main.getConfig().getString("language").equalsIgnoreCase(getConfig().getString("language"))) {
try {
Files.copy(this.resourceFile, this.configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
config.load(configFile);
Files.copy(getResourceFile(), getConfigFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
getConfig().load(getConfigFile());
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}

View file

@ -1,6 +1,7 @@
package fr.flifloo.StaffToggle.Configurations;
public enum PlayerConfig {
STATE(".state"),
INVENTORY(".inv"),
ARMOR(".armor"),
LEVEL(".level"),

View file

@ -2,17 +2,25 @@ package fr.flifloo.StaffToggle;
import fr.flifloo.StaffToggle.Commands.Staff;
import fr.flifloo.StaffToggle.Configurations.*;
import net.luckperms.api.LuckPerms;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public Language language;
public StaffConf staffConf;
public LuckPerms luckPerms;
@Override
public void onEnable() {
saveDefaultConfig();
language = new Language(this);
staffConf = new StaffConf(this);
RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
if (provider != null) {
luckPerms = provider.getProvider();
}
registerCommands();
}

View file

@ -1,13 +1,12 @@
language: en
unknon: "&cUnknown command !"
staff:
help: "&3=========={Staff help}==========\n
premissionDenide: "&cYou don't have the permission to do this command !"
notPlayer: "&cCommand only for players !"
userNull: "$cCant change permissions !"
unknonGroup: "&cCant find your staff group !"
enable: "Staff mod enabled !"
disable: "Staff mod disabled !"
help: "&3=========={Staff help}==========\n
&e/staff modo&9: Toggle moderation\n
&e/staff admin&9: Toggle administration\n
&3=============================="
modo:
on: "&eModeration on"
off: "&6Moderation off"
admin:
on: "&eAdministration on"
off: "&6Administration off"