Skip to content

Commit

Permalink
ChestBoat test
Browse files Browse the repository at this point in the history
  • Loading branch information
Arbee4ever committed Apr 11, 2022
1 parent d099684 commit 63bb0c5
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 39 deletions.
55 changes: 34 additions & 21 deletions src/main/java/net/arbee/addola/AddolaClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.arbee.addola;

import io.netty.buffer.Unpooled;
import net.arbee.addola.client.render.BoatItemRenderer;
import net.arbee.addola.client.render.ChestBoatEntityRenderer;
import net.arbee.addola.client.render.ChestBoatItemRenderer;
Expand All @@ -13,13 +14,19 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendereregistry.v1.EntityRendererRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.MinecraftVersion;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.BoatItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;

import java.util.UUID;
Expand All @@ -29,27 +36,6 @@ public class AddolaClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(SpawnChestBoatEntityPacketSender.IDENTIFIER, (client, handler, buf, responseSender) -> {
final int entityId = buf.readInt();
final UUID uuid = buf.readUuid();
final double x = buf.readDouble();
final double y = buf.readDouble();
final double z = buf.readDouble();
final double xVelocity = buf.readDouble();
final double yVelocity = buf.readDouble();
final double zVelocity = buf.readDouble();
final float pitch = buf.readFloat();
final float yaw = buf.readFloat();
final ChestBoatEntity boat = new ChestBoatEntity(AddolaEntities.CHESTBOAT, client.world);
boat.setEntityId(entityId);
boat.setUuid(uuid);
boat.setPos(x, y, z);
boat.setVelocity(xVelocity, yVelocity, zVelocity);
boat.setYaw(yaw);
boat.pitch = pitch;
client.execute(() -> client.world.addEntity(entityId, boat));
});

BuiltinItemRendererRegistry.INSTANCE.register(Items.ACACIA_BOAT, new BoatItemRenderer());
BuiltinItemRendererRegistry.INSTANCE.register(Items.OAK_BOAT, new BoatItemRenderer());
BuiltinItemRendererRegistry.INSTANCE.register(Items.BIRCH_BOAT, new BoatItemRenderer());
Expand All @@ -63,5 +49,32 @@ public void onInitializeClient() {
BuiltinItemRendererRegistry.INSTANCE.register(AddolaItems.ACACIA_CHESTBOAT_ITEM, new ChestBoatItemRenderer());
BuiltinItemRendererRegistry.INSTANCE.register(AddolaItems.DARKOAK_CHESTBOAT_ITEM, new ChestBoatItemRenderer());
EntityRendererRegistry.INSTANCE.register(AddolaEntities.CHESTBOAT, (dispatcher, context) -> new ChestBoatEntityRenderer(dispatcher));

receiveEntityPacket();
}

