From daa291b2b8d7c114f2905314686c6e2bb23306aa Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Sat, 5 Dec 2020 17:26:18 -0300 Subject: [PATCH] Implement #70 --- .../viafabric/platform/VRLoader.java | 13 ++ .../viafabric/platform/VRViaConfig.java | 7 +- .../providers/VRHandItemProvider.java | 133 ++++++++++++++++++ 3 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/github/creeper123123321/viafabric/providers/VRHandItemProvider.java diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRLoader.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRLoader.java index 7e8c67c..a9bed41 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRLoader.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRLoader.java @@ -25,11 +25,15 @@ package com.github.creeper123123321.viafabric.platform; +import com.github.creeper123123321.viafabric.providers.VRHandItemProvider; import com.github.creeper123123321.viafabric.providers.VRVersionProvider; +import net.fabricmc.api.EnvType; +import net.fabricmc.loader.api.FabricLoader; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; import us.myles.ViaVersion.bungee.providers.BungeeMovementTransmitter; import us.myles.ViaVersion.protocols.base.VersionProvider; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; public class VRLoader implements ViaPlatformLoader { @@ -37,6 +41,15 @@ public class VRLoader implements ViaPlatformLoader { public void load() { Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BungeeMovementTransmitter()); Via.getManager().getProviders().use(VersionProvider.class, new VRVersionProvider()); + + if (Via.getPlatform().getConf().isItemCache()) { + VRHandItemProvider handProvider = new VRHandItemProvider(); + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + handProvider.registerClientTick(); + } + handProvider.registerServerTick(); + Via.getManager().getProviders().use(HandItemProvider.class, handProvider); + } } @Override diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java index cdac054..95b1665 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java @@ -37,7 +37,7 @@ public class VRViaConfig extends AbstractViaConfig { // Based on Sponge ViaVersion private static List UNSUPPORTED = Arrays.asList("anti-xray-patch", "bungee-ping-interval", "bungee-ping-save", "bungee-servers", "quick-move-action-fix", "nms-player-ticking", - "item-cache", "velocity-ping-interval", "velocity-ping-save", "velocity-servers", + "velocity-ping-interval", "velocity-ping-save", "velocity-servers", "blockconnection-method", "change-1_9-hitbox", "change-1_14-hitbox"); public VRViaConfig(File configFile) { @@ -66,11 +66,6 @@ public class VRViaConfig extends AbstractViaConfig { return false; } - @Override - public boolean isItemCache() { - return false; - } - @Override public boolean isNMSPlayerTicking() { return false; diff --git a/src/main/java/com/github/creeper123123321/viafabric/providers/VRHandItemProvider.java b/src/main/java/com/github/creeper123123321/viafabric/providers/VRHandItemProvider.java new file mode 100644 index 0000000..7d42866 --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viafabric/providers/VRHandItemProvider.java @@ -0,0 +1,133 @@ +/* + * MIT License + * + * Copyright (c) 2018- creeper123123321 + * Copyright (c) 2019- 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.providers; + +import com.github.creeper123123321.viafabric.ViaFabric; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.event.world.WorldTickCallback; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.item.ItemStack; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.World; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +public class VRHandItemProvider extends HandItemProvider { + public Item clientItem = null; + public Map serverPlayers = new ConcurrentHashMap<>(); + + @Override + public Item getHandItem(UserConnection info) { + Item serverItem; + if (info.isClientSide()) { + return getClientItem(); + } else if ((serverItem = serverPlayers.get(info.getProtocolInfo().getUuid())) != null) { + return new Item(serverItem); + } + return super.getHandItem(info); + } + + private Item getClientItem() { + if (clientItem == null) { + return new Item(0, (byte) 0, (short) 0, null); + } + return new Item(clientItem); + } + + @Environment(EnvType.CLIENT) + public void registerClientTick() { + try { + ClientTickEvents.END_WORLD_TICK.register(clientWorld -> tickClient()); + } catch (NoClassDefFoundError ignored) { + try { + WorldTickCallback.EVENT.register(world -> { + if (world.isClient) { + tickClient(); + } + }); + } catch (NoClassDefFoundError ignored2) { + ViaFabric.JLOGGER.info("Fabric Lifecycle V0/V1 isn't installed"); + } + } + } + + public void registerServerTick() { + try { + ServerTickEvents.END_WORLD_TICK.register(this::tickServer); + } catch (NoClassDefFoundError ignored) { + WorldTickCallback.EVENT.register(world -> { + if (!world.isClient) { + tickServer(world); + } + }); + } + } + + private void tickClient() { + ClientPlayerEntity p = MinecraftClient.getInstance().player; + if (p != null) { + clientItem = fromNative(p.inventory.getMainHandStack()); + } + } + + private void tickServer(World world) { + serverPlayers.clear(); + world.getPlayers().forEach(it -> serverPlayers + .put(it.getUuid(), fromNative(it.inventory.getMainHandStack()))); + } + + private Item fromNative(ItemStack original) { + int id = swordId(Registry.ITEM.getId(original.getItem()).toString()); + return new Item(id, (byte) original.getCount(), (short) original.getDamage(), null); + } + + private int swordId(String id) { + // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 + switch (id) { + case "minecraft:iron_sword": + return 267; + case "minecraft:wooden_sword": + return 268; + case "minecraft:golden_sword": + return 272; + case "minecraft:diamond_sword": + return 276; + case "minecraft:stone_sword": + return 283; + } + return 0; + } +}