1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Fixing permissions

This commit is contained in:
Zrips 2023-01-05 18:14:50 +02:00
parent 2d71e67318
commit 2da51e65b1
8 changed files with 315 additions and 241 deletions

View File

@ -997,6 +997,8 @@ public final class Jobs extends JavaPlugin {
if (jPlayer == null)
return;
CMIDebug.d("action");
List<JobProgression> progression = jPlayer.getJobProgression();
int numjobs = progression.size();

View File

@ -112,10 +112,10 @@ public class PermissionManager {
}
}
private static Map<String, Boolean> getAll(Player player) {
private static Map<String, Boolean> getAll(Player player, String perm) {
Map<String, Boolean> mine = new HashMap<>();
for (PermissionAttachmentInfo permission : player.getEffectivePermissions()) {
if (permission.getPermission().startsWith("jobs."))
if (permission.getPermission().startsWith(perm))
mine.put(permission.getPermission(), permission.getValue());
}
@ -170,21 +170,14 @@ public class PermissionManager {
if (!perm.endsWith("."))
perm += ".";
Map<String, Boolean> permissions = jPlayer.getPermissionsCache();
if (force || permissions == null || getDelay(perm) + jPlayer.getLastPermissionUpdate() < System.currentTimeMillis()) {
if (permissions == null) {
permissions = getAll(player);
} else {
permissions.clear();
permissions.putAll(getAll(player));
}
jPlayer.setPermissionsCache(permissions);
jPlayer.setLastPermissionUpdate(System.currentTimeMillis());
}
double amount = Double.NEGATIVE_INFINITY;
for (Map.Entry<String, Boolean> permission : permissions.entrySet()) {
permissionInfo permInfo = jPlayer.getPermissionsCache(perm);
if (force || getDelay(perm) + permInfo.getTime() < System.currentTimeMillis()) {
Map<String, Boolean> perms = getAll(player, perm);
for (Map.Entry<String, Boolean> permission : perms.entrySet()) {
if (!permission.getKey().startsWith(perm) || !permission.getValue())
continue;
try {
@ -198,7 +191,15 @@ public class PermissionManager {
}
}
return amount == Double.NEGATIVE_INFINITY ? 0D : amount;
permInfo.setTime(System.currentTimeMillis());
permInfo.setValue(amount == Double.NEGATIVE_INFINITY ? 0D : amount);
jPlayer.addToPermissionsCache(perm, permInfo);
}
CMIDebug.d("Max: ", permInfo.getValue());
return permInfo.getValue();
}
public boolean hasPermission(JobsPlayer jPlayer, String perm) {
@ -209,18 +210,17 @@ public class PermissionManager {
if (player == null)
return false;
Map<String, Boolean> permissions = jPlayer.getPermissionsCache();
permissionInfo permInfo = jPlayer.getPermissionsCache(perm);
if (permissions == null || getDelay(perm) + jPlayer.getLastPermissionUpdate() < System.currentTimeMillis()) {
if (permissions == null) {
permissions = new HashMap<>();
jPlayer.setPermissionsCache(permissions);
}
permissions.put(perm, player.hasPermission(perm));
jPlayer.setLastPermissionUpdate(System.currentTimeMillis());
if (getDelay(perm) + permInfo.getTime() < System.currentTimeMillis()) {
permInfo.setState(player.hasPermission(perm));
permInfo.setTime(System.currentTimeMillis());
jPlayer.addToPermissionsCache(perm, permInfo);
}
return permissions.getOrDefault(perm, false);
CMIDebug.d("this return");
return permInfo.getState();
}
}

View File

@ -22,22 +22,22 @@ import net.Zrips.CMILib.Chat.ChatMessageListEdit;
import net.Zrips.CMILib.Chat.ChatMessageListEdit.ChatEditType;
import net.Zrips.CMILib.Chat.ChatMessageObjectEdit;
import net.Zrips.CMILib.Colors.CMIChatColor;
import net.Zrips.CMILib.Container.CMIList;
import net.Zrips.CMILib.Container.CMINumber;
import net.Zrips.CMILib.Container.CMIText;
import net.Zrips.CMILib.Container.PageInfo;
import net.Zrips.CMILib.FileHandler.ConfigReader;
import net.Zrips.CMILib.Locale.LC;
import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.RawMessages.RawMessage;
import net.Zrips.CMILib.RawMessages.RawMessageCommand;
public class editquests implements Cmd {
private enum Action {
update, editline, moveup, movedown, createnew, addline, info, deleteLine, gui, list, delete, editCommands;
list;
public static Action getByName(String name) {
if (name.equalsIgnoreCase("new"))
return Action.createnew;
for (Action one : Action.values()) {
if (one.name().equalsIgnoreCase(name))
return one;
@ -46,8 +46,8 @@ public class editquests implements Cmd {
}
}
HashMap<String, Quest> tempQuests = new HashMap<String, Quest>();
HashMap<String, List<String>> tempObjectives = new HashMap<String, List<String>>();
static HashMap<String, Quest> tempQuests = new HashMap<String, Quest>();
static HashMap<String, List<String>> tempObjectives = new HashMap<String, List<String>>();
@Override
public boolean perform(Jobs plugin, final CommandSender sender, String[] args) {
@ -81,6 +81,16 @@ public class editquests implements Cmd {
switch (action) {
case list:
listQuests(sender, page);
break;
}
return true;
}
private static void listQuests(CommandSender sender, int page) {
LC.info_Spliter.sendMessage(sender);
Set<Quest> quests = new LinkedHashSet<Quest>();
for (Job job : Jobs.getJobs()) {
quests.addAll(job.getQuests());
@ -102,7 +112,11 @@ public class editquests implements Cmd {
.getDisplayName(), "[questName]", quest.getQuestName())) {
@Override
public void onDelete() {
if (quest.getJob() != null) {
removeQuestInFile(quest.getConfigName(), quest.getJob().getName());
quest.getJob().getQuests().remove(quest);
}
listQuests(sender, page);
}
@Override
@ -115,13 +129,11 @@ public class editquests implements Cmd {
}
CMOE.print();
pi.autoPagination(sender, JobsCommands.LABEL + " " + editquests.class.getSimpleName());
break;
}
return true;
}
private static List<String> getRecords(Quest quest, String section) {
private List<String> getRecords(Quest quest, String section) {
List<String> objectives = new ArrayList<String>();
if (quest.getJob() == null)
@ -155,7 +167,7 @@ public class editquests implements Cmd {
return objectives;
}
private ConfigReader getQuestConfig(String jobName) {
private static ConfigReader getQuestConfig(String jobName) {
ConfigReader cfg = null;
@ -185,7 +197,10 @@ public class editquests implements Cmd {
return cfg;
}
private boolean removeQuestInFile(CommandSender sender, String questName, String jobName) {
private static boolean removeQuestInFile(String questName, String jobName) {
if (questName == null)
return false;
ConfigReader cfg = getQuestConfig(jobName);
@ -199,7 +214,7 @@ public class editquests implements Cmd {
return true;
}
private boolean updateQuestInFile(CommandSender sender, Quest quest) {
private static boolean updateQuestInFile(CommandSender sender, Quest quest) {
if (quest.getJob() == null)
return false;
@ -247,8 +262,9 @@ public class editquests implements Cmd {
return true;
}
private void objectivesWindow(CommandSender sender, Quest quest) {
private static void objectivesWindow(CommandSender sender, Quest quest) {
LC.info_Spliter.sendMessage(sender);
RawMessage rm = new RawMessage();
rm.addText(quest.getQuestName() + " objectives");
@ -274,8 +290,9 @@ public class editquests implements Cmd {
cmle.print();
}
private void rewardCommandsWindow(CommandSender sender, Quest quest) {
private static void rewardCommandsWindow(CommandSender sender, Quest quest) {
LC.info_Spliter.sendMessage(sender);
RawMessage rm = new RawMessage();
rm.addText(quest.getQuestName() + " reward commmands");
@ -297,8 +314,9 @@ public class editquests implements Cmd {
cmle.print();
}
private void rewardDescWindow(CommandSender sender, Quest quest) {
private static void rewardDescWindow(CommandSender sender, Quest quest) {
LC.info_Spliter.sendMessage(sender);
RawMessage rm = new RawMessage();
rm.addText(quest.getQuestName() + " reward description");
@ -320,8 +338,9 @@ public class editquests implements Cmd {
cmle.print();
}
private void restrictedAreaWindow(CommandSender sender, Quest quest) {
private static void restrictedAreaWindow(CommandSender sender, Quest quest) {
LC.info_Spliter.sendMessage(sender);
RawMessage rm = new RawMessage();
rm.addText(quest.getQuestName() + " restricted areas");
@ -343,13 +362,13 @@ public class editquests implements Cmd {
cmle.print();
}
private void mainWindow(CommandSender sender, Quest quest) {
private static void mainWindow(CommandSender sender, Quest quest) {
LC.info_Spliter.sendMessage(sender);
RawMessage rm = new RawMessage();
rm.addText("&eName: &f" + quest.getQuestName());
rm.addText("&7Name: &f" + quest.getQuestName());
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", quest.getQuestName()));
RawMessageCommand rmc = new RawMessageCommand() {
@Override
@ -372,59 +391,8 @@ public class editquests implements Cmd {
};
rm.addCommand(rmc);
rm.addText("\n");
List<String> objectives = getRecords(quest, "Objectives");
if (!tempObjectives.containsKey(sender.getName()))
tempObjectives.put(sender.getName(), objectives);
else
objectives = tempObjectives.get(sender.getName());
rm.addText((objectives.isEmpty() ? "&c" : "&e") + "Objectives");
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Objectives"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
objectivesWindow(sender, quest);
}
};
rm.addCommand(rmc);
rm.addText("\n");
rm.addText((quest.getRewardCmds().isEmpty() ? "&c" : "&e") + "Reward commands");
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Reward commands"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
rewardCommandsWindow(sender, quest);
}
};
rm.addCommand(rmc);
rm.addText("\n");
rm.addText("&eDescription");
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Description"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
rewardDescWindow(sender, quest);
}
};
rm.addCommand(rmc);
rm.addText("\n");
rm.addText("&eRestricted areas");
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Restricted areas"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
restrictedAreaWindow(sender, quest);
}
};
rm.addCommand(rmc);
String jobName = quest.getJob() == null ? "&c-" : quest.getJob().getName();
rm.addText("\n");
rm.addText("&eJob: &f" + jobName);
rm.addText(" &7Job: &f" + jobName);
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", jobName));
rmc = new RawMessageCommand() {
@Override
@ -446,7 +414,7 @@ public class editquests implements Cmd {
if (quest.getJob() != j) {
if (quest.getJob() != null) {
removeQuestInFile(sender, quest.getConfigName(), quest.getJob().getName());
removeQuestInFile(quest.getConfigName(), quest.getJob().getName());
quest.getJob().getQuests().remove(quest);
}
j.getQuests().add(quest);
@ -469,8 +437,7 @@ public class editquests implements Cmd {
};
rm.addCommand(rmc);
rm.addText("\n");
rm.addText("&eChance: &f" + quest.getChance());
rm.addText(" &7Chance: &f" + quest.getChance());
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", quest.getChance()));
rmc = new RawMessageCommand() {
@Override
@ -503,7 +470,7 @@ public class editquests implements Cmd {
rm.addText("\n");
rm.addText("&eFrom level: &f" + quest.getMinLvl());
rm.addText("&7Level from: &f" + quest.getMinLvl());
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", quest.getMinLvl()));
rmc = new RawMessageCommand() {
@Override
@ -538,8 +505,7 @@ public class editquests implements Cmd {
};
rm.addCommand(rmc);
rm.addText("\n");
rm.addText("&eTo level: &f" + (quest.getMaxLvl() == null ? "-" : quest.getMaxLvl()));
rm.addText(" &7to: &f" + (quest.getMaxLvl() == null ? "-" : quest.getMaxLvl()));
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", (quest.getMaxLvl() == null ? "-" : quest.getMaxLvl())));
rmc = new RawMessageCommand() {
@Override
@ -574,6 +540,71 @@ public class editquests implements Cmd {
};
rm.addCommand(rmc);
rm.addText("\n");
List<String> objectives = getRecords(quest, "Objectives");
if (!tempObjectives.containsKey(sender.getName()))
tempObjectives.put(sender.getName(), objectives);
else
objectives = tempObjectives.get(sender.getName());
String objectiveString = CMIList.listToString(objectives, " ");
if (objectiveString.length() > 32)
objectiveString = objectiveString.substring(0, 32) + "..";
rm.addText((objectives.isEmpty() ? "&c" : "&7") + "Objectives" + (objectiveString.isBlank() ? "" : " - &f" + objectiveString));
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Objectives"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
objectivesWindow(sender, quest);
}
};
rm.addCommand(rmc);
rm.addText("\n");
String rewardsString = CMIList.listToString(quest.getRewardCmds(), " ");
if (rewardsString.length() > 32)
rewardsString = rewardsString.substring(0, 30) + "..";
rm.addText((quest.getRewardCmds().isEmpty() ? "&c" : "&7") + "Reward commands" + (rewardsString.isBlank() ? "" : " - &f" + rewardsString));
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Reward commands"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
rewardCommandsWindow(sender, quest);
}
};
rm.addCommand(rmc);
rm.addText("\n");
String descString = CMIList.listToString(quest.getDescription(), " ");
if (descString.length() > 32)
descString = descString.substring(0, 30) + "..";
rm.addText("&7Description" + (rewardsString.isBlank() ? "" : " - &f" + descString));
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Description"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
rewardDescWindow(sender, quest);
}
};
rm.addCommand(rmc);
rm.addText("\n");
String restrictedString = CMIList.listToString(quest.getRestrictedAreas(), " ");
if (restrictedString.length() > 32)
restrictedString = restrictedString.substring(0, 30) + "..";
rm.addText("&7Restricted areas" + (restrictedString.isBlank() ? "" : " - &f" + restrictedString));
rm.addHover(LC.modify_editSymbolHover.getLocale("[text]", "Restricted areas"));
rmc = new RawMessageCommand() {
@Override
public void run(CommandSender sender) {
restrictedAreaWindow(sender, quest);
}
};
rm.addCommand(rmc);
rm.show(sender);
}
}

View File

@ -1430,6 +1430,7 @@ public class ConfigManager {
quest.setRewardCmds(sqsection.getStringList("RewardCommands"));
quest.setDescription(sqsection.getStringList("RewardDesc"));
quest.setRestrictedArea(sqsection.getStringList("RestrictedAreas"));
quest.setEnabled(sqsection.getBoolean("Enabled", true));
quests.add(quest);
} catch (Exception e) {

View File

@ -34,6 +34,7 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.permissionInfo;
import com.gamingmesh.jobs.Signs.SignTopType;
import com.gamingmesh.jobs.api.JobsLevelUpEvent;
import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes;
@ -85,8 +86,7 @@ public class JobsPlayer {
private long seen = System.currentTimeMillis();
private Map<String, Boolean> permissionsCache;
private long lastPermissionUpdate = -1L;
private Map<String, permissionInfo> permissionsCache = new HashMap<>();
private final Map<String, Map<String, QuestProgression>> qProgression = new HashMap<>();
private int doneQuests = 0;
@ -924,6 +924,9 @@ public class JobsPlayer {
clearBossMaps();
isOnline = false;
blockOwnerShipInform = null;
permissionsCache.clear();
Jobs.getPlayerManager().addPlayerToCache(this);
}
@ -961,24 +964,16 @@ public class JobsPlayer {
this.seen = seen;
}
public Map<String, Boolean> getPermissionsCache() {
public Map<String, permissionInfo> getPermissionsCache() {
return permissionsCache;
}
public void setPermissionsCache(Map<String, Boolean> permissionsCache) {
this.permissionsCache = permissionsCache;
public permissionInfo getPermissionsCache(String perm) {
return permissionsCache.getOrDefault(perm, new permissionInfo());
}
public void setPermissionsCache(String permission, Boolean state) {
permissionsCache.put(permission, state);
}
public long getLastPermissionUpdate() {
return lastPermissionUpdate;
}
public void setLastPermissionUpdate(Long lastPermissionUpdate) {
this.lastPermissionUpdate = lastPermissionUpdate;
public void addToPermissionsCache(String permission, permissionInfo permInfo) {
permissionsCache.put(permission, permInfo);
}
/**

View File

@ -20,6 +20,8 @@ public class Quest {
private int chance = 100, minLvl = 0;
private Integer maxLvl;
private boolean enabled = false;
private final List<String> rewardCmds = new ArrayList<>(), rewards = new ArrayList<>(), area = new ArrayList<>();
private boolean stopped = false;
@ -217,4 +219,12 @@ public class Quest {
public boolean hasAction(ActionType action) {
return actions.contains(action);
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@ -419,6 +419,7 @@ public final class JobsPaymentListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
final Block block = event.getBlock();
if (!Jobs.getGCManager().canPerformActionInWorld(block.getWorld()))
@ -433,9 +434,12 @@ public final class JobsPaymentListener implements Listener {
if (Jobs.getGCManager().disablePaymentIfRiding && player.isInsideVehicle())
return;
CMIDebug.d("BlockBreakEvent2");
// check if in creative
if (!payIfCreative(player))
return;
CMIDebug.d("BlockBreakEvent3");
if (!Jobs.getPermissionHandler().hasWorldPermission(player, player.getLocation().getWorld().getName()))
return;
@ -1895,12 +1899,7 @@ public final class JobsPaymentListener implements Listener {
}
public static boolean payIfCreative(Player player) {
if (Jobs.getGCManager().payInCreative() && player.getGameMode() == GameMode.CREATIVE)
return true;
if (player.getGameMode() == GameMode.CREATIVE && Jobs.getPermissionManager().hasPermission(Jobs.getPlayerManager().getJobsPlayer(player), "jobs.paycreative"))
return true;
return player.getGameMode() != GameMode.CREATIVE;
return player.getGameMode() != GameMode.CREATIVE || Jobs.getGCManager().payInCreative() || Jobs.getPermissionManager().hasPermission(Jobs.getPlayerManager().getJobsPlayer(player), "jobs.paycreative");
}
// Prevent item durability loss

View File

@ -0,0 +1,36 @@
package com.gamingmesh.jobs;
public class permissionInfo {
private long time = 0L;
private boolean state = false;
private double value = 0D;
public long getTime() {
return time;
}
public permissionInfo setTime(long time) {
this.time = time;
return this;
}
public boolean getState() {
return state;
}
public permissionInfo setState(boolean state) {
this.state = state;
return this;
}
public double getValue() {
return value;
}
public permissionInfo setValue(double value) {
this.value = value;
return this;
}
}