Skip to content

Commit

Permalink
Add SetPassengers packet
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Sep 15, 2024
1 parent 2393b87 commit ba51781
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.oliver.fancysitula.api.packets;

import java.util.List;

public abstract class FS_ClientboundSetPassengersPacket implements FS_ClientboundPacket {

protected int entityId;
protected List<Integer> passengers;

public FS_ClientboundSetPassengersPacket(int entityId, List<Integer> passengers) {
this.entityId = entityId;
this.passengers = passengers;
}

public int getEntityId() {
return entityId;
}

public void setEntityId(int entityId) {
this.entityId = entityId;
}

public List<Integer> getPassengers() {
return passengers;
}

public void setPassengers(List<Integer> passengers) {
this.passengers = passengers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,30 @@ public FS_ClientboundSetEquipmentPacket createSetEquipmentPacket(ServerVersion s
public FS_ClientboundSetEquipmentPacket createSetEquipmentPacket(int entityId, Map<FS_EquipmentSlot, ItemStack> equipment) {
return createSetEquipmentPacket(ServerVersion.getCurrentVersion(), entityId, equipment);
}

/**
* Creates a new FS_ClientboundSetPassengersPacket instance based on the server version
*
* @param entityId ID of the vehicle entity
* @param passengers List of entity IDs to set as passengers
*/
public FS_ClientboundSetPassengersPacket createSetPassengersPacket(
ServerVersion serverVersion, int entityId, List<Integer> passengers) {
switch (serverVersion) {
case v1_20_5, v1_20_6, v1_21, v1_21_1 -> {
return new de.oliver.fancysitula.versions.v1_20_6.packets.ClientboundSetPassengersPacketImpl(entityId, passengers);
}
default -> throw new IllegalArgumentException("Unsupported server version: " + serverVersion.getVersion());
}
}

/**
* Creates a new FS_ClientboundSetPassengersPacket instance based on the current server version
*
* @param entityId ID of the vehicle entity
* @param passengers List of entity IDs to set as passengers
*/
public FS_ClientboundSetPassengersPacket createSetPassengersPacket(int entityId, List<Integer> passengers) {
return createSetPassengersPacket(ServerVersion.getCurrentVersion(), entityId, passengers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.oliver.fancysitula.versions.v1_20_6.packets;

import de.oliver.fancysitula.api.entities.FS_RealPlayer;
import de.oliver.fancysitula.api.packets.FS_ClientboundSetPassengersPacket;
import de.oliver.fancysitula.api.utils.reflections.ReflectionUtils;
import de.oliver.fancysitula.versions.v1_20_6.utils.VanillaPlayerAdapter;
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
import net.minecraft.server.level.ServerPlayer;

import java.util.List;

public class ClientboundSetPassengersPacketImpl extends FS_ClientboundSetPassengersPacket {

public ClientboundSetPassengersPacketImpl(int entityId, List<Integer> passengers) {
super(entityId, passengers);
}


@Override
public Object createPacket() {
int[] passengers = new int[this.passengers.size()];
for (int i = 0; i < this.passengers.size(); i++) {
passengers[i] = this.passengers.get(i);
}

try {
ClientboundSetPassengersPacket packet = ReflectionUtils.createUnsafeInstance(ClientboundSetPassengersPacket.class);
ReflectionUtils.setFinalField(packet, "vehicle", entityId);
ReflectionUtils.setFinalField(packet, "passengers", passengers);
return packet;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@Override
public void send(FS_RealPlayer player) {
ClientboundSetPassengersPacket packet = (ClientboundSetPassengersPacket) createPacket();

ServerPlayer vanillaPlayer = VanillaPlayerAdapter.asVanilla(player.getBukkitPlayer());
vanillaPlayer.connection.send(packet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.oliver.fancysitula.versions.v1_20_6.packets;

import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class ClientboundSetPassengersPacketImplTest {

@Test
void createPacket() {
// Setup packet
int vehicleID = 712;
List<Integer> passengers = new ArrayList<>();
passengers.add(571);
passengers.add(572);

ClientboundSetPassengersPacketImpl packet = new ClientboundSetPassengersPacketImpl(vehicleID, passengers);
ClientboundSetPassengersPacket createdPacket = (ClientboundSetPassengersPacket) packet.createPacket();

// Check packet
assert createdPacket.getVehicle() == vehicleID;
assert createdPacket.getPassengers().length == 2;
assert createdPacket.getPassengers()[0] == 571;
assert createdPacket.getPassengers()[1] == 572;
}
}

0 comments on commit ba51781

Please sign in to comment.