Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A way to control redstone-powered creations
- Loading branch information
1 parent
e0eb8d9
commit 3944ad9
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/net/aufdemrand/denizen/events/core/RedstoneSmartEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package net.aufdemrand.denizen.events.core; | ||
|
||
import net.aufdemrand.denizen.events.EventManager; | ||
import net.aufdemrand.denizen.events.SmartEvent; | ||
import net.aufdemrand.denizen.objects.Element; | ||
import net.aufdemrand.denizen.objects.dLocation; | ||
import net.aufdemrand.denizen.objects.dMaterial; | ||
import net.aufdemrand.denizen.objects.dObject; | ||
import net.aufdemrand.denizen.utilities.DenizenAPI; | ||
import net.aufdemrand.denizen.utilities.debugging.dB; | ||
import org.bukkit.Material; | ||
import org.bukkit.block.BlockFace; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.BlockRedstoneEvent; | ||
import org.bukkit.event.entity.EntityChangeBlockEvent; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class RedstoneSmartEvent implements SmartEvent, Listener { | ||
|
||
|
||
/////////////////// | ||
// SMARTEVENT METHODS | ||
/////////////// | ||
|
||
|
||
@Override | ||
public boolean shouldInitialize(Set<String> events) { | ||
|
||
for (String event : events) { | ||
|
||
Matcher m = Pattern.compile("on redstone recalculated", Pattern.CASE_INSENSITIVE) | ||
.matcher(event); | ||
|
||
if (m.matches()) { | ||
// Straight-forward enough, just pass from any match. | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
@Override | ||
public void _initialize() { | ||
DenizenAPI.getCurrentInstance().getServer().getPluginManager() | ||
.registerEvents(this, DenizenAPI.getCurrentInstance()); | ||
dB.log("Loaded Redstone SmartEvent."); | ||
} | ||
|
||
|
||
@Override | ||
public void breakDown() { | ||
BlockRedstoneEvent.getHandlerList().unregister(this); | ||
} | ||
|
||
////////////// | ||
// MECHANICS | ||
/////////// | ||
|
||
// <--[event] | ||
// @Events | ||
// redstone recalculated | ||
// | ||
// @Warning This event fires very very rapidly! | ||
// | ||
// @Triggers when a redstone wire is recalculated. | ||
// @Context | ||
// <context.location> returns the location of the block. | ||
// <context.old_current> returns what the redstone power level was. | ||
// <context.new_current> returns what the redstone power level is becoming. | ||
// | ||
// @Determine | ||
// "CANCELLED" to stop the block from falling. | ||
// | ||
// --> | ||
@EventHandler | ||
public void onBlockRedstone(BlockRedstoneEvent event) { | ||
Map<String, dObject> context = new HashMap<String, dObject>(); | ||
context.put("location", new dLocation(event.getBlock().getLocation())); | ||
context.put("old_current", new Element(event.getOldCurrent())); | ||
context.put("new_current", new Element(event.getNewCurrent())); | ||
String determination = EventManager.doEvents(Arrays.asList("redstone recalculated"), null, null, context, true); | ||
Element det = new Element(determination); | ||
if (det.isInt()) | ||
event.setNewCurrent(det.asInt()); | ||
} | ||
} |