Skip to content

Commit

Permalink
Add PermissionLimitation (PERMISSION aka globalPermission) and add
Browse files Browse the repository at this point in the history
PermissionKits for WORLD & CHUNK
  • Loading branch information
Ste3et committed Jan 4, 2024
1 parent 0bfa490 commit a00b50b
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 45 deletions.
Binary file modified .gradle/7.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Expand Up @@ -354,4 +354,8 @@ public ObjectID createObjectID(Location location) {
public ObjectID spawnProject(Location location) {
return FurnitureLib.getInstance().spawn(this, location);
}

public String toString() {
return this.getSystemID();
}
}
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.Predicate;

import org.bukkit.Location;
Expand All @@ -14,7 +15,7 @@
import de.Ste3et_C0st.FurnitureLib.main.ObjectID;
import de.Ste3et_C0st.FurnitureLib.main.Type.LimitationType;

public class ChunkLimitation extends Limitation{
public class ChunkLimitation extends PermissionKitLimit{

private static final String KEY = "Chunk";
private static final String headString = KEY + "Limit";
Expand All @@ -28,15 +29,21 @@ public ChunkLimitation() {
public int getAmount(Predicate<ObjectID> predicate) {
return (int) FurnitureManager.getInstance().getAllExistObjectIDs().filter(predicate).count();
}

@Override
public int getLimit(Project project, Location location, Player player) {
OptionalInt kitLimit = super.getKitLimit(project, location, player);
return kitLimit.isPresent() ? kitLimit.getAsInt() : super.getLimit(project, location, player);
}

@Override
public boolean canPlace(Location location, Project project, Player player) {
return getAmount(buildFilter(location, project, player)) < getLimit(project, location);
return getAmount(buildFilter(location, project, player)) < getLimit(project, location, player);
}

@Override
public void writeConfig() {
final List<String> headerConfig = Arrays.asList(
List<String> headerConfig = Arrays.asList(
"This is the ChunkLimitation file",
"You can limit the max amount of Furnitures each chunk",
"total.enable = (bool) | set default value for each project",
Expand Down
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
Expand All @@ -13,8 +14,11 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import com.google.common.collect.Lists;

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureConfig;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
import de.Ste3et_C0st.FurnitureLib.main.ObjectID;
import de.Ste3et_C0st.FurnitureLib.main.Type;
import de.Ste3et_C0st.FurnitureLib.main.Type.LimitationType;
Expand Down Expand Up @@ -47,20 +51,22 @@ protected File getFile() {
}

public YamlConfiguration loadYaml() {
return this.loadYaml("");
}

public YamlConfiguration loadYaml(final List<String> headerList) {
final String headerString = String.join("\n", headerList.toArray(new String[headerList.size()]));
return this.loadYaml(headerString);
return this.loadYaml(Lists.newArrayList());
}

@SuppressWarnings("deprecation")
public YamlConfiguration loadYaml(String headerString) {
public YamlConfiguration loadYaml(List<String> headerList) {
final YamlConfiguration configuration = YamlConfiguration.loadConfiguration(getFile());
configuration.options().copyDefaults(true);
configuration.options().copyHeader(true);
configuration.options().header(headerString);
if(headerList.isEmpty() == false) {
if(FurnitureLib.getVersionInt() > 18) {
configuration.options().setHeader(headerList);
}else {
final String headerString = String.join("\n", headerList.toArray(new String[headerList.size()]));
configuration.options().header(headerString);
}
}
return configuration;
}

Expand Down Expand Up @@ -109,10 +115,10 @@ protected void save(YamlConfiguration configuration, File file) {
}

public Optional<LimitationInforamtion> buildInforamtion(Player player, Location location, Project project) {
return Optional.of(new LimitationInforamtion(getEnum().name().toLowerCase(), getLimit(project, location), getAmount(buildFilter(location, project, player))));
return Optional.of(new LimitationInforamtion(getEnum().name().toLowerCase(), getLimit(project, location, player), getAmount(buildFilter(location, project, player))));
}

public int getLimit(Project project, Location location) {
public int getLimit(Project project, Location location, Player player) {
if(getEnum() == LimitationType.WORLD) {
final String world = location.getWorld().getName();
return worldMap.getOrDefault(world, -1);
Expand Down
@@ -1,13 +1,10 @@
package de.Ste3et_C0st.FurnitureLib.LimitationManager;

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureConfig;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureManager;
import de.Ste3et_C0st.FurnitureLib.main.ObjectID;
import de.Ste3et_C0st.FurnitureLib.main.Type.LimitationType;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;

Expand All @@ -21,7 +18,7 @@

public class LimitationManager {

private final List<Limitation> limitationListener = Arrays.asList(new ChunkLimitation(), new WorldLimitation(), new PlayerLimitation());
private final List<Limitation> limitationListener = Arrays.asList(new ChunkLimitation(), new WorldLimitation(), new PlayerLimitation(), new PermissionLimitation());
private boolean global = false;

public LimitationManager(FurnitureLib lib, LimitationType ... limitationType) {
Expand All @@ -46,10 +43,6 @@ public boolean isGlobal() {
return this.global;
}

private Integer returnIntProjectTotal(Player p) {
return FurnitureManager.getInstance().getFromPlayer(p.getUniqueId()).size();
}

public boolean canPlace(Player player, ObjectID obj) {
if (player.isOp()) return true;
if (FurnitureLib.getInstance().getPermission().hasPerm(player, "furniture.admin")) return true;
Expand All @@ -59,28 +52,12 @@ public boolean canPlace(Player player, ObjectID obj) {
final Location location = obj.getStartLocation();
if(Objects.isNull(project)) return false;

final int limitGlobal = FurnitureConfig.getFurnitureConfig().getLimitGlobal();
final List<LimitationInforamtion> informationList = new ArrayList<LimitationInforamtion>();

if(limitGlobal > 1) {
int playerTotal = returnIntProjectTotal(player);
if (limitGlobal > 0) {
//Bukkit.broadcastMessage("[DEBUG] config.yml limit: " + limitGlobal);
for (int i = limitGlobal; i > 0; i--) {
if (player.hasPermission("furniture.globallimit." + i)) {
informationList.add(new LimitationInforamtion("permission", i, playerTotal));
//Bukkit.broadcastMessage("[DEBUG] config.yml limit: ✅");
break;
}
}
}
}

limitationListener.stream().filter(Limitation::isActivate).forEach(entry -> {
final int maxSize = entry.getLimit(project, location), amountSize = entry.getAmount(entry.buildFilter(obj.getStartLocation(), project, player));
final int maxSize = entry.getLimit(project, location, player), amountSize = entry.getAmount(entry.buildFilter(obj.getStartLocation(), project, player));
final Optional<LimitationInforamtion> inforamtion = entry.buildInforamtion(player, obj.getStartLocation(), project);
if(inforamtion.isPresent()) informationList.add(inforamtion.get());
//Bukkit.broadcastMessage("[§7DEBUG§f] Limit Type {§d" + entry.getEnum().name() +"}: §a" + amountSize + "§f/§e" + maxSize + ":" + (inforamtion.isPresent() ? "§cX" : "§2✅"));
FurnitureLib.debug("LimitationManager -> {" + entry.getEnum().name() + "} " + amountSize + "/" + maxSize + " passed");
});

Expand Down
@@ -0,0 +1,71 @@
package de.Ste3et_C0st.FurnitureLib.LimitationManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.main.Type.LimitationType;

public abstract class PermissionKitLimit extends Limitation{

private static final String KEY = "Permission";
private static final String headString = KEY + "Kit";
private static final String PERMISSION = "furniture.limit.";

private HashMap<String, HashMap<Project, Integer>> kitMap = new HashMap<>();

private Supplier<String> permissionConsumer = () -> PERMISSION + getEnum().name().toLowerCase() + ".<kitName>";

public PermissionKitLimit(LimitationType type) {
super(type);
}

public YamlConfiguration loadYaml(List<String> headerList) {
if(headerList.isEmpty() == false) {
List<String> headerConfig = new ArrayList<>();
headerConfig.addAll(headerList);
headerConfig.addAll(Arrays.asList(
"----------------------",
"These Limitation type support permission Kits!",
"PermissionKit:",
" <kitName>:",
" chair: -1",
"---------------------",
"the permission for these kit is " + permissionConsumer.get()
));
headerList = headerConfig;
}
return super.loadYaml(headerList);
}

@Override
protected void ioProjectLimit(String headerString, Project project, YamlConfiguration configuration) {
if(configuration.isConfigurationSection(headString)) {
configuration.getConfigurationSection(headString).getKeys(false).forEach(key -> {
final String kit = headString + "." + key;
if(configuration.contains(kit + "." + project.getName())) {
final int amount = configuration.getInt(kit + "." + project.getName(), -1);
final HashMap<Project, Integer> kitMap = this.kitMap.getOrDefault(kit, new HashMap<>());
kitMap.put(project, amount);
this.kitMap.put(key, kitMap);
}
});
}

super.ioProjectLimit(headerString, project, configuration);
}

public OptionalInt getKitLimit(Project project, Location location, Player player) {
final List<String> permissionMap = kitMap.keySet().stream().filter(entry -> player.hasPermission(permissionConsumer.get().replace("<kitName>", entry.toLowerCase()))).collect(Collectors.toList());
return permissionMap.stream().filter(kitMap::containsKey).map(kitMap::get).mapToInt(entry -> entry.get(project)).max();
}
}
@@ -0,0 +1,52 @@
package de.Ste3et_C0st.FurnitureLib.LimitationManager;

import java.util.function.Predicate;

import org.bukkit.Location;
import org.bukkit.entity.Player;

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureConfig;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureManager;
import de.Ste3et_C0st.FurnitureLib.main.ObjectID;
import de.Ste3et_C0st.FurnitureLib.main.Type.LimitationType;

public class PermissionLimitation extends Limitation{

private static final String PERMISSION = "furniture.globallimit.";

public PermissionLimitation() {
super(LimitationType.PERMISSION);
}

@Override
public int getAmount(Predicate<ObjectID> projectAmount) {
return (int) FurnitureManager.getInstance().getAllExistObjectIDs().filter(projectAmount).count();
}

@Override
public boolean canPlace(Location location, Project project, Player player) {
return getAmount(buildFilter(location, project, player)) < getLimit(project, location, player);
}

@Override
public int getLimit(Project project, Location location, Player player) {
for (int i = FurnitureConfig.getFurnitureConfig().getLimitGlobal(); i > 0; i--) {
if (player.hasPermission(PERMISSION + i)) {
return i;
}
}
return 0;
}

@Override
public void writeConfig() {}

@Override
public void updateConfig(Project project) {}

@Override
public Predicate<ObjectID> buildFilter(Location location, Project project, Player player) {
return objectID -> objectID.hasProjectOBJ() && objectID.getUUID().equals(player.getUniqueId());
}
}
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.Predicate;

import org.bukkit.Location;
Expand All @@ -13,7 +14,7 @@
import de.Ste3et_C0st.FurnitureLib.main.ObjectID;
import de.Ste3et_C0st.FurnitureLib.main.Type.LimitationType;

public class PlayerLimitation extends Limitation{
public class PlayerLimitation extends PermissionKitLimit{

private static final String KEY = "Player";
private static final String headString = KEY + "Limit";
Expand All @@ -27,15 +28,21 @@ public PlayerLimitation() {
public int getAmount(Predicate<ObjectID> projectAmount) {
return (int) FurnitureManager.getInstance().getAllExistObjectIDs().filter(projectAmount).count();
}

@Override
public int getLimit(Project project, Location location, Player player) {
OptionalInt kitLimit = super.getKitLimit(project, location, player);
return kitLimit.isPresent() ? kitLimit.getAsInt() : super.getLimit(project, location, player);
}

@Override
public boolean canPlace(Location location, Project project, Player player) {
return getAmount(buildFilter(location, project, player)) < getLimit(project, location);
return getAmount(buildFilter(location, project, player)) < getLimit(project, location, player);
}

@Override
public void writeConfig() {
final List<String> headerConfig = Arrays.asList(
List<String> headerConfig = Arrays.asList(
"This is the PlayerLimitation file",
"You can limit the max amount of Furnitures each Player",
"total.enable = (bool) | set default value for each project",
Expand Down
Expand Up @@ -31,7 +31,7 @@ public int getAmount(Predicate<ObjectID> predicate) {

@Override
public boolean canPlace(Location location, Project project, Player player) {
return getAmount(buildFilter(location, project, player)) < getLimit(project, location);
return getAmount(buildFilter(location, project, player)) < getLimit(project, location, player);
}

@Override
Expand Down
Expand Up @@ -38,7 +38,8 @@ public enum ColorType {BLOCK, BANNER}
public enum LimitationType {
PLAYER,
CHUNK,
WORLD;
WORLD,
PERMISSION;
}

public enum SQLAction {
Expand Down
4 changes: 2 additions & 2 deletions FurnitureLib-Core/src/main/resources/config.yml
Expand Up @@ -61,8 +61,8 @@ world-options:
packetRenderMethode: false # send the furniture models if the player move packet have been triggered

limit-options:
limitConfig: PLAYER # there is only these options avaiable: PLAYER, WORLD, CHUNK you can simple combine then and the FurnitureLib use the config with the lowest value limitConfig: "PLAYER,WORLD"
limitGlobal: 150 # You can give your player the permissions furniture.globallimit.# | furniture.globallimit.125 -> let the player can place 125 furnitures objects it override the limitconfig
limitConfig: PERMISSION,PLAYER # there is only these options avaiable: PERMISSION, PLAYER, WORLD, CHUNK you can simple combine then and the FurnitureLib use the config with the lowest value limitConfig: "PERMISSION,PLAYER"
limitGlobal: 150 # PERMISSION: You can give your player the permissions furniture.globallimit.# | furniture.globallimit.125 -> let the player can place 125 furnitures objects it override the limitconfig

creative-options:
removeItems: false # remove the item if it placed via creative
Expand Down
Binary file modified buildSrc/.gradle/7.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified buildSrc/.gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.

0 comments on commit a00b50b

Please sign in to comment.