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

Commit 9d5cca0

Browse files
AnimalTameEvent 2: Electric Boogaloo (#156)
* Implement AnimalTameEvent * Missed MCP -> Yarn names in a JavaDoc * HorseBoneWithPlayerGoal -> HorseBondWithPlayerGoal * Make event cancelable. * Remove unneeded ISE * Replace cursed Inject with less cursed Redirect * Remove trailing whitespace Co-authored-by: Glitch <[email protected]>
1 parent cc2cddd commit 9d5cca0

File tree

7 files changed

+232
-0
lines changed

7 files changed

+232
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.living;
21+
22+
import net.minecraftforge.common.MinecraftForge;
23+
24+
import net.minecraft.entity.passive.AnimalEntity;
25+
import net.minecraft.entity.player.PlayerEntity;
26+
27+
/**
28+
* This event is fired when an {@link AnimalEntity} is tamed.
29+
*
30+
* <p>It is fired via ForgeEventFactory#onAnimalTame(AnimalEntity, PlayerEntity).
31+
* Forge fires this event for applicable vanilla animals, mods need to fire it themselves.
32+
* This event is cancellable. If canceled, taming the animal will fail.
33+
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
34+
*/
35+
36+
public class AnimalTameEvent extends LivingEvent {
37+
private final AnimalEntity animal;
38+
private final PlayerEntity tamer;
39+
40+
public AnimalTameEvent(AnimalEntity animal, PlayerEntity tamer) {
41+
super(animal);
42+
this.animal = animal;
43+
this.tamer = tamer;
44+
}
45+
46+
public AnimalEntity getAnimal() {
47+
return animal;
48+
}
49+
50+
public PlayerEntity getTamer() {
51+
return tamer;
52+
}
53+
54+
@Override
55+
public boolean isCancelable() {
56+
return true;
57+
}
58+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import net.minecraftforge.common.extensions.IForgeItem;
2727
import net.minecraftforge.event.entity.EntityEvent;
2828
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
29+
import net.minecraftforge.event.entity.living.AnimalTameEvent;
2930
import net.minecraftforge.event.entity.living.LivingAttackEvent;
3031
import net.minecraftforge.event.entity.living.LivingDamageEvent;
3132
import net.minecraftforge.event.entity.living.LivingDeathEvent;
@@ -53,6 +54,7 @@
5354
import net.minecraft.entity.SpawnType;
5455
import net.minecraft.entity.damage.DamageSource;
5556
import net.minecraft.entity.mob.MobEntity;
57+
import net.minecraft.entity.passive.AnimalEntity;
5658
import net.minecraft.entity.player.PlayerEntity;
5759
import net.minecraft.item.ItemStack;
5860
import net.minecraft.text.Text;
@@ -190,6 +192,10 @@ public static void onItemTooltip(ItemStack itemStack, PlayerEntity entityPlayer,
190192
MinecraftForge.EVENT_BUS.post(new ItemTooltipEvent(itemStack, entityPlayer, list, flags));
191193
}
192194

195+
public static boolean onAnimalTame(AnimalEntity animal, PlayerEntity tamer) {
196+
return MinecraftForge.EVENT_BUS.post(new AnimalTameEvent(animal, tamer));
197+
}
198+
193199
@Override
194200
public void onInitialize() {
195201
UseItemCallback.EVENT.register((player, world, hand) -> {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.patchworkmc.mixin.event.entity;
21+
22+
import java.util.Random;
23+
24+
import org.spongepowered.asm.mixin.Final;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.Shadow;
27+
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Redirect;
29+
30+
import net.minecraft.entity.ai.goal.HorseBondWithPlayerGoal;
31+
import net.minecraft.entity.passive.HorseBaseEntity;
32+
import net.minecraft.entity.player.PlayerEntity;
33+
34+
import net.patchworkmc.impl.event.entity.EntityEvents;
35+
36+
@Mixin(HorseBondWithPlayerGoal.class)
37+
public class MixinHorseBondWithPlayerGoal {
38+
@Shadow
39+
@Final
40+
private HorseBaseEntity horse;
41+
42+
@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 1))
43+
private int redirectHorseBondWithPlayerCheck(Random random, int bound) {
44+
int temper = horse.getTemper();
45+
int nextInt = random.nextInt(bound);
46+
47+
if (nextInt < temper) {
48+
if (EntityEvents.onAnimalTame(horse, (PlayerEntity) horse.getPassengerList().get(0))) {
49+
return Integer.MAX_VALUE; // Force nextInt > temper
50+
}
51+
}
52+
53+
return nextInt;
54+
}
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.patchworkmc.mixin.event.entity;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.Shadow;
24+
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.Inject;
26+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
27+
28+
import net.minecraft.entity.passive.AnimalEntity;
29+
import net.minecraft.entity.passive.OcelotEntity;
30+
import net.minecraft.entity.player.PlayerEntity;
31+
import net.minecraft.util.Hand;
32+
33+
import net.patchworkmc.impl.event.entity.EntityEvents;
34+
35+
@Mixin(OcelotEntity.class)
36+
public abstract class MixinOcelotEntity extends AnimalEntity {
37+
@Shadow
38+
protected abstract void showEmoteParticle(boolean positive);
39+
40+
protected MixinOcelotEntity() {
41+
//noinspection ConstantConditions
42+
super(null, null);
43+
}
44+
45+
@Inject(method = "interactMob", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/passive/OcelotEntity;setTrusting(Z)V"), cancellable = true)
46+
private void onOcelotTrusting(PlayerEntity player, Hand hand, CallbackInfoReturnable<Boolean> cir) {
47+
if (EntityEvents.onAnimalTame(this, player)) {
48+
this.showEmoteParticle(false);
49+
this.world.sendEntityStatus(this, (byte) 40);
50+
cir.setReturnValue(true);
51+
}
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.patchworkmc.mixin.event.entity;
21+
22+
import java.util.Random;
23+
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Redirect;
27+
28+
import net.minecraft.entity.passive.ParrotEntity;
29+
import net.minecraft.entity.passive.TameableEntity;
30+
import net.minecraft.entity.passive.WolfEntity;
31+
import net.minecraft.entity.player.PlayerEntity;
32+
import net.minecraft.util.Hand;
33+
34+
import net.patchworkmc.impl.event.entity.EntityEvents;
35+
36+
// CatEntity is intentionally omitted. See MinecraftForge/#7171
37+
@Mixin({ParrotEntity.class, WolfEntity.class})
38+
public abstract class MixinTameableEntitySubclass extends TameableEntity {
39+
protected MixinTameableEntitySubclass() {
40+
//noinspection ConstantConditions
41+
super(null, null);
42+
}
43+
44+
@Redirect(method = "interactMob", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I"))
45+
private int redirectTameCheck(Random random, int bound, PlayerEntity player, Hand hand) {
46+
int i = random.nextInt(bound);
47+
48+
if (i != 0 || EntityEvents.onAnimalTame(this, player)) {
49+
return -1; // Check failed
50+
}
51+
52+
return 0; // Check succeeds
53+
}
54+
}

patchwork-events-entity/src/main/resources/patchwork-events-entity.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"MixinExperienceOrbEntity",
1111
"MixinFishingBobberEntity",
1212
"MixinFurnaceOutputSlot",
13+
"MixinHorseBondWithPlayerGoal",
1314
"MixinItemEntity",
1415
"MixinLivingEntity",
1516
"MixinMobEntity",

patchwork-god-classes/src/main/java/net/minecraftforge/event/ForgeEventFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import net.minecraft.entity.SpawnType;
2929
import net.minecraft.entity.mob.MobEntity;
30+
import net.minecraft.entity.passive.AnimalEntity;
3031
import net.minecraft.entity.player.PlayerEntity;
3132
import net.minecraft.loot.LootManager;
3233
import net.minecraft.loot.LootTable;
@@ -82,4 +83,8 @@ public static LootTable loadLootTable(Identifier name, LootTable table, LootMana
8283
public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player) {
8384
return WorldEvents.fireBlockHarvesting(drops, world, pos, state, fortune, dropChance, silkTouch, player);
8485
}
86+
87+
public static boolean onAnimalTame(AnimalEntity animal, PlayerEntity tamer) {
88+
return EntityEvents.onAnimalTame(animal, tamer);
89+
}
8590
}

0 commit comments

Comments
 (0)