Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions paper-api/src/main/java/org/bukkit/GameRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,14 @@ private static UnaryOperator<Boolean> inverseBool() {
@NotNull
public abstract Class<T> getType();

/**
* Get the default value of this rule.
*
* @return the default value
*/
@NotNull
public abstract T getDefaultValue();

/**
* Get a {@link GameRule} by its name.
*
Expand Down
10 changes: 6 additions & 4 deletions paper-api/src/main/java/org/bukkit/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -3799,8 +3799,7 @@ default void playSound(@NotNull Entity entity, @NotNull String sound, float volu
* @param <T> the GameRule's type
* @return the current value
*/
@Nullable
public <T> T getGameRuleValue(@NotNull GameRule<T> rule);
public @NotNull <T> T getGameRuleValue(@NotNull GameRule<T> rule);

/**
* Get the default value for a given {@link GameRule}. This value is not
Expand All @@ -3809,9 +3808,12 @@ default void playSound(@NotNull Entity entity, @NotNull String sound, float volu
* @param rule the rule to return a default value for
* @param <T> the type of GameRule
* @return the default value
* @deprecated use {@link GameRule#getDefaultValue()} instead
*/
@Nullable
public <T> T getGameRuleDefault(@NotNull GameRule<T> rule);
@Deprecated(since = "1.21.11")
default <T> @NotNull T getGameRuleDefault(@NotNull GameRule<T> rule) {
return rule.getDefaultValue();
}

/**
* Set the given {@link GameRule}'s new value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,25 @@ public Class<T> getType() {
};
}

@Override
public T getDefaultValue() {
return shimLegacyValue(this.getHandle().defaultValue(), this);
}

@Override
public String translationKey() {
return this.getHandle().getDescriptionId();
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> T shimLegacyValue(T value, GameRule<?> rule){
if (rule instanceof CraftGameRule.LegacyGameRuleWrapper wrapper) {
return (T) wrapper.getToLegacyFromModern().apply(value);
}

return value;
}

public static class LegacyGameRuleWrapper<LEGACY, MODERN> extends CraftGameRule<LEGACY> {

private final Class<LEGACY> typeOverride;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1694,30 +1694,12 @@ public boolean isGameRule(String rule) {
return this.getHandle().getGameRules().rules.has(CraftGameRule.bukkitToMinecraft(bukkit));
}

public static <T> T shimLegacyValue(T value, org.bukkit.GameRule<?> gameRule){
//noinspection rawtypes unchecked
if (gameRule instanceof CraftGameRule.LegacyGameRuleWrapper legacyGameRuleWrapper) {
//noinspection unchecked
return (T) legacyGameRuleWrapper.getToLegacyFromModern().apply(value);
}

return value;
}

@Override
public <T> @Nullable T getGameRuleValue(org.bukkit.@NotNull GameRule<T> rule) {
public <T> @NotNull T getGameRuleValue(org.bukkit.@NotNull GameRule<T> rule) {
Preconditions.checkArgument(rule != null, "GameRule cannot be null");

T value = this.getHandle().getGameRules().get(CraftGameRule.bukkitToMinecraft(rule));
return shimLegacyValue(value, rule);
}

@Override
public <T> @Nullable T getGameRuleDefault(org.bukkit.@NotNull GameRule<T> rule) {
Preconditions.checkArgument(rule != null, "GameRule cannot be null");
T value = CraftGameRule.bukkitToMinecraft(rule).defaultValue();

return shimLegacyValue(value, rule);
return CraftGameRule.shimLegacyValue(value, rule);
}

@Override
Expand Down
Loading