Don't wrap direct type mappers

This commit is contained in:
KennyTV 2021-06-02 22:59:42 +02:00
parent 7b1f9c199a
commit fec777b349
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
4 changed files with 13 additions and 54 deletions

View File

@ -44,8 +44,7 @@ public abstract class PacketRemapper {
* @param type type to map * @param type type to map
*/ */
public void map(Type type) { public void map(Type type) {
TypeRemapper remapper = new TypeRemapper(type); handler(wrapper -> wrapper.write(type, wrapper.read(type)));
map(remapper, remapper);
} }
/** /**
@ -55,7 +54,7 @@ public abstract class PacketRemapper {
* @param newType new type * @param newType new type
*/ */
public void map(Type oldType, Type newType) { public void map(Type oldType, Type newType) {
map(new TypeRemapper(oldType), new TypeRemapper(newType)); handler(wrapper -> wrapper.write(newType, wrapper.read(oldType)));
} }
/** /**
@ -68,7 +67,7 @@ public abstract class PacketRemapper {
* @param transformer transformer to produce the new type * @param transformer transformer to produce the new type
*/ */
public <T1, T2> void map(Type<T1> oldType, Type<T2> newType, Function<T1, T2> transformer) { public <T1, T2> void map(Type<T1> oldType, Type<T2> newType, Function<T1, T2> transformer) {
map(new TypeRemapper<>(oldType), new ValueTransformer<T1, T2>(newType) { map(oldType, new ValueTransformer<T1, T2>(newType) {
@Override @Override
public T2 transform(PacketWrapper wrapper, T1 inputValue) throws Exception { public T2 transform(PacketWrapper wrapper, T1 inputValue) throws Exception {
return transformer.apply(inputValue); return transformer.apply(inputValue);
@ -99,7 +98,7 @@ public abstract class PacketRemapper {
* @param transformer transformer to produce the new type * @param transformer transformer to produce the new type
*/ */
public <T1, T2> void map(Type<T1> oldType, ValueTransformer<T1, T2> transformer) { public <T1, T2> void map(Type<T1> oldType, ValueTransformer<T1, T2> transformer) {
map(new TypeRemapper(oldType), transformer); map(new TypeRemapper<>(oldType), transformer);
} }
/** /**
@ -110,7 +109,7 @@ public abstract class PacketRemapper {
* @param <T> read/write type * @param <T> read/write type
*/ */
public <T> void map(ValueReader<T> inputReader, ValueWriter<T> outputWriter) { public <T> void map(ValueReader<T> inputReader, ValueWriter<T> outputWriter) {
valueRemappers.add(new ReadWriteValueHandler(inputReader, outputWriter)); handler(wrapper -> outputWriter.write(wrapper, inputReader.read(wrapper)));
} }
/** /**
@ -129,7 +128,7 @@ public abstract class PacketRemapper {
* @param value value to write * @param value value to write
*/ */
public <T> void create(Type<T> type, T value) { public <T> void create(Type<T> type, T value) {
valueRemappers.add(wrapper -> wrapper.write(type, value)); handler(wrapper -> wrapper.write(type, value));
} }
/** /**
@ -138,7 +137,7 @@ public abstract class PacketRemapper {
* @param type type to read * @param type type to read
*/ */
public void read(Type type) { public void read(Type type) {
valueRemappers.add(wrapper -> wrapper.read(type)); handler(wrapper -> wrapper.read(type));
} }
/** /**

View File

@ -1,40 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2021 ViaVersion and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.viaversion.viaversion.api.protocol.remapper;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
public final class ReadWriteValueHandler implements PacketHandler {
private final ValueReader reader;
private final ValueWriter writer;
public ReadWriteValueHandler(ValueReader reader, ValueWriter writer) {
this.reader = reader;
this.writer = writer;
}
@Override
public void handle(PacketWrapper wrapper) throws Exception {
writer.write(wrapper, reader.read(wrapper));
}
}

View File

@ -29,9 +29,9 @@ public interface ValueWriter<T> {
/** /**
* Write a value to a packet * Write a value to a packet
* *
* @param writer The packet wrapper to write to * @param wrapper The packet wrapper to write to
* @param inputValue The value to write * @param inputValue The value to write
* @throws Exception Throws exception if it fails to write * @throws Exception Throws exception if it fails to write
*/ */
void write(PacketWrapper writer, T inputValue) throws Exception; void write(PacketWrapper wrapper, T inputValue) throws Exception;
} }

View File

@ -119,11 +119,11 @@ public class InventoryPackets {
// Check extra bit for fast dismissal // Check extra bit for fast dismissal
if ((id & (1 << 30)) != 0 && wrapper.user().get(InventoryAcknowledgements.class).removeId(id)) { if ((id & (1 << 30)) != 0 && wrapper.user().get(InventoryAcknowledgements.class).removeId(id)) {
// Decode our requested inventory acknowledgement // Decode our requested inventory acknowledgement
int inventoryId = (id >> 16) & 0xFF; short inventoryId = (short) ((id >> 16) & 0xFF);
int confirmationId = id & 0xFFFF; short confirmationId = (short) (id & 0xFFFF);
PacketWrapper packet = wrapper.create(ServerboundPackets1_16_2.WINDOW_CONFIRMATION); PacketWrapper packet = wrapper.create(ServerboundPackets1_16_2.WINDOW_CONFIRMATION);
packet.write(Type.UNSIGNED_BYTE, (short) inventoryId); packet.write(Type.UNSIGNED_BYTE, inventoryId);
packet.write(Type.SHORT, (short) confirmationId); packet.write(Type.SHORT, confirmationId);
packet.write(Type.BYTE, (byte) 1); // Accept packet.write(Type.BYTE, (byte) 1); // Accept
packet.sendToServer(Protocol1_17To1_16_4.class); packet.sendToServer(Protocol1_17To1_16_4.class);
} }