Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit f1aa3da

Browse files
committed
AttackEntityEvent
1 parent f583ac8 commit f1aa3da

8 files changed

Lines changed: 99 additions & 8 deletions

File tree

patchwork-events-entity/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ version = getSubprojectVersion(project, "0.1.0")
33

44
dependencies {
55
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
compile project(path: ':patchwork-extensions-item', configuration: 'dev')
67
}

patchwork-events-entity/src/main/java/com/patchworkmc/impl/event/entity/EntityEvents.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.patchworkmc.impl.event.entity;
2121

2222
import net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.common.extensions.IForgeItem;
2324
import net.minecraftforge.event.entity.EntityEvent;
2425
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
2526
import net.minecraftforge.event.entity.living.LivingAttackEvent;
@@ -30,6 +31,7 @@
3031
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
3132
import net.minecraftforge.event.entity.living.LivingEvent;
3233
import net.minecraftforge.event.entity.living.LivingFallEvent;
34+
import net.minecraftforge.event.entity.player.AttackEntityEvent;
3335
import net.minecraftforge.event.entity.player.PlayerEvent;
3436
import net.minecraftforge.event.entity.player.PlayerFlyableFallEvent;
3537
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
@@ -46,6 +48,7 @@
4648
import net.minecraft.entity.damage.DamageSource;
4749
import net.minecraft.entity.mob.MobEntity;
4850
import net.minecraft.entity.player.PlayerEntity;
51+
import net.minecraft.item.ItemStack;
4952
import net.minecraft.server.network.ServerPlayerEntity;
5053
import net.minecraft.util.ActionResult;
5154
import net.minecraft.util.Hand;
@@ -140,7 +143,8 @@ public static boolean canEntitySpawnFromSpawner(MobEntity entity, World world, d
140143
Result result = canEntitySpawn(entity, world, x, y, z, spawner, SpawnType.SPAWNER);
141144

142145
if (result == Result.DEFAULT) {
143-
return entity.canSpawn(world, SpawnType.SPAWNER) && entity.canSpawn(world); //vanilla logic, but inverted since we're checking if it CAN spawn instead of if it CAN'T
146+
// Vanilla logic, but inverted since we're checking if it CAN spawn instead of if it CAN'T
147+
return entity.canSpawn(world, SpawnType.SPAWNER) && entity.canSpawn(world);
144148
} else {
145149
return result == Result.ALLOW;
146150
}
@@ -150,7 +154,8 @@ public static boolean canEntitySpawnNaturally(MobEntity entity, IWorld world, do
150154
Result result = canEntitySpawn(entity, world, x, y, z, spawner, spawnType);
151155

152156
if (result == Result.DEFAULT) {
153-
return !(sqDistanceFromPlayer > 16384.0D && entity.canImmediatelyDespawn(sqDistanceFromPlayer)) && entity.canSpawn(world, SpawnType.NATURAL) && entity.canSpawn(world); //vanilla logic, but inverted since we're checking if it CAN spawn instead of if it CAN'T
157+
// Vanilla logic, but inverted since we're checking if it CAN spawn instead of if it CAN'T
158+
return !(sqDistanceFromPlayer > 16384.0D && entity.canImmediatelyDespawn(sqDistanceFromPlayer)) && entity.canSpawn(world, SpawnType.NATURAL) && entity.canSpawn(world);
154159
} else {
155160
return result == Result.ALLOW;
156161
}
@@ -160,6 +165,22 @@ public static boolean doSpecialSpawn(MobEntity entity, IWorld world, double x, d
160165
return MinecraftForge.EVENT_BUS.post(new LivingSpawnEvent.SpecialSpawn(entity, world, x, y, z, spawner, spawnType));
161166
}
162167

168+
public static boolean attackEntity(PlayerEntity player, Entity target) {
169+
if (MinecraftForge.EVENT_BUS.post(new AttackEntityEvent(player, target))) {
170+
return false;
171+
}
172+
173+
ItemStack stack = player.getMainHandStack();
174+
175+
if (stack.isEmpty()) {
176+
return true;
177+
}
178+
179+
IForgeItem item = (IForgeItem) stack.getItem();
180+
181+
return !item.onLeftClickEntity(stack, player, target);
182+
}
183+
163184
@Override
164185
public void onInitialize() {
165186
UseItemCallback.EVENT.register((player, world, hand) -> {

patchwork-events-entity/src/main/java/com/patchworkmc/mixin/event/entity/MixinPlayerEntity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,13 @@ private float hookApplyDamageForDamageEvent(float damage, DamageSource source) {
119119

120120
return EntityEvents.onLivingDamage(entity, source, damage);
121121
}
122+
123+
@Inject(method = "attack(Lnet/minecraft/entity/Entity;)V", at = @At("HEAD"), cancellable = true)
124+
private void onAttackEntity(Entity target, CallbackInfo callback) {
125+
PlayerEntity player = (PlayerEntity) (Object) this;
126+
127+
if (!EntityEvents.attackEntity(player, target)) {
128+
callback.cancel();
129+
}
130+
}
122131
}

patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/living/LivingFallEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* <p>This event is cancellable.
3737
* If this event is cancelled, the {@link LivingEntity} does not take fall damage.</p>
3838
*
39-
* <p>This event is fired on the {@link MinecraftForge#EVENT_BUS}.</p>
39+
* <p>This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.</p>
4040
*/
4141
public class LivingFallEvent extends LivingEvent {
4242
private float distance;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.event.entity.player;
21+
22+
import net.minecraft.entity.Entity;
23+
import net.minecraft.entity.player.PlayerEntity;
24+
25+
/**
26+
* AttackEntityEvent is fired when a player attacks an Entity.
27+
*
28+
* <p>This event is fired whenever a player attacks an Entity in
29+
* {@link PlayerEntity#attack(Entity)} on either the client or the server.
30+
*
31+
* <p>This event is cancellable.
32+
* If this event is canceled, the player does not attack the Entity.
33+
*
34+
* <p>This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.
35+
*/
36+
public class AttackEntityEvent extends PlayerEvent {
37+
private final Entity target;
38+
39+
// For EventBus
40+
public AttackEntityEvent() {
41+
super();
42+
43+
this.target = null;
44+
}
45+
46+
public AttackEntityEvent(PlayerEntity player, Entity target) {
47+
super(player);
48+
49+
this.target = target;
50+
}
51+
52+
/**
53+
* @return the {@link Entity} that was damaged by the player.
54+
*/
55+
public Entity getTarget() {
56+
return target;
57+
}
58+
59+
@Override
60+
public boolean isCancelable() {
61+
return true;
62+
}
63+
}

patchwork-events-entity/src/main/resources/fabric.mod.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"depends": {
1919
"fabricloader": ">=0.4.0",
2020
"fabric": "*",
21-
"patchwork-fml": "*"
21+
"patchwork-fml": "*",
22+
"patchwork-extensions-item": "*"
2223
},
2324
"mixins": [
2425
"patchwork-events-entity.mixins.json"

patchwork-extensions-item/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ Patches: **LivingEntity**
6262
Forge classes: IForgeItemStack
6363
void onUsingTick(ItemStack stack, LivingEntity player, int count)
6464

65-
Forge classes: ForgeHooks
66-
boolean onLeftClickEntity(ItemStack stack, PlayerEntity player, Entity entity)
67-
6865
Patches: **BannerDuplicateRecipe**, **BookCloningRecipe**, **Recipe**, **BrewingStandBlockEntity**, **AbstractFurnaceBlockEntity**
6966
Forge classes: IForgeItemStack, ForgeHooks
7067
ItemStack getContainerItem(ItemStack itemStack)

patchwork-extensions-item/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ default boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, PlayerEntit
194194
default void onUsingTick(ItemStack stack, LivingEntity player, int count) {
195195
}
196196

197-
// TODO: Call locations: Forge classes: ForgeHooks
198197
/**
199198
* Called when the player Left Clicks (attacks) an entity. Processed before
200199
* damage is done, if return value is true further processing is canceled and

0 commit comments

Comments
 (0)