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

Commit cc2cddd

Browse files
Implement ItemFishedEvent (#147)
* Implement ItemFishedEvent * Make event cancelable * Actually use the event's rod damage * </p> nit Co-authored-by: TheGlitch76 <[email protected]>
1 parent 35cf663 commit cc2cddd

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 java.util.List;
23+
24+
import javax.annotation.Nonnegative;
25+
26+
import com.google.common.base.Preconditions;
27+
28+
import net.minecraft.entity.projectile.FishingBobberEntity;
29+
import net.minecraft.item.ItemStack;
30+
import net.minecraft.util.DefaultedList;
31+
32+
/**
33+
* This event is called when a player fishes an item.
34+
*
35+
* <p>This event is cancellable.
36+
* Canceling the event will cause the player to receive no items at all.
37+
* The hook will still take the damage specified</p>
38+
*/
39+
40+
public class ItemFishedEvent extends PlayerEvent {
41+
private final DefaultedList<ItemStack> stacks = DefaultedList.of();
42+
private final FishingBobberEntity hook;
43+
private int rodDamage;
44+
45+
public ItemFishedEvent(List<ItemStack> stacks, int rodDamage, FishingBobberEntity hook) {
46+
super(hook.getOwner());
47+
this.stacks.addAll(stacks);
48+
this.rodDamage = rodDamage;
49+
this.hook = hook;
50+
}
51+
52+
/**
53+
* Get the damage the rod will take.
54+
*
55+
* @return The damage the rod will take
56+
*/
57+
public int getRodDamage() {
58+
return rodDamage;
59+
}
60+
61+
/**
62+
* Specifies the amount of damage that the fishing rod should take.
63+
* This is not added to the pre-existing damage to be taken.
64+
*
65+
* @param rodDamage The damage the rod will take. Must be nonnegative
66+
*/
67+
public void damageRodBy(@Nonnegative int rodDamage) {
68+
Preconditions.checkArgument(rodDamage >= 0);
69+
this.rodDamage = rodDamage;
70+
}
71+
72+
/**
73+
* Use this to get the items the player will receive.
74+
* You cannot use this to modify the drops the player will get.
75+
* If you want to affect the loot, you should use LootTables.
76+
*/
77+
public DefaultedList<ItemStack> getDrops() {
78+
return stacks;
79+
}
80+
81+
/**
82+
* Use this to stuff related to the hook itself, like the position of the bobber.
83+
*/
84+
public FishingBobberEntity getHookEntity() {
85+
return hook;
86+
}
87+
88+
@Override
89+
public boolean isCancelable() {
90+
return true;
91+
}
92+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.List;
23+
24+
import net.minecraftforge.common.MinecraftForge;
25+
import net.minecraftforge.event.entity.player.ItemFishedEvent;
26+
import org.spongepowered.asm.mixin.Mixin;
27+
import org.spongepowered.asm.mixin.Shadow;
28+
import org.spongepowered.asm.mixin.Unique;
29+
import org.spongepowered.asm.mixin.injection.At;
30+
import org.spongepowered.asm.mixin.injection.Inject;
31+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
32+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
33+
34+
import net.minecraft.entity.projectile.FishingBobberEntity;
35+
import net.minecraft.item.ItemStack;
36+
import net.minecraft.loot.LootTable;
37+
import net.minecraft.loot.context.LootContext;
38+
39+
@Mixin(FishingBobberEntity.class)
40+
public abstract class MixinFishingBobberEntity {
41+
@Shadow
42+
private boolean field_7176; // is stuck to block
43+
44+
@Shadow
45+
public abstract void remove();
46+
47+
@Unique
48+
private final ThreadLocal<Integer> rodDamage = ThreadLocal.withInitial(() -> -1);
49+
50+
@Inject(method = "method_6957", at = @At(value = "INVOKE", target = "Lnet/minecraft/advancement/criterion/FishingRodHookedCriterion;trigger(Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/item/ItemStack;Lnet/minecraft/entity/projectile/FishingBobberEntity;Ljava/util/Collection;)V", ordinal = 1), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
51+
private void onReeledIn(ItemStack itemStack, CallbackInfoReturnable<Integer> cir, int i, LootContext.Builder builder, LootTable lootTable, List<ItemStack> list) {
52+
ItemFishedEvent event = new ItemFishedEvent(list, this.field_7176 ? 2 : 1, (FishingBobberEntity) (Object) this);
53+
MinecraftForge.EVENT_BUS.post(event);
54+
55+
if (event.isCanceled()) {
56+
this.remove();
57+
cir.setReturnValue(event.getRodDamage());
58+
} else {
59+
rodDamage.set(event.getRodDamage());
60+
}
61+
}
62+
63+
@Inject(method = "method_6957", at = @At("RETURN"), cancellable = true)
64+
private void onRodDamage(ItemStack itemStack, CallbackInfoReturnable<Integer> cir) {
65+
Integer damage = rodDamage.get();
66+
67+
if (damage != -1) {
68+
cir.setReturnValue(damage);
69+
rodDamage.set(-1);
70+
}
71+
}
72+
}

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
@@ -8,6 +8,7 @@
88
"MixinEntityTrackerEntry",
99
"MixinEntityType",
1010
"MixinExperienceOrbEntity",
11+
"MixinFishingBobberEntity",
1112
"MixinFurnaceOutputSlot",
1213
"MixinItemEntity",
1314
"MixinLivingEntity",

0 commit comments

Comments
 (0)