Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Right Clicks Air Fixes (#9)
Browse files Browse the repository at this point in the history
* Fixed right clicks air contexts

Also added player.block_on_cursor[#] tag.

* Default Distance for the Previous Commit

%.0 when on creative, 4.0 otherwise.

* Documentation Improvements

* Player Clicks Block Contexts

Also moved a check to an Utilities file.

* Fix Formatting Flaw
  • Loading branch information
Xenmai authored and mcmonkey4eva committed Mar 31, 2017
1 parent 43333c4 commit ccd3952
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
Expand Up @@ -8,11 +8,16 @@
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.tags.objects.PlayerTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.filter.cause.Root;
import org.spongepowered.api.util.blockray.BlockRay;
import org.spongepowered.api.util.blockray.BlockRayHit;
import org.spongepowered.api.world.World;

import java.util.HashMap;

Expand All @@ -22,7 +27,7 @@ public class PlayerRightClicksBlockScriptEvent extends ScriptEvent {
// @Events
// player right clicks block
//
// @Updated 2017/03/28
// @Updated 2017/03/30
//
// @Cancellable true
//
Expand All @@ -36,7 +41,9 @@ public class PlayerRightClicksBlockScriptEvent extends ScriptEvent {
// @Context
// player (PlayerTag) returns the player that did the right clicking.
// location (LocationTag) returns the location of the block that was right clicked.
// intersection (LocationTag) returns the exact point of intersection on the block where it was clicked (When available).
// precise_location (LocationTag) returns the exact point that was right clicked in the world.
// intersection_point (LocationTag) returns the exact point of intersection relative to the block (When available).
// impact_normal (LocationTag) returns the normal vector from the side of the block that was right clicked.
// hand (TextTag) returns the hand type that triggered the event.
//
// @Determinations
Expand All @@ -63,7 +70,11 @@ public boolean matches(ScriptEventData data) {

public LocationTag location;

public LocationTag intersection;
public LocationTag precise_location;

public LocationTag intersection_point;

public LocationTag impact_normal;

public TextTag hand;

Expand All @@ -74,7 +85,9 @@ public HashMap<String, AbstractTagObject> getDefinitions(ScriptEventData data) {
HashMap<String, AbstractTagObject> defs = super.getDefinitions(data);
defs.put("player", player);
defs.put("location", location);
defs.put("intersection", intersection);
defs.put("precise_location", precise_location);
defs.put("intersection_point", intersection_point);
defs.put("impact_normal", impact_normal);
defs.put("hand", hand);
return defs;
}
Expand All @@ -94,14 +107,20 @@ public void onRightClickBlock(InteractBlockEvent.Secondary evt, @Root Player pla
PlayerRightClicksBlockScriptEvent event = (PlayerRightClicksBlockScriptEvent) clone();
event.internal = evt;
event.player = new PlayerTag(player);
if (!evt.getTargetBlock().getLocation().isPresent()) {
// Sponge is dumb!
return;
if (evt.getTargetBlock().getLocation().isPresent()) {
event.location = new LocationTag(evt.getTargetBlock().getLocation().get());
event.precise_location = new LocationTag(evt.getInteractionPoint().get().add(evt.getTargetBlock().getPosition().toDouble()));
event.precise_location.getInternal().world = event.location.getInternal().world;
event.intersection_point = new LocationTag(evt.getInteractionPoint().get());
event.impact_normal = new LocationTag(evt.getTargetSide().asOffset());
}
event.location = new LocationTag(evt.getTargetBlock().getLocation().get());
if (evt.getInteractionPoint().isPresent()) {
event.intersection = new LocationTag(evt.getInteractionPoint().get());
event.intersection.getInternal().world = event.location.getInternal().world;
else {
BlockRayHit<World> brh = BlockRay.from(player).distanceLimit(Utilities.getHandReach(player)).build().end().get();
event.location = new LocationTag(brh.getLocation());
event.precise_location = new LocationTag(brh.getPosition());
event.precise_location.getInternal().world = event.location.getInternal().world;
event.intersection_point = new LocationTag(brh.getPosition().sub(brh.getBlockPosition().toDouble()));
event.impact_normal = new LocationTag(0, 0, 0);
}
event.hand = new TextTag(CoreUtilities.toLowerCase(evt.getHandType().toString()));
event.cancelled = evt.isCancelled();
Expand Down
Expand Up @@ -7,12 +7,11 @@
import com.denizenscript.denizen2core.tags.objects.TextTag;
import com.denizenscript.denizen2core.utilities.Action;
import com.denizenscript.denizen2core.utilities.Function2;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.util.blockray.BlockRay;

import java.util.HashMap;
import java.util.Optional;
Expand Down Expand Up @@ -81,6 +80,17 @@ public Player getInternal() {
// @Returns the gamemode of the player.
// -->
handlers.put("gamemode", (dat, obj) -> new TextTag(((PlayerTag) obj).internal.gameMode().get().toString()));
// <--[tag]
// @Name PlayerTag.block_on_cursor[<NumberTag>]
// @Updated 2017/03/30
// @Group Current Information
// @ReturnType LocationTag
// @Returns the block the player has their cursor on, up to a maximum distance. If no distance is specified, the default hand-reach distance is used.
// -->
handlers.put("block_on_cursor", (dat, obj) -> new LocationTag(BlockRay.from(((PlayerTag) obj).internal)
.stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1))
.distanceLimit(dat.hasNextModifier() ? NumberTag.getFor(dat.error, dat.getNextModifier()).getInternal() :
(Utilities.getHandReach(((PlayerTag) obj).internal))).build().end().get().getLocation()));
}

public static PlayerTag getFor(Action<String> error, String text) {
Expand Down
@@ -0,0 +1,11 @@
package com.denizenscript.denizen2sponge.utilities;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;

public class Utilities {

public static double getHandReach(Player player) {
return player.gameMode().equals(GameModes.CREATIVE) ? 5.0 : 4.0;
}
}

0 comments on commit ccd3952

Please sign in to comment.