Skip to content

Commit

Permalink
feat: drop items when dead
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 14, 2024
1 parent 9cf551d commit 876c903
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,19 @@ default void clearSlot(int slot) {
setItemStack(slot, EMPTY_SLOT_PLACE_HOLDER);
}

default void clearAllSlots() {
for (int slot = 0; slot < getItemStackArray().length; slot++) {
clearSlot(slot);
}
}

void addViewer(ContainerViewer viewer);

void removeViewer(ContainerViewer viewer);

default void removeAllViewers() {
getViewers().values().forEach(this::removeViewer);
}

ContainerViewer removeViewer(byte viewerId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.allaymc.api.entity.component.event;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.allaymc.api.eventbus.event.Event;

/**
* Allay Project 2024/6/14
*
* @author daoge_cmd
*/
@Getter
@AllArgsConstructor
public class EntityDieEvent extends Event {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import lombok.Getter;
import org.allaymc.api.eventbus.event.Event;

/**
* Allay Project 2024/2/26
*
* @author daoge_cmd
*/
@Getter
@AllArgsConstructor
public class EntityFallEvent extends Event {
Expand Down
6 changes: 5 additions & 1 deletion Allay-API/src/main/java/org/allaymc/api/world/Dimension.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,11 @@ default void addSound(float x, float y, float z, String sound, float volume, flo

default void dropItem(ItemStack itemStack, Vector3fc pos) {
var rand = ThreadLocalRandom.current();
dropItem(itemStack, pos, new org.joml.Vector3f(rand.nextFloat(0.2f) - 0.1f, 0.2f, rand.nextFloat(0.2f) - 0.1f), 10);
dropItem(itemStack, pos, new org.joml.Vector3f(rand.nextFloat(0.2f) - 0.1f, 0.2f, rand.nextFloat(0.2f) - 0.1f));
}

default void dropItem(ItemStack itemStack, Vector3fc pos, Vector3fc motion) {
dropItem(itemStack, pos, motion, 10);
}

default void dropItem(ItemStack itemStack, Vector3fc pos, Vector3fc motion, int pickupDelay) {
Expand Down
2 changes: 2 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ default void addTime(long amount) {

void setGameRule(GameRule gamerule, Object value);

<V> V getGameRule(GameRule gameRule);

default void broadcastPacket(BedrockPacket packet) {
for (var dim : getDimensions().values()) {
dim.broadcastPacket(packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.allaymc.api.component.annotation.Dependency;
import org.allaymc.api.container.Container;
import org.allaymc.api.container.ContainerViewer;
import org.allaymc.api.entity.init.SimpleEntityInitInfo;
import org.allaymc.api.entity.type.EntityTypes;
import org.allaymc.api.eventbus.EventHandler;
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
import org.joml.Vector3f;
Expand Down Expand Up @@ -77,10 +75,12 @@ private void onReplace(BlockOnReplaceEvent event) {
var pos = event.getCurrentBlockState().pos();
var dimension = pos.dimension();
var rand = ThreadLocalRandom.current();
container.removeAllViewers();
for (var itemStack : container.getItemStacks()) {
if (itemStack == Container.EMPTY_SLOT_PLACE_HOLDER) continue;
dimension.dropItem(itemStack, new Vector3f(pos.x() + rand.nextFloat(0.5f) + 0.25f, pos.y() + rand.nextFloat(0.5f) + 0.25f, pos.z() + rand.nextFloat(0.5f) + 0.25f));
}
container.clearAllSlots();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ protected void checkDead() {
if (attributeComponent.getHealth() == 0 && !dead) {
var event = new EntityDieEvent(thisEntity);
getWorld().getEventBus().callEvent(event);
manager.callEvent(new org.allaymc.api.entity.component.event.EntityDieEvent());
dead = true;
deadTimer = DEFAULT_DEAD_TIMER;
applyEntityEvent(EntityEventType.DEATH, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package org.allaymc.server.entity.component.common;

import org.allaymc.api.component.annotation.ComponentedObject;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.entity.component.event.EntityDieEvent;
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.component.annotation.ComponentIdentifier;
import org.allaymc.api.container.BaseContainerHolder;
import org.allaymc.api.container.Container;
import org.allaymc.api.container.FullContainerType;
import org.allaymc.api.entity.component.common.EntityContainerHolderComponent;
import org.allaymc.api.world.gamerule.GameRule;
import org.jetbrains.annotations.UnmodifiableView;
import org.joml.Vector3f;

import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
* Allay Project 2023/7/15
Expand All @@ -19,6 +26,8 @@ public class EntityContainerHolderComponentImpl extends BaseContainerHolder impl

@ComponentIdentifier
protected static final Identifier IDENTIFIER = new Identifier("minecraft:entity_inventory_holder_component");
@ComponentedObject
protected static Entity entity;

public EntityContainerHolderComponentImpl() {}

Expand All @@ -40,4 +49,29 @@ public <T extends Container> T getContainer(FullContainerType<T> type) {
public void addContainer(Container container) {
super.addContainer(container);
}

@EventHandler
protected void onDie(EntityDieEvent event) {
var pos = entity.getLocation();
var dimension = pos.dimension();
var rand = ThreadLocalRandom.current();
for (var container : getContainers().values()) {
container.removeAllViewers();
if (!canDropItemInContainers()) continue;
for (var itemStack : container.getItemStacks()) {
if (itemStack == Container.EMPTY_SLOT_PLACE_HOLDER) continue;
dimension.dropItem(
itemStack,
new Vector3f(pos.x() + rand.nextFloat(0.5f) + 0.25f, pos.y() + rand.nextFloat(0.5f) + 0.25f, pos.z() + rand.nextFloat(0.5f) + 0.25f),
new Vector3f(rand.nextFloat(1.0f) - 0.5f, 0.5f, rand.nextFloat(1.0f) - 0.5f),
40
);
}
container.clearAllSlots();
}
}

protected boolean canDropItemInContainers() {
return entity.getWorld().getGameRule(GameRule.DO_ENTITY_DROPS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ protected void syncData() {
}

protected void tryPickUpItems() {
if (dead || !spawned || willBeDespawnedNextTick) return;
var dimension = location.dimension;
// pick up items
var pickUpArea = new AABBf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.allaymc.api.container.impl.*;
import org.allaymc.api.entity.component.player.EntityPlayerContainerHolderComponent;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.world.gamerule.GameRule;
import org.allaymc.server.entity.component.common.EntityContainerHolderComponentImpl;

/**
Expand All @@ -31,4 +32,9 @@ public EntityPlayerContainerHolderComponentImpl() {
public void onInitFinish(ComponentInitInfo initInfo) {
addContainer(new PlayerInventoryContainer(player));
}

@Override
protected boolean canDropItemInContainers() {
return !(boolean)player.getWorld().getGameRule(GameRule.KEEP_INVENTORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ public void setGameRule(GameRule gamerule, Object value) {
worldData.setGameRule(gamerule, value);
}

@Override
public <V> V getGameRule(GameRule gameRule) {
return worldData.getGameRule(gameRule);
}

@Override
public void saveWorldData() {
getWorldStorage().writeWorldData(worldData);
Expand Down

0 comments on commit 876c903

Please sign in to comment.