2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
|
|
|
* Copyright (C) 2016-2021 ViaVersion and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.protocols.protocol1_9to1_8.packets;
|
2016-03-13 18:01:36 +01:00
|
|
|
|
2017-05-14 14:34:51 +02:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
|
|
|
import com.viaversion.viaversion.api.minecraft.Position;
|
|
|
|
import com.viaversion.viaversion.api.minecraft.chunks.Chunk1_8;
|
|
|
|
import com.viaversion.viaversion.api.minecraft.item.Item;
|
|
|
|
import com.viaversion.viaversion.api.protocol.Protocol;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
2021-04-26 21:16:10 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
|
|
|
|
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
|
|
|
|
import com.viaversion.viaversion.api.protocol.remapper.ValueCreator;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.type.Type;
|
2021-05-02 10:12:37 +02:00
|
|
|
import com.viaversion.viaversion.api.type.types.CustomByteType;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
|
2021-05-02 10:12:37 +02:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ServerboundPackets1_9;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.Effect;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.SoundEffect;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.types.Chunk1_9to1_8Type;
|
2021-04-27 13:41:39 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2021-05-02 10:12:37 +02:00
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2016-03-13 18:01:36 +01:00
|
|
|
|
2019-09-21 11:57:28 +02:00
|
|
|
import java.util.Optional;
|
2016-09-29 16:33:14 +02:00
|
|
|
|
2016-03-13 18:01:36 +01:00
|
|
|
public class WorldPackets {
|
|
|
|
public static void register(Protocol protocol) {
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.UPDATE_SIGN, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
2016-03-14 13:40:23 +01:00
|
|
|
map(Type.POSITION); // 0 - Sign Position
|
2019-04-23 16:34:17 +02:00
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Sign Line (json)
|
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 2 - Sign Line (json)
|
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 3 - Sign Line (json)
|
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 4 - Sign Line (json)
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.EFFECT, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.INT); // 0 - Effect ID
|
2016-04-02 15:46:30 +02:00
|
|
|
map(Type.POSITION); // 1 - Position
|
|
|
|
map(Type.INT); // 2 - Data
|
|
|
|
map(Type.BOOLEAN); // 3 - Disable relative volume
|
2016-03-13 18:01:36 +01:00
|
|
|
|
2016-03-15 01:34:20 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2016-03-17 14:58:49 +01:00
|
|
|
int id = wrapper.get(Type.INT, 0);
|
2016-04-10 15:29:25 +02:00
|
|
|
|
|
|
|
id = Effect.getNewId(id);
|
2016-03-17 14:58:49 +01:00
|
|
|
wrapper.set(Type.INT, 0, id);
|
2016-03-15 01:34:20 +01:00
|
|
|
}
|
|
|
|
});
|
2016-04-02 15:46:30 +02:00
|
|
|
// Rewrite potion effect as it changed to use a dynamic registry
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
int id = wrapper.get(Type.INT, 0);
|
|
|
|
if (id == 2002) {
|
|
|
|
int data = wrapper.get(Type.INT, 1);
|
|
|
|
int newData = ItemRewriter.getNewEffectID(data);
|
|
|
|
wrapper.set(Type.INT, 1, newData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.NAMED_SOUND, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.STRING); // 0 - Sound Name
|
|
|
|
// 1 - Sound Category ID
|
|
|
|
// Everything else get's written through
|
|
|
|
|
2016-03-15 01:34:20 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2016-03-17 14:58:49 +01:00
|
|
|
String name = wrapper.get(Type.STRING, 0);
|
|
|
|
|
|
|
|
SoundEffect effect = SoundEffect.getByName(name);
|
|
|
|
int catid = 0;
|
|
|
|
String newname = name;
|
|
|
|
if (effect != null) {
|
|
|
|
catid = effect.getCategory().getId();
|
|
|
|
newname = effect.getNewName();
|
|
|
|
}
|
|
|
|
wrapper.set(Type.STRING, 0, newname);
|
|
|
|
wrapper.write(Type.VAR_INT, catid); // Write Category ID
|
2016-03-27 20:13:19 +02:00
|
|
|
if (effect != null && effect.isBreaksound()) {
|
Refactor entity tracking and meta handling
This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs.
ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes.
ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries.
This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers).
All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2016-03-26 15:17:00 +01:00
|
|
|
int x = wrapper.passthrough(Type.INT); //Position X
|
|
|
|
int y = wrapper.passthrough(Type.INT); //Position Y
|
|
|
|
int z = wrapper.passthrough(Type.INT); //Position Z
|
2016-03-27 20:13:19 +02:00
|
|
|
if (tracker.interactedBlockRecently((int) Math.floor(x / 8.0), (int) Math.floor(y / 8.0), (int) Math.floor(z / 8.0))) {
|
2016-03-26 15:17:00 +01:00
|
|
|
wrapper.cancel();
|
|
|
|
}
|
|
|
|
}
|
2016-03-15 01:34:20 +01:00
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.CHUNK_DATA, new PacketRemapper() {
|
2016-03-14 17:09:50 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
2016-03-15 01:34:20 +01:00
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2016-03-14 17:09:50 +01:00
|
|
|
ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
|
2018-12-05 20:08:55 +01:00
|
|
|
Chunk1_9to1_8Type type = new Chunk1_9to1_8Type(clientChunks);
|
|
|
|
Chunk1_8 chunk = (Chunk1_8) wrapper.read(type);
|
2016-11-02 18:14:57 +01:00
|
|
|
if (chunk.isUnloadPacket()) {
|
2021-05-02 10:12:37 +02:00
|
|
|
wrapper.setId(ClientboundPackets1_9.UNLOAD_CHUNK);
|
2016-05-04 17:32:38 +02:00
|
|
|
|
2018-11-06 21:42:15 +01:00
|
|
|
wrapper.write(Type.INT, chunk.getX());
|
|
|
|
wrapper.write(Type.INT, chunk.getZ());
|
2016-11-02 18:14:57 +01:00
|
|
|
// Remove commandBlocks on chunk unload
|
|
|
|
CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
|
|
|
|
provider.unloadChunk(wrapper.user(), chunk.getX(), chunk.getZ());
|
2018-11-06 21:42:15 +01:00
|
|
|
} else {
|
|
|
|
wrapper.write(type, chunk);
|
|
|
|
// eat any other data (Usually happens with unload packets)
|
2016-11-02 18:14:57 +01:00
|
|
|
}
|
2016-04-13 16:05:50 +02:00
|
|
|
wrapper.read(Type.REMAINING_BYTES);
|
2016-03-14 17:09:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.MAP_BULK_CHUNK, null, new PacketRemapper() {
|
2016-09-25 20:34:24 +02:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
2021-05-02 10:12:37 +02:00
|
|
|
handler(wrapper -> {
|
|
|
|
wrapper.cancel(); // Cancel the packet from being sent
|
2016-09-29 16:33:14 +02:00
|
|
|
|
2021-05-02 10:12:37 +02:00
|
|
|
boolean skyLight = wrapper.read(Type.BOOLEAN);
|
|
|
|
int count = wrapper.read(Type.VAR_INT);
|
2016-09-29 16:33:14 +02:00
|
|
|
|
2021-05-02 10:12:37 +02:00
|
|
|
ChunkBulkSection[] chunks = new ChunkBulkSection[count];
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
chunks[i] = new ChunkBulkSection(wrapper, skyLight);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
|
|
|
|
for (ChunkBulkSection chunk : chunks) {
|
|
|
|
// Data is at the end
|
|
|
|
CustomByteType customByteType = new CustomByteType(chunk.getLength());
|
|
|
|
chunk.setData(wrapper.read(customByteType));
|
|
|
|
|
|
|
|
clientChunks.getBulkChunks().add(ClientChunks.toLong(chunk.getX(), chunk.getZ())); // Store for later
|
|
|
|
|
|
|
|
// Construct chunk packet
|
|
|
|
ByteBuf buffer = null;
|
|
|
|
try {
|
|
|
|
buffer = wrapper.user().getChannel().alloc().buffer();
|
|
|
|
|
|
|
|
Type.INT.write(buffer, chunk.getX());
|
|
|
|
Type.INT.write(buffer, chunk.getZ());
|
|
|
|
Type.BOOLEAN.write(buffer, true); // Always ground-up
|
|
|
|
Type.UNSIGNED_SHORT.write(buffer, chunk.getBitMask());
|
|
|
|
Type.VAR_INT.writePrimitive(buffer, chunk.getLength());
|
|
|
|
customByteType.write(buffer, chunk.getData());
|
|
|
|
|
|
|
|
// Send through this protocol again
|
|
|
|
PacketWrapper chunkPacket = PacketWrapper.create(ClientboundPackets1_8.CHUNK_DATA, buffer, wrapper.user());
|
2021-05-31 22:58:47 +02:00
|
|
|
chunkPacket.send(Protocol1_9To1_8.class, false);
|
2021-05-02 10:12:37 +02:00
|
|
|
} finally {
|
|
|
|
if (buffer != null) {
|
2019-05-01 16:15:48 +02:00
|
|
|
buffer.release();
|
|
|
|
}
|
2016-09-29 16:33:14 +02:00
|
|
|
}
|
2016-09-25 20:34:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.BLOCK_ENTITY_DATA, new PacketRemapper() {
|
2016-03-17 14:58:49 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.POSITION); // 0 - Block Position
|
|
|
|
map(Type.UNSIGNED_BYTE); // 1 - Action
|
|
|
|
map(Type.NBT); // 2 - NBT (Might not be present)
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
int action = wrapper.get(Type.UNSIGNED_BYTE, 0);
|
|
|
|
if (action == 1) { // Update Spawner
|
|
|
|
CompoundTag tag = wrapper.get(Type.NBT, 0);
|
|
|
|
if (tag != null) {
|
|
|
|
if (tag.contains("EntityId")) {
|
|
|
|
String entity = (String) tag.get("EntityId").getValue();
|
2021-03-14 16:57:37 +01:00
|
|
|
CompoundTag spawn = new CompoundTag();
|
|
|
|
spawn.put("id", new StringTag(entity));
|
|
|
|
tag.put("SpawnData", spawn);
|
2016-03-17 14:58:49 +01:00
|
|
|
} else { // EntityID does not exist
|
2021-03-14 16:57:37 +01:00
|
|
|
CompoundTag spawn = new CompoundTag();
|
|
|
|
spawn.put("id", new StringTag("AreaEffectCloud")); //Make spawners show up as empty when no EntityId is given.
|
|
|
|
tag.put("SpawnData", spawn);
|
2016-03-17 14:58:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (action == 2) { // Update Command Block
|
2016-11-02 18:14:57 +01:00
|
|
|
CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
|
|
|
|
provider.addOrUpdateBlock(wrapper.user(), wrapper.get(Type.POSITION, 0), wrapper.get(Type.NBT, 0));
|
|
|
|
|
2016-03-17 14:58:49 +01:00
|
|
|
// To prevent window issues don't send updates
|
|
|
|
wrapper.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.BLOCK_CHANGE, new PacketRemapper() {
|
2016-04-13 17:54:18 +02:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.POSITION);
|
|
|
|
map(Type.VAR_INT);
|
|
|
|
}
|
|
|
|
});
|
2016-03-18 21:22:38 +01:00
|
|
|
|
2016-03-13 18:01:36 +01:00
|
|
|
|
2016-03-14 13:40:23 +01:00
|
|
|
/* Incoming Packets */
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerServerbound(ServerboundPackets1_9.UPDATE_SIGN, new PacketRemapper() {
|
2016-03-14 13:40:23 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.POSITION); // 0 - Sign Position
|
2019-04-23 16:34:17 +02:00
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Sign Line (json)
|
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 2 - Sign Line (json)
|
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 3 - Sign Line (json)
|
|
|
|
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 4 - Sign Line (json)
|
2016-03-14 13:40:23 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerServerbound(ServerboundPackets1_9.PLAYER_DIGGING, new PacketRemapper() {
|
2016-03-14 13:40:23 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
2018-10-31 18:48:28 +01:00
|
|
|
map(Type.VAR_INT, Type.UNSIGNED_BYTE); // 0 - Status
|
2016-03-14 13:40:23 +01:00
|
|
|
map(Type.POSITION); // 1 - Position
|
2019-01-06 12:08:29 +01:00
|
|
|
map(Type.BYTE); // 2 - Face
|
2016-03-14 17:51:56 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
int status = wrapper.get(Type.UNSIGNED_BYTE, 0);
|
2016-04-20 19:31:18 +02:00
|
|
|
if (status == 6)
|
2016-03-14 17:51:56 +01:00
|
|
|
wrapper.cancel();
|
|
|
|
}
|
|
|
|
});
|
2016-03-16 22:52:00 +01:00
|
|
|
// Blocking
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
int status = wrapper.get(Type.UNSIGNED_BYTE, 0);
|
2016-04-21 22:13:10 +02:00
|
|
|
if (status == 5 || status == 4 || status == 3) {
|
Refactor entity tracking and meta handling
This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs.
ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes.
ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries.
This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers).
All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
|
|
|
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2016-03-17 00:34:03 +01:00
|
|
|
if (entityTracker.isBlocking()) {
|
2016-03-16 22:52:00 +01:00
|
|
|
entityTracker.setBlocking(false);
|
2021-04-05 18:53:57 +02:00
|
|
|
if (!Via.getConfig().isShowShieldWhenSwordInHand()) {
|
|
|
|
entityTracker.setSecondHand(null);
|
|
|
|
}
|
2016-03-16 22:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-14 13:40:23 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerServerbound(ServerboundPackets1_9.USE_ITEM, null, new PacketRemapper() {
|
2016-03-24 16:45:16 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
int hand = wrapper.read(Type.VAR_INT);
|
|
|
|
// Wipe the input buffer
|
|
|
|
wrapper.clearInputBuffer();
|
|
|
|
// First set this packet ID to Block placement
|
|
|
|
wrapper.setId(0x08);
|
2020-04-13 19:42:48 +02:00
|
|
|
wrapper.write(Type.POSITION, new Position(-1, (short) -1, -1));
|
2020-04-13 19:33:00 +02:00
|
|
|
wrapper.write(Type.UNSIGNED_BYTE, (short) 255);
|
2016-03-24 16:45:16 +01:00
|
|
|
// Write item in hand
|
2019-04-23 16:34:17 +02:00
|
|
|
Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
|
2016-03-24 16:45:16 +01:00
|
|
|
// Blocking patch
|
2016-09-25 16:40:30 +02:00
|
|
|
if (Via.getConfig().isShieldBlocking()) {
|
Refactor entity tracking and meta handling
This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs.
ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes.
ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries.
This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers).
All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2018-10-27 13:25:42 +02:00
|
|
|
|
2021-04-05 18:53:57 +02:00
|
|
|
// Check if the shield is already there or if we have to give it here
|
|
|
|
boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand();
|
|
|
|
|
2021-04-12 16:56:53 +02:00
|
|
|
// Method to identify the sword in hand
|
|
|
|
boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand()
|
|
|
|
: item != null && Protocol1_9To1_8.isSword(item.getIdentifier());
|
|
|
|
|
|
|
|
if (isSword) {
|
2017-06-01 18:23:16 +02:00
|
|
|
if (hand == 0) {
|
|
|
|
if (!tracker.isBlocking()) {
|
|
|
|
tracker.setBlocking(true);
|
2021-04-05 18:53:57 +02:00
|
|
|
|
|
|
|
// Check if the shield is already in the offhand
|
2021-04-12 16:56:53 +02:00
|
|
|
if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) {
|
2021-04-05 18:53:57 +02:00
|
|
|
|
|
|
|
// Set shield in offhand when interacting with main hand
|
|
|
|
Item shield = new Item(442, (byte) 1, (short) 0, null);
|
|
|
|
tracker.setSecondHand(shield);
|
|
|
|
}
|
2016-03-24 16:45:16 +01:00
|
|
|
}
|
2021-04-01 20:03:13 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 18:53:57 +02:00
|
|
|
// Use the main hand to trigger the blocking
|
|
|
|
boolean blockUsingMainHand = Via.getConfig().isNoDelayShieldBlocking()
|
|
|
|
&& !showShieldWhenSwordInHand;
|
2021-04-01 20:03:13 +02:00
|
|
|
|
2021-04-05 18:53:57 +02:00
|
|
|
if (blockUsingMainHand && hand == 1 || !blockUsingMainHand && hand == 0) {
|
2017-06-01 18:23:16 +02:00
|
|
|
wrapper.cancel();
|
2016-03-24 16:45:16 +01:00
|
|
|
}
|
2017-06-01 18:23:16 +02:00
|
|
|
} else {
|
2021-04-05 18:53:57 +02:00
|
|
|
if (!showShieldWhenSwordInHand) {
|
|
|
|
// Remove the shield from the offhand
|
|
|
|
tracker.setSecondHand(null);
|
|
|
|
}
|
2017-06-01 18:23:16 +02:00
|
|
|
tracker.setBlocking(false);
|
2016-03-24 16:45:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
wrapper.write(Type.ITEM, item);
|
|
|
|
|
2019-06-24 22:14:01 +02:00
|
|
|
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
|
|
|
|
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
|
|
|
|
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
|
2016-03-24 16:45:16 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerServerbound(ServerboundPackets1_9.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {
|
2016-03-14 13:40:23 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.POSITION); // 0 - Position
|
2019-05-27 18:50:08 +02:00
|
|
|
map(Type.VAR_INT, Type.UNSIGNED_BYTE); // 1 - Block Face
|
2021-04-16 08:45:33 +02:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
final int hand = wrapper.read(Type.VAR_INT); // 2 - Hand
|
|
|
|
if (hand != 0) wrapper.cancel();
|
|
|
|
}
|
|
|
|
});
|
2016-03-16 22:52:00 +01:00
|
|
|
create(new ValueCreator() {
|
|
|
|
@Override
|
|
|
|
public void write(PacketWrapper wrapper) throws Exception {
|
2019-04-23 16:34:17 +02:00
|
|
|
Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
|
2016-03-23 14:41:11 +01:00
|
|
|
wrapper.write(Type.ITEM, item); // 3 - Item
|
2016-03-16 22:52:00 +01:00
|
|
|
}
|
|
|
|
});
|
2016-03-14 13:40:23 +01:00
|
|
|
map(Type.UNSIGNED_BYTE); // 4 - X
|
|
|
|
map(Type.UNSIGNED_BYTE); // 5 - Y
|
|
|
|
map(Type.UNSIGNED_BYTE); // 6 - Z
|
|
|
|
|
2016-03-26 15:17:00 +01:00
|
|
|
//Register block place to fix sounds
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2019-05-27 18:50:08 +02:00
|
|
|
int face = wrapper.get(Type.UNSIGNED_BYTE, 0);
|
2016-03-27 20:13:19 +02:00
|
|
|
if (face == 255)
|
2016-03-26 15:17:00 +01:00
|
|
|
return;
|
|
|
|
Position p = wrapper.get(Type.POSITION, 0);
|
2019-11-24 21:55:46 +01:00
|
|
|
int x = p.getX();
|
2020-12-03 11:14:46 +01:00
|
|
|
int y = p.getY();
|
2019-11-24 21:55:46 +01:00
|
|
|
int z = p.getZ();
|
2016-03-27 20:13:19 +02:00
|
|
|
switch (face) {
|
|
|
|
case 0:
|
|
|
|
y--;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
y++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
z--;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
z++;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
x--;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
x++;
|
|
|
|
break;
|
2016-03-26 15:17:00 +01:00
|
|
|
}
|
Refactor entity tracking and meta handling
This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs.
ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes.
ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries.
This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers).
All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2016-03-27 20:13:19 +02:00
|
|
|
tracker.addBlockInteraction(new Position(x, y, z));
|
2016-03-26 15:17:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-02 18:14:57 +01:00
|
|
|
// Handle CommandBlocks
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
|
|
|
|
|
|
|
|
Position pos = wrapper.get(Type.POSITION, 0);
|
|
|
|
Optional<CompoundTag> tag = provider.get(wrapper.user(), pos);
|
|
|
|
// Send the Update Block Entity packet if present
|
|
|
|
if (tag.isPresent()) {
|
2021-04-26 22:54:43 +02:00
|
|
|
PacketWrapper updateBlockEntity = PacketWrapper.create(0x09, null, wrapper.user());
|
2016-11-02 18:14:57 +01:00
|
|
|
|
|
|
|
updateBlockEntity.write(Type.POSITION, pos);
|
|
|
|
updateBlockEntity.write(Type.UNSIGNED_BYTE, (short) 2);
|
|
|
|
updateBlockEntity.write(Type.NBT, tag.get());
|
|
|
|
|
2019-04-23 16:34:17 +02:00
|
|
|
updateBlockEntity.send(Protocol1_9To1_8.class);
|
2016-11-02 18:14:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-14 13:40:23 +01:00
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
2021-05-02 10:12:37 +02:00
|
|
|
|
|
|
|
public static final class ChunkBulkSection {
|
|
|
|
private final int x;
|
|
|
|
private final int z;
|
|
|
|
private final int bitMask;
|
|
|
|
private final int length;
|
|
|
|
private byte[] data;
|
|
|
|
|
|
|
|
public ChunkBulkSection(PacketWrapper wrapper, boolean skylight) throws Exception {
|
|
|
|
x = wrapper.read(Type.INT);
|
|
|
|
z = wrapper.read(Type.INT);
|
|
|
|
bitMask = wrapper.read(Type.UNSIGNED_SHORT);
|
|
|
|
|
|
|
|
int bitCount = Integer.bitCount(bitMask);
|
|
|
|
length = (bitCount * ((4096 * 2) + 2048)) + (skylight ? bitCount * 2048 : 0) + 256; // Thanks MCProtocolLib
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getX() {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getZ() {
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getBitMask() {
|
|
|
|
return bitMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLength() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte @Nullable [] getData() {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setData(byte[] data) {
|
|
|
|
this.data = data;
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|