mirror of
https://github.com/DRE2N/DungeonsXL.git
synced 2024-11-02 17:00:19 +01:00
Finish game rule API
This commit is contained in:
parent
49b94f8c82
commit
6b9ac9d624
@ -14,7 +14,9 @@
|
||||
*/
|
||||
package de.erethon.dungeonsxl.api.dungeon;
|
||||
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import java.util.Collection;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
/**
|
||||
* A {@link GameRule} where the value is a {@link java.util.Collection}.
|
||||
@ -25,8 +27,57 @@ import java.util.Collection;
|
||||
*/
|
||||
public class CollectionGameRule<T, V extends Collection<T>> extends GameRule<V> {
|
||||
|
||||
public CollectionGameRule(Class type, String key, V defaultValue) {
|
||||
super(type, key, defaultValue);
|
||||
/**
|
||||
* @param key the configuration key of the game rule
|
||||
* @param defaultValue the default value that is used when nothing is set
|
||||
*/
|
||||
public CollectionGameRule(String key, V defaultValue) {
|
||||
super(null, key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key the configuration key of the game rule
|
||||
* @param defaultValue the default value that is used when nothing is set
|
||||
* @param reader a functional interface that loads the value from config
|
||||
*/
|
||||
public CollectionGameRule(String key, V defaultValue, ConfigReader<V> reader) {
|
||||
super(null, key, defaultValue, reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation uses more expensive casting + catching the ClassCastException.
|
||||
* Developers should consider doing that themselves instead of wasting this cast.
|
||||
*
|
||||
* @param value the value
|
||||
* @return if the given value is an instance of {@link V}
|
||||
*/
|
||||
@Override
|
||||
public boolean isValidValue(Object value) {
|
||||
try {
|
||||
V v = (V) value;
|
||||
return true;
|
||||
} catch (ClassCastException exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public V fromConfig(DungeonsAPI api, GameRuleContainer container, ConfigurationSection config) {
|
||||
Object value = config.get(getKey());
|
||||
if (reader != null) {
|
||||
V v = reader.read(api, value);
|
||||
container.setState(this, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
V v;
|
||||
try {
|
||||
v = (V) value;
|
||||
} catch (ClassCastException exception) {
|
||||
return null;
|
||||
}
|
||||
container.setState(this, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2014-2020 Daniel Saukel
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNULesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package de.erethon.dungeonsxl.api.dungeon;
|
||||
|
||||
import de.erethon.caliburn.item.ExItem;
|
||||
import de.erethon.caliburn.mob.ExMob;
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A functional interface to deserialize a raw value read from a configuration.
|
||||
*
|
||||
* @param <V> the type of the object to read
|
||||
* @author Daniel Saukel
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ConfigReader<V> {
|
||||
|
||||
/**
|
||||
* Reads a set of Caliburn items.
|
||||
*/
|
||||
static final ConfigReader<Set<ExItem>> EX_ITEM_SET_READER = (api, value) -> {
|
||||
if (!(value instanceof Collection)) {
|
||||
return null;
|
||||
}
|
||||
Set<ExItem> set = new HashSet<>();
|
||||
for (Object entry : (Collection) value) {
|
||||
set.add(api.getCaliburn().getExItem(entry));
|
||||
}
|
||||
return set;
|
||||
};
|
||||
/**
|
||||
* Reads a set of Caliburn mobs.
|
||||
*/
|
||||
static final ConfigReader<Set<ExMob>> EX_MOB_SET_READER = (api, value) -> {
|
||||
if (!(value instanceof Collection)) {
|
||||
return null;
|
||||
}
|
||||
Set<ExMob> set = new HashSet<>();
|
||||
for (Object entry : (Collection) value) {
|
||||
set.add(api.getCaliburn().getExMob(entry));
|
||||
}
|
||||
return set;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads a game rule state from the configuration.
|
||||
*
|
||||
* @param api the DungeonsAPI instance
|
||||
* @param value the configuration object. This is the object received from using {@link org.bukkit.configuration.ConfigurationSection#get(String)}
|
||||
* with the String being {@link GameRule#getKey()}.
|
||||
* @return the game rule state read from configuration
|
||||
*/
|
||||
V read(DungeonsAPI api, Object value);
|
||||
|
||||
}
|
@ -14,15 +14,18 @@
|
||||
*/
|
||||
package de.erethon.dungeonsxl.api.dungeon;
|
||||
|
||||
import de.erethon.caliburn.CaliburnAPI;
|
||||
import de.erethon.caliburn.item.ExItem;
|
||||
import de.erethon.caliburn.mob.ExMob;
|
||||
import de.erethon.caliburn.mob.VanillaMob;
|
||||
import de.erethon.commons.chat.MessageUtil;
|
||||
import de.erethon.commons.misc.EnumUtil;
|
||||
import de.erethon.commons.misc.NumberUtil;
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import de.erethon.dungeonsxl.api.Requirement;
|
||||
import de.erethon.dungeonsxl.api.Reward;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@ -39,8 +42,6 @@ import org.bukkit.configuration.ConfigurationSection;
|
||||
* @param <V> the type of the game rule value
|
||||
* @author Daniel Saukel
|
||||
*/
|
||||
// TODO: These values aren't properly fetched from config yet: values that involve Caliburn, requirements & rewards; maybe messages.
|
||||
// Check special merging rules for damageProtectedEntities and interactionProtectedEntities.
|
||||
public class GameRule<V> {
|
||||
|
||||
/**
|
||||
@ -86,24 +87,40 @@ public class GameRule<V> {
|
||||
/**
|
||||
* A whitelist of breakable blocks. breakBlocks is supposed to be set to "true" if this should be used.
|
||||
*/
|
||||
public static final GameRule<Map<ExItem, HashSet<ExItem>>> BREAK_WHITELIST = new MapGameRule<>(Map.class, "breakWhitelist", null);
|
||||
public static final GameRule<Map<ExItem, HashSet<ExItem>>> BREAK_WHITELIST = new MapGameRule<>("breakWhitelist", null, (api, value) -> {
|
||||
if (!(value instanceof ConfigurationSection)) {
|
||||
return null;
|
||||
}
|
||||
ConfigurationSection section = (ConfigurationSection) value;
|
||||
Map<ExItem, HashSet<ExItem>> map = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : section.getValues(false).entrySet()) {
|
||||
ExItem tool = api.getCaliburn().getExItem(entry.getKey());
|
||||
if (tool == null) {
|
||||
continue;
|
||||
}
|
||||
HashSet<ExItem> blocks = new HashSet<>();
|
||||
blocks.addAll(api.getCaliburn().deserializeExItemList(section, entry.getKey()));
|
||||
map.put(tool, blocks);
|
||||
}
|
||||
return map;
|
||||
});
|
||||
/**
|
||||
* A list of all entity types that shall be protected from damage. If this is left out AND if breakBlocks is false, armor stands, paintings and item frames
|
||||
* will be protected by default. If this is left out and if breakBlocks is true, nothing will be protected by default.
|
||||
*/
|
||||
public static final GameRule<Set<ExMob>> DAMAGE_PROTECTED_ENTITIES = new CollectionGameRule<>(Set.class, "damageProtectedEntities", new HashSet<>(Arrays.asList(
|
||||
public static final GameRule<Set<ExMob>> DAMAGE_PROTECTED_ENTITIES = new CollectionGameRule<>("damageProtectedEntities", new HashSet<>(Arrays.asList(
|
||||
VanillaMob.ARMOR_STAND,
|
||||
VanillaMob.ITEM_FRAME,
|
||||
VanillaMob.PAINTING
|
||||
)));
|
||||
)), ConfigReader.EX_MOB_SET_READER);
|
||||
/**
|
||||
* If this is left out AND if breakBlocks is false, armor stands and item frames will be protected by default. If this is left out and if breakBlocks is
|
||||
* true, nothing will be protected by default.
|
||||
*/
|
||||
public static final GameRule<Set<ExMob>> INTERACTION_PROTECTED_ENTITIES = new CollectionGameRule<>(Set.class, "interactionProtectedEntities", new HashSet<>(Arrays.asList(
|
||||
public static final GameRule<Set<ExMob>> INTERACTION_PROTECTED_ENTITIES = new CollectionGameRule<>("interactionProtectedEntities", new HashSet<>(Arrays.asList(
|
||||
VanillaMob.ARMOR_STAND,
|
||||
VanillaMob.ITEM_FRAME
|
||||
)));
|
||||
)), ConfigReader.EX_MOB_SET_READER);
|
||||
/**
|
||||
* If blocks may be placed.
|
||||
*/
|
||||
@ -111,7 +128,7 @@ public class GameRule<V> {
|
||||
/**
|
||||
* A whitelist of placeable blocks. placeBlocks is supposed to be set to "true" if this should be used.
|
||||
*/
|
||||
public static final GameRule<Set<ExItem>> PLACE_WHITELIST = new CollectionGameRule<>(Set.class, "placeWhitelist", null);
|
||||
public static final GameRule<Set<ExItem>> PLACE_WHITELIST = new CollectionGameRule<>("placeWhitelist", null, ConfigReader.EX_ITEM_SET_READER);
|
||||
/**
|
||||
* If it should rain permanently in the dungeon.
|
||||
* <p>
|
||||
@ -183,64 +200,137 @@ public class GameRule<V> {
|
||||
/**
|
||||
* A list of requirements. Note that requirements will be ignored if the player has the dxl.ignorerequirements permission node.
|
||||
*/
|
||||
public static final GameRule<List<Requirement>> REQUIREMENTS = new CollectionGameRule<>(List.class, "requirements", new ArrayList<>());
|
||||
public static final GameRule<List<Requirement>> REQUIREMENTS = new CollectionGameRule<>("requirements", new ArrayList<>(), (api, value) -> {
|
||||
if (!(value instanceof ConfigurationSection)) {
|
||||
return null;
|
||||
}
|
||||
ConfigurationSection section = (ConfigurationSection) value;
|
||||
List<Requirement> requirements = new ArrayList<>();
|
||||
for (String key : section.getValues(false).keySet()) {
|
||||
Class<? extends Requirement> clss = api.getRequirementRegistry().get(key);
|
||||
if (clss == null) {
|
||||
MessageUtil.log(api, "&4Could not find requirement named \"" + key + "\".");
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Constructor constructor = clss.getConstructor(DungeonsAPI.class);
|
||||
if (constructor == null) {
|
||||
constructor = clss.getConstructor();
|
||||
if (constructor == null) {
|
||||
MessageUtil.log(api, "&4Requirement \"" + key + "\" is not implemented properly with a (DungeonsAPI) constructor.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
requirements.add((Requirement) constructor.newInstance(api));
|
||||
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
|
||||
| IllegalArgumentException | InvocationTargetException exception) {
|
||||
MessageUtil.log(api, "&4Requirement \"" + key + "\" is not implemented properly with a (DungeonsAPI) constructor.");
|
||||
}
|
||||
}
|
||||
return requirements;
|
||||
});
|
||||
/**
|
||||
* One of these Dungeons must be finished ("any" for any dungeon).
|
||||
*/
|
||||
public static final GameRule<List<String>> MUST_FINISH_ONE = new CollectionGameRule<>(List.class, "mustFinishOne", null);
|
||||
public static final GameRule<List<String>> MUST_FINISH_ONE = new CollectionGameRule<>("mustFinishOne", null);
|
||||
/**
|
||||
* All of these Dungeons must be finished. If you do not want any, leave this empty.
|
||||
*/
|
||||
public static final GameRule<List<String>> MUST_FINISH_ALL = new CollectionGameRule<>(List.class, "mustFinishAll", null);
|
||||
public static final GameRule<List<String>> MUST_FINISH_ALL = new CollectionGameRule<>("mustFinishAll", null);
|
||||
/**
|
||||
* This can be used to give rewards. The default implementation does not do this at the moment.
|
||||
*/
|
||||
public static final GameRule<List<Reward>> REWARDS = new CollectionGameRule<>(List.class, "rewards", new ArrayList<>());
|
||||
public static final GameRule<List<Reward>> REWARDS = new CollectionGameRule<Reward, List<Reward>>("rewards", new ArrayList<>(), (api, value) -> {
|
||||
if (!(value instanceof ConfigurationSection)) {
|
||||
return null;
|
||||
}
|
||||
ConfigurationSection section = (ConfigurationSection) value;
|
||||
List<Reward> rewards = new ArrayList<>();
|
||||
for (String key : section.getValues(false).keySet()) {
|
||||
Class<? extends Reward> clss = api.getRewardRegistry().get(key);
|
||||
if (clss == null) {
|
||||
MessageUtil.log(api, "&4Could not find reward named \"" + key + "\".");
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Constructor constructor = clss.getConstructor(DungeonsAPI.class);
|
||||
if (constructor == null) {
|
||||
constructor = clss.getConstructor();
|
||||
if (constructor == null) {
|
||||
MessageUtil.log(api, "&4Reward \"" + key + "\" is not implemented properly with a (DungeonsAPI) constructor.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
rewards.add((Reward) constructor.newInstance(api));
|
||||
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
|
||||
| IllegalArgumentException | InvocationTargetException exception) {
|
||||
MessageUtil.log(api, "&4Reward \"" + key + "\" is not implemented properly with a (DungeonsAPI) constructor.");
|
||||
}
|
||||
}
|
||||
return rewards;
|
||||
});
|
||||
/**
|
||||
* These commands can be used by all players if they are in the dungeon. DXL commands like /dxl leavecan be used by default.
|
||||
*/
|
||||
public static final GameRule<List<String>> GAME_COMMAND_WHITELIST = new CollectionGameRule<>(List.class, "gameCommandWhitelist", new ArrayList<>());
|
||||
public static final GameRule<List<String>> GAME_COMMAND_WHITELIST = new CollectionGameRule<>("gameCommandWhitelist", new ArrayList<>());
|
||||
/**
|
||||
* A list of permissions players get while they play the game. The permissions get removed as soon as the player leaves the game. Requires Vault and a
|
||||
* permissions plugin like PermissionsEx.
|
||||
*/
|
||||
public static final GameRule<List<String>> GAME_PERMISSIONS = new CollectionGameRule<>(List.class, "gamePermissions", new ArrayList<>());
|
||||
public static final GameRule<List<String>> GAME_PERMISSIONS = new CollectionGameRule<>("gamePermissions", new ArrayList<>());
|
||||
/**
|
||||
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
|
||||
*/
|
||||
public static final GameRule<String> TITLE = new GameRule<>(String.class, "title", null);
|
||||
public static final GameRule<String> TITLE = new GameRule<>(String.class, "title.title", null);
|
||||
/**
|
||||
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
|
||||
*/
|
||||
public static final GameRule<String> SUBTITLE = new GameRule<>(String.class, "subtitle", null);
|
||||
public static final GameRule<String> SUBTITLE = new GameRule<>(String.class, "title.subtitle", null);
|
||||
/**
|
||||
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
|
||||
*/
|
||||
public static final GameRule<String> ACTION_BAR = new GameRule<>(String.class, "actionBar", null);
|
||||
public static final GameRule<String> ACTION_BAR = new GameRule<>(String.class, "title.actionBar", null);
|
||||
/**
|
||||
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
|
||||
*/
|
||||
public static final GameRule<String> CHAT = new GameRule<>(String.class, "chat", null);
|
||||
public static final GameRule<String> CHAT = new GameRule<>(String.class, "title.chat", null);
|
||||
/**
|
||||
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
|
||||
*/
|
||||
public static final GameRule<Integer> TITLE_FADE_IN = new GameRule<>(Integer.class, "titleFadeIn", 20);
|
||||
public static final GameRule<Integer> TITLE_FADE_IN = new GameRule<>(Integer.class, "title.fadeIn", 20);
|
||||
/**
|
||||
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
|
||||
*/
|
||||
public static final GameRule<Integer> TITLE_FADE_OUT = new GameRule<>(Integer.class, "titleFadeOut", 20);
|
||||
public static final GameRule<Integer> TITLE_FADE_OUT = new GameRule<>(Integer.class, "title.fadeOut", 20);
|
||||
/**
|
||||
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
|
||||
*/
|
||||
public static final GameRule<Integer> TITLE_SHOW = new GameRule<>(Integer.class, "titleShow", 60);
|
||||
public static final GameRule<Integer> TITLE_SHOW = new GameRule<>(Integer.class, "title.show", 60);
|
||||
/**
|
||||
* Messages; also to be created with /dxl msg
|
||||
*/
|
||||
public static final GameRule<Map<Integer, String>> MESSAGES = new MapGameRule<>(Map.class, "msgs", new HashMap<>());
|
||||
public static final GameRule<Map<Integer, String>> MESSAGES = new MapGameRule<>("msgs", new HashMap<>(), (api, value) -> {
|
||||
if (!(value instanceof ConfigurationSection)) {
|
||||
return null;
|
||||
}
|
||||
ConfigurationSection section = (ConfigurationSection) value;
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : section.getValues(false).entrySet()) {
|
||||
int id = NumberUtil.parseInt(entry.getKey(), -1);
|
||||
if (id == -1) {
|
||||
continue;
|
||||
}
|
||||
if (!(entry.getValue() instanceof String)) {
|
||||
continue;
|
||||
}
|
||||
map.put(id, (String) entry.getValue());
|
||||
}
|
||||
return map;
|
||||
});
|
||||
/**
|
||||
* Items you cannot drop or destroy.
|
||||
*/
|
||||
public static final GameRule<List<ExItem>> SECURE_OBJECTS = new CollectionGameRule<>(List.class, "secureObjects", new ArrayList<>());
|
||||
public static final GameRule<Set<ExItem>> SECURE_OBJECTS = new CollectionGameRule<>("secureObjects", new HashSet<>(), ConfigReader.EX_ITEM_SET_READER);
|
||||
/**
|
||||
* If group tags are used.
|
||||
*/
|
||||
@ -269,15 +359,32 @@ public class GameRule<V> {
|
||||
}
|
||||
|
||||
protected Class<V> type;
|
||||
protected ConfigReader<V> reader;
|
||||
private String key;
|
||||
private V defaultValue;
|
||||
|
||||
/**
|
||||
* @param type the class of V
|
||||
* @param key the configuration key of the game rule
|
||||
* @param defaultValue the default value that is used when nothing is set
|
||||
*/
|
||||
public GameRule(Class<V> type, String key, V defaultValue) {
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type the class of V
|
||||
* @param key the configuration key of the game rule
|
||||
* @param defaultValue the default value that is used when nothing is set
|
||||
* @param reader a functional interface that loads the value from config
|
||||
*/
|
||||
public GameRule(Class<V> type, String key, V defaultValue, ConfigReader<V> reader) {
|
||||
this(type, key, defaultValue);
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configuration key of the game rule.
|
||||
*
|
||||
@ -312,13 +419,17 @@ public class GameRule<V> {
|
||||
* If the type of this game rule is an enum, Strings as config values that are the {@link Enum#name()} of an enum value are converted automatically.
|
||||
*
|
||||
* @param api the API instance
|
||||
* @param caliburn the CaliburnAPI instance
|
||||
* @param container the game rule container whose state is to be set
|
||||
* @param config the config to fetch the value from
|
||||
* @return the value
|
||||
*/
|
||||
public V fromConfig(DungeonsAPI api, CaliburnAPI caliburn, GameRuleContainer container, ConfigurationSection config) {
|
||||
public V fromConfig(DungeonsAPI api, GameRuleContainer container, ConfigurationSection config) {
|
||||
Object value = config.get(getKey());
|
||||
if (reader != null) {
|
||||
V v = reader.read(api, value);
|
||||
container.setState(this, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
if (Enum.class.isAssignableFrom(type)) {
|
||||
if (!(value instanceof String)) {
|
||||
|
@ -15,6 +15,7 @@
|
||||
package de.erethon.dungeonsxl.api.dungeon;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -100,6 +101,18 @@ public class GameRuleContainer {
|
||||
*/
|
||||
public void merge(GameRuleContainer subsidiary) {
|
||||
rules.entrySet().forEach(e -> e.getKey().merge(this, subsidiary, this));
|
||||
|
||||
// If we are using the last subsidiary rules (the default rules) and if blocks may be broken...
|
||||
if (subsidiary != DEFAULT_VALUES || !getState(GameRule.BREAK_BLOCKS)) {
|
||||
return;
|
||||
}
|
||||
// ...then it makes no sense to set *ProtectedEntities to default where several block-like entities (like paintings) are protected.
|
||||
if (getState(GameRule.DAMAGE_PROTECTED_ENTITIES) == DEFAULT_VALUES.getState(GameRule.DAMAGE_PROTECTED_ENTITIES)) {
|
||||
setState(GameRule.DAMAGE_PROTECTED_ENTITIES, new HashSet<>());
|
||||
}
|
||||
if (getState(GameRule.INTERACTION_PROTECTED_ENTITIES) == DEFAULT_VALUES.getState(GameRule.INTERACTION_PROTECTED_ENTITIES)) {
|
||||
setState(GameRule.INTERACTION_PROTECTED_ENTITIES, new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,9 @@
|
||||
*/
|
||||
package de.erethon.dungeonsxl.api.dungeon;
|
||||
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import java.util.Map;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
/**
|
||||
* A {@link GameRule} where the value is a {@link java.util.Map}.
|
||||
@ -26,8 +28,44 @@ import java.util.Map;
|
||||
*/
|
||||
public class MapGameRule<TK, TV, V extends Map<TK, TV>> extends GameRule<V> {
|
||||
|
||||
public MapGameRule(Class type, String key, V defaultValue) {
|
||||
super(type, key, defaultValue);
|
||||
/**
|
||||
* @param key the configuration key of the game rule
|
||||
* @param defaultValue the default value that is used when nothing is set
|
||||
* @param reader a functional interface that loads the value from config
|
||||
*/
|
||||
public MapGameRule(String key, V defaultValue, ConfigReader<V> reader) {
|
||||
super(null, key, defaultValue, reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation uses more expensive casting + catching the ClassCastException.
|
||||
* Developers should consider doing that themselves instead of wasting this cast.
|
||||
*
|
||||
* @param value the value
|
||||
* @return if the given value is an instance of {@link V}
|
||||
*/
|
||||
@Override
|
||||
public boolean isValidValue(Object value) {
|
||||
try {
|
||||
V v = (V) value;
|
||||
return true;
|
||||
} catch (ClassCastException exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public V fromConfig(DungeonsAPI api, GameRuleContainer container, ConfigurationSection config) {
|
||||
if (reader == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
V v = reader.read(api, config.getConfigurationSection(getKey()));
|
||||
if (v == null) {
|
||||
return null;
|
||||
}
|
||||
container.setState(this, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -17,7 +17,7 @@
|
||||
package de.erethon.dungeonsxl.requirement;
|
||||
|
||||
import de.erethon.commons.chat.MessageUtil;
|
||||
import de.erethon.dungeonsxl.DungeonsXL;
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import de.erethon.dungeonsxl.api.Requirement;
|
||||
import de.erethon.dungeonsxl.api.dungeon.Game;
|
||||
import de.erethon.dungeonsxl.api.dungeon.GameRule;
|
||||
@ -33,13 +33,13 @@ import org.bukkit.entity.Player;
|
||||
*/
|
||||
public class FeeLevelRequirement implements Requirement {
|
||||
|
||||
private DungeonsXL plugin;
|
||||
private DungeonsAPI api;
|
||||
|
||||
private int fee;
|
||||
private Boolean keepInventory;
|
||||
|
||||
public FeeLevelRequirement(DungeonsXL plugin) {
|
||||
this.plugin = plugin;
|
||||
public FeeLevelRequirement(DungeonsAPI api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
/* Getters and setters */
|
||||
@ -69,7 +69,7 @@ public class FeeLevelRequirement implements Requirement {
|
||||
return player.getLevel() >= fee;
|
||||
}
|
||||
|
||||
DGamePlayer dPlayer = (DGamePlayer) plugin.getPlayerCache().getGamePlayer(player);
|
||||
DGamePlayer dPlayer = (DGamePlayer) api.getPlayerCache().getGamePlayer(player);
|
||||
return dPlayer != null ? dPlayer.getData().getOldLevel() >= fee : true;
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ public class FeeLevelRequirement implements Requirement {
|
||||
player.setLevel(player.getLevel() - fee);
|
||||
|
||||
} else {
|
||||
DGamePlayer dPlayer = (DGamePlayer) plugin.getPlayerCache().getGamePlayer(player);
|
||||
DGamePlayer dPlayer = (DGamePlayer) api.getPlayerCache().getGamePlayer(player);
|
||||
if (dPlayer == null) {
|
||||
return;
|
||||
}
|
||||
@ -95,7 +95,7 @@ public class FeeLevelRequirement implements Requirement {
|
||||
return keepInventory;
|
||||
}
|
||||
|
||||
Game game = plugin.getGame(player);
|
||||
Game game = api.getGame(player);
|
||||
GameRuleContainer rules = null;
|
||||
if (game != null) {
|
||||
rules = game.getRules();
|
||||
|
@ -18,6 +18,7 @@ package de.erethon.dungeonsxl.requirement;
|
||||
|
||||
import de.erethon.commons.chat.MessageUtil;
|
||||
import de.erethon.dungeonsxl.DungeonsXL;
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import de.erethon.dungeonsxl.api.Requirement;
|
||||
import de.erethon.dungeonsxl.config.DMessage;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
@ -33,8 +34,8 @@ public class FeeMoneyRequirement implements Requirement {
|
||||
|
||||
private double fee;
|
||||
|
||||
public FeeMoneyRequirement(DungeonsXL plugin) {
|
||||
econ = plugin.getEconomyProvider();
|
||||
public FeeMoneyRequirement(DungeonsAPI api) {
|
||||
econ = ((DungeonsXL) api).getEconomyProvider();
|
||||
}
|
||||
|
||||
/* Getters and setters */
|
||||
|
@ -18,6 +18,7 @@ package de.erethon.dungeonsxl.requirement;
|
||||
|
||||
import de.erethon.caliburn.CaliburnAPI;
|
||||
import de.erethon.caliburn.item.ExItem;
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import de.erethon.dungeonsxl.api.Requirement;
|
||||
import java.util.List;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@ -33,8 +34,8 @@ public class ForbiddenItemsRequirement implements Requirement {
|
||||
|
||||
private List<ExItem> forbiddenItems;
|
||||
|
||||
public ForbiddenItemsRequirement(CaliburnAPI caliburn) {
|
||||
this.caliburn = caliburn;
|
||||
public ForbiddenItemsRequirement(DungeonsAPI api) {
|
||||
caliburn = api.getCaliburn();
|
||||
}
|
||||
|
||||
/* Getters and setters */
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
package de.erethon.dungeonsxl.requirement;
|
||||
|
||||
import de.erethon.dungeonsxl.DungeonsXL;
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import de.erethon.dungeonsxl.api.Requirement;
|
||||
import de.erethon.dungeonsxl.api.player.PlayerGroup;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@ -27,13 +27,13 @@ import org.bukkit.entity.Player;
|
||||
*/
|
||||
public class GroupSizeRequirement implements Requirement {
|
||||
|
||||
private DungeonsXL plugin;
|
||||
private DungeonsAPI api;
|
||||
|
||||
private int minimum;
|
||||
private int maximum;
|
||||
|
||||
public GroupSizeRequirement(DungeonsXL plugin) {
|
||||
this.plugin = plugin;
|
||||
public GroupSizeRequirement(DungeonsAPI api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,7 +73,7 @@ public class GroupSizeRequirement implements Requirement {
|
||||
|
||||
@Override
|
||||
public boolean check(Player player) {
|
||||
PlayerGroup group = plugin.getPlayerGroup(player);
|
||||
PlayerGroup group = api.getPlayerGroup(player);
|
||||
int size = group.getMembers().size();
|
||||
return size >= minimum && size <= maximum;
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package de.erethon.dungeonsxl.requirement;
|
||||
|
||||
import de.erethon.caliburn.CaliburnAPI;
|
||||
import de.erethon.caliburn.item.ExItem;
|
||||
import de.erethon.dungeonsxl.api.DungeonsAPI;
|
||||
import de.erethon.dungeonsxl.api.Requirement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -34,8 +35,8 @@ public class KeyItemsRequirement implements Requirement {
|
||||
|
||||
private List<ExItem> keyItems;
|
||||
|
||||
public KeyItemsRequirement(CaliburnAPI caliburn) {
|
||||
this.caliburn = caliburn;
|
||||
public KeyItemsRequirement(DungeonsAPI api) {
|
||||
caliburn = api.getCaliburn();
|
||||
}
|
||||
|
||||
/* Getters and setters */
|
||||
|
@ -38,7 +38,6 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
public class WorldConfig extends GameRuleContainer {
|
||||
|
||||
private DungeonsXL plugin;
|
||||
private CaliburnAPI caliburn;
|
||||
|
||||
private File file;
|
||||
|
||||
@ -47,7 +46,6 @@ public class WorldConfig extends GameRuleContainer {
|
||||
|
||||
public WorldConfig(DungeonsXL plugin) {
|
||||
this.plugin = plugin;
|
||||
caliburn = plugin.getCaliburn();
|
||||
}
|
||||
|
||||
public WorldConfig(DungeonsXL plugin, File file) {
|
||||
@ -67,7 +65,7 @@ public class WorldConfig extends GameRuleContainer {
|
||||
// Load & Save
|
||||
public void load(ConfigurationSection config) {
|
||||
for (GameRule rule : GameRule.VALUES) {
|
||||
rule.fromConfig(plugin, caliburn, this, config);
|
||||
rule.fromConfig(plugin, this, config);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user