Skip to content

Commit

Permalink
Updated Via API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Aug 23, 2024
1 parent 1187575 commit 7f1cb82
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 91 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ ViaLegacy needs to have custom netty handlers in the pipeline which handle <= 1.
To implement the changes you should add something similar to the following lines to your netty pipeline (After the ViaVersion handlers are added):
```java
if (serverTargetVersion.olderThanOrEqualTo(LegacyProtocolVersion.r1_6_4)) { // Only add those handlers if the server version is <= 1.6.4
user.getProtocolInfo().getPipeline().add(PreNettyBaseProtocol.INSTANCE); // Allow to intercept the handshake packet
// You can either add a codec (if your pipeline is built for that)
channel.pipeline().addBefore("length-codec", "vialegacy-pre-netty-length-codec", new PreNettyLengthCodec(user));
// or two seperate netty handlers
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ public void onWindowClose() {
for (int i = 1; i <= 9; i++) {
final Item item = this.openContainerItems[i];
if (item == null) continue;
dropItem(this.getUser(), item, false);
dropItem(this.user(), item, false);
this.openContainerItems[i] = null;
}
}
for (int i = 0; i < 4; i++) {
final Item item = this.craftingInventory[i];
if (item == null) continue;
dropItem(this.getUser(), item, false);
dropItem(this.user(), item, false);
this.craftingInventory[i] = null;
}

if (this.cursorItem != null) {
dropItem(this.getUser(), this.cursorItem, false);
dropItem(this.user(), this.cursorItem, false);
this.cursorItem = null;
}
this.openWindowType = -1;
Expand All @@ -98,10 +98,10 @@ public void onWindowClick(final byte windowId, final short slot, final byte butt
if (slot == -999) {
if (this.cursorItem != null) {
if (leftClick) {
dropItem(this.getUser(), this.cursorItem, false);
dropItem(this.user(), this.cursorItem, false);
this.cursorItem = null;
} else {
dropItem(this.getUser(), this.splitStack(this.cursorItem, 1), false);
dropItem(this.user(), this.splitStack(this.cursorItem, 1), false);
}
}
} else {
Expand Down Expand Up @@ -255,8 +255,8 @@ public void onBlockPlace(final BlockPosition position, final short direction) {
if (direction == 255) { // interact
AlphaItems.doInteract(handItem);
} else { // place
final IdAndData placedAgainst = this.getUser().get(ChunkTracker.class).getBlockNotNull(position);
final IdAndData targetBlock = this.getUser().get(ChunkTracker.class).getBlockNotNull(position.getRelative(BlockFaceUtil.getFace(direction)));
final IdAndData placedAgainst = this.user().get(ChunkTracker.class).getBlockNotNull(position);
final IdAndData targetBlock = this.user().get(ChunkTracker.class).getBlockNotNull(position.getRelative(BlockFaceUtil.getFace(direction)));
AlphaItems.doPlace(handItem, direction, placedAgainst);

if (handItem.identifier() < 256 || handItem.identifier() == ItemList1_6.reed.itemId()) { // block item
Expand Down Expand Up @@ -339,7 +339,7 @@ public void addItem(final Item item) {

// Add support for cheating items and classic block placement
public void handleCreativeSetSlot(short slot, Item item) {
if (!this.getUser().getProtocolInfo().serverProtocolVersion().equals(LegacyProtocolVersion.c0_30cpe)) item = fixItem(item);
if (!this.user().getProtocolInfo().serverProtocolVersion().equals(LegacyProtocolVersion.c0_30cpe)) item = fixItem(item);
if (slot <= 0) return;

if (slot <= 4) {
Expand Down Expand Up @@ -404,7 +404,7 @@ private void updateCursorItem() {
}

private void updateInventorySlot(final byte windowId, final short slot, final Item item) {
final PacketWrapper setSlot = PacketWrapper.create(ClientboundPacketsb1_1.CONTAINER_SET_SLOT, this.getUser());
final PacketWrapper setSlot = PacketWrapper.create(ClientboundPacketsb1_1.CONTAINER_SET_SLOT, this.user());
setSlot.write(Types.BYTE, windowId); // window id
setSlot.write(Types.SHORT, slot); // slot
setSlot.write(Typesb1_1.NBTLESS_ITEM, copyItem(item)); // item
Expand All @@ -422,7 +422,7 @@ private void updatePlayerInventory() {
}

private void updateInventory(final byte windowId, final Item[] items) {
final PacketWrapper windowItems = PacketWrapper.create(ClientboundPacketsb1_1.CONTAINER_SET_CONTENT, this.getUser());
final PacketWrapper windowItems = PacketWrapper.create(ClientboundPacketsb1_1.CONTAINER_SET_CONTENT, this.user());
windowItems.write(Types.BYTE, windowId); // window id
windowItems.write(Types1_4_2.NBTLESS_ITEM_ARRAY, copyItems(items)); // items
windowItems.send(Protocola1_2_3_5_1_2_6Tob1_0_1_1_1.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public BlockDigStorage(UserConnection user, final BlockPosition position, final

public void tick() {
if (this.tick >= 5) {
Protocolb1_2_0_2Tob1_3_0_1.sendBlockDigPacket(this.getUser(), (byte) 0, this.position, this.facing);
Protocolb1_2_0_2Tob1_3_0_1.sendBlockDigPacket(this.user(), (byte) 0, this.position, this.facing);
this.tick = 0;
} else {
this.tick++;
}
Protocolb1_2_0_2Tob1_3_0_1.sendBlockDigPacket(this.getUser(), (byte) 1, this.position, this.facing);
Protocolb1_2_0_2Tob1_3_0_1.sendBlockDigPacket(this.user(), (byte) 1, this.position, this.facing);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void finish(final int sizeX, final int sizeY, final int sizeZ) {
throw new IllegalStateException("Failed to load level", e);
}

final short maxChunkSectionCount = Via.getManager().getProviders().get(ClassicWorldHeightProvider.class).getMaxChunkSectionCount(this.getUser());
final short maxChunkSectionCount = Via.getManager().getProviders().get(ClassicWorldHeightProvider.class).getMaxChunkSectionCount(this.user());

this.chunkXCount = sizeX >> 4;
if (sizeX % 16 != 0) this.chunkXCount++;
Expand All @@ -103,7 +103,7 @@ public void finish(final int sizeX, final int sizeY, final int sizeZ) {
if (chunkX < 0 || chunkX >= this.chunkXCount || chunkZ < 0 || chunkZ >= this.chunkZCount) {
final Chunk chunk = ChunkUtil.createEmptyChunk(chunkX, chunkZ, Math.max(8, this.sectionYCount), this.sectionBitmask);
ChunkUtil.setDummySkylight(chunk, true);
final PacketWrapper chunkData = PacketWrapper.create(ClientboundPacketsa1_0_15.LEVEL_CHUNK, this.getUser());
final PacketWrapper chunkData = PacketWrapper.create(ClientboundPacketsa1_0_15.LEVEL_CHUNK, this.user());
chunkData.write(Types1_1.CHUNK, chunk);
chunkData.send(Protocolc0_28_30Toa1_0_15.class);
}
Expand All @@ -113,11 +113,11 @@ public void finish(final int sizeX, final int sizeY, final int sizeZ) {
}

public void tick() {
final ClassicPositionTracker positionTracker = this.getUser().get(ClassicPositionTracker.class);
final ClassicPositionTracker positionTracker = this.user().get(ClassicPositionTracker.class);
if (!positionTracker.spawned) return;

final long start = System.currentTimeMillis();
this.getUser().getChannel().eventLoop().submit(() -> {
this.user().getChannel().eventLoop().submit(() -> {
ClassicLevelStorage.this.eventLoopPing = System.currentTimeMillis() - start;
});

Expand Down Expand Up @@ -151,7 +151,7 @@ public void sendChunks(final ChunkCoord center, final int radius, int limit) {

public void sendChunk(final ChunkCoord coord) {
if (!this.shouldSend(coord)) return;
final ClassicBlockRemapper remapper = this.getUser().get(ClassicBlockRemapper.class);
final ClassicBlockRemapper remapper = this.user().get(ClassicBlockRemapper.class);

this.classicLevel.calculateLight(coord.chunkX * 16, coord.chunkZ * 16, this.subChunkXLength, this.subChunkZLength);

Expand Down Expand Up @@ -179,7 +179,7 @@ public void sendChunk(final ChunkCoord coord) {
this.loadedChunks.add(coord);

final Chunk viaChunk = new BaseChunk(coord.chunkX, coord.chunkZ, true, false, this.sectionBitmask, modernSections, new int[256], new ArrayList<>());
final PacketWrapper chunkData = PacketWrapper.create(ClientboundPacketsa1_0_15.LEVEL_CHUNK, this.getUser());
final PacketWrapper chunkData = PacketWrapper.create(ClientboundPacketsa1_0_15.LEVEL_CHUNK, this.user());
chunkData.write(Types1_1.CHUNK, viaChunk);
chunkData.send(Protocolc0_28_30Toa1_0_15.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void updateHax(final boolean flying, final boolean noClip, final boolean
statusMessage += " ";
statusMessage += this.respawn ? "§aRespawn" : "§cRespawn";

final PacketWrapper chatMessage = PacketWrapper.create(ClientboundPacketsa1_0_15.CHAT, this.getUser());
final PacketWrapper chatMessage = PacketWrapper.create(ClientboundPacketsa1_0_15.CHAT, this.user());
chatMessage.write(Typesb1_7_0_3.STRING, statusMessage); // message
chatMessage.send(Protocolc0_28_30Toa1_0_15.class);
}
Expand All @@ -86,7 +86,7 @@ public void setOpLevel(final byte opLevel) {
this.opLevel = opLevel;

if (this.haxEnabled) {
final ClassicServerTitleStorage serverTitleStorage = this.getUser().get(ClassicServerTitleStorage.class);
final ClassicServerTitleStorage serverTitleStorage = this.user().get(ClassicServerTitleStorage.class);
if (serverTitleStorage != null) { // Some servers update the op level before sending the login packet. Just ignore that because the op level is resent in the login packet.
this.updateHax(serverTitleStorage.isFlyEffectivelyEnabled(), serverTitleStorage.isNoclipEffectivelyEnabled(), serverTitleStorage.isSpeedEffectivelyEnabled(), serverTitleStorage.isRespawnEffectivelyEnabled());
}
Expand All @@ -98,8 +98,8 @@ public byte getOpLevel() {
}

public void updateAbilities() {
if (this.getUser().getProtocolInfo().getPipeline().contains(Protocolr1_2_1_3Tor1_2_4_5.class)) {
final PacketWrapper playerAbilities = PacketWrapper.create(ClientboundPackets1_2_4.PLAYER_ABILITIES, this.getUser());
if (this.user().getProtocolInfo().getPipeline().contains(Protocolr1_2_1_3Tor1_2_4_5.class)) {
final PacketWrapper playerAbilities = PacketWrapper.create(ClientboundPackets1_2_4.PLAYER_ABILITIES, this.user());
playerAbilities.write(Types.BOOLEAN, true); // invulnerable
playerAbilities.write(Types.BOOLEAN, false); // flying
playerAbilities.write(Types.BOOLEAN, this.flying); // allow flying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public boolean isFlyDisabled() {
}

public boolean isFlyEffectivelyEnabled() {
final boolean isOp = this.getUser().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
final boolean isOp = this.user().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
return (this.isHaxDisabled() ? this.isFlyEnabled() : !this.isFlyDisabled()) || (isOp && this.isOphaxEnabled());
}

Expand All @@ -69,7 +69,7 @@ public boolean isNoclipDisabled() {
}

public boolean isNoclipEffectivelyEnabled() {
final boolean isOp = this.getUser().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
final boolean isOp = this.user().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
return (this.isHaxDisabled() ? this.isNoclipEnabled() : !this.isNoclipDisabled()) || (isOp && this.isOphaxEnabled());
}

Expand All @@ -82,7 +82,7 @@ public boolean isRespawnDisabled() {
}

public boolean isRespawnEffectivelyEnabled() {
final boolean isOp = this.getUser().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
final boolean isOp = this.user().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
return (this.isHaxDisabled() ? this.isRespawnEnabled() : !this.isRespawnDisabled()) || (isOp && this.isOphaxEnabled());
}

Expand All @@ -95,7 +95,7 @@ public boolean isSpeedDisabled() {
}

public boolean isSpeedEffectivelyEnabled() {
final boolean isOp = this.getUser().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
final boolean isOp = this.user().get(ClassicOpLevelStorage.class).getOpLevel() >= 100;
return (this.isHaxDisabled() ? this.isSpeedEnabled() : !this.isSpeedDisabled()) || (isOp && this.isOphaxEnabled());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void tick() {
final PendingBlockEntry pendingBlockEntry = it.next();
if (pendingBlockEntry.decrementAndCheckIsExpired()) {
it.remove();
final PacketWrapper blockChange = PacketWrapper.create(ClientboundPackets1_2_1.BLOCK_UPDATE, this.getUser());
final PacketWrapper blockChange = PacketWrapper.create(ClientboundPackets1_2_1.BLOCK_UPDATE, this.user());
blockChange.write(Types1_7_6.BLOCK_POSITION_UBYTE, pendingBlockEntry.getPosition()); // position
blockChange.write(Types.UNSIGNED_BYTE, (short) pendingBlockEntry.getBlock().getId()); // block id
blockChange.write(Types.UNSIGNED_BYTE, (short) pendingBlockEntry.getBlock().getData()); // block data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void playSoundAt(Location sourceLocation, Location targetLocation, Confi
return;
}

final PacketWrapper entitySound = PacketWrapper.create(ClientboundPackets1_3_1.CUSTOM_SOUND, this.getUser());
final PacketWrapper entitySound = PacketWrapper.create(ClientboundPackets1_3_1.CUSTOM_SOUND, this.user());
entitySound.write(Types1_6_4.STRING, sound.getSound().getSoundName()); // sound
entitySound.write(Types.INT, ((int) sourceLocation.getX()) * 8); // x
entitySound.write(Types.INT, ((int) sourceLocation.getY()) * 8); // y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
import com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap;
import com.viaversion.viaversion.libs.fastutil.objects.Object2IntOpenHashMap;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ClientboundStatusPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundStatusPackets;
import com.viaversion.viaversion.protocols.base.*;
import com.viaversion.viaversion.protocols.base.v1_7.ClientboundBaseProtocol1_7;
import com.viaversion.viaversion.protocols.v1_8to1_9.packet.ClientboundPackets1_8;
import com.viaversion.viaversion.util.IdAndData;
Expand Down Expand Up @@ -849,6 +846,13 @@ public void register() {
);
this.cancelClientbound(ClientboundPackets1_6_4.SET_CREATIVE_MODE_SLOT);

this.registerServerboundTransition(ServerboundHandshakePackets.CLIENT_INTENTION, null, wrapper -> {
wrapper.cancel();
wrapper.read(Types.VAR_INT); // protocol version
final String hostname = wrapper.read(Types.STRING); // hostname
final int port = wrapper.read(Types.UNSIGNED_SHORT); // port
wrapper.user().put(new HandshakeStorage(hostname, port));
});
this.registerServerboundTransition(ServerboundStatusPackets.STATUS_REQUEST, ServerboundPackets1_6_4.SERVER_PING, wrapper -> {
final HandshakeStorage handshakeStorage = wrapper.user().get(HandshakeStorage.class);
final String ip = handshakeStorage.getHostname();
Expand Down Expand Up @@ -1104,6 +1108,7 @@ public void register(ViaProviders providers) {
public void init(UserConnection userConnection) {
userConnection.put(new PreNettySplitter(Protocolr1_6_4Tor1_7_2_5.class, ClientboundPackets1_6_4::getPacket));

userConnection.put(new ProtocolMetadataStorage());
userConnection.put(new PlayerInfoStorage());
userConnection.put(new StatisticsStorage());
userConnection.put(new DimensionTracker());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void trackEntity(final int entityId, final EntityTypes1_8.EntityType enti
this.entityMap.put(entityId, entityType);

if (entityType == EntityTypes1_8.EntityType.HORSE || entityType == EntityTypes1_8.EntityType.WITHER_SKULL) {
this.hologramParts.put(entityId, new HologramPartEntity(this.getUser(), entityId, entityType));
this.hologramParts.put(entityId, new HologramPartEntity(this.user(), entityId, entityType));
}
}

Expand Down
Loading

0 comments on commit 7f1cb82

Please sign in to comment.