Skip to content

Commit

Permalink
Added DropItemEvent check for each item dropped
Browse files Browse the repository at this point in the history
Added comments to build.yml workflow file
  • Loading branch information
B1n-ry committed Nov 4, 2023
1 parent e47e649 commit a43d417
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 20 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Automatically build the project and run any configured tests for every push
# and submitted pull request. This can help catch issues that only occur on
# certain platforms or Java versions, and provides a first line of defence
# against bad commits.

name: build
on: [pull_request, push]

Expand All @@ -7,7 +12,7 @@ jobs:
matrix:
# Use these Java versions
java: [
17, # Current Java LTS & minimum supported by Minecraft
17, # Current Java LTS & minimum supported by Minecraft
]
# and run on both Linux and Windows
os: [ubuntu-22.04, windows-2022]
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/b1n_ry/yigd/compat/CompatComponent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.components.InventoryComponent;
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropItemEvent;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -49,8 +50,7 @@ public CompatComponent(T inventory) {
public void dropItems(ServerWorld world, Vec3d pos) {
DefaultedList<ItemStack> items = this.getAsStackList();
for (ItemStack stack : items) {
if (DropItemEvent.EVENT.invoker().shouldDropItem(stack, pos.x, pos.y, pos.z, world))
ItemScatterer.spawn(world, pos.x, pos.y, pos.z, stack);
InventoryComponent.dropItemIfToBeDropped(stack, pos.x, pos.y, pos.z, world);
}
}
public abstract void clear();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/b1n_ry/yigd/compat/InventorioCompat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.components.InventoryComponent;
import com.b1n_ry.yigd.config.YigdConfig;
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropRuleEvent;
Expand Down Expand Up @@ -122,7 +123,7 @@ public CompatComponent<DefaultedList<ItemStack>> handleDropRules(DeathContext co

switch (dropRule) {
case KEEP -> soulboundItems.set(i, stack);
case DROP -> ItemScatterer.spawn(context.getWorld(), deathPos.x, deathPos.y, deathPos.z, stack);
case DROP -> InventoryComponent.dropItemIfToBeDropped(stack, deathPos.x, deathPos.y, deathPos.z, context.getWorld());
}
if (dropRule != DropRule.PUT_IN_GRAVE)
this.inventory.set(i, ItemStack.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.components.InventoryComponent;
import com.b1n_ry.yigd.config.YigdConfig;
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropRuleEvent;
Expand Down Expand Up @@ -88,7 +89,7 @@ public CompatComponent<Long> handleDropRules(DeathContext context) {
if (dropRule == DropRule.PUT_IN_GRAVE)
dropRule = DropRuleEvent.EVENT.invoker().getDropRule(stack, -1, context, true);
switch (dropRule) {
case DROP -> ItemScatterer.spawn(context.getWorld(), deathPos.x, deathPos.y, deathPos.z, stack);
case DROP -> InventoryComponent.dropItemIfToBeDropped(stack, deathPos.x, deathPos.y, deathPos.z, context.getWorld());
case DESTROY -> this.inventory = 0L;
case KEEP -> soulbound += this.inventory;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/b1n_ry/yigd/compat/OriginsCompat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.components.InventoryComponent;
import com.b1n_ry.yigd.config.YigdConfig;
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropRuleEvent;
Expand Down Expand Up @@ -165,7 +166,7 @@ public CompatComponent<Map<String, DefaultedList<ItemStack>>> handleDropRules(De

switch (dropRule) {
case KEEP -> soulboundStacks.set(i, item);
case DROP -> ItemScatterer.spawn(context.getWorld(), deathPos.x, deathPos.y, deathPos.z, item);
case DROP -> InventoryComponent.dropItemIfToBeDropped(item, deathPos.x, deathPos.y, deathPos.z, context.getWorld());
}
if (dropRule != DropRule.PUT_IN_GRAVE)
items.set(i, ItemStack.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.components.InventoryComponent;
import com.b1n_ry.yigd.config.YigdConfig;
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropRuleEvent;
Expand Down Expand Up @@ -93,7 +94,7 @@ public CompatComponent<ItemStack> handleDropRules(DeathContext context) {

Vec3d deathPos = context.getDeathPos();
if (dropRule == DropRule.DROP)
ItemScatterer.spawn(context.getWorld(), deathPos.x, deathPos.y, deathPos.z, this.inventory);
InventoryComponent.dropItemIfToBeDropped(this.inventory, deathPos.x, deathPos.y, deathPos.z, context.getWorld());

if (dropRule != DropRule.PUT_IN_GRAVE)
this.clear();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/b1n_ry/yigd/compat/TrinketsCompat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.components.InventoryComponent;
import com.b1n_ry.yigd.config.YigdConfig;
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropRuleEvent;
Expand Down Expand Up @@ -242,7 +243,7 @@ public CompatComponent<Map<String, Map<String, DefaultedList<Pair<TrinketEnums.D

switch (dropRule) {
case KEEP -> soulboundItems.set(i, new Pair<>(TrinketEnums.DropRule.DEFAULT, item));
case DROP -> ItemScatterer.spawn(context.getWorld(), deathPos.x, deathPos.y, deathPos.z, item);
case DROP -> InventoryComponent.dropItemIfToBeDropped(item, deathPos.x, deathPos.y, deathPos.z, context.getWorld());
}

if (dropRule != DropRule.PUT_IN_GRAVE)
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/b1n_ry/yigd/components/GraveComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public ActionResult claim(ServerPlayerEntity player, ServerWorld world, BlockSta
this.dropAll();

if (this.world != null && addGraveItem)
ItemScatterer.spawn(this.world, this.pos.getX(), this.pos.getY(), this.pos.getZ(), graveItem);
InventoryComponent.dropItemIfToBeDropped(graveItem, this.pos.getX(), this.pos.getY(), this.pos.getZ(), this.world);
}

if (!config.graveConfig.persistentGraves.enabled) {
Expand Down Expand Up @@ -489,8 +489,7 @@ public void applyToPlayer(ServerPlayerEntity player, ServerWorld world, BlockPos
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
if (DropItemEvent.EVENT.invoker().shouldDropItem(stack, x, y, z, world))
ItemScatterer.spawn(world, x, y, z, stack);
InventoryComponent.dropItemIfToBeDropped(stack, x, y, z, world);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/b1n_ry/yigd/components/InventoryComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private InventoryComponent handleDropRules(DeathContext context) {
DropRule dropRule = DropRuleEvent.EVENT.invoker().getDropRule(item, i, context, true);
switch (dropRule) {
case KEEP -> soulboundInventory.items.set(i, item);
case DROP -> ItemScatterer.spawn(context.getWorld(), deathPos.x, deathPos.y, deathPos.z, item);
case DROP -> InventoryComponent.dropItemIfToBeDropped(item, deathPos.x, deathPos.y, deathPos.z, context.getWorld());
}

if (dropRule != DropRule.PUT_IN_GRAVE)
Expand Down Expand Up @@ -221,8 +221,7 @@ private void loseRandomItem(DeathContext context) {
public void dropAll(ServerWorld world, Vec3d pos) {
for (ItemStack stack : this.items) {
if (stack.isEmpty()) continue;
if (DropItemEvent.EVENT.invoker().shouldDropItem(stack, pos.x, pos.y, pos.z, world))
ItemScatterer.spawn(world, pos.x, pos.y, pos.z, stack);
InventoryComponent.dropItemIfToBeDropped(stack, pos.x, pos.y, pos.z, world);
}

for (CompatComponent<?> compatComponent : this.modInventoryItems.values()) {
Expand Down Expand Up @@ -493,6 +492,11 @@ public static InventoryComponent fromNbt(NbtCompound nbt) {
return new InventoryComponent(items, compatComponents, mainSize, armorSize, offHandSize);
}

public static void dropItemIfToBeDropped(ItemStack stack, double x, double y, double z, ServerWorld world) {
if (DropItemEvent.EVENT.invoker().shouldDropItem(stack, x, y, z, world))
ItemScatterer.spawn(world, x, y, z, stack);
}

public static void clearPlayer(ServerPlayerEntity player) {
player.getInventory().clear();

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/b1n_ry/yigd/components/RespawnComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ public void apply(ServerPlayerEntity player) {
if (this.soulboundInventory != null) {
DefaultedList<ItemStack> extraItems = this.soulboundInventory.applyToPlayer(player);

double x = player.getX();
double y = player.getY();
double z = player.getZ();
ServerWorld world = player.getServerWorld();
for (ItemStack stack : extraItems) {
double x = player.getX();
double y = player.getY();
double z = player.getZ();
ServerWorld world = player.getServerWorld();
if (DropItemEvent.EVENT.invoker().shouldDropItem(stack, x, y, z, world))
ItemScatterer.spawn(world, x, y, z, stack);
InventoryComponent.dropItemIfToBeDropped(stack, x, y, z, world);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/b1n_ry/yigd/events/DropItemEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

/**
* Here custom logic for item drops can be applied. If any items from any mod has a special drop function,
* this event can be used.
* Vanilla minecraft doesn't use this, but it exists if any mod wants to use it.
*/
public interface DropItemEvent {
Event<DropItemEvent> EVENT = EventFactory.createArrayBacked(DropItemEvent.class, dropItemEvents -> (stack, x, y, z, world) -> {
for (DropItemEvent event : dropItemEvents) {
Expand Down

0 comments on commit a43d417

Please sign in to comment.