Skip to content

Commit

Permalink
Merge pull request #299 from RedstoneTools/dev
Browse files Browse the repository at this point in the history
Release 2.0.0
  • Loading branch information
Matthias1590 authored Jun 24, 2023
2 parents 10e793f + c5219d8 commit f64cb94
Show file tree
Hide file tree
Showing 79 changed files with 1,572 additions and 1,040 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.parallel=true
loader_version=0.14.6

# Mod Properties
mod_version = 1.18.2-1.2.0
mod_version = 1.18.2-2.0.0
maven_group = tools.redstone
archives_base_name = redstonetools

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rip.hippo.inject.Doctor;
import rip.hippo.inject.Injector;
import tools.redstone.redstonetools.macros.WorldlessCommandHelper;
import tools.redstone.redstonetools.utils.DependencyLookup;
import tools.redstone.redstonetools.utils.ReflectionUtils;

import java.nio.file.Path;

public class RedstoneToolsClient implements ClientModInitializer {

public static final String MOD_ID = "redstonetools";
public static final String MOD_VERSION = "v" + FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow().getMetadata().getVersion().getFriendlyString();
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final Path CONFIG_DIR = FabricLoader.getInstance().getConfigDir().resolve("redstonetools");
public static final Injector INJECTOR = Doctor.createInjector(ReflectionUtils.getModules());

@Override
Expand All @@ -24,12 +29,14 @@ public void onInitializeClient() {

// Register features
ReflectionUtils.getFeatures().forEach(feature -> {
LOGGER.trace("Registering feature {}", feature);
LOGGER.trace("Registering feature {}", feature.getClass().getName());

if (feature.requiresWorldEdit() && !DependencyLookup.WORLDEDIT_PRESENT) {
LOGGER.warn("Feature {} requires WorldEdit, but WorldEdit is not loaded. Skipping registration.", feature.getName());
return;
}
feature.register();
});

// should call the "static" block
WorldlessCommandHelper.dummyNetworkHandler.getCommandDispatcher();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory;
import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry;
import net.minecraft.world.GameRules;
import tools.redstone.redstonetools.utils.DependencyLookup;


public class RedstoneToolsGameRules {
Expand All @@ -14,6 +15,9 @@ private RedstoneToolsGameRules() {

public static void register() {
DO_CONTAINER_DROPS = GameRuleRegistry.register("doContainerDrops", GameRules.Category.DROPS, GameRuleFactory.createBooleanRule(true));
DO_BLOCK_UPDATES_AFTER_EDIT = GameRuleRegistry.register("doBlockUpdatesAfterEdit", GameRules.Category.UPDATES, GameRuleFactory.createBooleanRule(false));

if (DependencyLookup.WORLDEDIT_PRESENT) {
DO_BLOCK_UPDATES_AFTER_EDIT = GameRuleRegistry.register("doBlockUpdatesAfterEdit", GameRules.Category.UPDATES, GameRuleFactory.createBooleanRule(false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,47 @@
import net.minecraft.server.command.ServerCommandSource;

public abstract class AbstractFeature {
private final Feature feature;

private final Feature featureInfo;
private final String id;

{
feature = getClass().getAnnotation(Feature.class);
featureInfo = getClass().getAnnotation(Feature.class);

if (feature == null) {
if (featureInfo == null) {
throw new IllegalStateException("Feature " + getClass() + " is not annotated with @Feature");
}

String id = featureInfo.id();
if (id.isEmpty()) {
// derive id from name
// Air Place -> airplace
id = featureInfo.name()
.toLowerCase()
.replace(" ", "");
}

this.id = id;
}

public String getID() {
return id;
}

public String getName() {
return feature.name();
return featureInfo.name();
}

public String getDescription() {
return feature.description();
return featureInfo.description();
}

public String getCommand() {
return feature.command();
return featureInfo.command();
}

public boolean requiresWorldEdit() {
return featureInfo.worldedit();
}

/**
Expand All @@ -35,4 +56,5 @@ public void register() {
}

protected abstract void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Feature {
String id() default "";
String name();
String description();
String command();
boolean worldedit() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Argument<T> {
private String name;
private final TypeSerializer<T, ?> type;
private boolean optional = false;
private T value;
private volatile T value;
private T defaultValue;

private Argument(TypeSerializer<T, ?> type) {
Expand All @@ -21,10 +21,15 @@ public static <T> Argument<T> ofType(TypeSerializer<T, ?> type) {
public Argument<T> withDefault(T defaultValue) {
optional = true;
this.defaultValue = defaultValue;
this.value = defaultValue; // for options, temporary

return this;
}

public T getDefaultValue() {
return defaultValue;
}

public Argument<T> named(String name) {
this.name = name;

Expand Down Expand Up @@ -52,7 +57,7 @@ public boolean isOptional() {
}

@SuppressWarnings("unchecked")
public void setValue(CommandContext<?> context) {
public void updateValue(CommandContext<?> context) {
try {
value = (T) context.getArgument(name, Object.class);
} catch (IllegalArgumentException e) {
Expand All @@ -64,6 +69,10 @@ public void setValue(CommandContext<?> context) {
}
}

public void setValue(T value) {
this.value = value;
}

public T getValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import tools.redstone.redstonetools.utils.ColoredBlockType;

public class ColoredBlockTypeSerializer extends EnumSerializer<ColoredBlockType> {
private static final ColoredBlockTypeSerializer INSTANCE = new ColoredBlockTypeSerializer();

private ColoredBlockTypeSerializer() {
super(ColoredBlockType.class);
}

public static ColoredBlockTypeSerializer coloredBlockType() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tools.redstone.redstonetools.features.arguments;
package tools.redstone.redstonetools.features.arguments.serializers;

import tools.redstone.redstonetools.features.arguments.serializers.EnumSerializer;
import tools.redstone.redstonetools.utils.DirectionArgument;

public class DirectionSerializer extends EnumSerializer<DirectionArgument> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,28 @@ public T deserialize(String serialized) {
}

private T deserializeUnchecked(String serialized) {
boolean isNegative = false;
if (serialized.length() == 1) {
return tryParse(serialized);
}

if(serialized.charAt(0) == '-' && serialized.chars().filter(ch -> ch == '-').count() == 1){
isNegative = true;
serialized = serialized.replace("-","");
}

if (serialized.charAt(0) == '0') {
var prefixedBase = serialized.substring(0, 2);
var number = serialized.substring(2);

// TODO(Refactor): Write a NumberBase.fromCharacter method instead of this that iterates of the NumberBases (add the char to the NumberBase constructor)
var numberBase = switch (prefixedBase.charAt(1)) {
case 'b' -> NumberBase.BINARY;
case 'o' -> NumberBase.OCTAL;
case 'd' -> NumberBase.DECIMAL;
case 'x' -> NumberBase.HEXADECIMAL;
default -> null;
};

if (numberBase != null) {
return tryParse(number, numberBase.toInt());
if(serialized.length() > 1) {
var prefixedBase = serialized.substring(0, 2);
var number = serialized.substring(2);

var numberBase = NumberBase.fromPrefix(prefixedBase).orElse(null);

if (numberBase != null) {
return isNegative ? tryParse("-" + number, numberBase.toInt()) : tryParse(number, numberBase.toInt());
}
} else {
return tryParse(serialized,10);
}
}

Expand All @@ -90,10 +93,10 @@ private T deserializeUnchecked(String serialized) {
throw new IllegalArgumentException("Invalid base '" + parts[1] + "'.");
}

return tryParse(number, base);
return isNegative ? tryParse("-"+number, base) : tryParse(number, base);
}

return tryParse(serialized);
return isNegative ? tryParse("-"+serialized) : tryParse(serialized);
}

private T tryParse(String string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import tools.redstone.redstonetools.utils.NumberArg;

import java.util.Optional;

public class NumberSerializer extends IntLikeSerializer<NumberArg> {
private static final NumberSerializer INSTANCE = new NumberSerializer(null,null);

public static NumberSerializer numberArg(){
return INSTANCE;
}

public static NumberSerializer numberArg(NumberArg min) {
return new NumberSerializer(min, null);
}

public static NumberSerializer numberArg(NumberArg min, NumberArg max) {
return new NumberSerializer(min, max);
}

private NumberSerializer(NumberArg min, NumberArg max){
super(NumberArg.class,min, max);
}

@Override
protected Optional<NumberArg> tryParseOptional(String string, int radix) {
try {
return Optional.of(new NumberArg(string, radix));
} catch (NumberFormatException ignored) {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import tools.redstone.redstonetools.utils.SignalBlock;

public class SignalBlockSerializer extends EnumSerializer<SignalBlock> {
private static final SignalBlockSerializer INSTANCE = new SignalBlockSerializer();

private SignalBlockSerializer() {
super(SignalBlock.class);
}

public static SignalBlockSerializer signalBlock() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ protected TypeSerializer(Class<T> clazz) {
this.clazz = clazz;
}

public Class<T> getTypeClass() {
return clazz;
}

/* ArgumentType impl */
@Override
public final T parse(StringReader reader) throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@
import tools.redstone.redstonetools.features.Feature;
import tools.redstone.redstonetools.features.arguments.Argument;
import tools.redstone.redstonetools.features.feedback.Feedback;
import tools.redstone.redstonetools.utils.NumberArg;

import java.math.BigInteger;

import static tools.redstone.redstonetools.features.arguments.serializers.BigIntegerSerializer.bigInteger;
import static tools.redstone.redstonetools.features.arguments.serializers.NumberBaseSerializer.numberBase;
import static tools.redstone.redstonetools.features.arguments.serializers.NumberSerializer.numberArg;

@AutoService(AbstractFeature.class)
@Feature(name = "Base Convert", description = "Converts a number from one base to another.", command = "base")
public class BaseConvertFeature extends CommandFeature {

public static final Argument<BigInteger> number = Argument
.ofType(bigInteger());
public static final Argument<NumberArg> inputNum = Argument
.ofType(numberArg());
public static final Argument<Integer> toBase = Argument
.ofType(numberBase())
.withDefault(10);

@Override
protected Feedback execute(ServerCommandSource source) {
var output = number.getValue().toString(toBase.getValue());
var input = inputNum.getValue().toPrefixedString();
var output = inputNum.getValue().toPrefixedString(toBase.getValue());

return Feedback.success(output);
return Feedback.success("{} = {}", input, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static tools.redstone.redstonetools.features.arguments.serializers.NumberBaseSerializer.numberBase;

@AutoService(AbstractFeature.class)
@Feature(name = "Binary Block Read", description = "Interprets your WorldEdit selection as a binary number.", command = "/read")
@Feature(name = "Binary Block Read", description = "Interprets your WorldEdit selection as a binary number.", command = "/read", worldedit = true)
public class BinaryBlockReadFeature extends CommandFeature {
private static final BlockStateArgument LIT_LAMP_ARG = new BlockStateArgument(
Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, true),
Expand Down Expand Up @@ -67,7 +67,7 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep
var spacingVector = direction.multiply(offset.getValue());

if (direction.getBlockX() + direction.getBlockY() + direction.getBlockZ() > 1) {
return Feedback.invalidUsage("The selection must have 2 axis the same");
return Feedback.invalidUsage("The selection must have 2 axis the same.");
}

var bits = new StringBuilder();
Expand Down Expand Up @@ -95,7 +95,7 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep
}

var output = Integer.toString(Integer.parseInt(bits.toString(), 2), toBase.getValue());
return Feedback.success(output);
return Feedback.success("{}.", output);
}

}
Loading

0 comments on commit f64cb94

Please sign in to comment.