clean some implementation code, implement new via changes, fixed mod dump, 0.2.2-SNAPSHOT, fix semver

This commit is contained in:
creeper123123321 2020-04-16 13:39:00 -03:00
parent bd08e8dc68
commit 1d2170e161
11 changed files with 144 additions and 85 deletions

View File

@ -9,11 +9,11 @@ plugins {
group = "com.github.creeper123123321.viafabric" group = "com.github.creeper123123321.viafabric"
val gitVersion: groovy.lang.Closure<Any> by extra val gitVersion: groovy.lang.Closure<Any> by extra
version = "0.2.1-SNAPSHOT+" + try { version = "0.2.2-SNAPSHOT+" + try {
gitVersion() gitVersion()
} catch (e: Exception) { } catch (e: Exception) {
"unknown" "unknown"
} + "+1.14-1.15" } + "-mc-1.14-1.15"
extra.set("archivesBaseName", "ViaFabric") extra.set("archivesBaseName", "ViaFabric")
description = "Client-side and server-side ViaVersion implementation for Fabric" description = "Client-side and server-side ViaVersion implementation for Fabric"

View File

@ -24,10 +24,8 @@
package com.github.creeper123123321.viafabric.commands; package com.github.creeper123123321.viafabric.commands;
import net.fabricmc.api.EnvType; import io.github.cottonmc.clientcommands.CottonClientCommandSource;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientCommandSource;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.server.command.CommandSource; import net.minecraft.server.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
@ -38,7 +36,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import java.util.UUID; import java.util.UUID;
public class NMSCommandSender implements ViaCommandSender { public class NMSCommandSender implements ViaCommandSender {
private CommandSource source; private final CommandSource source;
public NMSCommandSender(CommandSource source) { public NMSCommandSender(CommandSource source) {
this.source = source; this.source = source;
@ -54,9 +52,8 @@ public class NMSCommandSender implements ViaCommandSender {
public void sendMessage(String s) { public void sendMessage(String s) {
if (source instanceof ServerCommandSource) { if (source instanceof ServerCommandSource) {
((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), false); ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), false);
} else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { } else if (source instanceof CottonClientCommandSource) {
MinecraftClient.getInstance().player ((CottonClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), false);
.sendMessage(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)));
} }
} }
@ -65,7 +62,7 @@ public class NMSCommandSender implements ViaCommandSender {
if (source instanceof ServerCommandSource) { if (source instanceof ServerCommandSource) {
Entity entity = ((ServerCommandSource) source).getEntity(); Entity entity = ((ServerCommandSource) source).getEntity();
if (entity != null) return entity.getUuid(); if (entity != null) return entity.getUuid();
} else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { } else if (source instanceof CottonClientCommandSource) {
return MinecraftClient.getInstance().player.getUuid(); return MinecraftClient.getInstance().player.getUuid();
} }
return UUID.fromString(getName()); return UUID.fromString(getName());
@ -75,7 +72,7 @@ public class NMSCommandSender implements ViaCommandSender {
public String getName() { public String getName() {
if (source instanceof ServerCommandSource) { if (source instanceof ServerCommandSource) {
return ((ServerCommandSource) source).getName(); return ((ServerCommandSource) source).getName();
} else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { } else if (source instanceof CottonClientCommandSource) {
return MinecraftClient.getInstance().player.getEntityName(); return MinecraftClient.getInstance().player.getEntityName();
} }
return "?"; return "?";

View File

@ -31,13 +31,12 @@ import io.netty.handler.codec.MessageToMessageDecoder;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List; import java.util.List;
public class VRDecodeHandler extends MessageToMessageDecoder<ByteBuf> { public class VRDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private UserConnection user; private final UserConnection user;
public VRDecodeHandler(UserConnection user) { public VRDecodeHandler(UserConnection user) {
this.user = user; this.user = user;
@ -68,9 +67,6 @@ public class VRDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx); super.channelInactive(ctx);
ProtocolInfo info = user.get(ProtocolInfo.class); Via.getManager().handleDisconnect(user);
if (info.getUuid() != null) {
Via.getManager().removePortedClient(info.getUuid());
}
} }
} }

View File

@ -35,7 +35,7 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List; import java.util.List;
public class VREncodeHandler extends MessageToMessageEncoder<ByteBuf> { public class VREncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private UserConnection user; private final UserConnection user;
public VREncodeHandler(UserConnection user) { public VREncodeHandler(UserConnection user) {
this.user = user; this.user = user;

View File

@ -70,9 +70,6 @@ public class FabricDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx); // May call decode super.channelInactive(ctx); // May call decode
ProtocolInfo pi = user.get(ProtocolInfo.class); Via.getManager().handleDisconnect(user);
if (pi.getUuid() != null) {
Via.getManager().removePortedClient(pi.getUuid());
}
} }
} }

View File

@ -144,7 +144,7 @@ public abstract class MixinMultiplayerScreen extends Screen {
} }
@Inject(method = "render", at = { @Inject(method = "render", at = {
@At(value = "INVOKE", target = "Lnet/minecraft/client/gui/Screen;render(IIF)V"), @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;render(IIF)V"),
@At(value = "INVOKE", target = "Lnet/minecraft/class_437;render(IIF)V") // todo check if refmap was fixed @At(value = "INVOKE", target = "Lnet/minecraft/class_437;render(IIF)V") // todo check if refmap was fixed
}, remap = false) }, remap = false)
private void onRender(int int_1, int int_2, float float_1, CallbackInfo ci) { private void onRender(int int_1, int int_2, float float_1, CallbackInfo ci) {

View File

@ -28,7 +28,7 @@ import us.myles.ViaVersion.api.boss.BossColor;
import us.myles.ViaVersion.api.boss.BossStyle; import us.myles.ViaVersion.api.boss.BossStyle;
import us.myles.ViaVersion.boss.CommonBoss; import us.myles.ViaVersion.boss.CommonBoss;
public class VRBossBar extends CommonBoss { public class VRBossBar extends CommonBoss<Void> {
public VRBossBar(String title, float health, BossColor color, BossStyle style) { public VRBossBar(String title, float health, BossColor color, BossStyle style) {
super(title, health, color, style); super(title, health, color, style);
} }

View File

@ -0,0 +1,48 @@
/*
* 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.platform;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.ViaConnectionManager;
public class VRConnectionManager extends ViaConnectionManager {
@Override
public void onLoginSuccess(UserConnection connection) {
if (connection instanceof VRClientSideUserConnection) {
this.connections.add(connection);
} else {
super.onLoginSuccess(connection);
}
}
@Override
public void onDisconnect(UserConnection connection) {
if (connection instanceof VRClientSideUserConnection) {
this.connections.remove(connection);
} else {
super.onDisconnect(connection);
}
}
}

View File

@ -49,6 +49,7 @@ import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaConnectionManager;
import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
@ -62,19 +63,24 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class VRPlatform implements ViaPlatform { public class VRPlatform implements ViaPlatform<UUID> {
private VRViaConfig config; private final VRViaConfig config;
private File dataFolder; private final File dataFolder;
private final ViaConnectionManager connectionManager;
private final ViaAPI<UUID> api;
public VRPlatform() { public VRPlatform() {
Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric"); Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric");
config = new VRViaConfig(configDir.resolve("viaversion.yml").toFile()); config = new VRViaConfig(configDir.resolve("viaversion.yml").toFile());
dataFolder = configDir.toFile(); dataFolder = configDir.toFile();
connectionManager = new VRConnectionManager();
api = new VRViaAPI();
} }
public static MinecraftServer getServer() { public static MinecraftServer getServer() {
@ -127,22 +133,23 @@ public class VRPlatform implements ViaPlatform {
@Override @Override
public TaskId runSync(Runnable runnable) { public TaskId runSync(Runnable runnable) {
// Kick task needs to be on main thread if (getServer() != null) {
Executor executor = ViaFabric.EVENT_LOOP; return runServerSync(runnable);
boolean alreadyLogged;
MinecraftServer server = getServer();
if (server != null) {
alreadyLogged = true;
executor = server;
} else { } else {
alreadyLogged = false; return runEventLoop(runnable);
} }
}
private TaskId runServerSync(Runnable runnable) {
// Kick task needs to be on main thread
return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer()));
}
private TaskId runEventLoop(Runnable runnable) {
return new FutureTaskId( return new FutureTaskId(
CompletableFuture.runAsync(runnable, executor) CompletableFuture.runAsync(runnable, ViaFabric.EVENT_LOOP)
.exceptionally(throwable -> { .exceptionally(throwable -> {
if (!alreadyLogged) {
throwable.printStackTrace(); throwable.printStackTrace();
}
return null; return null;
}) })
); );
@ -187,32 +194,39 @@ public class VRPlatform implements ViaPlatform {
public ViaCommandSender[] getOnlinePlayers() { public ViaCommandSender[] getOnlinePlayers() {
MinecraftServer server = getServer(); MinecraftServer server = getServer();
if (server != null && server.isOnThread()) { if (server != null && server.isOnThread()) {
// Not thread safe return getServerPlayers();
return server.getPlayerManager().getPlayerList().stream() }
.map(Entity::getCommandSource) return Via.getManager().getConnectedClients().values().stream()
.map(NMSCommandSender::new) .map(UserCommandSender::new)
.toArray(ViaCommandSender[]::new); .toArray(ViaCommandSender[]::new);
} }
return Via.getManager().getPortedPlayers().values().stream()
.map(UserCommandSender::new) private ViaCommandSender[] getServerPlayers() {
return getServer().getPlayerManager().getPlayerList().stream()
.map(Entity::getCommandSource)
.map(NMSCommandSender::new)
.toArray(ViaCommandSender[]::new); .toArray(ViaCommandSender[]::new);
} }
@Override @Override
public void sendMessage(UUID uuid, String s) { public void sendMessage(UUID uuid, String s) {
UserConnection user = Via.getManager().getPortedPlayers().get(uuid); UserConnection user = Via.getManager().getConnection(uuid);
if (user instanceof VRClientSideUserConnection) { if (user instanceof VRClientSideUserConnection) {
sendMessageClient(s); sendMessageClient(s);
} else { } else {
runSync(() -> { sendMessageServer(uuid, s);
}
}
private void sendMessageServer(UUID uuid, String s) {
MinecraftServer server = getServer(); MinecraftServer server = getServer();
if (server == null) return; if (server == null) return;
runServerSync(() -> {
ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid);
if (player == null) return; if (player == null) return;
player.sendChatMessage(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), MessageType.SYSTEM); player.sendChatMessage(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), MessageType.SYSTEM);
}); });
} }
}
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
private void sendMessageClient(String s) { private void sendMessageClient(String s) {
@ -229,19 +243,13 @@ public class VRPlatform implements ViaPlatform {
@Override @Override
public boolean kickPlayer(UUID uuid, String s) { public boolean kickPlayer(UUID uuid, String s) {
UserConnection user = Via.getManager().getPortedPlayers().get(uuid); UserConnection user = Via.getManager().getConnection(uuid);
if (user instanceof VRClientSideUserConnection) { if (user instanceof VRClientSideUserConnection) {
return kickClient(s); return kickClient(s);
} else { } else {
MinecraftServer server = getServer(); return kickServer(uuid, s);
if (server != null && server.isOnThread()) {
ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid);
if (player == null) return false;
player.networkHandler.disconnect(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)));
} }
} }
return false;
}
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
private boolean kickClient(String msg) { private boolean kickClient(String msg) {
@ -258,14 +266,32 @@ public class VRPlatform implements ViaPlatform {
return false; return false;
} }
private boolean kickServer(UUID uuid, String s) {
MinecraftServer server = getServer();
if (server == null) return false;
Supplier<Boolean> kickTask = () -> {
ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid);
if (player == null) return false;
player.networkHandler.disconnect(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)));
return true;
};
if (server.isOnThread()) {
return kickTask.get();
} else {
ViaFabric.JLOGGER.log(Level.WARNING, "Weird!? Player kicking was called off-thread", new Throwable());
runServerSync(kickTask::get);
}
return false; // Can't know if it worked
}
@Override @Override
public boolean isPluginEnabled() { public boolean isPluginEnabled() {
return true; return true;
} }
@Override @Override
public ViaAPI getApi() { public ViaAPI<UUID> getApi() {
return new VRViaAPI(); return api;
} }
@Override @Override
@ -294,11 +320,12 @@ public class VRPlatform implements ViaPlatform {
List<PluginInfo> mods = new ArrayList<>(); List<PluginInfo> mods = new ArrayList<>();
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
mods.add(new PluginInfo(true, mods.add(new PluginInfo(true,
mod.getMetadata().getName(), mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")",
mod.getMetadata().getVersion().getFriendlyString(), mod.getMetadata().getVersion().getFriendlyString(),
null, null,
mod.getMetadata().getAuthors().stream() mod.getMetadata().getAuthors().stream()
.map(info -> info.getName() + "(" + info.getContact().asMap() + ")") .map(info -> info.getName()
+ (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap()))
.collect(Collectors.toList()) .collect(Collectors.toList())
)); ));
} }
@ -311,4 +338,9 @@ public class VRPlatform implements ViaPlatform {
public boolean isOldClientsAllowed() { public boolean isOldClientsAllowed() {
return true; return true;
} }
@Override
public ViaConnectionManager getConnectionManager() {
return connectionManager;
}
} }

View File

@ -38,15 +38,10 @@ import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.UUID; import java.util.UUID;
public class VRViaAPI implements ViaAPI<Void> { public class VRViaAPI implements ViaAPI<UUID> {
@Override
public int getPlayerVersion(Void o) {
throw new UnsupportedOperationException();
}
@Override @Override
public int getPlayerVersion(UUID uuid) { public int getPlayerVersion(UUID uuid) {
UserConnection con = Via.getManager().getPortedPlayers().get(uuid); UserConnection con = Via.getManager().getConnection(uuid);
if (con != null) { if (con != null) {
return con.get(ProtocolInfo.class).getProtocolVersion(); return con.get(ProtocolInfo.class).getProtocolVersion();
} }
@ -58,9 +53,8 @@ public class VRViaAPI implements ViaAPI<Void> {
} }
@Override @Override
@Deprecated public boolean isInjected(UUID uuid) {
public boolean isPorted(UUID uuid) { return Via.getManager().isClientConnected(uuid);
return Via.getManager().getPortedPlayers().containsKey(uuid);
} }
@Override @Override
@ -68,24 +62,19 @@ public class VRViaAPI implements ViaAPI<Void> {
return Via.getPlatform().getPluginVersion(); return Via.getPlatform().getPluginVersion();
} }
@Override
public void sendRawPacket(Void o, ByteBuf byteBuf) throws IllegalArgumentException {
throw new UnsupportedOperationException();
}
@Override @Override
public void sendRawPacket(UUID uuid, ByteBuf byteBuf) throws IllegalArgumentException { public void sendRawPacket(UUID uuid, ByteBuf byteBuf) throws IllegalArgumentException {
UserConnection ci = Via.getManager().getPortedPlayers().get(uuid); UserConnection ci = Via.getManager().getConnection(uuid);
ci.sendRawPacket(byteBuf); ci.sendRawPacket(byteBuf);
} }
@Override @Override
public BossBar createBossBar(String s, BossColor bossColor, BossStyle bossStyle) { public BossBar<Void> createBossBar(String s, BossColor bossColor, BossStyle bossStyle) {
return new VRBossBar(s, 1f, bossColor, bossStyle); return new VRBossBar(s, 1f, bossColor, bossStyle);
} }
@Override @Override
public BossBar createBossBar(String s, float v, BossColor bossColor, BossStyle bossStyle) { public BossBar<Void> createBossBar(String s, float v, BossColor bossColor, BossStyle bossStyle) {
return new VRBossBar(s, v, bossColor, bossStyle); return new VRBossBar(s, v, bossColor, bossStyle);
} }

View File

@ -29,7 +29,7 @@ import us.myles.ViaVersion.api.platform.TaskId;
import java.util.concurrent.Future; import java.util.concurrent.Future;
public class FutureTaskId implements TaskId { public class FutureTaskId implements TaskId {
private Future<?> object; private final Future<?> object;
public FutureTaskId(Future<?> object) { public FutureTaskId(Future<?> object) {
this.object = object; this.object = object;