Skip to content

Commit

Permalink
Rewrote "on structure grows" in ScriptEvent format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talamar1 committed Jul 1, 2015
1 parent 636c4b9 commit 3384847
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 67 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -658,6 +658,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new SheepDyedScriptEvent());
ScriptEvent.registerScriptEvent(new SheepRegrowsScriptEvent());
ScriptEvent.registerScriptEvent(new SpawnChangeScriptEvent());
ScriptEvent.registerScriptEvent(new StructureGrowsScriptEvent());
ScriptEvent.registerScriptEvent(new SlimeSplitsScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleCollidesBlockScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleCollidesEntityScriptEvent());
Expand Down
@@ -0,0 +1,127 @@
package net.aufdemrand.denizen.events.world;


import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.objects.Element;
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.block.BlockState;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.StructureGrowEvent;

import java.util.HashMap;

public class StructureGrowsScriptEvent extends ScriptEvent implements Listener {

// <--[event]
// @Events
// structure grows (naturally/from bonemeal) (in <world>)
// <structure> grows (naturally/from bonemeal) (in <world>)
//
// @Cancellable true
//
// @Triggers when a structure (a tree or a mushroom) grows in a world.
//
// @Context
// <context.world> returns the dWorld the structure grew in.
// <context.location> returns the dLocation the structure grew at.
// <context.structure> returns an Element of the structure's type.
// <context.blocks> returns a dList of all block locations to be modified.
// <context.new_materials> returns a dList of the new block materials, to go with <context.blocks>.
//
// -->

public StructureGrowsScriptEvent() {
instance = this;
}

public static StructureGrowsScriptEvent instance;
public dWorld world;
public dLocation location;
public Element structure;
public dList blocks;
public dList new_materials;
public StructureGrowEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
return CoreUtilities.getXthArg(1, CoreUtilities.toLowerCase(s)).equals("grows");
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String struct = CoreUtilities.getXthArg(0, lower);
if (!struct.equals("structure") && !struct.equals(CoreUtilities.toLowerCase(structure.asString()))) {
return false;
}

String wCheck = lower.indexOf(" in ") > 0 ? CoreUtilities.getXthArg(1,lower.substring(lower.indexOf(" in ")+4)):"";
if (wCheck.length() > 0 && !wCheck.equals("world") && !wCheck.equals(CoreUtilities.toLowerCase(world.getName()))) {
return false;
}
if (CoreUtilities.getXthArg(2,lower).equals("from") && !event.isFromBonemeal()) {
return false;
} else if (CoreUtilities.getXthArg(2,lower).equals("naturally") && event.isFromBonemeal()) {
return false;
}
return true;
}

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

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

@Override
public void destroy() {
StructureGrowEvent.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("world", world);
context.put("location", location);
context.put("structure", structure);
context.put("blocks", blocks);
context.put("new_materials", new_materials);
return context;
}

@EventHandler
public void onStructureGrow(StructureGrowEvent event) {
world = new dWorld(event.getWorld());
location = new dLocation(event.getLocation());
structure = new Element(event.getSpecies().name());
blocks = new dList();
new_materials = new dList();
for (BlockState block : event.getBlocks()) {
blocks.add(new dLocation(block.getLocation()).identify());
new_materials.add(dMaterial.getMaterialFrom(block.getType(), block.getRawData()).identify());
}
this.event = event;
cancelled = event.isCancelled();
fire();
event.setCancelled(cancelled);
}
}
Expand Up @@ -2083,71 +2083,4 @@ public void weatherChange(WeatherChangeEvent event) {
event.setCancelled(true);
}


/////////////////////
// WORLD EVENTS
/////////////////

// <--[event]
// @Events
// structure grows (naturally/from bonemeal) (in <world>)
// <structure> grows (naturally/from bonemeal) (in <world>)
//
// @Triggers when a structure (a tree or a mushroom) grows in a world.
// @Context
// <context.world> returns the dWorld the structure grew in.
// <context.location> returns the dLocation the structure grew at.
// <context.structure> returns an Element of the structure's type.
// <context.blocks> returns a dList of all block locations to be modified.
// <context.new_materials> returns a dList of the new block materials, to go with <context.blocks>.
//
// @Determine
// "CANCELLED" to stop the structure from growing.
//
// -->
@EventHandler
public void structureGrow(StructureGrowEvent event) {

Map<String, dObject> context = new HashMap<String, dObject>();
dWorld world = new dWorld(event.getWorld());
String treeType = event.getSpecies().name();

context.put("world", world);
context.put("location", new dLocation(event.getLocation()));
context.put("structure", new Element(treeType));

dList structure = new dList();
dList mats = new dList();
for (BlockState state : event.getBlocks()) {
structure.add(new dLocation(state.getLocation()).identify());
mats.add(dMaterial.getMaterialFrom(state.getType(), state.getRawData()).identify());
}
context.put("blocks", structure);
context.put("new_materials", mats);

List<String> events = new ArrayList<String>();
events.add("structure grows");
events.add("structure grows in " + world.identifySimple());
events.add(treeType + " grows");
events.add(treeType + " grows in " + world.identifySimple());

if (event.isFromBonemeal()) {
events.add("structure grows from bonemeal");
events.add("structure grows from bonemeal in " + world.identifySimple());
events.add(treeType + " grows from bonemeal");
events.add(treeType + " grows from bonemeal in " + world.identifySimple());
}
else {
events.add("structure grows naturally");
events.add("structure grows naturally in " + world.identifySimple());
events.add(treeType + " grows naturally");
events.add(treeType + " grows naturally in " + world.identifySimple());
}

String determination = doEvents(events, null, null, context, true);

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

}

0 comments on commit 3384847

Please sign in to comment.