Skip to content

Commit

Permalink
by default apply physics in adjustblock, add 'no_physics'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 5, 2020
1 parent 8c30729 commit 0e3183e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
Expand Up @@ -15,6 +15,9 @@

public interface BlockHelper {


void applyPhysics(Location location);

int idFor(Material mat);

MaterialData getFlowerpotContents(Block block);
Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.scripts.commands.world;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.utilities.blocks.ModernBlockData;
Expand All @@ -20,14 +21,14 @@ public class AdjustBlockCommand extends AbstractCommand {
public AdjustBlockCommand() {
setName("adjustblock");
setSyntax("adjustblock [<location>|...] [<mechanism>](:<value>)");
setRequiredArguments(2, 2);
setRequiredArguments(2, 3);
}

// <--[command]
// @Name AdjustBlock
// @Syntax adjustblock [<location>|...] [<mechanism>](:<value>)
// @Syntax adjustblock [<location>|...] [<mechanism>](:<value>) (no_physics)
// @Required 2
// @Maximum 2
// @Maximum 3
// @Short Adjusts a mechanism on the material of a block at the location.
// @Group core
// @Guide https://guide.denizenscript.com/guides/basics/mechanisms.html
Expand All @@ -37,13 +38,21 @@ public AdjustBlockCommand() {
// That is, an equivalent to <@link command adjust>, but that directly applies a "MaterialTag" mechanism onto a block.
//
// Input a location or list of locations, and the mechanism to apply.
//
// Use the "no_physics" argument to indicate that the change should not apply a physics update.
// If not specified, physics will apply to the block and nearby blocks.
//
// @Tags
// <LocationTag.material>
//
// @Usage
// Use to put snow on the block at the player's feet.
// - adjust <player.location.below> snowy:true
//
// @Usage
// Use to switch on the lever that the player is looking at, without actually providing redstone power.
// - adjust <player.cursor_on> switched:true no_physics
//
// -->

@Override
Expand All @@ -54,6 +63,10 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
&& arg.matchesArgumentList(LocationTag.class)) {
scriptEntry.addObject("locations", arg.asType(ListTag.class).filter(LocationTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("no_physics")
&& arg.matches("no_physics")) {
scriptEntry.addObject("no_physics", new ElementTag(true));
}
else if (!scriptEntry.hasObject("mechanism")) {
if (arg.hasPrefix()) {
scriptEntry.addObject("mechanism", new ElementTag(arg.getPrefix().getValue()));
Expand All @@ -79,20 +92,32 @@ else if (!scriptEntry.hasObject("mechanism")) {
public void execute(ScriptEntry scriptEntry) {
ElementTag mechanismName = scriptEntry.getElement("mechanism");
ElementTag value = scriptEntry.getElement("mechanism_value");
ElementTag noPhysics = scriptEntry.getElement("no_physics");
List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
ArgumentHelper.debugList("locations", locations)
+ mechanismName.debug()
+ (noPhysics == null ? "" : noPhysics.debug())
+ (value == null ? "" : value.debug()));
}
boolean doPhysics = noPhysics == null || !noPhysics.asBoolean();
for (LocationTag location : locations) {
Block block = location.getBlock();
BlockData data = block.getBlockData();
MaterialTag specialMaterial = new MaterialTag(new ModernBlockData(data));
Mechanism mechanism = new Mechanism(mechanismName, value, scriptEntry.entryData.getTagContext());
specialMaterial.safeAdjust(mechanism);
block.setBlockData(data);
block.setBlockData(data, false);
if (doPhysics) {
NMSHandler.getBlockHelper().applyPhysics(location);
NMSHandler.getBlockHelper().applyPhysics(location.clone().add(1, 0, 0));
NMSHandler.getBlockHelper().applyPhysics(location.clone().add(-1, 0, 0));
NMSHandler.getBlockHelper().applyPhysics(location.clone().add(0, 0, 1));
NMSHandler.getBlockHelper().applyPhysics(location.clone().add(0, 0, -1));
NMSHandler.getBlockHelper().applyPhysics(location.clone().add(0, 1, 0));
NMSHandler.getBlockHelper().applyPhysics(location.clone().add(0, -1, 0));
}
}

}
Expand Down
Expand Up @@ -18,9 +18,11 @@
import org.bukkit.block.Block;
import org.bukkit.block.FlowerPot;
import org.bukkit.block.Skull;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_12_R1.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.v1_12_R1.block.CraftSkull;
import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_12_R1.util.CraftMagicNumbers;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.material.MaterialData;

Expand All @@ -31,6 +33,12 @@

public class BlockHelperImpl implements BlockHelper {

@Override
public void applyPhysics(Location location) {
BlockPosition pos = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
((CraftWorld) location.getWorld()).getHandle().applyPhysics(pos, CraftMagicNumbers.getBlock(location.getBlock().getType()), false);
}

@Override
public List<Location> getBlocksList(PortalCreateEvent event) {
List<Location> blocks = new ArrayList<>();
Expand Down
Expand Up @@ -17,12 +17,14 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.craftbukkit.v1_13_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_13_R2.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.v1_13_R2.block.CraftBlockState;
import org.bukkit.craftbukkit.v1_13_R2.block.CraftSkull;
import org.bukkit.craftbukkit.v1_13_R2.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_13_R2.util.CraftLegacy;
import org.bukkit.craftbukkit.v1_13_R2.util.CraftMagicNumbers;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.material.MaterialData;

Expand All @@ -33,6 +35,12 @@

public class BlockHelperImpl implements BlockHelper {

@Override
public void applyPhysics(Location location) {
BlockPosition pos = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
((CraftWorld) location.getWorld()).getHandle().applyPhysics(pos, CraftMagicNumbers.getBlock(location.getBlock().getType()));
}

@Override
public List<Location> getBlocksList(PortalCreateEvent event) {
List<Location> blocks = new ArrayList<>();
Expand Down
Expand Up @@ -18,12 +18,14 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftBlockState;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftSkull;
import org.bukkit.craftbukkit.v1_14_R1.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_14_R1.util.CraftLegacy;
import org.bukkit.craftbukkit.v1_14_R1.util.CraftMagicNumbers;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.material.MaterialData;

Expand All @@ -37,6 +39,12 @@ public class BlockHelperImpl implements BlockHelper {

public static final Field craftBlockEntityState_tileEntity;

@Override
public void applyPhysics(Location location) {
BlockPosition pos = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
((CraftWorld) location.getWorld()).getHandle().applyPhysics(pos, CraftMagicNumbers.getBlock(location.getBlock().getType()));
}

static {
Field f = null;
try {
Expand Down
Expand Up @@ -18,12 +18,14 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_15_R1.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.v1_15_R1.block.CraftBlockState;
import org.bukkit.craftbukkit.v1_15_R1.block.CraftSkull;
import org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_15_R1.legacy.CraftLegacy;
import org.bukkit.craftbukkit.v1_15_R1.util.CraftMagicNumbers;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.material.MaterialData;

Expand All @@ -38,6 +40,12 @@ public class BlockHelperImpl implements BlockHelper {
public static final Field craftBlockEntityState_tileEntity = ReflectionHelper.getFields(CraftBlockEntityState.class).get("tileEntity");
public static final Field craftSkull_profile = ReflectionHelper.getFields(CraftSkull.class).get("profile");

@Override
public void applyPhysics(Location location) {
BlockPosition pos = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
((CraftWorld) location.getWorld()).getHandle().applyPhysics(pos, CraftMagicNumbers.getBlock(location.getBlock().getType()));
}

@Override
public ModernBlockData parseBlockData(Material material, String otherData) {
CraftBlockData data = CraftBlockData.newData(material, otherData);
Expand Down

0 comments on commit 0e3183e

Please sign in to comment.