Skip to content

Commit

Permalink
Add mythicmob spawn event
Browse files Browse the repository at this point in the history
  • Loading branch information
bailletced committed Jul 24, 2023
1 parent 73e02d6 commit 937dbcf
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 61 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/fortlisa/mythicsk/MythicSk.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import com.fortlisa.mythicsk.api.MythicListener;
import com.fortlisa.mythicsk.skript.skillMetadata.types.Types;
import io.lumine.mythic.bukkit.events.MythicMechanicLoadEvent;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/com/fortlisa/mythicsk/api/MythicListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import com.fortlisa.mythicsk.MythicSk;
import com.fortlisa.mythicsk.api.mechanics.SkriptFunctionMechanic;
import com.fortlisa.mythicsk.api.targets.SkriptFunctionEntityTargeter;
import com.fortlisa.mythicsk.api.events.MythicMobSpawnEvent;
import io.lumine.mythic.api.config.MythicLineConfig;
import io.lumine.mythic.api.skills.SkillManager;
import io.lumine.mythic.bukkit.MythicBukkit;
import io.lumine.mythic.bukkit.events.MythicMechanicLoadEvent;
import io.lumine.mythic.bukkit.events.MythicTargeterLoadEvent;
import io.lumine.mythic.core.mobs.ActiveMob;
import io.lumine.mythic.core.skills.SkillExecutor;
import io.lumine.mythic.core.skills.targeters.LocationTargeter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.scheduler.BukkitRunnable;

