Skip to content

Commit

Permalink
(experimental) modifyblock event source argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 16, 2020
1 parent 46f16f0 commit bc334e0
Showing 1 changed file with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.denizenscript.denizen.scripts.commands.world;

import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.WorldHelper;
import com.denizenscript.denizen.objects.CuboidTag;
import com.denizenscript.denizen.objects.EllipsoidTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
Expand All @@ -24,10 +21,16 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
Expand All @@ -37,8 +40,8 @@ public class ModifyBlockCommand extends AbstractCommand implements Listener, Hol

public ModifyBlockCommand() {
setName("modifyblock");
setSyntax("modifyblock [<location>|.../<ellipsoid>/<cuboid>] [<material>|...] (no_physics/naturally) (delayed) (<script>) (<percent chance>|...)");
setRequiredArguments(2, 6);
setSyntax("modifyblock [<location>|.../<ellipsoid>/<cuboid>] [<material>|...] (no_physics/naturally) (delayed) (<script>) (<percent chance>|...) (source:<player>)");
setRequiredArguments(2, 7);
DenizenAPI.getCurrentInstance().getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
// Keep the list empty automatically - we don't want to still block physics so much later that something else edited the block!
Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {
Expand All @@ -54,9 +57,9 @@ public void run() {

// <--[command]
// @Name ModifyBlock
// @Syntax modifyblock [<location>|.../<ellipsoid>/<cuboid>] [<material>|...] (no_physics/naturally) (delayed) (<script>) (<percent chance>|...)
// @Syntax modifyblock [<location>|.../<ellipsoid>/<cuboid>] [<material>|...] (no_physics/naturally) (delayed) (<script>) (<percent chance>|...) (source:<player>)
// @Required 2
// @Maximum 6
// @Maximum 7
// @Short Modifies blocks.
// @Group world
//
Expand All @@ -70,6 +73,7 @@ public void run() {
// Use 'delayed' to make the modifyblock slowly edit blocks at a time pace roughly equivalent to the server's limits.
// Note that specify a list of locations will take more time in parsing than in the actual block modification.
// Optionally, specify a script to be ran after the delayed edits finish. (Doesn't fire if delayed is not set.)
// Optionally, specify a source player. When set, Bukkit events will fire that identify that player as the source of a change, and potentially cancel the change.
//
// The modifyblock command is ~waitable. Refer to <@link language ~waitable>.
//
Expand Down Expand Up @@ -134,6 +138,11 @@ else if (!scriptEntry.hasObject("depth")
&& arg.matchesInteger()) {
scriptEntry.addObject("depth", new ElementTag(arg.getValue()));
}
else if (!scriptEntry.hasObject("source")
&& arg.matchesPrefix("source")
&& arg.matchesArgumentType(PlayerTag.class)) {
scriptEntry.addObject("source", arg.asType(PlayerTag.class));
}
else if (arg.matches("no_physics")) {
scriptEntry.addObject("physics", new ElementTag(false));
}
Expand Down Expand Up @@ -188,6 +197,7 @@ public void execute(final ScriptEntry scriptEntry) {
final ElementTag heightElement = scriptEntry.getElement("height");
final ElementTag depthElement = scriptEntry.getElement("depth");
final ScriptTag script = scriptEntry.getObjectTag("script");
final PlayerTag source = scriptEntry.getObjectTag("source");
ListTag percents = scriptEntry.getObjectTag("percents");

if (percents != null && percents.size() != materials.size()) {
Expand All @@ -198,7 +208,6 @@ public void execute(final ScriptEntry scriptEntry) {
final List<MaterialTag> materialList = materials.filter(MaterialTag.class, scriptEntry);

if (scriptEntry.dbCallShouldDebug()) {

Debug.report(scriptEntry, getName(), (locations == null ? location_list.debug() : ArgumentHelper.debugList("locations", locations))
+ materials.debug()
+ physics.debug()
Expand All @@ -208,10 +217,11 @@ public void execute(final ScriptEntry scriptEntry) {
+ natural.debug()
+ delayed.debug()
+ (script != null ? script.debug() : "")
+ (percents != null ? percents.debug() : ""));

+ (percents != null ? percents.debug() : "")
+ (source != null ? source.debug() : ""));
}

Player sourcePlayer = source == null ? null : source.getPlayerEntity();
final boolean doPhysics = physics.asBoolean();
final boolean isNatural = natural.asBoolean();
final int radius = radiusElement.asInt();
Expand Down Expand Up @@ -262,7 +272,7 @@ public void run() {
else {
nLoc = LocationTag.valueOf(location_list.get(index));
}
handleLocation(nLoc, index, materialList, doPhysics, isNatural, radius, height, depth, percs);
handleLocation(nLoc, index, materialList, doPhysics, isNatural, radius, height, depth, percs, sourcePlayer, scriptEntry);
index++;
if (System.currentTimeMillis() - start > 50) {
break;
Expand Down Expand Up @@ -294,13 +304,13 @@ public void run() {
int index = 0;
if (locations != null) {
for (ObjectTag obj : locations) {
handleLocation((LocationTag) obj, index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
handleLocation((LocationTag) obj, index, materialList, doPhysics, isNatural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
}
else {
for (String str : location_list) {
handleLocation(LocationTag.valueOf(str), index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
handleLocation(LocationTag.valueOf(str), index, materialList, doPhysics, isNatural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
}
Expand Down Expand Up @@ -329,7 +339,7 @@ void postComplete(Location loc, boolean was_static) {
}

void handleLocation(LocationTag location, int index, List<MaterialTag> materialList, boolean doPhysics,
boolean isNatural, int radius, int height, int depth, List<Float> percents) {
boolean isNatural, int radius, int height, int depth, List<Float> percents, Player source, ScriptEntry entry) {

MaterialTag material;
if (percents == null) {
Expand All @@ -354,6 +364,22 @@ void handleLocation(LocationTag location, int index, List<MaterialTag> materialL
location.setX(location.getBlockX());
location.setY(location.getBlockY());
location.setZ(location.getBlockZ());
if (source != null) {
Event event;
if (material.getMaterial() == Material.AIR) {
event = new BlockBreakEvent(location.getBlock(), source);
}
else {
event = new BlockPlaceEvent(location.getBlock(), material.getModernData().getBlockState(), location.getBlock(), new ItemTag(material, 1).getItemStack(), source, true, EquipmentSlot.HAND);
}
Bukkit.getPluginManager().callEvent(event);
if (((Cancellable) event).isCancelled()) {
if (entry.dbCallShouldDebug()) {
Debug.echoDebug(entry, "Source event cancelled, not changing block.");
}
return;
}
}
setBlock(location, material, doPhysics, isNatural);

if (radius != 0) {
Expand Down

0 comments on commit bc334e0

Please sign in to comment.