Make dropped items properly work

This commit is contained in:
Holly Stubbs 2023-12-25 02:45:21 +00:00
parent 328ddca458
commit b02ac676e2
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
4 changed files with 35 additions and 9 deletions

View file

@ -1,5 +1,5 @@
import { Console } from "hsconsole"; import { Console } from "hsconsole";
import { Endian, IReader, createWriter } from "bufferstuff"; import { IReader } from "bufferstuff";
import { MinecraftServer } from "./MinecraftServer"; import { MinecraftServer } from "./MinecraftServer";
import { Packet } from "./enums/Packet"; import { Packet } from "./enums/Packet";
import { PacketAnimation } from "./packets/Animation"; import { PacketAnimation } from "./packets/Animation";
@ -14,13 +14,10 @@ import { Player } from "./entities/Player";
import { Socket } from "net"; import { Socket } from "net";
import Vec3 from "./Vec3"; import Vec3 from "./Vec3";
import { PacketRespawn } from "./packets/Respawn"; import { PacketRespawn } from "./packets/Respawn";
import { PacketSpawnPosition } from "./packets/SpawnPosition";
import { PacketPlayerBlockPlacement } from "./packets/PlayerBlockPlacement"; import { PacketPlayerBlockPlacement } from "./packets/PlayerBlockPlacement";
import { Inventory } from "./inventories/Inventory";
import { PacketHoldingChange } from "./packets/HoldingChange"; import { PacketHoldingChange } from "./packets/HoldingChange";
import { PacketDisconnectKick } from "./packets/DisconnectKick"; import { PacketDisconnectKick } from "./packets/DisconnectKick";
import { ItemStack } from "./inventories/ItemStack"; import { ItemStack } from "./inventories/ItemStack";
import { PacketWindowItems } from "./packets/WindowItems";
import { Block } from "./blocks/Block"; import { Block } from "./blocks/Block";
import { EntityItem } from "./entities/EntityItem"; import { EntityItem } from "./entities/EntityItem";
import AABB from "./AABB"; import AABB from "./AABB";
@ -28,12 +25,13 @@ import { PacketSoundEffect } from "./packets/SoundEffect";
import { SoundEffects } from "./enums/SoundEffects"; import { SoundEffects } from "./enums/SoundEffects";
import { PacketUseEntity } from "./packets/UseEntity"; import { PacketUseEntity } from "./packets/UseEntity";
import { EntityLiving } from "./entities/EntityLiving"; import { EntityLiving } from "./entities/EntityLiving";
import PlayerInventory from "./inventories/PlayerInventory";
export class MPClient { export class MPClient {
private readonly mcServer:MinecraftServer; private readonly mcServer:MinecraftServer;
private readonly socket:Socket; private readonly socket:Socket;
public entity:Player; public entity:Player;
private inventory:Inventory; private inventory:PlayerInventory;
private dimension:number; private dimension:number;
private holdingIndex:number = 36; // First hotbar slot. private holdingIndex:number = 36; // First hotbar slot.
@ -200,11 +198,12 @@ export class MPClient {
const itemId = blockBehaviour.droppedItem(brokenBlockId); const itemId = blockBehaviour.droppedItem(brokenBlockId);
if (itemId !== -1) { if (itemId !== -1) {
const itemCount = blockBehaviour.droppedCount(brokenBlockId); const itemCount = blockBehaviour.droppedCount(brokenBlockId);
this.entity.sendToNearby(new PacketSoundEffect(SoundEffects.BLOCK_BREAK, x, y, z, brokenBlockId).writeData());
for (let i = 0; i < ((itemCount - 1) >> 6) + 1; i++) { for (let i = 0; i < ((itemCount - 1) >> 6) + 1; i++) {
const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemId, Math.min(itemCount - 64 * i, 64), metadata)); const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemId, Math.min(itemCount - 64 * i, 64), metadata));
itemEntity.position.set(x + 0.5, y + 0.5, z + 0.5); itemEntity.position.set(x + 0.5, y + 0.5, z + 0.5);
itemEntity.pickupDelay = 10;
this.entity.world.addEntity(itemEntity); this.entity.world.addEntity(itemEntity);
this.entity.sendToNearby(new PacketSoundEffect(SoundEffects.BLOCK_BREAK, x, y, z, brokenBlockId).writeData());
} }
} }
} }
@ -214,7 +213,30 @@ export class MPClient {
// Special drop item case // Special drop item case
if (packet.status === 4) { if (packet.status === 4) {
// TODO: Handle dropping items const itemStack = this.getHeldItemStack();
if (itemStack !== null && itemStack.size > 0) {
itemStack.size--;
const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemStack.itemID, 1, itemStack.damage));
itemEntity.pickupDelay = 10;
itemEntity.position.set(this.entity.position.x, this.entity.position.y + 1.50, this.entity.position.z);
itemEntity.motion.set(
-Math.sin((this.entity.rotation.yaw / 180) * Math.PI) * Math.cos((this.entity.rotation.pitch / 180) * Math.PI) * 0.3,
-Math.sin((this.entity.rotation.pitch / 180) * Math.PI) * 0.3 + 0.1,
Math.cos((this.entity.rotation.yaw / 180) * Math.PI) * Math.cos((this.entity.rotation.pitch / 180) * Math.PI) * 0.3
);
// Add random motion vector
const twoPIRandomised = Math.random() * Math.PI * 2;
const rngMult = 0.02 * Math.random();
itemEntity.motion.add(
Math.cos(twoPIRandomised) * rngMult,
(Math.random() - Math.random()) * 0.1,
Math.sin(twoPIRandomised) * rngMult
);
this.entity.world.addEntity(itemEntity);
this.inventory.dropEmptyItemStacks();
this.inventory.sendUpdatedStacks([this.holdingIndex]);
}
return; return;
} }

