Skip to content

Commit

Permalink
Enhance the limitation check
Browse files Browse the repository at this point in the history
  • Loading branch information
Ste3et committed Aug 1, 2022
1 parent 66ecf13 commit 8244d1c
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 71 deletions.
Binary file modified .gradle/7.2/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.Ste3et_C0st.FurnitureLib.LimitationManager;

public class LimitationInforamtion {

private final String type;
private final int max, amount;

public LimitationInforamtion(String type, int max, int amount) {
this.type = type;
this.max = max;
this.amount = amount;
}

public String getType() {
return type;
}

public int getMax() {
return max;
}

public int getAmount() {
return amount;
}

public boolean isCanceld() {
if(isInfinite()) return false;
return amount + 1 > max;
}

public boolean isInfinite() {
return max < 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.Utilitis.LanguageManager;
import de.Ste3et_C0st.FurnitureLib.Utilitis.StringTranslator;
import de.Ste3et_C0st.FurnitureLib.Utilitis.config;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureConfig;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
Expand All @@ -14,21 +15,30 @@
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

public class LimitationManager {

public List<LimitationObject> objectList = new ArrayList<LimitationObject>();
public LimitationType type = null;
private final List<LimitationType> limitationListener = new ArrayList<LimitationType>();
FurnitureLib lib;
private boolean global = false;

public LimitationManager(FurnitureLib lib, LimitationType limitationType) {
this.lib = lib;
this.type = limitationType;
loadDefault();
}

public LimitationManager(FurnitureLib lib, LimitationType ... limitationType) {
this.lib = lib;
this.limitationListener.addAll(Arrays.asList(limitationType));
this.loadDefault();
}

public void setGlobal(boolean bool) {
this.global = bool;
Expand All @@ -50,6 +60,12 @@ private Integer returnIntProjectChunk(Chunk c, Project pro) {
if (pro == null) return i;
return (int) FurnitureManager.getInstance().getInChunk(c).stream().filter(obj -> obj.getProject().equals(pro.getName())).count();
}

private Integer returnIntProjectChunk(int chunkX, int chunkZ, World world, Project pro) {
int i = 0;
if (pro == null) return i;
return (int) FurnitureManager.getInstance().getInChunkByCoord(chunkX, chunkZ, world).stream().filter(obj -> obj.getProject().equals(pro.getName())).count();
}

private Integer returnProjectWorld(World w, Project pro) {
int i = 0;
Expand All @@ -65,93 +81,94 @@ public boolean canPlace(Player p, ObjectID obj) {
if(Objects.isNull(obj.getWorld())) return false;
Project pro = obj.getProjectOBJ();
LimitationObject limitOBJ = getLimitOBJ(p, pro);

World world = p.getWorld();

if (limitOBJ != null) {
if (limitOBJ.total && limitOBJ.totalAmount == -1) return true;
}

if(Objects.isNull(type) || Objects.isNull(pro)) {
if(Objects.isNull(pro)) {
return true;
}

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

if(limitGlobal > 1) {
int playerTotal = returnIntProjectTotal(p);
//Permissions range check start
if (limitGlobal > 0) {
for (int i = limitGlobal; i > 0; i--) {
if (p.hasPermission("furniture.globallimit." + i)) {
if (playerTotal < i) {
String s = LanguageManager.getInstance().getString("message.LimitAnnouncer");
s = s.replace("#TYPE#", pro.getDisplayName()).replace("#SYSTEMID#", pro.getName()).replace("#CURRENT#", playerTotal + 1 + "").replace("#MAX#", i + "");
p.sendMessage(s);
return true;
} else {
p.sendMessage(LanguageManager.getInstance().getString("message.LimitReachedMaximum"));
return false;
}
informationList.add(new LimitationInforamtion("permission", i, playerTotal));
}
}
}
}

if (LimitationType.PLAYER == this.type) {
int player = returnIntProject(p, pro);
//Permissions range check end

int maxPlayer = limitOBJ.getAmountFromType(pro.getName());
FurnitureLib.debug("LimitationManager -> {Player} " + player + "/" + maxPlayer);
if (maxPlayer < 0) return true;
if (player < maxPlayer) {
String s = LanguageManager.getInstance().getString("message.LimitAnnouncer");
s = s.replace("#TYPE#", pro.getName()).replace("#CURRENT#", player + 1 + "").replace("#MAX#", maxPlayer + "");
p.sendMessage(s);
return true;
} else {
p.sendMessage(LanguageManager.getInstance().getString("message.LimitReachedMaximum"));
return false;
}
} else if (LimitationType.WORLD == this.type) {
int maxWorld = (Objects.nonNull(limitOBJ) && limitOBJ.total) ? limitOBJ.totalAmount : pro.getAmountWorld(obj.getWorld());
int world = this.global ? FurnitureManager.getInstance().getInWorld(obj.getWorld()).size() : returnProjectWorld(obj.getWorld(), pro);
FurnitureLib.debug("LimitationManager -> {World} " + world + "/" + maxWorld);
if (maxWorld < 0) return true;
if (world < maxWorld) {
String s = LanguageManager.getInstance().getString("message.LimitAnnouncer");
s = s.replace("#TYPE#", pro.getName()).replace("#CURRENT#", world + 1 + "").replace("#MAX#", maxWorld + "");
p.sendMessage(s);
return true;
} else {
p.sendMessage(LanguageManager.getInstance().getString("message.LimitReachedWorld"));
return false;
}
} else if (LimitationType.CHUNK == this.type) {
int maxChunk = (Objects.nonNull(limitOBJ) && limitOBJ.total) ? limitOBJ.totalAmount : pro.getAmountChunk();
int chunk = this.global ? FurnitureManager.getInstance().getInChunk(obj.getChunk()).size() : returnIntProjectChunk(obj.getChunk(), pro);
FurnitureLib.debug("LimitationManager -> {Chunk} " + chunk + "/" + maxChunk);
if (maxChunk < 0) return true;
if (chunk < maxChunk) {
String s = LanguageManager.getInstance().getString("message.LimitAnnouncer");
s = s.replace("#TYPE#", pro.getName()).replace("#CURRENT#", chunk + 1 + "").replace("#MAX#", maxChunk + "");
p.sendMessage(s);
return true;
} else {
p.sendMessage(LanguageManager.getInstance().getString("message.LimitReachedChunk"));
return false;
}
for(LimitationType type : this.limitationListener) {
final int maxSize, amountSize;

switch(type) {
case PLAYER:
maxSize = limitOBJ.getAmountFromType(pro.getName());
amountSize = returnIntProject(p, pro);
break;
case WORLD:
maxSize = (Objects.nonNull(limitOBJ) && limitOBJ.total) ? limitOBJ.totalAmount : pro.getAmountWorld(obj.getWorld());
amountSize = this.global ? FurnitureManager.getInstance().getInWorld(world).size() : returnProjectWorld(world, pro);
break;
case CHUNK:
final int xChunk = obj.getStartLocation().getBlockX() >> 4, zChunk = obj.getStartLocation().getBlockZ() >> 4;
maxSize = (Objects.nonNull(limitOBJ) && limitOBJ.total) ? limitOBJ.totalAmount : pro.getAmountChunk();
amountSize = this.global ? FurnitureManager.getInstance().getInChunk(obj.getChunk()).size() : returnIntProjectChunk(xChunk, zChunk, world, pro);
break;
default:
maxSize = -1;
amountSize = 0;
break;
}

LimitationInforamtion inforamtion = new LimitationInforamtion(type.name().toLowerCase(), maxSize, amountSize);
informationList.add(inforamtion);
FurnitureLib.debug("LimitationManager -> {" + inforamtion.getType() + "} " + amountSize + "/" + maxSize + " passed");
}

if(informationList.isEmpty() == false) {
informationList.stream().sorted((k1,k2) -> Integer.compare(k1.getMax(), k2.getMax())).collect(Collectors.toList());
Optional<LimitationInforamtion> canceldLimit = informationList.stream().filter(LimitationInforamtion::isCanceld).findFirst();
if(canceldLimit.isPresent()) {
LimitationInforamtion object = canceldLimit.get();
p.sendMessage(LanguageManager.getInstance().getString("message.limit." + object.getType() + ".reached",
new StringTranslator("#amount#", Integer.toString(object.getAmount())),
new StringTranslator("#size#", Integer.toString(object.getMax())),
new StringTranslator("#project#", pro.getDisplayName()),
new StringTranslator("#world#", world.getName())
));
return false;
}else {
LimitationInforamtion infoLimit = informationList.stream().findFirst().orElse(null);
if(Objects.nonNull(infoLimit)) {
p.sendMessage(LanguageManager.getInstance().getString("message.limit." + infoLimit.getType() + ".info",
new StringTranslator("#amount#", Integer.toString(infoLimit.getAmount() + 1)),
new StringTranslator("#size#", Integer.toString(infoLimit.getMax())),
new StringTranslator("#project#", pro.getDisplayName()),
new StringTranslator("#world#", world.getName())
));
}
}
}
return true;
}

public void loadDefault() {
if (LimitationType.PLAYER == this.type) {
if (this.limitationListener.contains(LimitationType.PLAYER)) {
config c = new config(lib);
FileConfiguration file = c.getConfig(this.type.name().toLowerCase(), "/limitation/");
LimitationObject defaultSection = new LimitationObject(type, "default");
FileConfiguration file = c.getConfig(LimitationType.PLAYER.name().toLowerCase(), "/limitation/");
LimitationObject defaultSection = new LimitationObject(LimitationType.PLAYER, "default");
if (file.isConfigurationSection("PlayerLimit")) {
for (String s : file.getConfigurationSection("PlayerLimit").getKeys(false)) {
if (!s.equalsIgnoreCase("default")) {
LimitationObject limitOBJ = new LimitationObject(type, s);
LimitationObject limitOBJ = new LimitationObject(LimitationType.PLAYER, s);
if (!objectList.contains(limitOBJ)) {
objectList.add(limitOBJ);
}
Expand All @@ -165,7 +182,7 @@ public void loadDefault() {
}

public void loadDefault(String project) {
if (LimitationType.PLAYER == this.type) {
if (this.limitationListener.contains(LimitationType.PLAYER)) {
objectList.forEach(obj -> {
obj.addDefault(project);
obj.loadProjects(project);
Expand All @@ -175,7 +192,7 @@ public void loadDefault(String project) {

public LimitationObject getLimitOBJ(Player p, Project project) {
LimitationObject lobj = null;
if (LimitationType.PLAYER == this.type) {
if (this.limitationListener.contains(LimitationType.PLAYER)) {
int i = -1;
for (LimitationObject obj : this.objectList) {
if (obj.def) {
Expand Down Expand Up @@ -203,7 +220,7 @@ public LimitationObject getDefault() {
return null;
}

public LimitationType getType() {
return this.type;
public List<LimitationType> getTypes(){
return this.limitationListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.Database.Database;
import de.Ste3et_C0st.FurnitureLib.LimitationManager.LimitationType;
import de.Ste3et_C0st.FurnitureLib.Utilitis.LanguageManager;
import de.Ste3et_C0st.FurnitureLib.Utilitis.SkullMetaPatcher;
import de.Ste3et_C0st.FurnitureLib.Utilitis.Wrapper.ChatComponentWrapper;
Expand Down Expand Up @@ -87,7 +92,7 @@ public DumpHandler(CommandSender sender) {
packetInfos.addProperty("autoPure", FurnitureConfig.getFurnitureConfig().isAutoPurge());
packetInfos.addProperty("useGamemode", FurnitureConfig.getFurnitureConfig().useGamemode());
packetInfos.addProperty("language", LanguageManager.getInstance().getLanguage());
packetInfos.addProperty("limitConfig", FurnitureConfig.getFurnitureConfig().getLimitManager().getType().name());
packetInfos.addProperty("limitConfig", FurnitureConfig.getFurnitureConfig().getLimitManager().getTypes().stream().map(Enum::toString).collect(Collectors.joining(",")));
packetInfos.addProperty("regionMemberAccess", FurnitureConfig.getFurnitureConfig().haveRegionMemberAccess());
packetInfos.addProperty("eventType", FurnitureConfig.getFurnitureConfig().getDefaultEventType().name());
packetInfos.addProperty("publicType", FurnitureConfig.getFurnitureConfig().getDefaultPublicType().name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,18 @@ private void loadNewConfig() {

//limit config
String limitConfig = getConfig().getString("limit-options.limitConfig", "PLAYER").toUpperCase();
this.limitManager = new LimitationManager(instance, LimitationType.valueOf(limitConfig.toUpperCase()));
List<LimitationType> type = new ArrayList<LimitationType>();
if(limitConfig.contains(",")) {
String[] arrays = limitConfig.split(",");
for(String str : arrays) {
try {
type.add(LimitationType.valueOf(str.toUpperCase()));
}catch (Exception e) {
e.printStackTrace();
}
}
}
this.limitManager = new LimitationManager(instance, type.toArray(new LimitationType[type.size()]));
this.limitGlobal = getConfig().getInt("limit-options.limitGlobal", -1);

//protection config
Expand All @@ -226,8 +237,8 @@ private void loadNewConfig() {
debug("Config->rotateOnSit:" + rotateOnSit);
debug("Config->limitConfig:" + limitConfig);
debug("Config->limitGlobal:" + limitGlobal);
debug("Config->PlaceMode.Access:" + type.name);
debug("Config->PlaceMode.Mode:" + mode.name);
debug("Config->PlaceMode.Access:" + this.type.name);
debug("Config->PlaceMode.Mode:" + this.mode.name);
debug("Config->update:" + update);

this.registerRenderEvents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public HashSet<ObjectID> getInChunk(Chunk c) {
return new HashSet<ObjectID>(getObjectStreamFromWorld(c.getWorld()).filter(entry -> entry.getBlockX() >> 4 == x && entry.getBlockZ() >> 4 == z).collect(Collectors.toList()));
}

public HashSet<ObjectID> getInChunkByCoord(int x, int z, World world) {
return new HashSet<ObjectID>(getObjectStreamFromWorld(world).filter(entry -> entry.getBlockX() >> 4 == x && entry.getBlockZ() >> 4 == z).collect(Collectors.toList()));
}

public HashSet<fEntity> getfArmorStandByObjectID(ObjectID id) {
return id.getPacketList();
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/de/Ste3et_C0st/FurnitureLib/main/Type.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.Ste3et_C0st.FurnitureLib.main;

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.Utilitis.LanguageManager;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
Expand All @@ -10,6 +11,7 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -35,7 +37,11 @@ public enum DataBaseType {MySQL, SQLite}

public enum ColorType {BLOCK, BANNER}

public enum LimitationType {PLAYER, CHUNK, WORLD}
public enum LimitationType {
PLAYER(),
CHUNK(),
WORLD()
}

public enum SQLAction {SAVE, UPDATE, REMOVE, PURGE, NOTHING}

Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/language/EN_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ message:
reload: '&7FurnitureLib has been &2&lreloaded'
NoPermissions: '&cYou do not have permissions to do this.'
IgnoredWorld: "&7You can't place furniture models at &2%WORLD%"
limit:
world:
reached: '&7You reached the World Furniture Limit of: &c#amount#/#size#'
info: '&7You have placed: &e#amount#/#size# &7for project &a#project# &7in world &a#world#'
player:
reached: '&7You reached the Player Furniture Limit of: &c#amount#/#size#'
info: '&7You have placed: &c#amount#/#size# for project #project#'
chunk:
reached: '&7You reached the Chunk Furniture Limit of: &c#amount#/#size#'
info: '&7You have placed: &c#amount#/#size# for project #project#'
permission:
reached: '&7You reached the Chunk Permission Limit of: &c#amount#/#size#'
info: '&7You have placed: &c#amount#/#size# for project #project#'
command:
help:
header:
Expand Down

0 comments on commit 8244d1c

Please sign in to comment.