From 94cbf72e3985786057487840885f139329f51287 Mon Sep 17 00:00:00 2001 From: FireController1847 Date: Mon, 14 Oct 2019 11:37:44 -0600 Subject: [PATCH] Re-add PlayerGiveXpEvent & PlayerChangeLevelEvent (#6165) --- .../entity/player/PlayerEntity.java.patch | 26 +++- .../entity/player/PlayerPickupXpEvent.java | 16 +-- .../event/entity/player/PlayerXpEvent.java | 121 ++++++++++++++++++ .../entity/player/PlayerXpEventTest.java | 54 ++++++++ src/test/resources/META-INF/mods.toml | 4 +- 5 files changed, 207 insertions(+), 14 deletions(-) create mode 100644 src/main/java/net/minecraftforge/event/entity/player/PlayerXpEvent.java create mode 100644 src/test/java/net/minecraftforge/debug/entity/player/PlayerXpEventTest.java diff --git a/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch b/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch index 64161e3d8..9664d1fdc 100644 --- a/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch +++ b/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch @@ -425,7 +425,29 @@ } } -@@ -1703,7 +1818,10 @@ +@@ -1462,6 +1577,10 @@ + } + + public void func_195068_e(int p_195068_1_) { ++ net.minecraftforge.event.entity.player.PlayerXpEvent.XpChange event = new net.minecraftforge.event.entity.player.PlayerXpEvent.XpChange(this, p_195068_1_); ++ if (net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event)) return; ++ p_195068_1_ = event.getAmount(); ++ + this.func_85039_t(p_195068_1_); + this.field_71106_cc += (float)p_195068_1_ / (float)this.func_71050_bK(); + this.field_71067_cb = MathHelper.func_76125_a(this.field_71067_cb + p_195068_1_, 0, Integer.MAX_VALUE); +@@ -1501,6 +1620,10 @@ + } + + public void func_82242_a(int p_82242_1_) { ++ net.minecraftforge.event.entity.player.PlayerXpEvent.LevelChange event = new net.minecraftforge.event.entity.player.PlayerXpEvent.LevelChange(this, p_82242_1_); ++ if (net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event)) return; ++ p_82242_1_ = event.getLevels(); ++ + this.field_71068_ca += p_82242_1_; + if (this.field_71068_ca < 0) { + this.field_71068_ca = 0; +@@ -1703,7 +1826,10 @@ } public ITextComponent func_145748_c_() { @@ -437,7 +459,7 @@ return this.func_208016_c(itextcomponent); } -@@ -1939,4 +2057,44 @@ +@@ -1939,4 +2065,44 @@ return this.field_221260_g; } } diff --git a/src/main/java/net/minecraftforge/event/entity/player/PlayerPickupXpEvent.java b/src/main/java/net/minecraftforge/event/entity/player/PlayerPickupXpEvent.java index 7465fd915..eb3777c9c 100644 --- a/src/main/java/net/minecraftforge/event/entity/player/PlayerPickupXpEvent.java +++ b/src/main/java/net/minecraftforge/event/entity/player/PlayerPickupXpEvent.java @@ -19,27 +19,21 @@ package net.minecraftforge.event.entity.player; -import net.minecraftforge.eventbus.api.Cancelable; import net.minecraft.entity.item.ExperienceOrbEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraftforge.eventbus.api.Cancelable; /** - * This event is called when a player collides with a EntityXPOrb on the ground. - * The event can be canceled, and no further processing will be done. + * Use {@link PlayerXpEvent.PickupXp} */ @Cancelable -public class PlayerPickupXpEvent extends PlayerEvent +@Deprecated +public class PlayerPickupXpEvent extends PlayerXpEvent.PickupXp { - private final ExperienceOrbEntity orb; public PlayerPickupXpEvent(PlayerEntity player, ExperienceOrbEntity orb) { - super(player); - this.orb = orb; + super(player, orb); } - public ExperienceOrbEntity getOrb() - { - return orb; - } } diff --git a/src/main/java/net/minecraftforge/event/entity/player/PlayerXpEvent.java b/src/main/java/net/minecraftforge/event/entity/player/PlayerXpEvent.java new file mode 100644 index 000000000..4b9ceced1 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/player/PlayerXpEvent.java @@ -0,0 +1,121 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.event.entity.player; + +import net.minecraft.entity.item.ExperienceOrbEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.Cancelable; + +/** + * PlayerXpEvent is fired whenever an event involving player experience occurs.
+ * If a method utilizes this {@link net.minecraftforge.eventbus.api.Event} as its parameter, the method will + * receive every child event of this class.
+ *
+ * All children of this event are fired on the {@link MinecraftForge#EVENT_BUS}. + */ +public class PlayerXpEvent extends PlayerEvent +{ + + public PlayerXpEvent(PlayerEntity player) + { + super(player); + } + + /** + * This event is fired after the player collides with an experience orb, but before the player has been given the experience. + * It can be cancelled, and no further processing will be done. + */ + @Cancelable + public static class PickupXp extends PlayerXpEvent + { + + private final ExperienceOrbEntity orb; + + public PickupXp(PlayerEntity player, ExperienceOrbEntity orb) + { + super(player); + this.orb = orb; + } + + public ExperienceOrbEntity getOrb() + { + return orb; + } + + } + + /** + * This event is fired when the player's experience changes through the {@link PlayerEntity#giveExperiencePoints} method. + * It can be cancelled, and no further processing will be done. + */ + @Cancelable + public static class XpChange extends PlayerXpEvent + { + + private int amount; + + public XpChange(PlayerEntity player, int amount) + { + super(player); + this.amount = amount; + } + + public int getAmount() + { + return this.amount; + } + + public void setAmount(int amount) + { + this.amount = amount; + } + + } + + /** + * This event is fired when the player's experience level changes through the {@link PlayerEntity#addExperienceLevel} method. + * It can be cancelled, and no further processing will be done. + */ + @Cancelable + public static class LevelChange extends PlayerXpEvent + { + + private int levels; + + public LevelChange(PlayerEntity player, int levels) + { + super(player); + this.levels = levels; + } + + public int getLevels() + { + return this.levels; + } + + public void setLevels(int levels) + { + this.levels = levels; + } + + } + +} diff --git a/src/test/java/net/minecraftforge/debug/entity/player/PlayerXpEventTest.java b/src/test/java/net/minecraftforge/debug/entity/player/PlayerXpEventTest.java new file mode 100644 index 000000000..2d007e40c --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/entity/player/PlayerXpEventTest.java @@ -0,0 +1,54 @@ +package net.minecraftforge.debug.entity.player; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraftforge.event.entity.player.PlayerPickupXpEvent; +import net.minecraftforge.event.entity.player.PlayerXpEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod("player_xp_event_test") +@Mod.EventBusSubscriber() +public class PlayerXpEventTest +{ + + private static final boolean ENABLE = false; + private static Logger logger = LogManager.getLogger(PlayerXpEventTest.class); + + @SubscribeEvent + public static void onPlayerXpEvent(PlayerXpEvent event) + { + if (!ENABLE) return; + logger.info("The PlayerXpEvent has been called!"); + } + + @SubscribeEvent + public static void onPlayerPickupXpOld(PlayerPickupXpEvent event) + { + if (!ENABLE) return; + logger.info("The deprecated PlayerPickupXpEvent has been called!"); + } + + @SubscribeEvent + public static void onPlayerPickupXp(PlayerXpEvent.PickupXp event) + { + if (!ENABLE) return; + logger.info("{} picked up an experience orb worth {}", event.getPlayer().getName().getString(), event.getOrb().getXpValue()); + } + + @SubscribeEvent + public static void onPlayerXpChange(PlayerXpEvent.XpChange event) + { + if (!ENABLE) return; + logger.info("{} has been given {} experience", event.getPlayer().getName().getString(), event.getAmount()); + } + + @SubscribeEvent + public static void onPlayerLevelChange(PlayerXpEvent.LevelChange event) + { + if (!ENABLE) return; + logger.info("{} has changed {} levels", event.getPlayer().getName().getString(), event.getLevels()); + } + +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 32c28d058..d74f85d9c 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -54,4 +54,6 @@ loaderVersion="[28,)" [[mods]] modId="flower_pot_test" [[mods]] - modId="forgeblockstatesloader" \ No newline at end of file + modId="player_xp_event_test" +[[mods]] + modId="forgeblockstatesloader"