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

Commit 35cf663

Browse files
authored
Impl PlayerChangedDimensionEvent, PlayerRespawnEvent and PlayerLoggedOutEvent (#118)
* Impl PlayerChangedDimensionEvent, PlayerRespawnEvent and PlayerLoggedOutEvent * Impl ItemPickupEvent, ItemCraftedEvent, ItemSmeltedEvent in PlayerEvent * Add methods to the god class * Apply leo60228's suggestion
1 parent f7fc388 commit 35cf663

File tree

10 files changed

+442
-27
lines changed

10 files changed

+442
-27
lines changed

patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/player/PlayerEvent.java

Lines changed: 121 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919

2020
package net.minecraftforge.event.entity.player;
2121

22+
import javax.annotation.Nonnull;
23+
2224
import net.minecraftforge.event.entity.living.LivingEvent;
2325

2426
import net.minecraft.entity.player.PlayerEntity;
27+
import net.minecraft.inventory.Inventory;
28+
import net.minecraft.item.ItemStack;
29+
import net.minecraft.world.dimension.DimensionType;
2530
import net.minecraft.entity.Entity;
31+
import net.minecraft.entity.ItemEntity;
2632

2733
/**
2834
* PlayerEvent is fired whenever an event involving Living entities occurs.
@@ -57,16 +63,6 @@ public PlayerEntity getPlayer() {
5763
return playerEntity;
5864
}
5965

60-
/**
61-
* Called on the server at the end of {@link net.minecraft.server.PlayerManager#onPlayerConnect(net.minecraft.network.ClientConnection, net.minecraft.server.network.ServerPlayerEntity)}
62-
* when the player has finished logging in.
63-
*/
64-
public static class PlayerLoggedInEvent extends PlayerEvent {
65-
public PlayerLoggedInEvent(PlayerEntity player) {
66-
super(player);
67-
}
68-
}
69-
7066
/**
7167
* Fired when an Entity is started to be "tracked" by this player (the player receives updates about this entity, e.g. motion).
7268
*/
@@ -131,17 +127,125 @@ public boolean isWasDeath() {
131127
}
132128
}
133129

