This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.3.2] RadioSettings, Fixed Hmc command
- Loading branch information
1 parent
2c5aef0
commit af4ab94
Showing
10 changed files
with
253 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...main/java/me/earth/pingbypass/api/command/impl/arguments/DescriptionArgumentTypeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package me.earth.pingbypass.api.command.impl.arguments; | ||
|
||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import me.earth.pingbypass.api.traits.HasDescription; | ||
import me.earth.pingbypass.api.traits.Nameable; | ||
import me.earth.pingbypass.api.traits.Streamable; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class DescriptionArgumentTypeImpl<T extends Nameable & HasDescription> extends NameableArgumentTypeImpl<T> implements DescriptionArgumentType<T> { | ||
public DescriptionArgumentTypeImpl(Streamable<T> nameables, String type) { | ||
super(nameables, type); | ||
} | ||
|
||
@Override | ||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { | ||
return DescriptionArgumentType.super.listSuggestions(context, builder); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
pb-api/src/main/java/me/earth/pingbypass/api/setting/impl/types/RadioSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package me.earth.pingbypass.api.setting.impl.types; | ||
|
||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import lombok.Getter; | ||
import me.earth.pingbypass.api.config.ConfigType; | ||
import me.earth.pingbypass.api.config.JsonParser; | ||
import me.earth.pingbypass.api.registry.Registry; | ||
import me.earth.pingbypass.api.registry.impl.OrderedRegistryImpl; | ||
import me.earth.pingbypass.api.setting.Complexity; | ||
import me.earth.pingbypass.api.setting.impl.SettingImpl; | ||
import me.earth.pingbypass.api.traits.CanBeVisible; | ||
import me.earth.pingbypass.api.traits.Nameable; | ||
import net.minecraft.network.chat.Component; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Similar to an {@link EnumSetting}, but allows values of any type, not only Enums and thus also supports adding values later. | ||
* | ||
* @param <T> the type of object handled by this setting. | ||
*/ | ||
public class RadioSetting<T extends Nameable> extends SettingImpl<T> { | ||
@Getter | ||
private final Registry<T> values; | ||
private final Class<T> type; | ||
|
||
public RadioSetting(Function<T, Component> componentFactory, CanBeVisible visibility, Complexity complexity, | ||
ConfigType configType, String description, T defaultValue, String name, Class<T> type, LinkedHashSet<T> values, | ||
Function<Registry<T>, ArgumentType<T>> argumentType, Function<Registry<T>, JsonParser<T>> parser) { | ||
this(new OrderedRegistryImpl<>(), componentFactory, visibility, complexity, configType, description, defaultValue, name, type, values, argumentType, parser); | ||
} | ||
|
||
private RadioSetting(Registry<T> registry, Function<T, Component> componentFactory, CanBeVisible visibility, Complexity complexity, | ||
ConfigType configType, String description, T defaultValue, String name, Class<T> type, LinkedHashSet<T> values, | ||
Function<Registry<T>, ArgumentType<T>> argumentType, Function<Registry<T>, JsonParser<T>> parser) { | ||
super(componentFactory, visibility, argumentType.apply(registry), complexity, parser.apply(registry), configType, description, defaultValue, name); | ||
this.type = type; | ||
this.values = registry; | ||
values.forEach(this.values::register); | ||
} | ||
|
||
@Override | ||
public Class<T> getType() { | ||
return type; | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
pb-api/src/main/java/me/earth/pingbypass/api/setting/impl/types/RadioSettingBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package me.earth.pingbypass.api.setting.impl.types; | ||
|
||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import me.earth.pingbypass.api.command.impl.arguments.DescriptionArgumentTypeImpl; | ||
import me.earth.pingbypass.api.command.impl.arguments.NameableArgumentTypeImpl; | ||
import me.earth.pingbypass.api.config.JsonParser; | ||
import me.earth.pingbypass.api.config.impl.Parsers; | ||
import me.earth.pingbypass.api.registry.Registry; | ||
import me.earth.pingbypass.api.setting.impl.AbstractSettingBuilder; | ||
import me.earth.pingbypass.api.traits.HasDescription; | ||
import me.earth.pingbypass.api.traits.Nameable; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.function.Function; | ||
|
||
public final class RadioSettingBuilder<T extends Nameable> extends AbstractSettingBuilder<T, RadioSetting<T>, RadioSettingBuilder<T>> { | ||
private final Class<T> type; | ||
|
||
private Function<Registry<T>, ArgumentType<T>> argumentTypeFactory; | ||
private Function<Registry<T>, JsonParser<T>> parserFactory; | ||
private LinkedHashSet<T> values = new LinkedHashSet<>(); | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
public RadioSettingBuilder(Class<T> type) { | ||
this.type = type; | ||
withArgumentTypeFactory(registry -> HasDescription.class.isAssignableFrom(type) | ||
? (ArgumentType<T>) new DescriptionArgumentTypeImpl(registry, "value") | ||
: new NameableArgumentTypeImpl<>(registry, "value")) | ||
.withParserFactory(Parsers::registry) | ||
.withArgumentType(NameableArgumentTypeImpl.empty()) // just to make .verify happy | ||
.withParser(Parsers.dummy()); // just to make .verify happy | ||
} | ||
|
||
public RadioSettingBuilder<T> withArgumentTypeFactory(Function<Registry<T>, ArgumentType<T>> argumentTypeFactory) { | ||
this.argumentTypeFactory = argumentTypeFactory; | ||
return getSelf(); | ||
} | ||
|
||
public RadioSettingBuilder<T> withParserFactory(Function<Registry<T>, JsonParser<T>> parserFactory) { | ||
this.parserFactory = parserFactory; | ||
return getSelf(); | ||
} | ||
|
||
@SafeVarargs | ||
public final RadioSettingBuilder<T> withValues(T... values) { | ||
this.values = new LinkedHashSet<>(); | ||
this.values.addAll(Arrays.asList(values)); | ||
if (getDefaultValue() == null) { | ||
if (values.length > 0) { | ||
super.withValue(values[0]); | ||
} | ||
} else { | ||
this.values.add(getDefaultValue()); | ||
} | ||
|
||
return getSelf(); | ||
} | ||
|
||
@Override | ||
protected RadioSetting<T> create() { | ||
return new RadioSetting<>(getComponentFactory(), getVisibility(), getComplexity(), getConfigType(), getDescription(), | ||
getDefaultValue(), getName(), type, values, argumentTypeFactory, parserFactory); | ||
} | ||
|
||
@Override | ||
public RadioSettingBuilder<T> withValue(T value) { | ||
values.add(value); | ||
return super.withValue(value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
pb-api/src/test/java/me/earth/pingbypass/api/setting/settings/RadioSettingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package me.earth.pingbypass.api.setting.settings; | ||
|
||
import com.google.gson.JsonPrimitive; | ||
import com.mojang.brigadier.StringReader; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import me.earth.pingbypass.api.config.ConfigParseException; | ||
import me.earth.pingbypass.api.setting.impl.SettingRegistryImpl; | ||
import me.earth.pingbypass.api.setting.impl.types.RegistersSettingTypes; | ||
import me.earth.pingbypass.api.traits.Nameable; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class RadioSettingTest { | ||
@Test | ||
public void testCreate() throws CommandSyntaxException { | ||
var settingRegistry = new SettingRegistryRegisteringSettings(); | ||
var builder = settingRegistry.radioBuilder("Test", NameableValue.class, "TestDescription", NameableValue.VALUE_1, NameableValue.VALUE_3); | ||
var radioSetting = builder.build(); | ||
assertEquals("Test", radioSetting.getName()); | ||
assertEquals(NameableValue.class, radioSetting.getType()); | ||
assertEquals("TestDescription", radioSetting.getDescription()); | ||
assertEquals(NameableValue.VALUE_1, radioSetting.getDefaultValue()); | ||
assertEquals(NameableValue.VALUE_1, radioSetting.getValue()); | ||
assertTrue(radioSetting.getValues().contains("1")); | ||
assertFalse(radioSetting.getValues().contains("2")); | ||
assertTrue(radioSetting.getValues().contains("3")); | ||
radioSetting.fromJson(new JsonPrimitive("3")); | ||
assertEquals(NameableValue.VALUE_3, radioSetting.getValue()); | ||
assertThrows(ConfigParseException.class, () -> radioSetting.fromJson(new JsonPrimitive("2"))); | ||
radioSetting.getValues().register(NameableValue.VALUE_2); | ||
radioSetting.fromJson(new JsonPrimitive("2")); | ||
assertEquals(NameableValue.VALUE_2, radioSetting.getValue()); | ||
assertEquals(NameableValue.VALUE_2, radioSetting.getArgumentType().parse(new StringReader("2"))); | ||
} | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
private enum NameableValue implements Nameable { | ||
VALUE_1("1"), | ||
VALUE_2("2"), | ||
VALUE_3("3"); | ||
|
||
private final String name; | ||
} | ||
|
||
private static final class SettingRegistryRegisteringSettings extends SettingRegistryImpl implements RegistersSettingTypes { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters