/
InvisibleCommand.java
151 lines (140 loc) · 6.17 KB
/
InvisibleCommand.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.npc.traits.InvisibleTrait;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.ArmorStand;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class InvisibleCommand extends AbstractCommand {
// <--[command]
// @Name Invisible
// @Syntax invisible [<entity>] (state:true/false/toggle)
// @Required 1
// @Short Makes an NPC or entity go invisible
// @Group entity
//
// @Description
// For non-armor stand entities, applies a maximum duration invisibility potion.
// For armor stands, toggles them invisible.
// Applies the 'invisible' trait to NPCs.
//
// NPCs can't be made invisible if not added to the playerlist.
// (The invisible trait adds the NPC to the playerlist when set)
// See <@link language invisible trait>)
//
// @Tags
// None
//
// @Usage
// Use to makes the player invisible.
// - invisible <player> state:true
//
// @Usage
// Use to make the attached NPC visible if previously invisible, and invisible if not
// - invisible <npc> state:toggle
// -->
enum Action {TRUE, FALSE, TOGGLE}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("state")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("state", arg.asElement());
}
else if (!scriptEntry.hasObject("target")
&& arg.matches("player")
&& Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("target", Utilities.getEntryPlayer(scriptEntry).getDenizenEntity());
}
else if (!scriptEntry.hasObject("target")
&& arg.matches("npc")
&& Utilities.entryHasNPC(scriptEntry)) {
scriptEntry.addObject("target", Utilities.getEntryNPC(scriptEntry).getDenizenEntity());
}
else if (!scriptEntry.hasObject("target")
&& arg.matchesArgumentType(EntityTag.class)) {
scriptEntry.addObject("target", arg.asType(EntityTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("state")) {
scriptEntry.addObject("state", new ElementTag("TRUE"));
}
if (!scriptEntry.hasObject("target") || !((EntityTag) scriptEntry.getObjectTag("target")).isValid()) {
throw new InvalidArgumentsException("Must specify a valid target!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag state = scriptEntry.getElement("state");
EntityTag target = (EntityTag) scriptEntry.getObject("target");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), state.debug() + target.debug());
}
if (target.isCitizensNPC()) {
NPC npc = target.getDenizenNPC().getCitizen();
if (!npc.hasTrait(InvisibleTrait.class)) {
npc.addTrait(InvisibleTrait.class);
}
InvisibleTrait trait = npc.getTrait(InvisibleTrait.class);
switch (Action.valueOf(state.asString().toUpperCase())) {
case FALSE:
trait.setInvisible(false);
break;
case TRUE:
trait.setInvisible(true);
break;
case TOGGLE:
trait.toggle();
break;
}
}
else {
switch (Action.valueOf(state.asString().toUpperCase())) {
case FALSE:
if (target.getBukkitEntity() instanceof ArmorStand) {
((ArmorStand) target.getBukkitEntity()).setVisible(true);
}
else {
target.getLivingEntity().removePotionEffect(PotionEffectType.INVISIBILITY);
}
break;
case TRUE:
if (target.getBukkitEntity() instanceof ArmorStand) {
((ArmorStand) target.getBukkitEntity()).setVisible(false);
}
else {
new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(target.getLivingEntity());
}
break;
case TOGGLE:
if (target.getBukkitEntity() instanceof ArmorStand) {
if (((ArmorStand) target.getBukkitEntity()).isVisible()) {
((ArmorStand) target.getBukkitEntity()).setVisible(true);
}
else {
((ArmorStand) target.getBukkitEntity()).setVisible(false);
}
}
else {
if (target.getLivingEntity().hasPotionEffect(PotionEffectType.INVISIBILITY)) {
target.getLivingEntity().removePotionEffect(PotionEffectType.INVISIBILITY);
}
else {
new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(target.getLivingEntity());
}
}
break;
}
}
}
}