Fix parsing of command cooldowns with invalid data (#4212)

This commit is contained in:
Josh Roy 2021-06-08 16:51:47 -04:00 committed by GitHub
parent a28d0823b2
commit ecea17195a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 2 deletions

View File

@ -2,10 +2,12 @@ package com.earth2me.essentials.config;
import com.earth2me.essentials.config.annotations.DeleteIfIncomplete;
import com.earth2me.essentials.config.annotations.DeleteOnEmpty;
import com.earth2me.essentials.config.entities.CommandCooldown;
import com.earth2me.essentials.config.entities.LazyLocation;
import com.earth2me.essentials.config.processors.DeleteIfIncompleteProcessor;
import com.earth2me.essentials.config.processors.DeleteOnEmptyProcessor;
import com.earth2me.essentials.config.serializers.BigDecimalTypeSerializer;
import com.earth2me.essentials.config.serializers.CommandCooldownSerializer;
import com.earth2me.essentials.config.serializers.LocationTypeSerializer;
import com.earth2me.essentials.config.serializers.MaterialTypeSerializer;
import net.ess3.api.InvalidWorldException;
@ -55,6 +57,7 @@ public class EssentialsConfiguration {
.register(BigDecimal.class, new BigDecimalTypeSerializer())
.register(LazyLocation.class, new LocationTypeSerializer())
.register(Material.class, new MaterialTypeSerializer())
.register(CommandCooldown.class, new CommandCooldownSerializer())
.build();
private final AtomicInteger pendingWrites = new AtomicInteger(0);

View File

@ -1,11 +1,9 @@
package com.earth2me.essentials.config.entities;
import com.earth2me.essentials.config.processors.DeleteIfIncompleteProcessor;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import java.util.regex.Pattern;
@ConfigSerializable
public class CommandCooldown implements DeleteIfIncompleteProcessor.IncompleteEntity {
private Pattern pattern;

View File

@ -0,0 +1,40 @@
package com.earth2me.essentials.config.serializers;
import com.earth2me.essentials.config.entities.CommandCooldown;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
import java.util.regex.Pattern;
public class CommandCooldownSerializer implements TypeSerializer<CommandCooldown> {
@Override
public CommandCooldown deserialize(Type type, ConfigurationNode node) throws SerializationException {
try {
final Pattern pattern = node.node("pattern").get(Pattern.class);
if (node.node("value").isNull()) {
return null;
}
final Long longValue = node.node("value").getLong();
final CommandCooldown cooldown = new CommandCooldown();
cooldown.pattern(pattern);
cooldown.value(longValue);
return cooldown;
} catch (final SerializationException ignored) {
}
return null;
}
@Override
public void serialize(Type type, @Nullable CommandCooldown obj, ConfigurationNode node) throws SerializationException {
if (obj == null || obj.isIncomplete()) {
node.raw(null);
return;
}
node.node("pattern").set(Pattern.class, obj.pattern());
node.node("value").set(Long.class, obj.value());
}
}