Fix game rule merging

This commit is contained in:
Daniel Saukel 2020-03-18 01:12:11 +01:00
parent 767a5eddaf
commit f8ea5eff69
3 changed files with 41 additions and 8 deletions

View File

@ -83,8 +83,24 @@ 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 write = writeTo.getState(this);
write.addAll(subsidiary.getState(this));
write.addAll(overriding.getState(this)); V subsidiaryState = subsidiary.getState(this);
if (subsidiaryState != null) {
if (write == null) {
write = subsidiaryState;
} else {
write.addAll(subsidiaryState);
}
}
V overridingState = overriding.getState(this);
if (overridingState != null) {
if (write == null) {
write = overridingState;
} else {
write.addAll(overridingState);
}
}
writeTo.setState(this, write); writeTo.setState(this, write);
} }

View File

@ -36,10 +36,13 @@ public class GameRuleContainer {
} }
} }
private Map<GameRule<?>, Object> rules;
/** /**
* Initializes an emtpy GameRuleContainer. * Initializes an emtpy GameRuleContainer.
*/ */
public GameRuleContainer() { public GameRuleContainer() {
rules = new HashMap<>();
} }
/** /**
@ -51,8 +54,6 @@ public class GameRuleContainer {
rules = new HashMap<>(container.rules); rules = new HashMap<>(container.rules);
} }
private Map<GameRule<?>, Object> rules = new HashMap<>();
/** /**
* Returns the state of the GameRule or UNDEFINED_STATE if it is not defined * Returns the state of the GameRule or UNDEFINED_STATE if it is not defined
* *
@ -86,7 +87,7 @@ public class GameRuleContainer {
} }
/** /**
* Removes the rule from the map sothat a subsidiary provider can set it. * Removes the rule from the map sothat a subsidiary container can set it.
* *
* @param rule the GameRule to unset * @param rule the GameRule to unset
*/ */
@ -100,7 +101,7 @@ public class GameRuleContainer {
* @param subsidiary the GameRules that override the values that are null. * @param subsidiary the GameRules that override the values that are null.
*/ */
public void merge(GameRuleContainer subsidiary) { public void merge(GameRuleContainer subsidiary) {
rules.entrySet().forEach(e -> e.getKey().merge(this, subsidiary, this)); 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 we are using the last subsidiary rules (the default rules) and if blocks may be broken...
if (subsidiary != DEFAULT_VALUES || !getState(GameRule.BREAK_BLOCKS)) { if (subsidiary != DEFAULT_VALUES || !getState(GameRule.BREAK_BLOCKS)) {

View File

@ -71,8 +71,24 @@ 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 write = writeTo.getState(this);
write.putAll(subsidiary.getState(this));
write.putAll(overriding.getState(this)); V subsidiaryState = subsidiary.getState(this);
if (subsidiaryState != null) {
if (write == null) {
write = subsidiaryState;
} else {
write.putAll(subsidiaryState);
}
}
V overridingState = overriding.getState(this);
if (overridingState != null) {
if (write == null) {
write = overridingState;
} else {
write.putAll(overridingState);
}
}
writeTo.setState(this, write); writeTo.setState(this, write);
} }