View file

@ -264,7 +264,6 @@ export class Entity implements IEntity {
// TODO: Handle X and Z collisions. // TODO: Handle X and Z collisions.
if (this.entityAABB.intersects(blockBoundingBox)) { if (this.entityAABB.intersects(blockBoundingBox)) {
const intersectionY = this.entityAABB.intersectionY(blockBoundingBox); const intersectionY = this.entityAABB.intersectionY(blockBoundingBox);
console.log(intersectionY);
this.position.add(0, intersectionY, 0); this.position.add(0, intersectionY, 0);
this.motion.y = 0; this.motion.y = 0;
this.onGround = true; this.onGround = true;

View file

@ -31,7 +31,6 @@ export class EntityItem extends Entity {
} else { } else {
let playerCollided = await this.collidesWithPlayer(this.entityAABB); let playerCollided = await this.collidesWithPlayer(this.entityAABB);
if (playerCollided !== undefined) { if (playerCollided !== undefined) {
console.log(playerCollided.username);
playerCollided.inventory.addItemStack(this.itemStack); playerCollided.inventory.addItemStack(this.itemStack);
playerCollided.itemPickup(this, this.itemStack.size); playerCollided.itemPickup(this, this.itemStack.size);
if (this.itemStack.size <= 0) { if (this.itemStack.size <= 0) {

View file

@ -17,6 +17,12 @@ export class PacketSetSlot implements IPacket {
this.itemId = itemId; this.itemId = itemId;
this.itemCount = itemCount; this.itemCount = itemCount;
this.itemUses = itemUses; this.itemUses = itemUses;
} else if (typeof(windowId) === "number" && typeof(slot) === "number" && typeof(itemId) === "number") {
this.windowId = windowId;
this.slot = slot;
this.itemId = itemId;
this.itemCount = Number.MIN_VALUE;
this.itemUses = Number.MIN_VALUE;
} else { } else {
this.windowId = Number.MIN_VALUE; this.windowId = Number.MIN_VALUE;
this.slot = Number.MIN_VALUE; this.slot = Number.MIN_VALUE;