public class MythicListener implements Listener {
public static void register() {
Expand All @@ -25,6 +28,27 @@ public MythicListener() {
MythicSk.instance.getServer().getPluginManager().registerEvents(this, MythicSk.instance);
}

@EventHandler
public void onCreatureSpawnEvent(CreatureSpawnEvent e) {
if (e.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.CUSTOM) && !e.isCancelled()) {
Entity bukkitEntity = e.getEntity();
new BukkitRunnable() {
@Override
public void run() {
if (!(MythicBukkit.inst().getAPIHelper().isMythicMob(bukkitEntity))) return;
ActiveMob activeMob = MythicBukkit.inst().getAPIHelper().getMythicMobInstance(bukkitEntity);
//TODO : implement logic related to spawner
// if (activeMob.getSpawner()!=null) {
// mmMythicSpawnerSpawnEvent e = new mmMythicSpawnerSpawnEvent(am.getSpawner(), am);
// Bukkit.getServer().getPluginManager().callEvent(e);
// }
MythicMobSpawnEvent e = new MythicMobSpawnEvent(activeMob);
Bukkit.getServer().getPluginManager().callEvent(e);
}
}.runTaskLater(MythicSk.instance, 1);
}
}

@EventHandler
public void onMythicMobsCustomMechanicsLoad(MythicMechanicLoadEvent e) {
switch(e.getMechanicName().toLowerCase()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fortlisa.mythicsk.api.events;

import io.lumine.mythic.core.mobs.ActiveMob;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

public class MythicMobSpawnEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private ActiveMob activeMob;

public MythicMobSpawnEvent(ActiveMob activeMob) {
this.activeMob = activeMob;
}

public ActiveMob getActiveMob() {
return this.activeMob;
}

public Entity getEntity() {
return this.activeMob.getEntity().getBukkitEntity();
}

@Override
public @NotNull HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,24 @@ public class SkriptFunctionMechanic extends SkillMechanic implements INoTargetSk
private static final int LOCATION_POS=2;
Function<?> function;
String name;
Object[][] parameters;
int dataPos,locationPos,entityPos;

public SkriptFunctionMechanic(SkillExecutor se, String skill, MythicLineConfig mlc) {
super(se, skill, mlc);
Bukkit.getLogger().info("Mechanic passe : "+se.getTargeters().toString());
this.name=mlc.getString("name","");
this.function=Functions.getFunction(name);

if(function!=null) {
// parameters = new Object[function.getParameters().length][];
// for (int i = 0; i < function.getParameters().length; i++) {
// String type = function.getParameter(i).getType().getCodeName();
// switch (type) {
// case "skilldata":
// dataPos = i;
// break;
// case "location":
// locationPos = i;
// break;
// case "entity":
// entityPos = i;
// break;
// }
// }
}
else {
if(function==null) {
Bukkit.getLogger().warning("Cant find function "+name);
}
}

@Override
public SkillResult cast(SkillMetadata skillMetadata) {
Bukkit.getLogger().info("entityTargets"+skillMetadata.getEntityTargets().toString());
// Bukkit.getLogger().info(parameters.toString());
this.function.execute(new Object[][] {
{skillMetadata},
});

return SkillResult.SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fortlisa.mythicsk.api.targets;

import ch.njol.skript.lang.function.Function;
import ch.njol.skript.util.Task;
import com.fortlisa.mythicsk.MythicSk;
import io.lumine.mythic.api.adapters.AbstractEntity;
import io.lumine.mythic.api.config.MythicLineConfig;
Expand All @@ -11,18 +10,11 @@
import io.lumine.mythic.core.skills.targeters.IEntitySelector;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.scheduler.BukkitScheduler;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.ExecutionException;

public class SkriptFunctionEntityTargeter extends IEntitySelector {
Function<Entity[]> function;
Object[][]parameters;

private static BukkitScheduler scheduler;

public SkriptFunctionEntityTargeter(SkillExecutor se, MythicLineConfig mlc, Function skriptFunction) {
super(se, mlc);
Expand All @@ -41,12 +33,9 @@ public HashSet<AbstractEntity> getEntities(SkillMetadata skillMetadata) {
});
if(result!=null) {
for(int i=0;i<result.length;i++) {
Bukkit.getLogger().info("Addin 1 tareget");
targets.add(BukkitAdapter.adapt((Entity) result[i]));
}
}
Bukkit.getLogger().info("result"+targets.toString());
Bukkit.getLogger().info("PASSE BEFORE");
return targets;
}
).get();
Expand All @@ -55,27 +44,5 @@ public HashSet<AbstractEntity> getEntities(SkillMetadata skillMetadata) {
} catch (ExecutionException e) {
throw new RuntimeException(e);
}

// Task.callSync(() -> function.execute(new Object[][] {
// {skillMetadata},
// }), MythicSk.instance);

// Object[] result =
// Task.callSync(() -> function.execute(new Object[][] {
// {skillMetadata},
// }));

// HashSet<AbstractEntity> targets = new HashSet<>();
// if(result!=null) {
// for(int i=0;i<result.length;i++) {
// Bukkit.getLogger().info("Addin 1 tareget");
// targets.add(BukkitAdapter.adapt((Entity) result[i]));
// }
// }

// Bukkit.getLogger().info("result"+targets.toString());
//
// Bukkit.getLogger().info("PASSE AFTER");
// return targets;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fortlisa.mythicsk.skript.skillMetadata.types;
package com.fortlisa.mythicsk.skript;

import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
Expand All @@ -7,6 +7,7 @@
import ch.njol.skript.registrations.Classes;
import io.lumine.mythic.api.skills.Skill;
import io.lumine.mythic.api.skills.SkillMetadata;
import io.lumine.mythic.core.mobs.ActiveMob;

public class Types {
static {
Expand All @@ -31,5 +32,27 @@ public SkillMetadata parse(String string, ParseContext context) {
}
})
);

Classes.registerClass(new ClassInfo<ActiveMob>(ActiveMob.class, "activemob")
.user("activemob")
.name("activemob")
.defaultExpression(new EventValueExpression<>(ActiveMob.class))
.parser(new Parser<ActiveMob>() {
@Override
public String toString(ActiveMob activeMob, int i) {
return activeMob.getType().getInternalName();
}

@Override
public String toVariableNameString(ActiveMob activeMob) {
return activeMob.getUniqueId().toString();
}

@Override
public ActiveMob parse(String string, ParseContext context) {
return null;
}
})
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fortlisa.mythicsk.skript.events;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.util.SimpleEvent;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Getter;
import com.fortlisa.mythicsk.api.events.MythicMobSpawnEvent;
import io.lumine.mythic.core.mobs.ActiveMob;
import org.bukkit.entity.Entity;

import javax.annotation.Nullable;

public class SimpleEvents {
static {
//MythicMob spawn event
Skript.registerEvent("MythicMob spawn", SimpleEvent.class, MythicMobSpawnEvent.class,
"mythicmob spawn")
.description("Called when a mythicMob spawn.")
.examples("");
EventValues.registerEventValue(MythicMobSpawnEvent.class, ActiveMob.class, new Getter<ActiveMob, MythicMobSpawnEvent>() {
@Override
public @Nullable ActiveMob get(MythicMobSpawnEvent event) {
return event.getActiveMob();
}
}, 0);

EventValues.registerEventValue(MythicMobSpawnEvent.class, Entity.class, new Getter<Entity, MythicMobSpawnEvent>() {
@Override
@Nullable
public Entity get(MythicMobSpawnEvent e) {
return e.getEntity();
}}, 0);
}
}

0 comments on commit 937dbcf

Please sign in to comment.