use ClientSideReference

This commit is contained in:
Gerrygames 2019-04-11 19:34:34 +02:00
parent 2e27ce0ce9
commit bdfe47c1c2
3 changed files with 53 additions and 98 deletions

View File

@ -1,96 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2018 creeper123123321 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.github.creeper123123321.viafabric.mixin.client;
import net.fabricmc.fabric.impl.network.PacketTypes;
import net.minecraft.client.network.packet.CustomPayloadS2CPacket;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
import net.minecraft.util.PacketByteBuf;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import us.myles.ViaVersion.api.Via;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
@Mixin(CustomPayloadS2CPacket.class)
public class MixinCustomPayloadPacket {
@Inject(method = "read", at = @At(value = "HEAD", target = "Lnet/minecraft/network/Packet;read(Lnet/minecraft/util/PacketByteBuf;)V"))
private void onRead(PacketByteBuf buf, CallbackInfo callbackInfo) throws IOException {
try {
int readerIndex = buf.readerIndex();
Identifier identifier = buf.readIdentifier();
buf.readerIndex(readerIndex);
if (identifier.equals(PacketTypes.REGISTER) || identifier.equals(PacketTypes.UNREGISTER)) {
Set<Identifier> identifiers = new HashSet();
StringBuilder builder = new StringBuilder();
while (buf.readerIndex() < buf.writerIndex()) {
char c = (char) buf.readByte();
if (c == 0 || buf.readableBytes() == 0) {
String s = builder.toString();
if (!s.isEmpty()) {
try {
identifiers.add(new Identifier(s));
} catch (InvalidIdentifierException ex) {
Via.getPlatform().getLogger().info("Ignoring invalid custom payload identifier in (un)register:\n" + ex.getMessage());
}
}
builder = new StringBuilder();
} else {
builder.append(c);
}
}
buf.clear();
buf.writeIdentifier(identifier);
boolean first = true;
for (Identifier id : identifiers) {
if (!first) {
buf.writeByte(0);
} else {
first = false;
}
buf.writeBytes(id.toString().getBytes(StandardCharsets.US_ASCII));
}
}
} catch (InvalidIdentifierException ex) {
Via.getPlatform().getLogger().warning("Ignoring invalid custom payload identifier:\n" + ex.getMessage());
buf.clear();
buf.writeString("viafabric:invalid");
}
}
}

View File

@ -24,12 +24,64 @@
package com.github.creeper123123321.viafabric.protocol;
import com.google.common.base.Joiner;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
public class ClientSideReference extends Protocol {
@Override
protected void registerPackets() {
// Plugin Message
registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String channel = wrapper.passthrough(Type.STRING);
try {
new Identifier(channel);
} catch (InvalidIdentifierException ex) {
Via.getPlatform().getLogger().warning("Ignoring invalid custom payload identifier: " + ex.getMessage());
wrapper.set(Type.STRING, 0, "viafabric:invalid");
return;
}
if (channel.equals("minecraft:register") || channel.equals("minecraft:unregister")) {
String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0");
List<String> filteredChannels = new LinkedList<>();
for (String c : channels) {
try {
new Identifier(c);
} catch (InvalidIdentifierException ex) {
Via.getPlatform().getLogger().warning("Ignoring invalid custom payload identifier in " + channel + ": " + ex.getMessage());
continue;
}
filteredChannels.add(c);
}
wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(filteredChannels).getBytes(StandardCharsets.UTF_8));
}
}
});
}
});
}
@Override

View File

@ -8,8 +8,7 @@
],
"client": [
"client.MixinClientConnectionChInit",
"client.MixinMultiplayerScreen",
"client.MixinCustomPayloadPacket"
"client.MixinMultiplayerScreen"
],
"injectors": {
"defaultRequire": 1