Skip to content

Commit 46027ab

Browse files
committed
Add Kube event classes for living entities and mob effects, refactor event handler structure
1 parent 41807f8 commit 46027ab

34 files changed

+1037
-260
lines changed

src/main/java/com/mangojellypudding/mangojs/events/ServerEventHandler.java renamed to src/main/java/com/mangojellypudding/mangojs/event/ServerEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mangojellypudding.mangojs.events;
1+
package com.mangojellypudding.mangojs.event;
22

33
import com.mangojellypudding.mangojs.MangoJS;
44
import com.mangojellypudding.mangojs.kubejs.utils.MangoUtilsJS;
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package com.mangojellypudding.mangojs.kubejs.event;
2+
3+
import com.mangojellypudding.mangojs.MangoJS;
4+
import com.mangojellypudding.mangojs.kubejs.event.living.*;
5+
import com.mangojellypudding.mangojs.kubejs.plugin.event.LivingEvents;
6+
import net.minecraft.resources.ResourceKey;
7+
import net.minecraft.world.entity.EntityType;
8+
import net.neoforged.bus.api.SubscribeEvent;
9+
import net.neoforged.fml.common.EventBusSubscriber;
10+
import net.neoforged.neoforge.event.entity.living.*;
11+
import net.neoforged.neoforge.event.entity.living.LivingEvent.*;
12+
13+
@EventBusSubscriber(modid = MangoJS.MODID)
14+
public class KubeLivingEventHandler {
15+
public static ResourceKey<EntityType<?>> getKey(LivingEvent event) {
16+
return event.getEntity().getType().kjs$getKey();
17+
}
18+
19+
@SubscribeEvent
20+
public static void breath(LivingBreatheEvent event) throws Throwable {
21+
var key = getKey(event);
22+
23+
if (LivingEvents.BREATH.hasListeners(key)) {
24+
LivingEvents.BREATH.post(event.getEntity(), key, new LivingBreathKubeEvent(event));
25+
}
26+
}
27+
28+
@SubscribeEvent
29+
public static void changeTarget(LivingChangeTargetEvent event) {
30+
var key = getKey(event);
31+
32+
if (LivingEvents.CHANGE_TARGET.hasListeners(key)) {
33+
LivingEvents.CHANGE_TARGET.post(event.getEntity(), key, new LivingChangeTargetKubeEvent(event));
34+
}
35+
}
36+
37+
@SubscribeEvent
38+
public static void beforeConversion(LivingConversionEvent.Pre event) {
39+
var key = getKey(event);
40+
41+
if (LivingEvents.BEFORE_CONVERSION.hasListeners(key)) {
42+
LivingEvents.BEFORE_CONVERSION.post(event.getEntity(), key, new LivingConversionKubeEvent.Pre(event));
43+
}
44+
}
45+
46+
@SubscribeEvent
47+
public static void afterConversion(LivingConversionEvent.Post event) {
48+
var key = getKey(event);
49+
50+
if (LivingEvents.AFTER_CONVERSION.hasListeners(key)) {
51+
LivingEvents.AFTER_CONVERSION.post(event.getEntity(), key, new LivingConversionKubeEvent.Post(event));
52+
}
53+
}
54+
55+
@SubscribeEvent
56+
public static void destroyBlock(LivingDestroyBlockEvent event) {
57+
var key = getKey(event);
58+
59+
if (LivingEvents.DESTROY_BLOCK.hasListeners(key)) {
60+
LivingEvents.DESTROY_BLOCK.post(event.getEntity(), key, new LivingDestroyBlockKubeEvent(event));
61+
}
62+
}
63+
64+
@SubscribeEvent
65+
public static void drown(LivingDrownEvent event) {
66+
var key = getKey(event);
67+
68+
if (LivingEvents.DROWN.hasListeners(key)) {
69+
LivingEvents.DROWN.post(event.getEntity(), key, new LivingDrownKubeEvent(event));
70+
}
71+
}
72+
73+
@SubscribeEvent
74+
public static void experienceDrops(LivingExperienceDropEvent event) {
75+
var key = getKey(event);
76+
77+
if (LivingEvents.EXPERIENCE_DROPS.hasListeners(key)) {
78+
LivingEvents.EXPERIENCE_DROPS.post(event.getEntity(), key, new LivingExperienceDropKubeEvent(event));
79+
}
80+
}
81+
82+
@SubscribeEvent
83+
public static void fall(LivingFallEvent event) {
84+
var key = getKey(event);
85+
86+
if (LivingEvents.FALL.hasListeners(key)) {
87+
LivingEvents.FALL.post(event.getEntity(), key, new LivingFallKubeEvent(event));
88+
}
89+
}
90+
91+
@SubscribeEvent
92+
public static void getProjectile(LivingGetProjectileEvent event) {
93+
var key = getKey(event);
94+
95+
if (LivingEvents.GET_PROJECTILE.hasListeners(key)) {
96+
LivingEvents.GET_PROJECTILE.post(event.getEntity(), key, new LivingGetProjectileKubeEvent(event));
97+
}
98+
}
99+
100+
@SubscribeEvent
101+
public static void heal(LivingHealEvent event) {
102+
var key = getKey(event);
103+
104+
if (LivingEvents.HEAL.hasListeners(key)) {
105+
LivingEvents.HEAL.post(event.getEntity(), key, new LivingHealKubeEvent(event));
106+
}
107+
}
108+
109+
@SubscribeEvent
110+
public static void incomingDamage(LivingIncomingDamageEvent event) {
111+
var key = getKey(event);
112+
113+
if (LivingEvents.INCOMING_DAMAGE.hasListeners(key)) {
114+
LivingEvents.INCOMING_DAMAGE.post(event.getEntity(), key, new LivingIncomingDamageKubeEvent(event));
115+
}
116+
}
117+
118+
@SubscribeEvent
119+
public static void jump(LivingJumpEvent event) {
120+
var key = getKey(event);
121+
122+
if (LivingEvents.JUMP.hasListeners(key)) {
123+
LivingEvents.JUMP.post(event.getEntity(), key, new LivingJumpKubeEvent(event));
124+
}
125+
}
126+
127+
@SubscribeEvent
128+
public static void knockBack(LivingKnockBackEvent event) {
129+
var key = getKey(event);
130+
131+
if (LivingEvents.KNOCK_BACK.hasListeners(key)) {
132+
LivingEvents.KNOCK_BACK.post(event.getEntity(), key, new LivingKnockBackKubeEvent(event));
133+
}
134+
}
135+
136+
@SubscribeEvent
137+
public static void shieldBlock(LivingShieldBlockEvent event) {
138+
var key = getKey(event);
139+
140+
if (LivingEvents.SHIELD_BLOCK.hasListeners(key)) {
141+
LivingEvents.SHIELD_BLOCK.post(event.getEntity(), key, new LivingShieldBlockKubeEvent(event));
142+
}
143+
}
144+
145+
@SubscribeEvent
146+
public static void swapItem(LivingSwapItemsEvent.Hands event) {
147+
var key = getKey(event);
148+
149+
if (LivingEvents.SWAP_ITEM.hasListeners(key)) {
150+
LivingEvents.SWAP_ITEM.post(event.getEntity(), key, new LivingSwapItemKubeEvent.Hands(event));
151+
}
152+
}
153+
154+
@SubscribeEvent
155+
public static void totem(LivingUseTotemEvent event) {
156+
var key = getKey(event);
157+
158+
if (LivingEvents.TOTEM.hasListeners(key)) {
159+
LivingEvents.TOTEM.post(event.getEntity(), key, new LivingUseTotemKubeEvent(event));
160+
}
161+
}
162+
163+
@SubscribeEvent
164+
public static void useItemFinish(LivingEntityUseItemEvent.Finish event) {
165+
var key = getKey(event);
166+
167+
if (LivingEvents.USE_ITEM_FINISH.hasListeners(key)) {
168+
LivingEvents.USE_ITEM_FINISH.post(event.getEntity(), key, new LivingEntityUseItemKubeEvent.Finish(event));
169+
}
170+
}
171+
172+
@SubscribeEvent
173+
public static void useItemStart(LivingEntityUseItemEvent.Start event) {
174+
var key = getKey(event);
175+
176+
if (LivingEvents.USE_ITEM_START.hasListeners(key)) {
177+
LivingEvents.USE_ITEM_START.post(event.getEntity(), key, new LivingEntityUseItemKubeEvent.Start(event));
178+
}
179+
}
180+
181+
@SubscribeEvent
182+
public static void useItemStop(LivingEntityUseItemEvent.Stop event) {
183+
var key = getKey(event);
184+
185+
if (LivingEvents.USE_ITEM_STOP.hasListeners(key)) {
186+
LivingEvents.USE_ITEM_STOP.post(event.getEntity(), key, new LivingEntityUseItemKubeEvent.Stop(event));
187+
}
188+
}
189+
190+
@SubscribeEvent
191+
public static void useItemTick(LivingEntityUseItemEvent.Tick event) {
192+
var key = getKey(event);
193+
194+
if (LivingEvents.USE_ITEM_TICK.hasListeners(key)) {
195+
LivingEvents.USE_ITEM_TICK.post(event.getEntity(), key, new LivingEntityUseItemKubeEvent.Tick(event));
196+
}
197+
}
198+
199+
@SubscribeEvent
200+
public static void visibility(LivingVisibilityEvent event) {
201+
var key = getKey(event);
202+
203+
if (LivingEvents.VISIBILITY.hasListeners(key)) {
204+
LivingEvents.VISIBILITY.post(event.getEntity(), key, new LivingVisibilityKubeEvent(event));
205+
}
206+
}
207+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.mangojellypudding.mangojs.kubejs.event;
2+
3+
import com.mangojellypudding.mangojs.MangoJS;
4+
import com.mangojellypudding.mangojs.kubejs.event.mob_effect.*;
5+
import com.mangojellypudding.mangojs.kubejs.plugin.event.LivingEvents;
6+
import com.mangojellypudding.mangojs.kubejs.plugin.event.MobEffectEvents;
7+
import net.minecraft.resources.ResourceKey;
8+
import net.minecraft.world.entity.EntityType;
9+
import net.neoforged.bus.api.SubscribeEvent;
10+
import net.neoforged.fml.common.EventBusSubscriber;
11+
import net.neoforged.neoforge.event.entity.living.MobEffectEvent;
12+
import net.neoforged.neoforge.event.entity.living.MobEffectEvent.*;
13+
14+
@EventBusSubscriber(modid = MangoJS.MODID)
15+
public class KubeMobEffectEventHandler {
16+
public static ResourceKey<EntityType<?>> getKey(MobEffectEvent event) {
17+
return event.getEntity().getType().kjs$getKey();
18+
}
19+
20+
@SubscribeEvent
21+
public static void added(Added event) {
22+
var key = getKey(event);
23+
24+
if (MobEffectEvents.ADDED.hasListeners(key)) {
25+
MobEffectEvents.ADDED.post(event.getEntity(), key, new MobEffectAddedKubeEvent(event));
26+
}
27+
}
28+
29+
@SubscribeEvent
30+
public static void removed(Remove event) {
31+
var key = getKey(event);
32+
33+
if (MobEffectEvents.REMOVE.hasListeners(key)) {
34+
MobEffectEvents.REMOVE.post(event.getEntity(), key, new MobEffectRemoveKubeEvent(event));
35+
}
36+
}
37+
38+
@SubscribeEvent
39+
public static void applicable(Applicable event) {
40+
var key = getKey(event);
41+
42+
if (MobEffectEvents.APPLICABLE.hasListeners(key)) {
43+
MobEffectEvents.APPLICABLE.post(event.getEntity(), key, new MobEffectApplicableKubeEvent(event));
44+
}
45+
}
46+
47+
@SubscribeEvent
48+
public static void expired(Expired event) {
49+
var key = getKey(event);
50+
51+
if (MobEffectEvents.EXPIRED.hasListeners(key)) {
52+
MobEffectEvents.EXPIRED.post(event.getEntity(), key, new MobEffectExpiredKubeEvent(event));
53+
}
54+
}
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mangojellypudding.mangojs.kubejs.event.living;
2+
3+
import net.minecraft.world.entity.LivingEntity;
4+
import net.neoforged.neoforge.event.entity.living.LivingEvent;
5+
6+
public class KubeLivingEntityEvent<T extends LivingEvent> implements dev.latvian.mods.kubejs.entity.KubeLivingEntityEvent {
7+
protected T event;
8+
9+
public KubeLivingEntityEvent(T event) {
10+
this.event = event;
11+
}
12+
13+
@Override
14+
public LivingEntity getEntity() {
15+
return event.getEntity();
16+
}
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.mangojellypudding.mangojs.kubejs.event.living;
2+
3+
import dev.latvian.mods.kubejs.typings.Info;
4+
import net.neoforged.neoforge.event.entity.living.LivingBreatheEvent;
5+
6+
public class LivingBreathKubeEvent extends KubeLivingEntityEvent<LivingBreatheEvent> {
7+
8+
public LivingBreathKubeEvent(LivingBreatheEvent event) {
9+
super(event);
10+
}
11+
12+
public boolean getCanBreath() {
13+
return event.canBreathe();
14+
}
15+
16+
public void setCanBreath(boolean canBreath) {
17+
event.setCanBreathe(canBreath);
18+
}
19+
20+
@Info("If the entity cannot breathe, their air value will be reduced by `getConsumeAirAmount()`.")
21+
public int getConsumeAirAmount() {
22+
return event.getConsumeAirAmount();
23+
}
24+
25+
public void setConsumeAirAmount(int consumeAirAmount) {
26+
event.setConsumeAirAmount(consumeAirAmount);
27+
}
28+
29+
@Info("If the entity can breathe, their air value will be increased by `getRefillAirAmount()`.")
30+
public int getRefillAirAmount() {
31+
return event.getRefillAirAmount();
32+
}
33+
34+
public void setRefillAirAmount(int refillAirAmount) {
35+
event.setRefillAirAmount(refillAirAmount);
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mangojellypudding.mangojs.kubejs.event.living;
2+
3+
import dev.latvian.mods.kubejs.typings.Info;
4+
import net.minecraft.world.entity.LivingEntity;
5+
import net.neoforged.neoforge.event.entity.living.LivingChangeTargetEvent;
6+
7+
8+
public class LivingChangeTargetKubeEvent extends KubeLivingEntityEvent<LivingChangeTargetEvent> {
9+
10+
public LivingChangeTargetKubeEvent(LivingChangeTargetEvent event) {
11+
super(event);
12+
}
13+
14+
@Info("returns the target that should originally be set. The return value cannot be affected by calling `setNewAboutToBeSetTarget(LivingEntity)`.")
15+
public LivingEntity getOriginal() {
16+
return event.getOriginalAboutToBeSetTarget();
17+
}
18+
19+
@Info("returns the new target that this entity will have. The return value can be affected by calling `setNewAboutToBeSetTarget(LivingEntity)`.")
20+
public LivingEntity getTarget() {
21+
return event.getNewAboutToBeSetTarget();
22+
}
23+
24+
public void setTarget(LivingEntity target) {
25+
event.setNewAboutToBeSetTarget(target);
26+
}
27+
28+
@Info("returns the target type that caused the change of targets.")
29+
public LivingChangeTargetEvent.ILivingTargetType getTargetType() {
30+
return event.getTargetType();
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.mangojellypudding.mangojs.kubejs.event.living;
2+
3+
import dev.latvian.mods.kubejs.typings.Info;
4+
import net.minecraft.world.entity.EntityType;
5+
import net.minecraft.world.entity.LivingEntity;
6+
import net.neoforged.neoforge.event.entity.living.LivingConversionEvent;
7+
8+
public class LivingConversionKubeEvent extends KubeLivingEntityEvent<LivingConversionEvent> {
9+
public LivingConversionKubeEvent(LivingConversionEvent event) {
10+
super(event);
11+
}
12+
13+
public static class Pre extends KubeLivingEntityEvent<LivingConversionEvent.Pre> {
14+
public Pre(LivingConversionEvent.Pre event) {
15+
super(event);
16+
}
17+
18+
@Info("Gets the entity type of the new entity this living entity is converting to")
19+
public EntityType<? extends LivingEntity> getOutcome() {
20+
return event.getOutcome();
21+
}
22+
}
23+
24+
public static class Post extends KubeLivingEntityEvent<LivingConversionEvent.Post> {
25+
public Post(LivingConversionEvent.Post event) {
26+
super(event);
27+
}
28+
29+
@Info("Gets the entity type of the new entity this living entity is converting to")
30+
public LivingEntity getOutcome() {
31+
return event.getOutcome();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)