130+
public static class ItemPickupEvent extends PlayerEvent {
131+
/**
132+
* Original EntityItem with current remaining stack size.
133+
*/
134+
private final ItemEntity originalEntity;
135+
/**
136+
* Clone item stack, containing the item and amount picked up.
137+
*/
138+
private final ItemStack stack;
139+
140+
public ItemPickupEvent(PlayerEntity player, ItemEntity entPickedUp, ItemStack stack) {
141+
super(player);
142+
this.originalEntity = entPickedUp;
143+
this.stack = stack;
144+
}
145+
146+
public ItemStack getStack() {
147+
return stack;
148+
}
149+
150+
public ItemEntity getOriginalEntity() {
151+
return originalEntity;
152+
}
153+
}
154+
155+
public static class ItemCraftedEvent extends PlayerEvent {
156+
@Nonnull
157+
private final ItemStack crafting;
158+
private final Inventory craftMatrix;
159+
160+
public ItemCraftedEvent(PlayerEntity player, @Nonnull ItemStack crafting, Inventory craftMatrix) {
161+
super(player);
162+
this.crafting = crafting;
163+
this.craftMatrix = craftMatrix;
164+
}
165+
166+
@Nonnull
167+
public ItemStack getCrafting() {
168+
return this.crafting;
169+
}
170+
171+
public Inventory getInventory() {
172+
return this.craftMatrix;
173+
}
174+
}
175+
176+
public static class ItemSmeltedEvent extends PlayerEvent {
177+
@Nonnull
178+
private final ItemStack smelting;
179+
180+
public ItemSmeltedEvent(PlayerEntity player, @Nonnull ItemStack crafting) {
181+
super(player);
182+
this.smelting = crafting;
183+
}
184+
185+
@Nonnull
186+
public ItemStack getSmelting() {
187+
return this.smelting;
188+
}
189+
}
190+
191+
/**
192+
* Called on the server at the end of {@link net.minecraft.server.PlayerManager#onPlayerConnect(net.minecraft.network.ClientConnection, net.minecraft.server.network.ServerPlayerEntity)}
193+
* when the player has finished logging in.
194+
*/
195+
public static class PlayerLoggedInEvent extends PlayerEvent {
196+
public PlayerLoggedInEvent(PlayerEntity player) {
197+
super(player);
198+
}
199+
}
200+
201+
public static class PlayerLoggedOutEvent extends PlayerEvent {
202+
public PlayerLoggedOutEvent(PlayerEntity player) {
203+
super(player);
204+
}
205+
}
206+
207+
public static class PlayerRespawnEvent extends PlayerEvent {
208+
private final boolean alive;
209+
210+
public PlayerRespawnEvent(PlayerEntity player, boolean alive) {
211+
super(player);
212+
this.alive = alive;
213+
}
214+
215+
/**
216+
* Did this respawn event come from the player conquering the end?
217+
* TODO: Forge should name this to isAlive.
218+
* @return if this respawn was because the player conquered the end
219+
*/
220+
public boolean isEndConquered() {
221+
return this.alive;
222+
}
223+
}
224+
225+
public static class PlayerChangedDimensionEvent extends PlayerEvent {
226+
private final DimensionType fromDim;
227+
private final DimensionType toDim;
228+
229+
public PlayerChangedDimensionEvent(PlayerEntity player, DimensionType fromDim, DimensionType toDim) {
230+
super(player);
231+
this.fromDim = fromDim;
232+
this.toDim = toDim;
233+
}
234+
235+
public DimensionType getFrom() {
236+
return this.fromDim;
237+
}
238+
239+
public DimensionType getTo() {
240+
return this.toDim;
241+
}
242+
}
243+
134244
/*TODO Events:
135245
HarvestCheck
136246
BreakSpeed
137247
NameFormat
138248
LoadFromFile
139249
SaveToFile
140-
Visibility
141-
ItemPickupEvent
142-
ItemCraftedEvent
143-
ItemSmeltedEvent
144-
PlayerLoggedOutEvent
145-
PlayerRespawnEvent
146-
PlayerChangedDimensionEvent*/
250+
Visibility called by ForgeHooks.getPlayerVisibilityDistance, but latter is not called elsewhere*/
147251
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
3838
import net.minecraftforge.event.entity.player.AttackEntityEvent;
3939
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
40-
import net.minecraftforge.event.entity.player.PlayerEvent;
4140
import net.minecraftforge.event.entity.player.PlayerFlyableFallEvent;
4241
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
4342
import net.minecraftforge.eventbus.api.Event;
@@ -98,11 +97,6 @@ public static void onEnteringChunk(Entity entity, int newChunkX, int newChunkZ,
9897
MinecraftForge.EVENT_BUS.post(new EntityEvent.EnteringChunk(entity, newChunkX, newChunkZ, oldChunkX, oldChunkZ));
9998
}
10099

