Skip to content

Commit 41807f8

Browse files
committed
Add living entity event classes and update package structure
need fix: com.mangojellypudding.mangojs.kubejs.living.MangoJSLivingEventHandler.defaultHandler
1 parent c934163 commit 41807f8

22 files changed

+349
-26
lines changed

src/main/java/com/mangojellypudding/mangojs/kubejs/MangoJSPlugin.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mangojellypudding.mangojs.kubejs.living;
2+
3+
import dev.latvian.mods.kubejs.entity.KubeLivingEntityEvent;
4+
import dev.latvian.mods.kubejs.typings.Info;
5+
import net.minecraft.world.entity.EntityType;
6+
import net.minecraft.world.entity.LivingEntity;
7+
import net.neoforged.neoforge.event.entity.living.LivingConversionEvent;
8+
9+
public class AfterLivingConversionKubeEvent implements KubeLivingEntityEvent {
10+
LivingEntity entity;
11+
LivingEntity outcome;
12+
13+
public AfterLivingConversionKubeEvent(LivingConversionEvent.Post event) {
14+
this.entity = event.getEntity();
15+
this.outcome = event.getOutcome();
16+
}
17+
@Override
18+
public LivingEntity getEntity() {
19+
return null;
20+
}
21+
22+
@Info("Gets the entity type of the new entity this living entity is converting to")
23+
public LivingEntity getOutcome() {
24+
return outcome;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mangojellypudding.mangojs.kubejs.living;
2+
3+
import dev.latvian.mods.kubejs.entity.KubeLivingEntityEvent;
4+
import dev.latvian.mods.kubejs.typings.Info;
5+
import net.minecraft.world.entity.EntityType;
6+
import net.minecraft.world.entity.LivingEntity;
7+
import net.neoforged.neoforge.event.entity.living.LivingConversionEvent;
8+
9+
public class BeforeLivingConversionKubeEvent implements KubeLivingEntityEvent {
10+
LivingEntity entity;
11+
EntityType<? extends LivingEntity> outcome;
12+
13+
public BeforeLivingConversionKubeEvent(LivingConversionEvent.Pre event) {
14+
this.entity = event.getEntity();
15+
this.outcome = event.getOutcome();
16+
}
17+
@Override
18+
public LivingEntity getEntity() {
19+
return null;
20+
}
21+
22+
@Info("Gets the entity type of the new entity this living entity is converting to")
23+
public EntityType<? extends LivingEntity> getOutcome() {
24+
return outcome;
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mangojellypudding.mangojs.kubejs.living;
2+
3+
import net.minecraft.world.entity.LivingEntity;
4+
import net.neoforged.neoforge.event.entity.living.LivingEvent;
5+
6+
public class KubeLivingEntityEvent implements dev.latvian.mods.kubejs.entity.KubeLivingEntityEvent {
7+
public KubeLivingEntityEvent(LivingEvent event) {
8+
this.entity = event.getEntity();
9+
}
10+
11+
private final LivingEntity entity;
12+
13+
@Override
14+
public LivingEntity getEntity() {
15+
return entity;
16+
}
17+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.mangojellypudding.mangojs.kubejs.living;
2+
3+
//import dev.latvian.mods.kubejs.entity.KubeLivingEntityEvent;
4+
import dev.latvian.mods.kubejs.typings.Info;
5+
import net.minecraft.world.entity.LivingEntity;
6+
import net.neoforged.neoforge.event.entity.living.LivingBreatheEvent;
7+
8+
public class LivingBreathKubeEvent extends KubeLivingEntityEvent {
9+
//public class LivingBreathKubeEvent implements KubeLivingEntityEvent {
10+
// private final LivingEntity entity;
11+
private boolean canBreath;
12+
private int consumeAirAmount;
13+
private int refillAirAmount;
14+
15+
public LivingBreathKubeEvent(LivingBreatheEvent event) {
16+
super(event);
17+
// this.entity = event.getEntity();
18+
this.canBreath = event.canBreathe();
19+
this.consumeAirAmount = event.getConsumeAirAmount();
20+
this.refillAirAmount = event.getRefillAirAmount();
21+
}
22+
23+
// @Override
24+
// public LivingEntity getEntity() {
25+
// return entity;
26+
// }
27+
28+
public boolean getCanBreath() {
29+
return canBreath;
30+
}
31+
32+
public void setCanBreath(boolean canBreath) {
33+
this.canBreath = canBreath;
34+
}
35+
36+
@Info("If the entity cannot breathe, their air value will be reduced by `getConsumeAirAmount()`.")
37+
public int getConsumeAirAmount() {
38+
return consumeAirAmount;
39+
}
40+
41+
public void setConsumeAirAmount(int consumeAirAmount) {
42+
this.consumeAirAmount = consumeAirAmount;
43+
}
44+
45+
@Info("If the entity can breathe, their air value will be increased by `getRefillAirAmount()`.")
46+
public int getRefillAirAmount() {
47+
return refillAirAmount;
48+
}
49+
50+
public void setRefillAirAmount(int refillAirAmount) {
51+
this.refillAirAmount = refillAirAmount;
52+
}
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.mangojellypudding.mangojs.kubejs.living;
2+
3+
import dev.latvian.mods.kubejs.entity.KubeLivingEntityEvent;
4+
import dev.latvian.mods.kubejs.typings.Info;
5+
import net.minecraft.world.entity.LivingEntity;
6+
import net.neoforged.neoforge.event.entity.living.LivingChangeTargetEvent;
7+
import net.neoforged.neoforge.event.entity.living.LivingChangeTargetEvent.LivingTargetType;
8+
9+
10+
public class LivingChangeTargetKubeEvent implements KubeLivingEntityEvent {
11+
LivingEntity entity;
12+
LivingEntity original;
13+
LivingEntity target;
14+
LivingTargetType targetType;
15+
16+
public LivingChangeTargetKubeEvent(LivingChangeTargetEvent event) {
17+
this.entity = event.getEntity();
18+
this.original = event.getOriginalAboutToBeSetTarget();
19+
this.target = event.getNewAboutToBeSetTarget();
20+
this.targetType = (LivingTargetType) event.getTargetType();
21+
}
22+
23+
@Override
24+
public LivingEntity getEntity() {
25+
return null;
26+
}
27+
28+
@Info("returns the target that should originally be set. The return value cannot be affected by calling `setNewAboutToBeSetTarget(LivingEntity)`.")
29+
public LivingEntity getOriginal() {
30+
return original;
31+
}
32+
33+
@Info("returns the new target that this entity will have. The return value can be affected by calling `setNewAboutToBeSetTarget(LivingEntity)`.")
34+
public LivingEntity getTarget() {
35+
return target;
36+
}
37+
38+
public void setTarget(LivingEntity target) {
39+
this.target = target;
40+
}
41+
42+
@Info("returns the target type that caused the change of targets.")
43+
public LivingTargetType getTargetType() {
44+
return targetType;
45+
}
46+
47+
public void setTargetType(LivingTargetType targetType) {
48+
this.targetType = targetType;
49+
}
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mangojellypudding.mangojs.kubejs.living;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.world.level.block.state.BlockState;
5+
import net.neoforged.neoforge.event.entity.living.LivingDestroyBlockEvent;
6+
7+
public class LivingDestroyBlockKubeEvent extends KubeLivingEntityEvent {
8+
BlockPos pos;
9+
BlockState state;
10+
public LivingDestroyBlockKubeEvent(LivingDestroyBlockEvent event) {
11+
super(event);
12+
this.pos = event.getPos();
13+
this.state = event.getState();
14+
}
15+
16+
public BlockPos getPos() {
17+
return pos;
18+
}
19+
20+
public BlockState getState() {
21+
return state;
22+
}
23+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.mangojellypudding.mangojs.kubejs.living;
2+
3+
import com.mangojellypudding.mangojs.MangoJS;
4+
import com.mangojellypudding.mangojs.kubejs.plugin.event.LivingEvents;
5+
import dev.latvian.mods.kubejs.event.TargetedEventHandler;
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+
12+
@EventBusSubscriber(modid = MangoJS.MODID)
13+
public class MangoJSLivingEventHandler {
14+
public static ResourceKey<EntityType<?>> getKey(LivingEvent event) {
15+
return event.getEntity().getType().kjs$getKey();
16+
}
17+
18+
public static void defaultHandler(
19+
LivingEvent event,
20+
TargetedEventHandler<ResourceKey<EntityType<?>>> handler,
21+
Class<? extends KubeLivingEntityEvent> kubeEvent) throws Throwable {
22+
var key = getKey(event);
23+
24+
if (handler.hasListeners(key)) {
25+
handler.post(event.getEntity(), key, kubeEvent.getConstructor(LivingEvent.class).newInstance(event));
26+
}
27+
}
28+
29+
@SubscribeEvent
30+
public static void breath(LivingBreatheEvent event) throws Throwable {
31+
// var key = getKey(event);
32+
//
33+
// if (LivingEvents.BREATH.hasListeners(key)) {
34+
// LivingEvents.BREATH.post(event.getEntity(), key, new LivingBreathKubeEvent(event));
35+
// }
36+
defaultHandler(event, LivingEvents.BREATH, LivingBreathKubeEvent.class);
37+
}
38+
39+
@SubscribeEvent
40+
public static void changeTarget(LivingChangeTargetEvent event) {
41+
var key = getKey(event);
42+
43+
if (LivingEvents.CHANGE_TARGET.hasListeners(key)) {
44+
LivingEvents.CHANGE_TARGET.post(event.getEntity(), key, new LivingChangeTargetKubeEvent(event));
45+
}
46+
}
47+
48+
@SubscribeEvent
49+
public static void beforeConversion(LivingConversionEvent.Pre event) {
50+
var key = getKey(event);
51+
52+
if (LivingEvents.BEFORE_CONVERSION.hasListeners(key)) {
53+
LivingEvents.BEFORE_CONVERSION.post(event.getEntity(), key, new BeforeLivingConversionKubeEvent(event));
54+
}
55+
}
56+
57+
@SubscribeEvent
58+
public static void afterConversion(LivingConversionEvent.Post event) {
59+
var key = getKey(event);
60+
61+
if (LivingEvents.AFTER_CONVERSION.hasListeners(key)) {
62+
LivingEvents.AFTER_CONVERSION.post(event.getEntity(), key, new AfterLivingConversionKubeEvent(event));
63+
}
64+
}
65+
66+
@SubscribeEvent
67+
public static void destroyBlock(LivingDestroyBlockEvent event) throws Throwable {
68+
// var key = getKey(event);
69+
//
70+
// if (LivingEvents.DESTROY_BLOCK.hasListeners(key)) {
71+
// LivingEvents.DESTROY_BLOCK.post(event.getEntity(), key, new LivingDestroyBlockKubeEvent(event));
72+
// }
73+
defaultHandler(event, LivingEvents.DESTROY_BLOCK, LivingDestroyBlockKubeEvent.class);
74+
}
75+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.mangojellypudding.mangojs.kubejs.plugin;
2+
3+
import com.mangojellypudding.mangojs.kubejs.plugin.bindings.MangoUtils;
4+
import com.mangojellypudding.mangojs.kubejs.plugin.event.LivingEvents;
5+
import com.mangojellypudding.mangojs.kubejs.plugin.wrapper.BlockPatternBuilderWrapper;
6+
import dev.latvian.mods.kubejs.event.EventGroupRegistry;
7+
import dev.latvian.mods.kubejs.plugin.KubeJSPlugin;
8+
import dev.latvian.mods.kubejs.script.BindingRegistry;
9+
10+
public class MangoJSPlugin implements KubeJSPlugin {
11+
@Override
12+
public void registerBindings(BindingRegistry bindings) {
13+
bindings.add("MangoUtils", MangoUtils.class);
14+
15+
bindings.add("BlockPatternBuilder", BlockPatternBuilderWrapper.class);
16+
}
17+
18+
@Override
19+
public void registerEvents(EventGroupRegistry registry) {
20+
registry.register(LivingEvents.GROUP);
21+
}
22+
}

src/main/java/com/mangojellypudding/mangojs/kubejs/bindings/MangoUtils.java renamed to src/main/java/com/mangojellypudding/mangojs/kubejs/plugin/bindings/MangoUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.mangojellypudding.mangojs.kubejs.bindings;
1+
package com.mangojellypudding.mangojs.kubejs.plugin.bindings;
22

3-
import com.mangojellypudding.mangojs.kubejs.bindings.modules.*;
3+
import com.mangojellypudding.mangojs.kubejs.plugin.bindings.modules.*;
44
import com.mangojellypudding.mangojs.kubejs.utils.MangoUtilsJS;
55
import com.mangojellypudding.mangojs.utils.Pair;
66
import dev.latvian.mods.kubejs.typings.Info;

0 commit comments

Comments
 (0)