Java 8, FunctionalInterface remapper

This commit is contained in:
Gerrygames 2019-05-13 12:43:38 +02:00
parent e8f8473e1c
commit 18f7459070
8 changed files with 54 additions and 11 deletions

View File

@ -3,17 +3,18 @@ package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.exception.InformativeException;
public abstract class PacketHandler implements ValueWriter {
@FunctionalInterface
public interface PacketHandler extends ValueWriter {
/**
* Handle a packet
*
* @param wrapper The associated wrapper
* @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
public void write(PacketWrapper writer, Object inputValue) throws Exception {
default void write(PacketWrapper writer, Object inputValue) throws Exception {
try {
handle(writer);
} catch (InformativeException e) {

View File

@ -7,6 +7,7 @@ import us.myles.ViaVersion.exception.InformativeException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public abstract class PacketRemapper {
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 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.
*
@ -55,7 +86,7 @@ public abstract class PacketRemapper {
* @param <T> The return type
*/
public <T> void map(ValueReader<T> inputReader, ValueWriter<T> outputWriter) {
valueRemappers.add(new Pair<ValueReader, ValueWriter>(inputReader, outputWriter));
valueRemappers.add(new Pair<>(inputReader, outputWriter));
}
/**

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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