Fix game rule merging

This commit is contained in:
Daniel Saukel 2020-04-11 02:01:52 +02:00
parent 539d3da8a9
commit 79ba04add4
4 changed files with 85 additions and 33 deletions

View File

@ -27,21 +27,27 @@ import org.bukkit.configuration.ConfigurationSection;
*/ */
public class CollectionGameRule<T, V extends Collection<T>> extends GameRule<V> { public class CollectionGameRule<T, V extends Collection<T>> extends GameRule<V> {
protected Copier<V> copier;
/** /**
* @param key the configuration key of the game rule * @param key the configuration key of the game rule
* @param defaultValue the default value that is used when nothing is set * @param defaultValue the default value that is used when nothing is set
* @param copier a method to copy the collection
*/ */
public CollectionGameRule(String key, V defaultValue) { public CollectionGameRule(String key, V defaultValue, Copier<V> copier) {
super(null, key, defaultValue); super(null, key, defaultValue);
this.copier = copier;
} }
/** /**
* @param key the configuration key of the game rule * @param key the configuration key of the game rule
* @param defaultValue the default value that is used when nothing is set * @param defaultValue the default value that is used when nothing is set
* @param reader a functional interface that loads the value from config * @param reader a functional interface that loads the value from config
* @param copier a method to copy the collection
*/ */
public CollectionGameRule(String key, V defaultValue, ConfigReader<V> reader) { public CollectionGameRule(String key, V defaultValue, ConfigReader<V> reader, Copier<V> copier) {
super(null, key, defaultValue, reader); super(null, key, defaultValue, reader);
this.copier = copier;
} }
/** /**
@ -82,25 +88,30 @@ public class CollectionGameRule<T, V extends Collection<T>> extends GameRule<V>
@Override @Override
public void merge(GameRuleContainer overriding, GameRuleContainer subsidiary, GameRuleContainer writeTo) { public void merge(GameRuleContainer overriding, GameRuleContainer subsidiary, GameRuleContainer writeTo) {
V write = writeTo.getState(this); V writeToState = writeTo.getState(this);
V write = writeToState != null ? copier.copy(writeTo.getState(this)) : null;
if (subsidiary != writeTo) {
V subsidiaryState = subsidiary.getState(this); V subsidiaryState = subsidiary.getState(this);
if (subsidiaryState != null) { if (subsidiaryState != null) {
if (write == null) { if (write == null) {
write = subsidiaryState; write = copier.copy(subsidiaryState);
} else { } else {
write.addAll(subsidiaryState); write.addAll(subsidiaryState);
} }
} }
}
if (overriding != writeTo) {
V overridingState = overriding.getState(this); V overridingState = overriding.getState(this);
if (overridingState != null) { if (overridingState != null) {
if (write == null) { if (write == null) {
write = overridingState; write = copier.copy(overridingState);
} else { } else {
write.addAll(overridingState); write.addAll(overridingState);
} }
} }
}
writeTo.setState(this, write); writeTo.setState(this, write);
} }

View File

@ -0,0 +1,34 @@
/*
* 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;
/**
* Copies an object.
*
* @param <T> the type of the object to copy
* @author Daniel Saukel
*/
@FunctionalInterface
public interface Copier<T> {
/**
* Returns a copy of the original.
*
* @param original the original
* @return a copy of the original
*/
T copy(T original);
}

View File

@ -117,7 +117,7 @@ public class GameRule<V> {
map.put(tool, blocks); map.put(tool, blocks);
} }
return map; return map;
}); }, HashMap::new);
/** /**
* 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 * 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. * will be protected by default. If this is left out and if breakBlocks is true, nothing will be protected by default.
@ -126,7 +126,7 @@ public class GameRule<V> {
VanillaMob.ARMOR_STAND, VanillaMob.ARMOR_STAND,
VanillaMob.ITEM_FRAME, VanillaMob.ITEM_FRAME,
VanillaMob.PAINTING VanillaMob.PAINTING
)), ConfigReader.EX_MOB_SET_READER); )), ConfigReader.EX_MOB_SET_READER, HashSet::new);
/** /**
* 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 * 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. * true, nothing will be protected by default.
@ -134,7 +134,7 @@ public class GameRule<V> {
public static final GameRule<Set<ExMob>> INTERACTION_PROTECTED_ENTITIES = new CollectionGameRule<>("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.ARMOR_STAND,
VanillaMob.ITEM_FRAME VanillaMob.ITEM_FRAME
)), ConfigReader.EX_MOB_SET_READER); )), ConfigReader.EX_MOB_SET_READER, HashSet::new);
/** /**
* If blocks may be placed. * If blocks may be placed.
*/ */
@ -142,7 +142,7 @@ public class GameRule<V> {
/** /**
* A whitelist of placeable blocks. placeBlocks is supposed to be set to "true" if this should be used. * 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<>("placeWhitelist", null, ConfigReader.EX_ITEM_SET_READER); public static final GameRule<Set<ExItem>> PLACE_WHITELIST = new CollectionGameRule<>("placeWhitelist", null, ConfigReader.EX_ITEM_SET_READER, HashSet::new);
/** /**
* If it should rain permanently in the dungeon. * If it should rain permanently in the dungeon.
* <p> * <p>
@ -244,15 +244,15 @@ public class GameRule<V> {
} }
} }
return requirements; return requirements;
}); }, ArrayList::new);
/** /**
* One of these Dungeons must be finished ("any" for any dungeon). * One of these Dungeons must be finished ("any" for any dungeon).
*/ */
public static final GameRule<List<String>> MUST_FINISH_ONE = new CollectionGameRule<>("mustFinishOne", null); public static final GameRule<List<String>> MUST_FINISH_ONE = new CollectionGameRule<>("mustFinishOne", null, ArrayList::new);
/** /**
* All of these Dungeons must be finished. If you do not want any, leave this empty. * 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<>("mustFinishAll", null); public static final GameRule<List<String>> MUST_FINISH_ALL = new CollectionGameRule<>("mustFinishAll", null, ArrayList::new);
/** /**
* This can be used to give rewards. The default implementation does not do this at the moment. * This can be used to give rewards. The default implementation does not do this at the moment.
*/ */
@ -277,23 +277,25 @@ public class GameRule<V> {
continue; continue;
} }
} }
rewards.add((Reward) constructor.newInstance(api)); Reward reward = (Reward) constructor.newInstance(api);
// reward.setup();
rewards.add(reward);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException exception) { | IllegalArgumentException | InvocationTargetException exception) {
MessageUtil.log(api, "&4Reward \"" + key + "\" is not implemented properly with a (DungeonsAPI) constructor."); MessageUtil.log(api, "&4Reward \"" + key + "\" is not implemented properly with a (DungeonsAPI) constructor.");
} }
} }
return rewards; return rewards;
}); }, ArrayList::new);
/** /**
* These commands can be used by all players if they are in the dungeon. DXL commands like /dxl leavecan be used by default. * 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<>("gameCommandWhitelist", new ArrayList<>()); public static final GameRule<List<String>> GAME_COMMAND_WHITELIST = new CollectionGameRule<>("gameCommandWhitelist", new ArrayList<>(), ArrayList::new);
/** /**
* 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 * 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. * permissions plugin like PermissionsEx.
*/ */
public static final GameRule<List<String>> GAME_PERMISSIONS = new CollectionGameRule<>("gamePermissions", new ArrayList<>()); public static final GameRule<List<String>> GAME_PERMISSIONS = new CollectionGameRule<>("gamePermissions", new ArrayList<>(), ArrayList::new);
/** /**
* Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work. * Use this to replace the default ready / new floor message. If titles are deactivated in the main config, this is not going to work.
*/ */
@ -342,11 +344,11 @@ public class GameRule<V> {
map.put(id, (String) entry.getValue()); map.put(id, (String) entry.getValue());
} }
return map; return map;
}); }, HashMap::new);
/** /**
* Items you cannot drop or destroy. * Items you cannot drop or destroy.
*/ */
public static final GameRule<Set<ExItem>> SECURE_OBJECTS = new CollectionGameRule<>("secureObjects", new HashSet<>(), ConfigReader.EX_ITEM_SET_READER); public static final GameRule<Set<ExItem>> SECURE_OBJECTS = new CollectionGameRule<>("secureObjects", new HashSet<>(), ConfigReader.EX_ITEM_SET_READER, HashSet::new);
/** /**
* If group tags are used. * If group tags are used.
*/ */
@ -481,7 +483,7 @@ public class GameRule<V> {
@Override @Override
public String toString() { public String toString() {
return "GameRule{key=" + key + "}"; return getClass().getSimpleName() + "{key=" + key + "}";
} }
} }

