From 18f7459070a21ea75223661048b61160239bfc27 Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Mon, 13 May 2019 12:43:38 +0200 Subject: [PATCH] Java 8, FunctionalInterface remapper --- .../api/remapper/PacketHandler.java | 7 ++-- .../api/remapper/PacketRemapper.java | 33 ++++++++++++++++++- .../ViaVersion/api/remapper/ValueCreator.java | 7 ++-- .../ViaVersion/api/remapper/ValueReader.java | 1 + .../api/remapper/ValueTransformer.java | 10 +++++- .../ViaVersion/api/remapper/ValueWriter.java | 1 + .../PlayerMovementMapper.java | 2 +- pom.xml | 4 +-- 8 files changed, 54 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketHandler.java b/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketHandler.java index c6088252b..93b0fd9a3 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketHandler.java @@ -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) { diff --git a/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketRemapper.java b/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketRemapper.java index 354f05614..c895bda2e 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketRemapper.java +++ b/common/src/main/java/us/myles/ViaVersion/api/remapper/PacketRemapper.java @@ -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> 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 The old return type. + * @param newType The new type + * @param The new return type. + * @param transformer The transformer to use to produce the new type. + */ + public void map(Type oldType, Type newType, Function transformer) { + map(new TypeRemapper<>(oldType), new ValueTransformer(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 The old return type. + * @param transformer The transformer to use to produce the new type. + * @param The new return type. + */ + public void map(ValueTransformer transformer) { + if (transformer.getInputType() == null) throw new IllegalArgumentException("Use map(Type, ValueTransformer) 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 The return type */ public void map(ValueReader inputReader, ValueWriter outputWriter) { - valueRemappers.add(new Pair(inputReader, outputWriter)); + valueRemappers.add(new Pair<>(inputReader, outputWriter)); } /** diff --git a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueCreator.java b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueCreator.java index fa5e5dead..c24f236a2 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueCreator.java +++ b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueCreator.java @@ -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) { diff --git a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueReader.java b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueReader.java index 8ee042ba2..85490f397 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueReader.java +++ b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueReader.java @@ -2,6 +2,7 @@ package us.myles.ViaVersion.api.remapper; import us.myles.ViaVersion.api.PacketWrapper; +@FunctionalInterface public interface ValueReader { /** * Reads value from a PacketWrapper diff --git a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueTransformer.java b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueTransformer.java index a1652c383..78b73cd98 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueTransformer.java +++ b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueTransformer.java @@ -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 implements ValueWriter { + @Getter + private final Type inputType; private final Type outputType; - public ValueTransformer(Type outputType) { + public ValueTransformer(Type inputType, Type outputType) { + this.inputType = inputType; this.outputType = outputType; } + public ValueTransformer(Type outputType) { + this(null, outputType); + } + /** * Transform a value from one type to another * diff --git a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueWriter.java b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueWriter.java index 39cc4d570..a24915f03 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueWriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/remapper/ValueWriter.java @@ -2,6 +2,7 @@ package us.myles.ViaVersion.api.remapper; import us.myles.ViaVersion.api.PacketWrapper; +@FunctionalInterface public interface ValueWriter { /** * Write a value to a packet diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/PlayerMovementMapper.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/PlayerMovementMapper.java index e653e9e4e..ed080865f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/PlayerMovementMapper.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/PlayerMovementMapper.java @@ -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); diff --git a/pom.xml b/pom.xml index ee74971f7..caca6654e 100644 --- a/pom.xml +++ b/pom.xml @@ -34,8 +34,8 @@ UTF-8 ${projectEncoding} ${projectEncoding} - 1.7 - 1.7 + 1.8 + 1.8