3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 08:30:09 +01:00

Java 8, FunctionalInterface remapper

Dieser Commit ist enthalten in:
Gerrygames 2019-05-13 12:43:38 +02:00
Ursprung e8f8473e1c
Commit 18f7459070
8 geänderte Dateien mit 54 neuen und 11 gelöschten Zeilen

Datei anzeigen

@ -3,17 +3,18 @@ package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.exception.InformativeException;
public abstract class PacketHandler implements ValueWriter { @FunctionalInterface
public interface PacketHandler extends ValueWriter {
/** /**
* Handle a packet * Handle a packet
* *
* @param wrapper The associated wrapper * @param wrapper The associated wrapper
* @throws Exception Throws exception if it failed to handle the packet * @throws Exception Throws exception if it failed to handle the packet
*/ */
public abstract void handle(PacketWrapper wrapper) throws Exception; void handle(PacketWrapper wrapper) throws Exception;
@Override @Override
public void write(PacketWrapper writer, Object inputValue) throws Exception { default void write(PacketWrapper writer, Object inputValue) throws Exception {
try { try {
handle(writer); handle(writer);
} catch (InformativeException e) { } catch (InformativeException e) {

Datei anzeigen

@ -7,6 +7,7 @@ import us.myles.ViaVersion.exception.InformativeException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function;
public abstract class PacketRemapper { public abstract class PacketRemapper {
private final List<Pair<ValueReader, ValueWriter>> valueRemappers = new ArrayList<>(); private final List<Pair<ValueReader, ValueWriter>> valueRemappers = new ArrayList<>();
@ -35,6 +36,36 @@ public abstract class PacketRemapper {
map(new TypeRemapper(oldType), new TypeRemapper(newType)); map(new TypeRemapper(oldType), new TypeRemapper(newType));
} }
/**
* Map a type from an old type to a transformed new type.
*
* @param oldType The old type
* @param <T1> The old return type.
* @param newType The new type
* @param <T2> The new return type.
* @param transformer The transformer to use to produce the new type.
*/
public <T1, T2> void map(Type<T1> oldType, Type<T2> newType, Function<T1, T2> transformer) {
map(new TypeRemapper<>(oldType), new ValueTransformer<T1, T2>(newType) {
@Override
public T2 transform(PacketWrapper wrapper, T1 inputValue) throws Exception {
return transformer.apply(inputValue);
}
});
}
/**
* Map a type from an old type to a transformed new type.
*
* @param <T1> The old return type.
* @param transformer The transformer to use to produce the new type.
* @param <T2> The new return type.
*/
public <T1, T2> void map(ValueTransformer<T1, T2> transformer) {
if (transformer.getInputType() == null) throw new IllegalArgumentException("Use map(Type<T1>, ValueTransformer<T1, T2>) for value transformers without specified input type!");
map(transformer.getInputType(), transformer);
}
/** /**
* Map a type from an old type to a transformed new type. * Map a type from an old type to a transformed new type.
* *
@ -55,7 +86,7 @@ public abstract class PacketRemapper {
* @param <T> The return type * @param <T> The return type
*/ */
public <T> void map(ValueReader<T> inputReader, ValueWriter<T> outputWriter) { public <T> void map(ValueReader<T> inputReader, ValueWriter<T> outputWriter) {
valueRemappers.add(new Pair<ValueReader, ValueWriter>(inputReader, outputWriter)); valueRemappers.add(new Pair<>(inputReader, outputWriter));
} }
/** /**

Datei anzeigen

@ -3,17 +3,18 @@ package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.exception.InformativeException;
public abstract class ValueCreator implements ValueWriter { @FunctionalInterface
public interface ValueCreator extends ValueWriter {
/** /**
* Write new values to a Packet. * Write new values to a Packet.
* *
* @param wrapper The packet to write to * @param wrapper The packet to write to
* @throws Exception Throws exception if it fails to write. * @throws Exception Throws exception if it fails to write.
*/ */
public abstract void write(PacketWrapper wrapper) throws Exception; void write(PacketWrapper wrapper) throws Exception;
@Override @Override
public void write(PacketWrapper writer, Object inputValue) throws Exception { default void write(PacketWrapper writer, Object inputValue) throws Exception {
try { try {
write(writer); write(writer);
} catch (InformativeException e) { } catch (InformativeException e) {

Datei anzeigen

@ -2,6 +2,7 @@ package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
@FunctionalInterface
public interface ValueReader<T> { public interface ValueReader<T> {
/** /**
* Reads value from a PacketWrapper * Reads value from a PacketWrapper

Datei anzeigen

@ -1,16 +1,24 @@
package us.myles.ViaVersion.api.remapper; package us.myles.ViaVersion.api.remapper;
import lombok.Getter;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.exception.InformativeException;
public abstract class ValueTransformer<T1, T2> implements ValueWriter<T1> { public abstract class ValueTransformer<T1, T2> implements ValueWriter<T1> {
@Getter
private final Type<T1> inputType;
private final Type<T2> outputType; private final Type<T2> outputType;
public ValueTransformer(Type<T2> outputType) { public ValueTransformer(Type<T1> inputType, Type<T2> outputType) {
this.inputType = inputType;
this.outputType = outputType; this.outputType = outputType;
} }
public ValueTransformer(Type<T2> outputType) {
this(null, outputType);
}
/** /**
* Transform a value from one type to another * Transform a value from one type to another
* *

Datei anzeigen

@ -2,6 +2,7 @@ package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
@FunctionalInterface
public interface ValueWriter<T> { public interface ValueWriter<T> {
/** /**
* Write a value to a packet * Write a value to a packet

Datei anzeigen

@ -5,7 +5,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker;
public class PlayerMovementMapper extends PacketHandler { public class PlayerMovementMapper implements PacketHandler {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
MovementTracker tracker = wrapper.user().get(MovementTracker.class); MovementTracker tracker = wrapper.user().get(MovementTracker.class);

Datei anzeigen

@ -34,8 +34,8 @@
<projectEncoding>UTF-8</projectEncoding> <projectEncoding>UTF-8</projectEncoding>
<project.build.sourceEncoding>${projectEncoding}</project.build.sourceEncoding> <project.build.sourceEncoding>${projectEncoding}</project.build.sourceEncoding>
<project.build.outputEncoding>${projectEncoding}</project.build.outputEncoding> <project.build.outputEncoding>${projectEncoding}</project.build.outputEncoding>
<maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
</properties> </properties>
<repositories> <repositories>