Skip to content

Commit

Permalink
Rewrote "on player places hanging" event in new ScriptEvent format.
Browse files Browse the repository at this point in the history
Removed from BukkitWorldScriptHelper.
Fixed couldMatch on other events because it was triggering multiple events when it shouldn't.
  • Loading branch information
Talamar1 committed Jun 14, 2015
1 parent f60b625 commit aad6a60
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -617,6 +617,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new PlayerDamagesBlockScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerJumpScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerPlacesBlockScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerPlacesHangingScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerStepsOnScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerTakesFromFurnaceScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerWalkScriptEvent());
Expand Down
Expand Up @@ -57,9 +57,8 @@ public EntityBreaksHangingScriptEvent() {
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String entName = CoreUtilities.getXthArg(0, lower);
return (CoreUtilities.getXthArg(1, lower).equals("breaks")
&& CoreUtilities.getXthArg(2, lower).equals("hanging"))
|| (entName.equals("entity") || dEntity.matches(entName));
return lower.contains("breaks hanging")
&& (entName.equals("entity") || dEntity.matches(entName));
}

@Override
Expand Down
Expand Up @@ -56,8 +56,8 @@ public HangingBreaksScriptEvent() {
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String mat = CoreUtilities.getXthArg(0, lower);
return CoreUtilities.getXthArg(0, lower).equals("breaks")
|| (mat.equals("hanging") || dEntity.matches(mat));
return CoreUtilities.getXthArg(1, lower).equals("breaks")
&& (mat.equals("hanging") || mat.equals("painting") || mat.equals("item_frame"));
}

@Override
Expand Down
Expand Up @@ -25,6 +25,8 @@ public class PlayerPlacesBlockScriptEvent extends ScriptEvent implements Listene
// player places <material> in notable cuboid
// player places block in <notable cuboid>
// player places <material> in <notable cuboid>
// NOTE: This event does not catch "hanging", "painting", or "item_frame" blocks.
// See "on player places hanging"
//
// @Cancellable true
//
Expand All @@ -50,7 +52,10 @@ public PlayerPlacesBlockScriptEvent() {

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
return s.toLowerCase().startsWith("player places");
String lower = CoreUtilities.toLowerCase(s);
String mat = CoreUtilities.getXthArg(2, lower);
return lower.startsWith("player places")
&& (!mat.equals("hanging") && !mat.equals("painting") && !mat.equals("item_frame"));
}

@Override
Expand Down
@@ -0,0 +1,133 @@
package net.aufdemrand.denizen.events.scriptevents;

import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dEllipsoid;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
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.hanging.HangingPlaceEvent;

import java.util.HashMap;

public class PlayerPlacesHangingScriptEvent extends ScriptEvent implements Listener {

// <--[event]
// @Events
// player places hanging (in <notable>)
// player places <hanging> (in <notable>)
//
// @Cancellable true
//
// @Triggers when a hanging entity (painting or itemframe) is placed.
//
// @Context
// <context.hanging> returns the dEntity of the hanging.
// <context.location> returns the dLocation of the block the hanging was placed on.
// <context.cuboids> returns a dList of the cuboids the hanging is in.
//
// -->

public PlayerPlacesHangingScriptEvent() {
instance = this;
}
public static PlayerPlacesHangingScriptEvent instance;
public dEntity hanging;
public dList cuboids;
public dLocation location;
public HangingPlaceEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String mat = CoreUtilities.getXthArg(2, lower);
return lower.startsWith("player places")
&& (mat.equals("hanging") || mat.equals("painting") || mat.equals("item_frame"));
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String hangCheck = CoreUtilities.getXthArg(2, lower);
if (!hangCheck.equals("hanging")
&& hanging.matchesEntity(hangCheck)){
return false;
}
String notable = null;
if (CoreUtilities.xthArgEquals(3, lower, "in")) {
notable = CoreUtilities.getXthArg(4, lower);
}
if (notable != null) {
if (dCuboid.matches(notable)) {
dCuboid cuboid = dCuboid.valueOf(notable);
if (!cuboid.isInsideCuboid(location)) {
return false;
}
}
else if (dEllipsoid.matches(notable)) {
dEllipsoid ellipsoid = dEllipsoid.valueOf(notable);
if (!ellipsoid.contains(location)) {
return false;
}
}
else {
dB.echoError("Invalid event 'IN ...' check [" + getName() + "]: '" + s + "' for " + scriptContainer.getName());
return false;
}
}

return true;
}

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

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

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

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

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

@EventHandler
public void pnPlayerPlacesHanging(HangingPlaceEvent event) {
hanging = new dEntity(event.getEntity());
location = new dLocation(event.getBlock().getLocation());
cuboids = new dList();
for (dCuboid cuboid: dCuboid.getNotableCuboidsContaining(location)) {
cuboids.add(cuboid.identifySimple());
}
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}
}
Expand Up @@ -136,41 +136,6 @@ public void timeEvent() {
}
}

// <--[event]
// @Events
// player places hanging
// player places <hanging>
//
// @Triggers when a hanging entity (painting or itemframe) is placed.
// @Context
// <context.hanging> returns the dEntity of the hanging.
// <context.location> returns the dLocation of the block the hanging was placed on.
//
// @Determine
// "CANCELLED" to stop the hanging from being placed.
//
// -->
@EventHandler
public void hangingPlace(HangingPlaceEvent event) {

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

Map<String, dObject> context = new HashMap<String, dObject>();
dEntity hanging = new dEntity(event.getEntity());

context.put("hanging", hanging);
context.put("location", new dLocation(event.getBlock().getLocation()));

String determination = doEvents(Arrays.asList
("player places hanging",
"player places " + hanging.identifyType()),
null, dEntity.getPlayerFrom(event.getPlayer()), context, true);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}

/////////////////////
// ENTITY EVENTS
/////////////////
Expand Down

0 comments on commit aad6a60

Please sign in to comment.