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

Commit b9e9615

Browse files
authored
Add the PlayerXpEvent events (add XP points/level, pickup XP orb) (#89)
Add the PlayerXpEvent events (add XP points/level, pickup XP orb)
1 parent d853a3d commit b9e9615

File tree

6 files changed

+204
-1
lines changed

6 files changed

+204
-1
lines changed

patchwork-events-entity/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
archivesBaseName = "patchwork-events-entity"
2-
version = getSubprojectVersion(project, "0.3.0")
2+
version = getSubprojectVersion(project, "0.4.0")
33

44
dependencies {
55
compile project(path: ':patchwork-fml', configuration: 'dev')
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.ExperienceOrbEntity;
23+
import net.minecraft.entity.player.PlayerEntity;
24+
25+
/**
26+
* Legacy version of PlayerXpEvent.PickupXp. Mods should move to PickupXp, and
27+
* this class is removed in 1.15.
28+
*/
29+
@Deprecated
30+
public class PlayerPickupXpEvent extends PlayerXpEvent.PickupXp {
31+
public PlayerPickupXpEvent(PlayerEntity player, ExperienceOrbEntity orb) {
32+
super(player, orb);
33+
}
34+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.ExperienceOrbEntity;
23+
import net.minecraft.entity.player.PlayerEntity;
24+
25+
public abstract class PlayerXpEvent extends PlayerEvent {
26+
public PlayerXpEvent(PlayerEntity player) {
27+
super(player);
28+
}
29+
30+
/**
31+
* An event representing a player picking up an XP orb entity.
32+
* <p>
33+
* For legacy reasons, the instances of this class should actually be of
34+
* type {@link PlayerPickupXpEvent}
35+
* </p>
36+
*/
37+
public static class PickupXp extends CancelablePlayerXpEvent {
38+
private final ExperienceOrbEntity orb;
39+
40+
public PickupXp(PlayerEntity player, ExperienceOrbEntity orb) {
41+
super(player);
42+
this.orb = orb;
43+
}
44+
45+
public ExperienceOrbEntity getOrb() {
46+
return orb;
47+
}
48+
}
49+
50+
public static class XpChange extends CancelablePlayerXpEvent {
51+
private int amount;
52+
53+
public XpChange(PlayerEntity player, int amount) {
54+
super(player);
55+
this.amount = amount;
56+
}
57+
58+
public int getAmount() {
59+
return amount;
60+
}
61+
62+
public void setAmount(int amount) {
63+
this.amount = amount;
64+
}
65+
}
66+
67+
public static class LevelChange extends CancelablePlayerXpEvent {
68+
private int levels;
69+
70+
public LevelChange(PlayerEntity player, int levels) {
71+
super(player);
72+
this.levels = levels;
73+
}
74+
75+
public int getLevels() {
76+
return this.levels;
77+
}
78+
79+
public void setLevels(int levels) {
80+
this.levels = levels;
81+
}
82+
}
83+
84+
// Helper, so we don't have to repeat isCancelable all the time
85+
private static class CancelablePlayerXpEvent extends PlayerXpEvent {
86+
private CancelablePlayerXpEvent(PlayerEntity player) {
87+
super(player);
88+
}
89+
90+
@Override
91+
public boolean isCancelable() {
92+
return true;
93+
}
94+
}
95+
}
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 net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.event.entity.player.PlayerPickupXpEvent;
24+
import org.objectweb.asm.Opcodes;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.injection.At;
27+
import org.spongepowered.asm.mixin.injection.Inject;
28+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
29+
30+
import net.minecraft.entity.player.PlayerEntity;
31+
import net.minecraft.entity.ExperienceOrbEntity;
32+
33+
@Mixin(ExperienceOrbEntity.class)
34+
public class MixinExperienceOrbEntity {
35+
// After checking we're on the server and the player is ready to pick up the orb, the first
36+
// thing the target method does is set experiencePickUpDelay, hence hook just before that.
37+
@Inject(method = "onPlayerCollision", cancellable = true, at = @At(value = "FIELD", opcode = Opcodes.H_PUTFIELD, ordinal = 0,
38+
target = "net/minecraft/entity/player/PlayerEntity.experiencePickUpDelay:I"))
39+
private void hookOnPlayerCollisionForPickup(PlayerEntity player, CallbackInfo ci) {
40+
@SuppressWarnings("ConstantConditions")
41+
ExperienceOrbEntity entity = (ExperienceOrbEntity) (Object) this;
42+
43+
if (MinecraftForge.EVENT_BUS.post(new PlayerPickupXpEvent(player, entity))) {
44+
ci.cancel();
45+
}
46+
}
47+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package net.patchworkmc.mixin.event.entity;
2121

22+
import net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.event.entity.player.PlayerXpEvent;
2224
import org.spongepowered.asm.mixin.Mixin;
2325
import org.spongepowered.asm.mixin.Shadow;
2426
import org.spongepowered.asm.mixin.injection.At;
@@ -128,4 +130,28 @@ private void onAttackEntity(Entity target, CallbackInfo callback) {
128130
callback.cancel();
129131
}
130132
}
133+
134+
@ModifyVariable(method = "addExperience", at = @At("HEAD"), ordinal = 0)
135+
private int onAddExperience(int points) {
136+
@SuppressWarnings("ConstantConditions")
137+
PlayerEntity player = (PlayerEntity) (Object) this;
138+
139+
PlayerXpEvent.XpChange event = new PlayerXpEvent.XpChange(player, points);
140+
MinecraftForge.EVENT_BUS.post(event);
141+
142+
// The only effect of passing in zero is a call to addScore(0), which shouldn't have any effect.
143+
return event.isCanceled() ? 0 : event.getAmount();
144+
}
145+
146+
@ModifyVariable(method = "addExperienceLevels", at = @At("HEAD"), ordinal = 0)
147+
private int onAddExperienceLevels(int levels) {
148+
@SuppressWarnings("ConstantConditions")
149+
PlayerEntity player = (PlayerEntity) (Object) this;
150+
151+
PlayerXpEvent.LevelChange event = new PlayerXpEvent.LevelChange(player, levels);
152+
MinecraftForge.EVENT_BUS.post(event);
153+
154+
// There are no effects from passing in zero levels, so do that if we've been canceled
155+
return event.isCanceled() ? 0 : event.getLevels();
156+
}
131157
}

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
@@ -6,6 +6,7 @@
66
"MixinEntity",
77
"MixinEntityTrackerEntry",
88
"MixinEntityType",
9+
"MixinExperienceOrbEntity",
910
"MixinLivingEntity",
1011
"MixinMobEntity",
1112
"MixinMobSpawnerLogic",

0 commit comments

Comments
 (0)