Added group limiter

This commit is contained in:
jameslfc19 2020-04-28 22:44:50 +01:00
parent 097174b221
commit 007fb3817f
4 changed files with 42 additions and 4 deletions

View File

@ -34,7 +34,10 @@ public class ChestLinkListener implements Listener {
if (info != null) {
if(event.getPlayer().hasPermission(Permissions.ADD)) {
if (Utils.isValidSignPosition(event.getBlockAgainst().getLocation())) {
Config.addChest(event.getPlayer(), info.getGroup(), event.getBlockAgainst().getLocation(),info.getPlayer());
if(!Config.addChest(event.getPlayer(), info.getGroup(), event.getBlockAgainst().getLocation(),info.getPlayer())){
sign.getBlock().breakNaturally();
return;
}
Messages.CHEST_ADDED(event.getPlayer(), info.getGroup(), info.getPlayer().getName());
setLine(sign, signChangeEvent, 0, ChatColor.RED + ChatColor.stripColor(signChangeEvent.getLine(0)));
setLine(sign, signChangeEvent, 1, ChatColor.GREEN + ChatColor.stripColor(signChangeEvent.getLine(1)));

View File

@ -3,6 +3,7 @@ package com.jamesdpeters.minecraft.chests.misc;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.serialize.InventoryStorage;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
public class Messages {
@ -104,4 +105,8 @@ public class Messages {
public static void CANNOT_RENAME_GROUP_DOESNT_EXIST(Player target, String oldidentifier){
target.sendMessage(ChatColor.RED+"Error renaming chest! "+ChatColor.WHITE+oldidentifier+ChatColor.RED+" doesn't exist!");
}
public static void OWNER_HAS_TOO_MANY_CHESTS(Player target, OfflinePlayer owner){
target.sendMessage(ChatColor.RED+"Owner: "+ChatColor.WHITE+owner.getName()+ChatColor.RED+" has reached the limit of groups allowed!");
}
}

View File

@ -7,11 +7,18 @@ public class Settings {
private static String CHECK_UPDATE = "update-checker";
private static String CHECK_UPDATE_PERIOD = "update-checker-period";
private static String LIMIT_CHESTS = "limit-chestlinks";
private static String LIMIT_CHESTS_NUMBER = "limit-chestlinks-amount";
private static Settings cf;
private FileConfiguration configuration;
private Plugin plugin;
private static boolean isUpdateCheckEnabled;
private static int updateCheckerPeriod;
private static boolean limitChests;
private static int limitChestsAmount;
public static void initConfig(Plugin plugin){
cf = new Settings();
cf.plugin = plugin;
@ -20,9 +27,13 @@ public class Settings {
//DEFAULT VALUES
cf.configuration.addDefault(CHECK_UPDATE,true);
cf.configuration.addDefault(CHECK_UPDATE_PERIOD,60*60);
cf.configuration.addDefault(LIMIT_CHESTS,false);
cf.configuration.addDefault(LIMIT_CHESTS_NUMBER,0);
cf.configuration.options().copyDefaults(true);
cf.plugin.saveConfig();
reloadConfig();
}
private static void save(){
@ -31,13 +42,20 @@ public class Settings {
public static void reloadConfig(){
cf.configuration = cf.plugin.getConfig();
isUpdateCheckEnabled = cf.configuration.getBoolean(CHECK_UPDATE);
updateCheckerPeriod = cf.configuration.getInt(CHECK_UPDATE_PERIOD);
limitChests = cf.configuration.getBoolean(LIMIT_CHESTS);
limitChestsAmount = cf.configuration.getInt(LIMIT_CHESTS_NUMBER);
}
/**
* GETTERS AND SETTERS
*/
public static boolean isUpdateCheckEnabled() {
return cf.configuration.getBoolean(CHECK_UPDATE);
return isUpdateCheckEnabled;
}
public static int getUpdateCheckerPeriodTicks() { return 20*cf.configuration.getInt(CHECK_UPDATE_PERIOD);}
public static int getUpdateCheckerPeriodTicks() { return 20*updateCheckerPeriod;}
public static boolean isLimitChests() { return limitChests; }
public static int getLimitChestsAmount() { return limitChestsAmount; }
}

View File

@ -2,6 +2,7 @@ package com.jamesdpeters.minecraft.chests.serialize;
import com.jamesdpeters.minecraft.chests.containers.ChestLinkInfo;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -83,12 +84,16 @@ public class Config {
return null;
}
public static void addChest(Player player, String identifier, Location chestLocation, OfflinePlayer owner){
public static boolean addChest(Player player, String identifier, Location chestLocation, OfflinePlayer owner){
//List of groups this player has.
HashMap<String, InventoryStorage> map = getInventoryStorageMap(owner.getUniqueId());
//Get Inventory Storage for the given group or create it if it doesnt exist.
if(!map.containsKey(identifier)){
if(isAtLimit(owner)){
Messages.OWNER_HAS_TOO_MANY_CHESTS(player,owner);
return false;
}
InventoryStorage storage = new InventoryStorage(owner,identifier,chestLocation);
map.put(identifier, storage);
}
@ -115,6 +120,7 @@ public class Config {
inventoryStorage.getLocations().add(chestLocation);
}
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP,1.0f,1f);
return true;
//saveASync();
}
@ -206,4 +212,10 @@ public class Config {
return true;
}
public static boolean isAtLimit(OfflinePlayer player){
if(Settings.isLimitChests()){
return getInventoryStorageMap(player.getUniqueId()).size() >= Settings.getLimitChestsAmount();
}
return false;
}
}