View File

@ -28,13 +28,17 @@ import org.bukkit.configuration.ConfigurationSection;
*/ */
public class MapGameRule<TK, TV, V extends Map<TK, TV>> extends GameRule<V> { public class MapGameRule<TK, TV, V extends Map<TK, TV>> extends GameRule<V> {
protected Copier<V> copier;
/** /**
* @param key the configuration key of the game rule * @param key the configuration key of the game rule
* @param defaultValue the default value that is used when nothing is set * @param defaultValue the default value that is used when nothing is set
* @param reader a functional interface that loads the value from config * @param reader a functional interface that loads the value from config
* @param copier a method to copy the map
*/ */
public MapGameRule(String key, V defaultValue, ConfigReader<V> reader) { public MapGameRule(String key, V defaultValue, ConfigReader<V> reader, Copier<V> copier) {
super(null, key, defaultValue, reader); super(null, key, defaultValue, reader);
this.copier = copier;
} }
/** /**
@ -70,12 +74,13 @@ public class MapGameRule<TK, TV, V extends Map<TK, TV>> extends GameRule<V> {
@Override @Override
public void merge(GameRuleContainer overriding, GameRuleContainer subsidiary, GameRuleContainer writeTo) { public void merge(GameRuleContainer overriding, GameRuleContainer subsidiary, GameRuleContainer writeTo) {
V write = writeTo.getState(this); V writeToState = writeTo.getState(this);
V write = writeToState != null ? copier.copy(writeTo.getState(this)) : null;
V subsidiaryState = subsidiary.getState(this); V subsidiaryState = subsidiary.getState(this);
if (subsidiaryState != null) { if (subsidiaryState != null) {
if (write == null) { if (write == null) {
write = subsidiaryState; write = copier.copy(subsidiaryState);
} else { } else {
write.putAll(subsidiaryState); write.putAll(subsidiaryState);
} }
@ -84,7 +89,7 @@ public class MapGameRule<TK, TV, V extends Map<TK, TV>> extends GameRule<V> {
V overridingState = overriding.getState(this); V overridingState = overriding.getState(this);
if (overridingState != null) { if (overridingState != null) {
if (write == null) { if (write == null) {
write = overridingState; write = copier.copy(overridingState);
} else { } else {
write.putAll(overridingState); write.putAll(overridingState);
} }