This commit is contained in:
creeper123123321 2020-12-05 17:26:18 -03:00
parent bac7e86217
commit daa291b2b8
3 changed files with 147 additions and 6 deletions

View File

@ -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

View File

@ -37,7 +37,7 @@ public class VRViaConfig extends AbstractViaConfig {
// Based on Sponge ViaVersion
private static List<String> 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;

View File

@ -0,0 +1,133 @@
/*
* MIT License
*
* Copyright (c) 2018- creeper123123321 <https://creeper123123321.keybase.pub/>
* Copyright (c) 2019- contributors <https://github.com/ViaVersion/ViaFabric/graphs/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<UUID, Item> 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;
}
}