101-
// PlayerEvents
102-
public static void onPlayerLoggedIn(PlayerEntity playerEntity) {
103-
MinecraftForge.EVENT_BUS.post(new PlayerEvent.PlayerLoggedInEvent(playerEntity));
104-
}
105-
106100
public static boolean onLivingAttack(LivingEntity entity, DamageSource src, float damage) {
107101
return MinecraftForge.EVENT_BUS.post(new LivingAttackEvent(entity, src, damage));
108102
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.impl.event.entity;
21+
22+
import net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.event.entity.player.PlayerEvent;
24+
25+
import net.minecraft.entity.ItemEntity;
26+
import net.minecraft.entity.player.PlayerEntity;
27+
import net.minecraft.inventory.Inventory;
28+
import net.minecraft.item.ItemStack;
29+
import net.minecraft.world.dimension.DimensionType;
30+
31+
public class PlayerEvents {
32+
public static void firePlayerChangedDimensionEvent(PlayerEntity player, DimensionType fromDim, DimensionType toDim) {
33+
MinecraftForge.EVENT_BUS.post(new PlayerEvent.PlayerChangedDimensionEvent(player, fromDim, toDim));
34+
}
35+
36+
public static void firePlayerLoggedIn(PlayerEntity player) {
37+
MinecraftForge.EVENT_BUS.post(new PlayerEvent.PlayerLoggedInEvent(player));
38+
}
39+
40+
public static void firePlayerLoggedOut(PlayerEntity player) {
41+
MinecraftForge.EVENT_BUS.post(new PlayerEvent.PlayerLoggedOutEvent(player));
42+
}
43+
44+
public static void firePlayerRespawnEvent(PlayerEntity player, boolean alive) {
45+
MinecraftForge.EVENT_BUS.post(new PlayerEvent.PlayerRespawnEvent(player, alive));
46+
}
47+
48+
public static void firePlayerItemPickupEvent(PlayerEntity player, ItemEntity item, ItemStack clone) {
49+
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemPickupEvent(player, item, clone));
50+
}
51+
52+
public static void firePlayerCraftingEvent(PlayerEntity player, ItemStack crafted, Inventory craftMatrix) {
53+
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, crafted, craftMatrix));
54+
}
55+
56+
public static void firePlayerSmeltedEvent(PlayerEntity player, ItemStack smelted) {
57+
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemSmeltedEvent(player, smelted));
58+
}
59+
}
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.patchworkmc.mixin.event.entity;
21+
22+
import org.spongepowered.asm.mixin.Final;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
import org.spongepowered.asm.mixin.Shadow;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28+
import org.spongepowered.asm.mixin.injection.At.Shift;
29+
30+
import net.minecraft.container.CraftingResultSlot;
31+
import net.minecraft.entity.player.PlayerEntity;
32+
import net.minecraft.inventory.CraftingInventory;
33+
import net.minecraft.item.ItemStack;
34+
35+
import net.patchworkmc.impl.event.entity.PlayerEvents;
36+
37+
@Mixin(CraftingResultSlot.class)
38+
public abstract class MixinCraftingResultSlot {
39+
@Shadow
40+
@Final
41+
private PlayerEntity player;
42+
43+
@Shadow
44+
@Final
45+
private CraftingInventory craftingInv;
46+
47+
@Inject(method = "onCrafted(Lnet/minecraft/item/ItemStack;)V",
48+
at = @At(
49+
value = "INVOKE",
50+
shift = Shift.AFTER,
51+
ordinal = 0,
52+
target = "net/minecraft/item/ItemStack.onCraft(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;I)V"
53+
)
54+
)
55+
private void onStackCrafted(ItemStack stack, CallbackInfo ci) {
56+
PlayerEvents.firePlayerCraftingEvent(player, stack, craftingInv);
57+
}
58+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.Final;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
import org.spongepowered.asm.mixin.Shadow;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28+
29+
import net.minecraft.container.FurnaceOutputSlot;
30+
import net.minecraft.entity.player.PlayerEntity;
31+
import net.minecraft.item.ItemStack;
32+
33+
import net.patchworkmc.impl.event.entity.PlayerEvents;
34+
35+
@Mixin(FurnaceOutputSlot.class)
36+
public abstract class MixinFurnaceOutputSlot {
37+
@Shadow
38+
@Final
39+
private PlayerEntity player;
40+
41+
@Inject(method = "onCrafted(Lnet/minecraft/item/ItemStack;)V",
42+
at = @At("RETURN")
43+
)
44+
private void onCraftingFinished(ItemStack stack, CallbackInfo ci) {
45+
PlayerEvents.firePlayerSmeltedEvent(player, stack);
46+
}
47+
}

0 commit comments

Comments
 (0)