Skip to content

Commit

Permalink
feat: introduce ItemAttackEntityEvent and ItemBreakBlockEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 23, 2024
1 parent 382698e commit 3d3df47
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ default NbtMap saveNBT() {
/**
* Called when the item is used to break a block.
* @param block The block being broken
* @param entity The entity breaking the block
* @param breaker The entity breaking the block
*/
default void onBreakBlock(BlockState block, Entity entity) {}
void onBreakBlock(BlockState block, Entity breaker);

/**
* Called when the item is used to attack an entity.
* @param attacker The entity attacking
* @param victim The entity being attacked
*/
default void onAttackEntity(Entity attacker, Entity victim) {}
void onAttackEntity(Entity attacker, Entity victim);

/**
* Get the break time bonus for breaking a block with this item.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.allaymc.api.item.component.event;

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

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/

@Getter
@Setter
@AllArgsConstructor
public class ItemAttackEntityEvent extends Event {
protected Entity attacker;
protected Entity victim;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.allaymc.api.item.component.event;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.eventbus.event.Event;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
@Getter
@Setter
@AllArgsConstructor
public class ItemBreakBlockEvent extends Event {
protected BlockState block;
protected Entity breaker;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import org.allaymc.api.data.VanillaBlockId;
import org.allaymc.api.data.VanillaItemId;
import org.allaymc.api.data.VanillaMaterialTypes;
import org.allaymc.api.item.component.event.ItemLoadExtraTagEvent;
import org.allaymc.api.item.component.event.ItemPlacedAsBlockEvent;
import org.allaymc.api.item.component.event.ItemSaveExtraTagEvent;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.item.component.event.*;
import org.allaymc.api.item.enchantment.SimpleEnchantmentInstance;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.component.annotation.ComponentIdentifier;
Expand Down Expand Up @@ -377,6 +376,16 @@ public void removeAllEnchantments() {
enchantments.clear();
}

@Override
public void onBreakBlock(BlockState block, Entity breaker) {
manager.callEvent(new ItemBreakBlockEvent(block, breaker));
}

@Override
public void onAttackEntity(Entity attacker, Entity victim) {
manager.callEvent(new ItemAttackEntityEvent(attacker, victim));
}

// 记录那些对工具品质有要求的方块的正确工具集合
private static final EnumMap<VanillaBlockId, VanillaItemId[]> CORRECT_TOOL_SPECIAL_MAP = new EnumMap<>(VanillaBlockId.class);

Expand Down

0 comments on commit 3d3df47

Please sign in to comment.