Skip to content

Commit

Permalink
move 'time changes' to a script event
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 15, 2020
1 parent d73d2e8 commit 2357348
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 43 deletions.
Expand Up @@ -210,6 +210,7 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(new PotionSplashScriptEvent());
ScriptEvent.registerScriptEvent(new SpawnChangeScriptEvent());
ScriptEvent.registerScriptEvent(new StructureGrowsScriptEvent());
ScriptEvent.registerScriptEvent(new TimeChangeScriptEvent());
ScriptEvent.registerScriptEvent(new WeatherChangesScriptEvent());
ScriptEvent.registerScriptEvent(new WorldInitsScriptEvent());
ScriptEvent.registerScriptEvent(new WorldLoadsScriptEvent());
Expand Down
@@ -0,0 +1,72 @@
package com.denizenscript.denizen.events.world;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.event.Listener;

public class TimeChangeScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// time changes (in <world>)
// time <0-23> in <world>
//
// @Regex ^on time [^\s]+( in [^\s]+)$
//
// @Triggers when the current time changes in a world (once per mine-hour).
//
// @Context
// <context.time> returns the current time (the hour, as a number from 0 to 23).
// <context.world> returns the world.
//
// -->

public TimeChangeScriptEvent() {
instance = this;
}

public static TimeChangeScriptEvent instance;

public int hour;

public WorldTag world;

@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("time")) {
return false;
}
String arg1 = path.eventArgLowerAt(1);
if (!arg1.equals("changes") && !ArgumentHelper.matchesInteger(arg1)) {
return false;
}
return true;
}

@Override
public boolean matches(ScriptPath path) {
if (path.eventArgLowerAt(2).equals("in") && !runGenericCheck(path.eventArgLowerAt(3), world.getName())) {
return false;
}
return super.matches(path);
}

@Override
public String getName() {
return "TimeChanges";
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("time")) {
return new ElementTag(hour);
}
else if (name.equals("world")) {
return world;
}
return super.getContext(name);
}
}
@@ -1,14 +1,11 @@
package com.denizenscript.denizen.scripts.containers.core;

import com.denizenscript.denizen.events.world.TimeChangeScriptEvent;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.ScoreboardHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.utilities.Settings;
import com.denizenscript.denizencore.events.OldEventManager;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
Expand All @@ -20,9 +17,7 @@
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.scoreboard.Scoreboard;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BukkitWorldScriptHelper implements Listener {
Expand All @@ -31,17 +26,6 @@ public BukkitWorldScriptHelper() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}

public static String doEvents(List<String> events, NPCTag npc, PlayerTag player, Map<String, ObjectTag> context, boolean useids) {
List<String> determ;
if (useids) {
determ = OldEventManager.doEvents(events, new BukkitScriptEntryData(player, npc), context, true);
}
else {
determ = OldEventManager.doEvents(events, new BukkitScriptEntryData(player, npc), context);
}
return determ.size() > 0 ? determ.get(0) : "none";
}

/////////////////////
// CUSTOM EVENTS
/////////////////
Expand All @@ -59,20 +43,6 @@ public void run() {

private final Map<String, Integer> current_time = new HashMap<>();

// <--[event]
// @Events
// time changes (in <world>)
// time <0-23> in <world>
//
// @Regex ^on time [^\s]+( in [^\s]+)$
//
// @Triggers when the current time changes in a world (once per mine-hour).
//
// @Context
// <context.time> returns the current time.
// <context.world> returns the world.
//
// -->
public void timeEvent() {
for (World world : Bukkit.getWorlds()) {
int hour = (int) (world.getTime() / 1000);
Expand All @@ -86,18 +56,9 @@ public void timeEvent() {

if (!current_time.containsKey(currentWorld.identifySimple())
|| current_time.get(currentWorld.identifySimple()) != hour) {
Map<String, ObjectTag> context = new HashMap<>();

context.put("time", new ElementTag(hour));
context.put("world", currentWorld);

doEvents(Arrays.asList
("time changes",
"time changes in " + currentWorld.identifySimple(),
"time " + hour + " in " + currentWorld.identifySimple()),
null, null, context, true);

current_time.put(currentWorld.identifySimple(), hour);
TimeChangeScriptEvent.instance.hour = hour;
TimeChangeScriptEvent.instance.world = currentWorld;
TimeChangeScriptEvent.instance.fire();
}
}
}
Expand Down

0 comments on commit 2357348

Please sign in to comment.