public void receiveEntityPacket() {
ClientSidePacketRegistry.INSTANCE.register(SpawnChestBoatEntityPacketSender.IDENTIFIER, (ctx, byteBuf) -> {
EntityType<?> et = Registry.ENTITY_TYPE.get(byteBuf.readVarInt());
UUID uuid = byteBuf.readUuid();
int entityId = byteBuf.readVarInt();
Vec3d pos = SpawnChestBoatEntityPacketSender.PacketBufUtil.readVec3d(byteBuf);
float pitch = SpawnChestBoatEntityPacketSender.PacketBufUtil.readAngle(byteBuf);
float yaw = SpawnChestBoatEntityPacketSender.PacketBufUtil.readAngle(byteBuf);
ctx.getTaskQueue().execute(() -> {
if (MinecraftClient.getInstance().world == null)
throw new IllegalStateException("Tried to spawn entity in a null world!");
Entity e = et.create(MinecraftClient.getInstance().world);
if (e == null)
throw new IllegalStateException("Failed to create instance of entity \"" + Registry.ENTITY_TYPE.getId(et) + "\"!");
e.updateTrackedPosition(pos);
e.setPos(pos.x, pos.y, pos.z);
e.pitch = pitch;
e.yaw = yaw;
e.setEntityId(entityId);
e.setUuid(uuid);
MinecraftClient.getInstance().world.addEntity(entityId, e);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

public class ChestBoatEntity extends BoatEntity {
private static final TrackedData<String> BLOCK_ENTITY;
ChestBoatEntity instance = this;


static {
Expand All @@ -58,7 +57,7 @@ public ChestBoatEntity(EntityType<? extends ChestBoatEntity> entityType, World w

@Override
public ActionResult interact(PlayerEntity player, Hand hand) {
if (((BoatEntityAccess)instance).getTicksUnderwater() < 60.0F) {
if (((BoatEntityAccess)this).getTicksUnderwater() < 60.0F) {
if (!this.world.isClient) {
Block block = Registry.BLOCK.get(Registry.ITEM.getId(player.getMainHandStack().getItem()));
if (player.isSneaking()) {
Expand Down Expand Up @@ -122,8 +121,8 @@ public void updatePassengerPosition(Entity passenger) {

Vec3d vec3d = (new Vec3d((double)f, 0.0D, 0.0D)).rotateY(-this.yaw * 0.017453292F - 1.5707964F);
passenger.updatePosition(this.getX() + vec3d.x, this.getY() + (double)g, this.getZ() + vec3d.z);
passenger.yaw += ((BoatEntityAccess)instance).getYawVelocity();
passenger.setHeadYaw(passenger.getHeadYaw() + ((BoatEntityAccess)instance).getYawVelocity());
passenger.yaw += ((BoatEntityAccess)this).getYawVelocity();
passenger.setHeadYaw(passenger.getHeadYaw() + ((BoatEntityAccess)this).getYawVelocity());
this.copyEntityData(passenger);
if (passenger instanceof AnimalEntity && this.getPassengerList().size() > 1) {
int j = passenger.getEntityId() % 2 == 0 ? 90 : 270;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,114 @@

import io.netty.buffer.Unpooled;
import net.arbee.addola.entity.vehicle.ChestBoatEntity;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.network.PacketContext;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;

import java.util.UUID;

public class SpawnChestBoatEntityPacketSender {
public static final Identifier IDENTIFIER = new Identifier("addola", "spawn_chestboat");

public static Packet<?> createSpawnPacket(final ChestBoatEntity boatEntity) {
final PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeInt(boatEntity.getEntityId());
buf.writeUuid(boatEntity.getUuid());
buf.writeDouble(boatEntity.getX());
buf.writeDouble(boatEntity.getY());
buf.writeDouble(boatEntity.getZ());
final Vec3d velocity = boatEntity.getVelocity();
buf.writeDouble(velocity.x);
buf.writeDouble(velocity.y);
buf.writeDouble(velocity.z);
buf.writeFloat(boatEntity.getPitch(1));
buf.writeFloat(boatEntity.yaw);
return ServerPlayNetworking.createS2CPacket(IDENTIFIER, buf);
if (boatEntity.world.isClient)
throw new IllegalStateException("SpawnPacketUtil.create called on the logical client!");
PacketByteBuf byteBuf = new PacketByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(Registry.ENTITY_TYPE.getRawId(boatEntity.getType()));
byteBuf.writeUuid(boatEntity.getUuid());
byteBuf.writeVarInt(boatEntity.getEntityId());

PacketBufUtil.writeVec3d(byteBuf, boatEntity.getPos());
PacketBufUtil.writeAngle(byteBuf, boatEntity.pitch);
PacketBufUtil.writeAngle(byteBuf, boatEntity.yaw);
return ServerPlayNetworking.createS2CPacket(IDENTIFIER, byteBuf);
}

private SpawnChestBoatEntityPacketSender() {
public static final class PacketBufUtil {

/**
* Packs a floating-point angle into a {@code byte}.
*
* @param angle
* angle
* @return packed angle
*/
public static byte packAngle(float angle) {
return (byte) MathHelper.floor(angle * 256 / 360);
}

/**
* Unpacks a floating-point angle from a {@code byte}.
*
* @param angleByte
* packed angle
* @return angle
*/
public static float unpackAngle(byte angleByte) {
return (angleByte * 360) / 256f;
}

/**
* Writes an angle to a {@link PacketByteBuf}.
*
* @param byteBuf
* destination buffer
* @param angle
* angle
*/
public static void writeAngle(PacketByteBuf byteBuf, float angle) {
byteBuf.writeByte(packAngle(angle));
}

/**
* Reads an angle from a {@link PacketByteBuf}.
*
* @param byteBuf
* source buffer
* @return angle
*/
public static float readAngle(PacketByteBuf byteBuf) {
return unpackAngle(byteBuf.readByte());
}

/**
* Writes a {@link Vec3d} to a {@link PacketByteBuf}.
*
* @param byteBuf
* destination buffer
* @param vec3d
* vector
*/
public static void writeVec3d(PacketByteBuf byteBuf, Vec3d vec3d) {
byteBuf.writeDouble(vec3d.x);
byteBuf.writeDouble(vec3d.y);
byteBuf.writeDouble(vec3d.z);
}

/**
* Reads a {@link Vec3d} from a {@link PacketByteBuf}.
*
* @param byteBuf
* source buffer
* @return vector
*/
public static Vec3d readVec3d(PacketByteBuf byteBuf) {
double x = byteBuf.readDouble();
double y = byteBuf.readDouble();
double z = byteBuf.readDouble();
return new Vec3d(x, y, z);
}
}
}

0 comments on commit 63bb0c5

Please sign in to comment.