diff --git a/src/main/java/ch/njol/skript/effects/EffWorldLoad.java b/src/main/java/ch/njol/skript/effects/EffWorldLoad.java new file mode 100644 index 00000000000..1417789789f --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffWorldLoad.java @@ -0,0 +1,100 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.effects; + +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.World.Environment; +import org.bukkit.WorldCreator; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +@Name("Load World") +@Description({ + "Load your worlds or unload your worlds", + "The load effect will create a new world if world doesn't already exist.", + "When attempting to load a normal vanilla world you must define it's environment i.e \"world_nether\" must be loaded with nether environment" +}) +@Examples({ + "load world \"world_nether\" with environment nether", + "load the world \"myCustomWorld\"", + "unload \"world_nether\"", + "unload \"world_the_end\" without saving", + "unload all worlds" +}) +@Since("INSERT VERSION") +public class EffWorldLoad extends Effect { + + static { + Skript.registerEffect(EffWorldLoad.class, + "load [[the] world[s]] %strings% [with environment %-environment%]", + "unload [[the] world[s]] %worlds% [:without saving]" + ); + } + + private boolean save, load; + private Expression worlds; + @Nullable + private Expression environment; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + worlds = exprs[0]; + load = matchedPattern == 0; + if (load) { + environment = (Expression) exprs[1]; + } else { + save = !parseResult.hasTag("without saving"); + } + return true; + } + + @Override + protected void execute(Event event) { + Environment environment = this.environment != null ? this.environment.getSingle(event) : null; + for (Object world : worlds.getArray(event)) { + if (load && world instanceof String) { + WorldCreator worldCreator = new WorldCreator((String) world); + if (environment != null) + worldCreator.environment(environment); + worldCreator.createWorld(); + } else if (!load && world instanceof World) { + Bukkit.unloadWorld((World) world, save); + } + } + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + if (load) + return "load the world(s) " + worlds.toString(event, debug) + (environment == null ? "" : " with environment " + environment.toString(event, debug)); + return "unload the world(s) " + worlds.toString(event, debug) + " " + (save ? "with saving" : "without saving"); + } + +} diff --git a/src/main/java/ch/njol/skript/effects/EffWorldSave.java b/src/main/java/ch/njol/skript/effects/EffWorldSave.java new file mode 100644 index 00000000000..2281ba44dcc --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffWorldSave.java @@ -0,0 +1,70 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.effects; + +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.World; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +@Name("Save World") +@Description({ + "Save all worlds or a given world manually.", + "Note: saving many worlds at once may possibly cause the server to freeze." +}) +@Examples({ + "save \"world_nether\"", + "save all worlds" +}) +@Since("INSERT VERSION") +public class EffWorldSave extends Effect { + + static { + Skript.registerEffect(EffWorldSave.class, "save [[the] world[s]] %worlds%"); + } + + private Expression worlds; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + worlds = (Expression) exprs[0]; + return true; + } + + @Override + protected void execute(Event event) { + for (World world : worlds.getArray(event)) + world.save(); + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "save the world(s) " + worlds.toString(event, debug); + } + +} diff --git a/src/main/java/ch/njol/skript/events/EvtWorld.java b/src/main/java/ch/njol/skript/events/EvtWorld.java new file mode 100644 index 00000000000..657d3f1a002 --- /dev/null +++ b/src/main/java/ch/njol/skript/events/EvtWorld.java @@ -0,0 +1,96 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.events; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.LiteralList; +import ch.njol.skript.lang.SkriptEvent; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import org.bukkit.World; +import org.bukkit.event.Event; +import org.bukkit.event.world.WorldInitEvent; +import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.event.world.WorldSaveEvent; +import org.bukkit.event.world.WorldUnloadEvent; +import org.bukkit.event.world.WorldEvent; +import org.eclipse.jdt.annotation.Nullable; + +public class EvtWorld extends SkriptEvent { + + static { + // World Save Event + Skript.registerEvent("World Save", EvtWorld.class, WorldSaveEvent.class, "world sav(e|ing) [of %-worlds%]") + .description("Called when a world is saved to disk. Usually all worlds are saved simultaneously, but world management plugins could change this.") + .examples( + "on world save of \"world\":", + "\tbroadcast \"The world %event-world% has been saved\"") + .since("1.0, INSERT VERSION (defining worlds)"); + + // World Init Event + Skript.registerEvent("World Init", EvtWorld.class, WorldInitEvent.class, "world init[ialization] [of %-worlds%]") + .description("Called when a world is initialized. As all default worlds are initialized before", + "any scripts are loaded, this event is only called for newly created worlds.", + "World management plugins might change the behaviour of this event though.") + .examples("on world init of \"world_the_end\":") + .since("1.0, INSERT VERSION (defining worlds)"); + + // World Unload Event + Skript.registerEvent("World Unload", EvtWorld.class, WorldUnloadEvent.class, "world unload[ing] [of %-worlds%]") + .description("Called when a world is unloaded. This event will never be called if you don't have a world management plugin.") + .examples( + "on world unload:", + "\tbroadcast \"the %event-world% has been unloaded!\"") + .since("1.0, INSERT VERSION (defining worlds)"); + + // World Load Event + Skript.registerEvent("World Load", EvtWorld.class, WorldLoadEvent.class, "world load[ing] [of %-worlds%]") + .description("Called when a world is loaded. As with the world init event, this event will not be called for the server's default world(s).") + .examples( + "on world load of \"world_nether\":", + "\tbroadcast \"The world %event-world% has been loaded!\"") + .since("1.0, INSERT VERSION (defining worlds)"); + } + + private Literal worlds; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Literal[] args, int matchedPattern, ParseResult parseResult) { + worlds = (Literal) args[0]; + if (worlds instanceof LiteralList && worlds.getAnd()) { + ((LiteralList) worlds).invertAnd(); + } + return true; + } + + @Override + public boolean check(Event event) { + if (worlds == null) + return true; + World evtWorld = ((WorldEvent) event).getWorld(); + return worlds.check(event, world -> world.equals(evtWorld)); + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "world save/init/unload/load" + (worlds == null ? "" : " of " + worlds.toString(event,debug)); + } + +} diff --git a/src/main/java/ch/njol/skript/events/SimpleEvents.java b/src/main/java/ch/njol/skript/events/SimpleEvents.java index f4897b19e30..4d19f3186e2 100644 --- a/src/main/java/ch/njol/skript/events/SimpleEvents.java +++ b/src/main/java/ch/njol/skript/events/SimpleEvents.java @@ -105,10 +105,6 @@ import org.bukkit.event.world.LootGenerateEvent; import org.bukkit.event.world.PortalCreateEvent; import org.bukkit.event.world.SpawnChangeEvent; -import org.bukkit.event.world.WorldInitEvent; -import org.bukkit.event.world.WorldLoadEvent; -import org.bukkit.event.world.WorldSaveEvent; -import org.bukkit.event.world.WorldUnloadEvent; import org.spigotmc.event.entity.EntityDismountEvent; import org.spigotmc.event.entity.EntityMountEvent; @@ -423,26 +419,6 @@ public class SimpleEvents { "\tkill event-entity") .since("2.2-dev13b"); } - Skript.registerEvent("World Init", SimpleEvent.class, WorldInitEvent.class, "world init[ialization]") - .description("Called when a world is initialised. As all default worlds are initialised before any scripts are loaded, this event is only called for newly created worlds.", - "World management plugins might change the behaviour of this event though.") - .examples("on world init:") - .since("1.0"); - Skript.registerEvent("World Load", SimpleEvent.class, WorldLoadEvent.class, "world load[ing]") - .description("Called when a world is loaded. As with the world init event, this event will not be called for the server's default world(s).") - .examples("on world load:", - "\tsend \"World is loading...\" to console") - .since("1.0"); - Skript.registerEvent("World Save", SimpleEvent.class, WorldSaveEvent.class, "world sav(e|ing)") - .description("Called when a world is saved to disk. Usually all worlds are saved simultaneously, but world management plugins could change this.") - .examples("on world saving:", - "\tbroadcast \"World has been saved!\"") - .since("1.0"); - Skript.registerEvent("World Unload", SimpleEvent.class, WorldUnloadEvent.class, "world unload[ing]") - .description("Called when a world is unloaded. This event might never be called if you don't have a world management plugin.") - .examples("on world unload:", - "\tcancel event") - .since("1.0"); if (Skript.classExists("org.bukkit.event.entity.EntityToggleGlideEvent")) { Skript.registerEvent("Gliding State Change", SimpleEvent.class, EntityToggleGlideEvent.class, "(gliding state change|toggl(e|ing) gliding)") .description("Called when an entity toggles glider on or off, or when server toggles gliding state of an entity forcibly.")