Skip to content

Commit

Permalink
switch command modernization
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 5, 2021
1 parent 911e31c commit aab0364
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 29 deletions.
Expand Up @@ -1582,7 +1582,7 @@ else if (mtr.angle == BlockFace.EAST) {
if (!(object.entity instanceof FallingBlock)) {
return null;
}
return new MaterialTag(new ModernBlockData(((FallingBlock) object.entity).getBlockData()));
return new MaterialTag(((FallingBlock) object.entity).getBlockData());
});

// <--[tag]
Expand Down
Expand Up @@ -3468,7 +3468,7 @@ public void adjust(Mechanism mechanism) {
return;
}
MaterialDirectional.getFrom(material).setFacing(Utilities.faceFor(faceVec.toVector()));
material.getModernData().setToBlock(block);
block.setBlockData(material.getModernData());
}

// <--[mechanism]
Expand Down
Expand Up @@ -39,7 +39,7 @@ private MaterialSwitchable(MaterialTag _material) {
material = _material;
}

MaterialTag material;
public MaterialTag material;

public static void registerTags() {

Expand Down
Expand Up @@ -10,6 +10,7 @@
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;

Expand Down Expand Up @@ -110,17 +111,21 @@ public void execute(ScriptEntry scriptEntry) {
specialMaterial.safeAdjust(mechanism);
if (doPhysics) {
block.setBlockData(data, false);
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));
applyPhysicsAt(location);
}
else {
ModifyBlockCommand.setBlock(block.getLocation(), specialMaterial, false, null);
}
}
}

public static void applyPhysicsAt(Location location) {
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 Up @@ -19,6 +19,7 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand Down Expand Up @@ -380,7 +381,9 @@ void handleLocation(LocationTag location, int index, List<MaterialTag> materialL
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);
BlockState state = NMSHandler.getBlockHelper().generateBlockState(material.getMaterial());
state.setBlockData(material.getModernData());
event = new BlockPlaceEvent(location.getBlock(), state, location.getBlock(), new ItemTag(material, 1).getItemStack(), source, true, EquipmentSlot.HAND);
}
Bukkit.getPluginManager().callEvent(event);
if (((Cancellable) event).isCancelled()) {
Expand Down
Expand Up @@ -285,7 +285,7 @@ else if (clazz == org.bukkit.Particle.DustOptions.class) {
}
else if (clazz == BlockData.class) {
MaterialTag blockMaterial = MaterialTag.valueOf(special_data.asString(), scriptEntry.getContext());
dataObject = blockmaterial.getModernData();
dataObject = blockMaterial.getModernData();
}
else if (clazz == ItemStack.class) {
ItemTag itemType = ItemTag.valueOf(special_data.asString(), scriptEntry.getContext());
Expand Down
Expand Up @@ -104,7 +104,7 @@ public void setWallSign(Block sign, BlockFace bf, MaterialTag material) {
sign.setType(material == null ? Material.OAK_WALL_SIGN : material.getMaterial(), false);
MaterialTag signMaterial = new MaterialTag(sign);
MaterialDirectional.getFrom(signMaterial).setFacing(bf);
signMaterial.getModernData().setToBlock(sign);
sign.setBlockData(signMaterial.getModernData());
}

@Override
Expand Down
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.properties.material.MaterialSwitchable;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
Expand All @@ -13,7 +14,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.block.data.Bisected;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -107,15 +108,14 @@ public void execute(final ScriptEntry scriptEntry) {
final ListTag interactLocations = scriptEntry.getObjectTag("locations");
long duration = ((DurationTag) scriptEntry.getObject("duration")).getTicks();
final SwitchState switchState = SwitchState.valueOf(scriptEntry.getElement("switchstate").asString());
final Player player = Utilities.entryHasPlayer(scriptEntry) ? Utilities.getEntryPlayer(scriptEntry).getPlayerEntity() : null;
// Switch the Block
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), interactLocations.debug()
+ ArgumentHelper.debugObj("duration", duration + "t")
+ ArgumentHelper.debugObj("switchstate", switchState.name()));
}
for (final LocationTag interactLocation : interactLocations.filter(LocationTag.class, scriptEntry)) {
switchBlock(scriptEntry, interactLocation, switchState, player);
switchBlock(scriptEntry, interactLocation, switchState);
// If duration set, schedule a delayed task.
if (duration > 0) {
// If this block already had a delayed task, cancel it.
Expand All @@ -128,26 +128,43 @@ public void execute(final ScriptEntry scriptEntry) {
}
Debug.echoDebug(scriptEntry, "Setting delayed task 'SWITCH' for " + interactLocation.identify());
// Store new delayed task ID, for checking against, then schedule new delayed task.
taskMap.put(interactLocation, Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> switchBlock(scriptEntry, interactLocation, SwitchState.TOGGLE, player), duration));
taskMap.put(interactLocation, Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> switchBlock(scriptEntry, interactLocation, SwitchState.TOGGLE), duration));
}
}
}

