This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
/
PlayerLeftClicksBlockScriptEvent.java
136 lines (118 loc) · 5.82 KB
/
PlayerLeftClicksBlockScriptEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.denizenscript.denizen2sponge.events.player;
import com.denizenscript.denizen2core.events.ScriptEvent;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
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.data.type.HandTypes;
import org.spongepowered.api.entity.living.player.Player;
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.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.blockray.BlockRay;
import org.spongepowered.api.util.blockray.BlockRayHit;
import org.spongepowered.api.world.World;
import java.util.HashMap;
public class PlayerLeftClicksBlockScriptEvent extends ScriptEvent {
// <--[event]
// @Events
// player left clicks block
//
// @Updated 2017/04/05
//
// @Cancellable true
//
// @Group Player
//
// @Triggers when a player left clicks a block.
//
// @Switch type (BlockTypeTag) checks the entity type.
//
// @Context
// player (PlayerTag) returns the player that did the left clicking.
// location (LocationTag) returns the location of the block that was left clicked.
// precise_location (LocationTag) returns the exact point that was left 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 left clicked.
//
// @Determinations
// None.
// -->
@Override
public String getName() {
return "PlayerLeftClicksBlock";
}
@Override
public boolean couldMatch(ScriptEventData data) {
return data.eventPath.startsWith("player left clicks block");
}
@Override
public boolean matches(ScriptEventData data) {
return D2SpongeEventHelper.checkBlockType(location.getInternal().toLocation().getBlock().getType(), data, this::error)
&& D2SpongeEventHelper.checkItem(new ItemTag(player.getInternal()
.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.of(ItemTypes.NONE, 1))), data, this::error);
}
public PlayerTag player;
public LocationTag location;
public LocationTag precise_location;
public LocationTag intersection_point;
public LocationTag impact_normal;
public InteractBlockEvent.Primary internal;
@Override
public HashMap<String, AbstractTagObject> getDefinitions(ScriptEventData data) {
HashMap<String, AbstractTagObject> defs = super.getDefinitions(data);
defs.put("player", player);
defs.put("location", location);
defs.put("precise_location", precise_location);
defs.put("intersection_point", intersection_point);
defs.put("impact_normal", impact_normal);
return defs;
}
@Override
public void enable() {
Sponge.getEventManager().registerListeners(Denizen2Sponge.instance, this);
}
@Override
public void disable() {
Sponge.getEventManager().unregisterListeners(this);
}
@Listener
public void onLeftClickBlock(InteractBlockEvent.Primary evt, @Root Player player) {
PlayerLeftClicksBlockScriptEvent event = (PlayerLeftClicksBlockScriptEvent) clone();
event.internal = evt;
event.player = new PlayerTag(player);
if (evt.getTargetBlock().getLocation().isPresent()) {
event.location = new LocationTag(evt.getTargetBlock().getLocation().get());
BlockRayHit<World> brh = BlockRay.from(player).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).end().get();
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.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());
// TODO: Switch back to these ^ once Sponge fixes the Interaction Point.
event.impact_normal = new LocationTag(evt.getTargetSide().asOffset());
}
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.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
}
@Override
public void applyDetermination(boolean errors, String determination, AbstractTagObject value) {
super.applyDetermination(errors, determination, value);
}
}