Skip to content

Commit

Permalink
Rewrote "player changes world" to ScriptEvent format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talamar1 committed Jul 4, 2015
1 parent 8a5bbff commit 07479fe
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -640,6 +640,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new PistonRetractsScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerBreaksBlockScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerChangesSignScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerChangesWorldScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerClosesInvScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerConsumesScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerDamagesBlockScriptEvent());
Expand Down
@@ -0,0 +1,116 @@
package net.aufdemrand.denizen.events.player;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;

import java.util.HashMap;
import java.util.List;

public class PlayerChangesWorldScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// player changes world (from <world>) (to <world>)
//
// @Cancellable false
//
// @Triggers when a player moves to a different world.
//
// @Context
// <context.origin_world> returns the dWorld that the player was previously on.
// <context.destination_world> returns the dWorld that the player is now in.
//
// -->

public PlayerChangesWorldScriptEvent() {
instance = this;
}

public static PlayerChangesWorldScriptEvent instance;
public dWorld origin_world;
public dWorld destination_world;
public PlayerChangedWorldEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
return CoreUtilities.toLowerCase(s).startsWith("player changes world");
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);

if (dEntity.isNPC(event.getPlayer())) {
return false;
}

List<String> data = CoreUtilities.split(lower, ' ');
for (int index = 3; index < data.size(); index++) {
if (data.get(index).equals("from")) {
if (!data.get(index+1).equals(origin_world.getName().toLowerCase())){
return false;
}
}
else if (data.get(index).equals("to")) {
if (!data.get(index+1).equals(destination_world.getName().toLowerCase())){
return false;
}
}
}

return true;
}

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

@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}

@Override
public void destroy() {
PlayerChangedWorldEvent.getHandlerList().unregister(this);
}

@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(dEntity.isPlayer(event.getPlayer()) ? dEntity.getPlayerFrom(event.getPlayer()) : null, null);
}

@Override
public HashMap<String, dObject> getContext() {
HashMap<String, dObject> context = super.getContext();
context.put("origin_world", origin_world);
context.put("destination_world", destination_world);
return context;
}

@EventHandler
public void onPlayerChangesWorld(PlayerChangedWorldEvent event) {
origin_world = new dWorld(event.getFrom());
destination_world = new dWorld(event.getPlayer().getWorld());
this.event = event;
fire();
}
}
Expand Up @@ -738,40 +738,6 @@ public void playerAnimation(PlayerAnimationEvent event) {
event.setCancelled(true);
}

// <--[event]
// @Events
// player changes world
// player changes world from <world>
// player changes world to <world>
// player changes world from <world> to <world>
//
// @Triggers when a player moves to a different world.
// @Context
// <context.origin_world> returns the dWorld that the player was previously on.
// <context.destination_world> returns the dWorld that the player is now in.
//
// -->
@EventHandler
public void playerChangedWorld(PlayerChangedWorldEvent event) {

if (dEntity.isNPC(event.getPlayer()))
return;

Map<String, dObject> context = new HashMap<String, dObject>();
dWorld originWorld = new dWorld(event.getFrom());
dWorld destinationWorld = new dWorld(event.getPlayer().getWorld());
context.put("origin_world", originWorld);
context.put("destination_world", destinationWorld);

doEvents(Arrays.asList
("player changes world",
"player changes world from " + originWorld.identifySimple(),
"player changes world to " + destinationWorld.identifySimple(),
"player changes world from " + originWorld.identifySimple() +
" to " + destinationWorld.identifySimple()),
null, dEntity.getPlayerFrom(event.getPlayer()), context, true);
}

// <--[event]
// @Events
// player drops item
Expand Down

0 comments on commit 07479fe

Please sign in to comment.