public static boolean switchState(Block b) {
ModernBlockData mbd = new ModernBlockData(b);
Boolean switchState = mbd.getSwitchState();
if (switchState != null) {
return switchState;
MaterialSwitchable switchable = MaterialSwitchable.getFrom(new MaterialTag(b.getBlockData()));
if (switchable == null) {
return false;
}
return false;
return switchable.getState();
}

// Break off this portion of the code from execute() so it can be used in both execute and the delayed runnable
public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, SwitchState switchState, Player player) {
boolean currentState = switchState(interactLocation.getBlock());
public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, SwitchState switchState) {
MaterialSwitchable switchable = MaterialSwitchable.getFrom(new MaterialTag(interactLocation.getBlock().getBlockData()));
if (switchable == null) {
return;
}
boolean currentState = switchable.getState();
if ((switchState.equals(SwitchState.ON) && !currentState) || (switchState.equals(SwitchState.OFF) && currentState) || switchState.equals(SwitchState.TOGGLE)) {
ModernBlockData mbd = new ModernBlockData(interactLocation.getBlock());
mbd.setSwitchState(interactLocation.getBlock(), !currentState);
switchable.setState(!currentState);
interactLocation.getBlock().setBlockData(switchable.material.getModernData());
if (switchable.material.getModernData() instanceof Bisected) {
Location other = interactLocation.clone();
if (((Bisected) switchable.material.getModernData()).getHalf() == Bisected.Half.TOP) {
other = other.add(0, -1, 0);
}
else {
other = other.add(0, 1, 0);
}
MaterialSwitchable switchable2 = MaterialSwitchable.getFrom(new MaterialTag(other.getBlock().getBlockData()));
switchable2.setState(!currentState);
other.getBlock().setBlockData(switchable2.material.getModernData());
AdjustBlockCommand.applyPhysicsAt(other);
}
AdjustBlockCommand.applyPhysicsAt(interactLocation);
Debug.echoDebug(scriptEntry, "Switched " + interactLocation.getBlock().getType().toString() + "! Current state now: " + (switchState(interactLocation.getBlock()) ? "ON" : "OFF"));
}
}
Expand Down
Expand Up @@ -371,7 +371,7 @@ else if (direction.startsWith("w")) {
}
MaterialTag signMaterial = new MaterialTag(signState.getBlock());
MaterialDirectional.getFrom(signMaterial).setFacing(bf);
signMaterial.getModernData().setToBlock(signState.getBlock());
signState.getBlock().setBlockData(signMaterial.getModernData());
}

/**
Expand Down
Expand Up @@ -134,7 +134,7 @@ private void updateBlock(MaterialTag material, DurationTag duration) {
}
this.material = material;
if (player.hasChunkLoaded(location.getChunk())) {
material.getModernData().sendFakeChangeTo(player.getPlayerEntity(), location);
player.getPlayerEntity().sendBlockChange(location, material.getModernData());
if (material.getMaterial().name().endsWith("_BANNER")) { // Banners are weird
location.getWorld().refreshChunk(chunkCoord.x, chunkCoord.z);
}
Expand Down

0 comments on commit aab0364

Please sign in to comment.