Skip to content

Commit

Permalink
places block event: Against switch/context
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 7, 2022
1 parent 3a2ec38 commit 7f3c9ce
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
Expand Up @@ -3,8 +3,10 @@
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -22,7 +24,7 @@ public class BlockBuiltScriptEvent extends BukkitScriptEvent implements Listener
//
// @Cancellable true
//
// @Triggers when an attempt is made to build a block on another block. Not necessarily caused by players.
// @Triggers when an attempt is made to build a block on another block. Not necessarily caused by players. Does not normally fire when players place blocks. Prefer <@link event player places block> for that.
//
// @Context
// <context.location> returns the LocationTag of the block the player is trying to build on.
Expand All @@ -32,6 +34,8 @@ public class BlockBuiltScriptEvent extends BukkitScriptEvent implements Listener
// @Determine
// "BUILDABLE" to allow the building.
//
// @Player when the event is triggered in relation to a player that is causing the block build.
//
// -->

public BlockBuiltScriptEvent() {
Expand Down Expand Up @@ -71,6 +75,11 @@ public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
return super.applyDetermination(path, determinationObj);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}

@Override
public void cancellationChanged() {
event.setBuildable(!cancelled);
Expand Down
Expand Up @@ -17,12 +17,12 @@ public class PlayerPlacesBlockScriptEvent extends BukkitScriptEvent implements L
// player places block
// player places <item>
//
// @Regex ^on player places [^\s]+$
//
// @Group Player
//
// @Location true
// @Switch using:<hand type> to only process the event if the player is using the specified hand type (HAND or OFF_HAND).
//
// @Switch using:<hand_type> to only process the event if the player is using the specified hand type (HAND or OFF_HAND).
// @Switch against:<location> to only process the event if block that this new block is being placed against matches the specified LocationTag matcher.
//
// @Cancellable true
//
Expand All @@ -34,31 +34,32 @@ public class PlayerPlacesBlockScriptEvent extends BukkitScriptEvent implements L
// <context.old_material> returns the MaterialTag of the block that was replaced.
// <context.item_in_hand> returns the ItemTag of the item in hand.
// <context.hand> returns the name of the hand that the block was in (HAND or OFF_HAND).
// <context.against> returns the LocationTag of the block this block was placed against.
//
// @Player Always.
//
// @Example
// on player places block:
//
// @Example
// after player places torch using:off_hand:
//
// @Example
// on player places cactus against:sand:
//
// -->

public PlayerPlacesBlockScriptEvent() {
registerCouldMatcher("player places <material>");
registerSwitches("using", "against");
}

public LocationTag location;
public LocationTag location, against;
public MaterialTag material;
public ElementTag hand;
public ItemTag item_in_hand;
public BlockPlaceEvent event;

@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("player places")) {
return false;
}
if (!couldMatchBlockOrItem(path.eventArgLowerAt(2))) {
return false;
}
return true;
}

@Override
public boolean matches(ScriptPath path) {
String mat = path.eventArgLowerAt(2);
Expand All @@ -71,6 +72,9 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, location)) {
return false;
}
if (!path.tryObjectSwitch("against", against)) {
return false;
}
return super.matches(path);
}

Expand All @@ -92,6 +96,8 @@ public ObjectTag getContext(String name) {
return item_in_hand;
case "hand":
return hand;
case "against":
return against;
}
return super.getContext(name);
}
Expand All @@ -104,6 +110,7 @@ public void onPlayerPlacesBlock(BlockPlaceEvent event) {
hand = new ElementTag(event.getHand().name());
material = new MaterialTag(event.getBlock());
location = new LocationTag(event.getBlock().getLocation());
against = new LocationTag(event.getBlockAgainst().getLocation());
item_in_hand = new ItemTag(event.getItemInHand());
this.event = event;
fire(event);
Expand Down
@@ -1,7 +1,6 @@
package com.denizenscript.denizen.objects;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.abstracts.ProfileEditor;
import com.denizenscript.denizen.nms.interfaces.EntityAnimation;
import com.denizenscript.denizen.nms.interfaces.FakePlayer;
Expand Down
@@ -1,6 +1,5 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
Expand Down
Expand Up @@ -35,7 +35,6 @@
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.core.SectionPos;
Expand Down
Expand Up @@ -41,7 +41,6 @@
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.core.SectionPos;
Expand Down
Expand Up @@ -37,7 +37,6 @@
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.core.SectionPos;
Expand Down

0 comments on commit 7f3c9ce

Please sign in to comment.