VIEW_ANIMATED_MAPS preference can now also be "unset"

This commit is contained in:
LOOHP committed 2023-08-30 13:41:37 +01:00
1 parent 3e482b37b3
commit d6bb052d12
8 files changed
+203 -11

No files matched your search

@@ -1614,7 +1614,7 @@ public class Commands implements CommandExecutor, TabCompleter {
if ("preference".equalsIgnoreCase(args[0])) {
IFPlayerPreference<?> preference = IFPlayerPreference.valueOf(args[1].toUpperCase());
if (preference != null) {
tab.addAll(preference.getSuggestedValues());
tab.addAll(preference.getSuggestedValues().keySet());
}
}
}
@@ -38,6 +38,7 @@ import com.loohp.imageframe.objectholders.ItemFrameSelectionManager;
import com.loohp.imageframe.objectholders.MapMarkerEditManager;
import com.loohp.imageframe.objectholders.RateLimitedPacketSendingManager;
import com.loohp.imageframe.objectholders.Scheduler;
import com.loohp.imageframe.objectholders.UnsetState;
import com.loohp.imageframe.updater.Updater;
import com.loohp.imageframe.utils.ChatColorUtils;
import com.loohp.imageframe.utils.MCVersion;
@@ -166,6 +167,17 @@ public class ImageFrame extends JavaPlugin {
return restrictImageUrls.stream().anyMatch(each -> normalized.startsWith(each.toLowerCase()));
}
public static <T extends UnsetState> T getPreferenceUnsetValue(Player player, IFPlayerPreference<T> preference) {
String prefix = "imageframe.preference.unsetdefault." + preference.name().toLowerCase() + ".";
for (Map.Entry<String, T> entry : preference.getSuggestedValues().entrySet()) {
T value = entry.getValue();
if (!value.isUnset() && player.hasPermission(prefix + entry.getKey().toLowerCase())) {
return value;
}
}
return preference.getDefaultValue(ifPlayerManager.getIFPlayer(player.getUniqueId()));
}
public static String getPreferenceTranslatedName(IFPlayerPreference<?> preference) {
return preferenceNames.getOrDefault(preference, preference.name().toLowerCase());
}
@@ -168,7 +168,7 @@ public class AnimatedFakeMapManager implements Listener, Runnable {
}
for (Map.Entry<Player, List<FakeItemUtils.ItemFrameUpdateData>> entry : updateData.entrySet()) {
Player player = entry.getKey();
if (ImageFrame.ifPlayerManager.getIFPlayer(player.getUniqueId()).getPreference(IFPlayerPreference.VIEW_ANIMATED_MAPS, boolean.class)) {
if (ImageFrame.ifPlayerManager.getIFPlayer(player.getUniqueId()).getPreference(IFPlayerPreference.VIEW_ANIMATED_MAPS, BooleanState.class).getCalculatedValue(() -> ImageFrame.getPreferenceUnsetValue(player, IFPlayerPreference.VIEW_ANIMATED_MAPS).getRawValue(true))) {
if (ImageFrame.viaHook && ViaHook.isPlayerLegacy(player)) {
if (!ImageFrame.viaDisableSmoothAnimationForLegacyPlayers) {
List<FakeItemUtils.ItemFrameUpdateData> list = entry.getValue();
@@ -182,7 +182,7 @@ public class AnimatedFakeMapManager implements Listener, Runnable {
}
}
for (Player player : Bukkit.getOnlinePlayers()) {
if (ImageFrame.ifPlayerManager.getIFPlayer(player.getUniqueId()).getPreference(IFPlayerPreference.VIEW_ANIMATED_MAPS, boolean.class)) {
if (ImageFrame.ifPlayerManager.getIFPlayer(player.getUniqueId()).getPreference(IFPlayerPreference.VIEW_ANIMATED_MAPS, BooleanState.class).getCalculatedValue(() -> ImageFrame.getPreferenceUnsetValue(player, IFPlayerPreference.VIEW_ANIMATED_MAPS).getRawValue(true))) {
ItemStack mainhand = player.getEquipment().getItemInMainHand();
ItemStack offhand = player.getEquipment().getItemInOffHand();
MapView mainHandView = MapUtils.getItemMapView(mainhand);
@@ -0,0 +1,122 @@
/*
* This file is part of ImageFrame.
*
* Copyright (C) 2023. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2023. Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.loohp.imageframe.objectholders;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BooleanSupplier;
import java.util.stream.Collectors;
public enum BooleanState implements UnsetState, PreferenceState {
TRUE(new JsonPrimitive(true)),
FALSE(new JsonPrimitive(false)),
UNSET(new JsonPrimitive("unset"));
public static final Map<String, BooleanState> STRING_VALUES_MAP = Collections.unmodifiableMap(Arrays.stream(values()).collect(Collectors.toMap(e -> e.toString(), e -> e, (x, y) -> y, LinkedHashMap::new)));
private final JsonElement jsonValue;
BooleanState(JsonElement jsonValue) {
this.jsonValue = jsonValue;
}
@Override
public String toString() {
return name().toLowerCase();
}
@Override
public boolean isUnset() {
return this.equals(UNSET);
}
@Override
public JsonElement getJsonValue() {
return jsonValue;
}
public Boolean getRawValue() {
switch (this) {
case TRUE:
return Boolean.TRUE;
case FALSE:
return Boolean.FALSE;
case UNSET:
return null;
}
return null;
}
public boolean getRawValue(boolean unsetIsTrue) {
switch (this) {
case TRUE:
return Boolean.TRUE;
case FALSE:
return Boolean.FALSE;
case UNSET:
return unsetIsTrue;
}
return unsetIsTrue;
}
public boolean getCalculatedValue(BooleanSupplier unsetValueFunction) {
switch (this) {
case TRUE:
return true;
case FALSE:
return false;
case UNSET:
return unsetValueFunction.getAsBoolean();
}
return unsetValueFunction.getAsBoolean();
}
public static BooleanState fromBoolean(boolean value) {
return value ? TRUE : FALSE;
}
public static BooleanState fromBoolean(Boolean value) {
return value == null ? UNSET : fromBoolean(value.booleanValue());
}
public static BooleanState fromString(String value) {
return STRING_VALUES_MAP.getOrDefault(value.toLowerCase(), UNSET);
}
public static BooleanState fromJsonValue(JsonElement jsonValue) {
if (jsonValue.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = jsonValue.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
return fromBoolean(jsonPrimitive.getAsBoolean());
} else if (jsonPrimitive.isString()) {
return fromString(jsonPrimitive.getAsString());
}
}
return UNSET;
}
}
@@ -21,12 +21,9 @@
package com.loohp.imageframe.objectholders;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@@ -36,7 +33,7 @@ public class IFPlayerPreference<T> {
private static final Map<String, IFPlayerPreference<?>> VALUES = new ConcurrentHashMap<>();
public static final IFPlayerPreference<Boolean> VIEW_ANIMATED_MAPS = new IFPlayerPreference<>("VIEW_ANIMATED_MAPS", "viewAnimatedMaps", boolean.class, Arrays.asList("true", "false"), i -> true, v -> new JsonPrimitive(v), j -> j.getAsBoolean(), s -> StringDeserializerResult.accepted(Boolean.parseBoolean(s)));
public static final IFPlayerPreference<BooleanState> VIEW_ANIMATED_MAPS = new IFPlayerPreference<>("VIEW_ANIMATED_MAPS", "viewAnimatedMaps", BooleanState.class, BooleanState.STRING_VALUES_MAP, i -> BooleanState.UNSET, v -> v.getJsonValue(), j -> BooleanState.fromJsonValue(j), s -> StringDeserializerResult.accepted(BooleanState.fromString(s)));
static {
register(VIEW_ANIMATED_MAPS);
@@ -57,17 +54,17 @@ public class IFPlayerPreference<T> {
private final String name;
private final String jsonName;
private final Class<T> type;
private final List<String> suggestedValues;
private final Map<String, T> suggestedValues;
private final Function<IFPlayer, T> defaultValue;
private final Function<T, JsonElement> serializer;
private final Function<JsonElement, T> deserializer;
private final Function<String, StringDeserializerResult<T>> stringDeserializer;
IFPlayerPreference(String name, String jsonName, Class<T> type, List<String> suggestedValues, Function<IFPlayer, T> defaultValue, Function<T, JsonElement> serializer, Function<JsonElement, T> deserializer, Function<String, StringDeserializerResult<T>> stringDeserializer) {
IFPlayerPreference(String name, String jsonName, Class<T> type, Map<String, T> suggestedValues, Function<IFPlayer, T> defaultValue, Function<T, JsonElement> serializer, Function<JsonElement, T> deserializer, Function<String, StringDeserializerResult<T>> stringDeserializer) {
this.name = name;
this.jsonName = jsonName;
this.type = type;
this.suggestedValues = suggestedValues;
this.suggestedValues = Collections.unmodifiableMap(suggestedValues);
this.defaultValue = defaultValue;
this.serializer = serializer;
this.deserializer = deserializer;
@@ -86,7 +83,7 @@ public class IFPlayerPreference<T> {
return type;
}
public List<String> getSuggestedValues() {
public Map<String, T> getSuggestedValues() {
return suggestedValues;
}
@@ -0,0 +1,31 @@
/*
* This file is part of ImageFrame.
*
* Copyright (C) 2023. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2023. Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.loohp.imageframe.objectholders;
import com.google.gson.JsonElement;
public interface PreferenceState {
String name();
JsonElement getJsonValue();
}
@@ -0,0 +1,29 @@
/*
* This file is part of ImageFrame.
*
* Copyright (C) 2023. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2023. Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.loohp.imageframe.objectholders;
public interface UnsetState extends PreferenceState {
default boolean isUnset() {
return name().equals("UNSET");
}
}
+1
View File
@@ -72,6 +72,7 @@ Messages:
Values:
"TRUE": "&aEnabled"
"FALSE": "&cDisabled"
UNSET: "&7Unset"
UpdateMessage: "&ePlayer Preference {Preference}&e has been updated to {Value}&e!"
Settings: