Skip to content

Commit d5b9710

Browse files
committed
Introduce VanishState
This state keeps track of the extra options and enables a simpler usage of 'simple vanish' vs customizations of vanish. Signed-off-by: Gabriel Harris-Rouquette <gabizou@me.com>
1 parent 682c3a2 commit d5b9710

File tree

4 files changed

+270
-3
lines changed

4 files changed

+270
-3
lines changed

src/main/java/org/spongepowered/api/data/Keys.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import org.spongepowered.api.data.value.SetValue;
9393
import org.spongepowered.api.data.value.Value;
9494
import org.spongepowered.api.data.value.WeightedCollectionValue;
95+
import org.spongepowered.api.effect.VanishState;
9596
import org.spongepowered.api.effect.particle.ParticleEffect;
9697
import org.spongepowered.api.effect.particle.ParticleOption;
9798
import org.spongepowered.api.effect.particle.ParticleType;
@@ -1388,7 +1389,7 @@ public final class Keys {
13881389
* Whether an {@link Entity} is currently invisible.
13891390
* This will only simply render the entity as vanished,
13901391
* but not prevent any entity updates being sent to clients.
1391-
* To fully "vanish" an {@link Entity}, use {@link #VANISH}.
1392+
* To fully "vanish" an {@link Entity}, use {@link #VANISH_STATE}.
13921393
*/
13931394
public static final Key<Value<Boolean>> IS_INVISIBLE = Keys.key(ResourceKey.sponge("is_invisible"), Boolean.class);
13941395

@@ -2629,6 +2630,19 @@ public final class Keys {
26292630
*/
26302631
public static final Key<Value<Boolean>> UPDATE_GAME_PROFILE = Keys.key(ResourceKey.sponge("update_game_profile"), Boolean.class);
26312632

2633+
/**
2634+
* The {@link VanishState} of an {@link Entity}.
2635+
*
2636+
* <p>The presence of a vanished entity will not be made known to a client;
2637+
* no packets pertaining to this entity are sent. Client-side, this entity
2638+
* will cease to exist. Server-side it may still be targeted by hostile
2639+
* entities or collide with other entities.</p>
2640+
*
2641+
* <p>Vanishing an {@link Entity} ridden by other entities (see
2642+
* {@link #PASSENGERS} will cause problems.</p>
2643+
*/
2644+
public static final Key<Value<VanishState>> VANISH_STATE = Keys.key(ResourceKey.sponge("vanish"), VanishState.class);
2645+
26322646
/**
26332647
* Whether an {@link Entity} is vanished.
26342648
*
@@ -2639,15 +2653,19 @@ public final class Keys {
26392653
*
26402654
* <p>Vanishing an {@link Entity} ridden by other entities (see
26412655
* {@link #PASSENGERS} will cause problems.</p>
2656+
* @deprecated use {@link #VANISH_STATE}
26422657
*/
2658+
@Deprecated
26432659
public static final Key<Value<Boolean>> VANISH = Keys.key(ResourceKey.sponge("vanish"), Boolean.class);
26442660

26452661
/**
26462662
* Whether an {@link Entity} ignores collision with other entities.
26472663
*
26482664
* <p>This state will be ignored if the {@link Entity} is not also
26492665
* vanished as per {@link #VANISH}.</p>
2666+
* @deprecated use {@link #VANISH_STATE}
26502667
*/
2668+
@Deprecated
26512669
public static final Key<Value<Boolean>> VANISH_IGNORES_COLLISION = Keys.key(ResourceKey.sponge("vanish_ignores_collision"), Boolean.class);
26522670

26532671
/**
@@ -2657,7 +2675,9 @@ public final class Keys {
26572675
*
26582676
* <p>This state will be ignored if the {@link Entity} is not also
26592677
* vanished as per {@link #VANISH}.}.</p>
2678+
* @deprecated use {@link #VANISH_STATE}
26602679
*/
2680+
@Deprecated
26612681
public static final Key<Value<Boolean>> VANISH_PREVENTS_TARGETING = Keys.key(ResourceKey.sponge("vanish_prevents_targeting"), Boolean.class);
26622682

26632683
/**
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.effect;
26+
27+
import org.spongepowered.api.Sponge;
28+
import org.spongepowered.api.entity.Entity;
29+
30+
/**
31+
* Represents the state of an {@link Entity}'s vanish state.
32+
* Accessible through {@link org.spongepowered.api.data.Keys#VANISH_STATE}
33+
*/
34+
public interface VanishState {
35+
36+
/**
37+
* Gets a vanished state via {@link Factory#vanish()} with the following
38+
* defaults:
39+
* <ul>
40+
* <li>{@link VanishState#invisible()} = {@code true}</li>
41+
* <li>{@link VanishState#affectsMonsterSpawning()} = {@code false}</li>
42+
* <li>{@link VanishState#untargetable()} = {@code true}</li>
43+
* <li>{@link VanishState#createsSounds()} = {@code false}</li>
44+
* <li>{@link VanishState#createsParticles()} = {@code false}</li>
45+
* </ul>
46+
*
47+
* @return A vanished state with the provided defaults
48+
*/
49+
static VanishState vanished() {
50+
return Sponge.game().factoryProvider().provide(VanishState.Factory.class).vanished();
51+
}
52+
53+
/**
54+
* A {@link VanishState} that is invisible with the following defaults:
55+
* <ul>
56+
* <li>{@link VanishState#invisible()} = {@code false}</li>
57+
* <li>{@link VanishState#affectsMonsterSpawning()} = {@code true}</li>
58+
* <li>{@link VanishState#untargetable()} = {@code false}</li>
59+
* <li>{@link VanishState#createsSounds()} = {@code true}</li>
60+
* <li>{@link VanishState#createsParticles()} = {@code true}</li>
61+
* </ul>
62+
*
63+
* @return A visible state with the provided defaults
64+
*/
65+
static VanishState unvanished() {
66+
return Sponge.game().factoryProvider().provide(VanishState.Factory.class).unvanished();
67+
}
68+
69+
/**
70+
* Gets whether the state is visible. If an {@link Entity} is visible, then
71+
* other aspects of this state will be ignored, such as
72+
* {@link #ignoresCollisions()}, {@link #untargetable()}, etc.
73+
*
74+
* @return Whether the {@link Entity} is visible
75+
*/
76+
boolean invisible();
77+
78+
/**
79+
* Gets a vanished {@link VanishState state} with {@link #ignoresCollisions()}
80+
* and {@link #untargetable()} set to {@code true}. If the state is already
81+
* {@link #invisible()}, then the same state is returned.
82+
*
83+
* @return The vanished state
84+
*/
85+
VanishState vanish();
86+
87+
/**
88+
* Gets a visible state {@link VanishState} with {@link #ignoresCollisions()}
89+
* and {@link #untargetable()} set to {@code false}. If the state is already
90+
* {@link #invisible()} being {@code false}, then the same state is returned.
91+
*
92+
* @return The visible state
93+
*/
94+
VanishState unvanish();
95+
96+
/**
97+
* Gets if the {@link Entity} will ignore collisions. In cases where
98+
* {@link #invisible()} is {@code false}, this will return {@code false}.
99+
*
100+
* @return Whether collisions will be ignored
101+
*/
102+
boolean ignoresCollisions();
103+
104+
/**
105+
* If {@link #invisible()} returns true, this will return the
106+
* {@link VanishState} with the desired flag of
107+
* {@link #ignoresCollisions()}.
108+
*
109+
* @param ignoresCollisions Whether collisions will be ignored
110+
* @return The new VanishState, the value changed
111+
*/
112+
VanishState ignoreCollisions(boolean ignoresCollisions);
113+
114+
/**
115+
* Gets if the {@link Entity} will be untargetable by entity ai. In cases
116+
* where {@link #invisible()} is {@code false}, this will return
117+
* {@code false}.
118+
*
119+
* @return Whether the {@link Entity} will be untargetable
120+
*/
121+
boolean untargetable();
122+
123+
/**
124+
* If {@link #invisible()} returns true, this will return the
125+
* {@link VanishState} with the desired flag of
126+
* {@link #ignoresCollisions()}.
127+
*
128+
* @param untargetable Whether the entity can be targeted by AI
129+
* @return The new VanishState, the value changed
130+
*/
131+
VanishState untargetable(boolean untargetable);
132+
133+
/**
134+
* Gets if {@link #affectsMonsterSpawning()} returns {@code false}, the
135+
* vanished {@link Entity} will not spawn monsters or affect near by monster
136+
* spawners.
137+
* <p>Note that this flag works in conjunction with
138+
* {@link org.spongepowered.api.data.Keys#AFFECTS_SPAWNING} such that either
139+
* one being {@code false} will disable spawning.</p>
140+
*
141+
* @return Whether the {@link Entity} will affect monster spawning
142+
*/
143+
boolean affectsMonsterSpawning();
144+
145+
/**
146+
* If {@link #invisible()} returns true, this will return the
147+
* {@link VanishState} with the desired flag.
148+
* <p>Note that this flag works in conjunction with
149+
* {@link org.spongepowered.api.data.Keys#AFFECTS_SPAWNING} such that either
150+
* one being false will disable spawning.</p>
151+
*
152+
* @param affectsMonsterSpawning Whether the {@link Entity} will affect
153+
* monster spawning
154+
* @return The new VanishState
155+
*/
156+
VanishState affectMonsterSpawning(boolean affectsMonsterSpawning);
157+
158+
/**
159+
* Gets if the {@link Entity} will produce sounds from various actions that
160+
* occur while {@link #invisible()} is {@code true}.
161+
*
162+
* @return Whether the {@link Entity} will produce sounds while invisible
163+
*/
164+
boolean createsSounds();
165+
166+
/**
167+
* If {@link #invisible()} returns true, this will return the
168+
* {@link VanishState} with the desired flag of
169+
* {@link #createsSounds()}.
170+
*
171+
* @param createSounds Whether the {@link Entity} will produce sounds
172+
* @return The new VanishState
173+
*/
174+
VanishState createSounds(boolean createSounds);
175+
176+
/**
177+
* Gets if the {@link Entity} will produce particles from various actions
178+
* that occur while {@link #invisible()} is {@code true}.
179+
*
180+
* @return Whether the {@link Entity} will produce particles while invisible
181+
*/
182+
boolean createsParticles();
183+
184+
/**
185+
* If {@link #invisible()} returns true, this will return the
186+
* {@link VanishState} with the desired flag of
187+
* {@link #createsParticles()}.
188+
*
189+
* @param createParticles Whether the {@link Entity} will produce particles
190+
* @return The new VanishState
191+
*/
192+
VanishState createParticles(boolean createParticles);
193+
194+
interface Factory {
195+
196+
/**
197+
* Creates a {@link VanishState} that is invisible with the following
198+
* defaults:
199+
* <ul>
200+
* <li>{@link VanishState#invisible()} = {@code true}</li>
201+
* <li>{@link VanishState#affectsMonsterSpawning()} = {@code false}</li>
202+
* <li>{@link VanishState#untargetable()} = {@code true}</li>
203+
* <li>{@link VanishState#createsSounds()} = {@code false}</li>
204+
* <li>{@link VanishState#createsParticles()} = {@code false}</li>
205+
* </ul>
206+
*
207+
* @return A newly created invisible {@link VanishState}
208+
*/
209+
VanishState vanished();
210+
211+
/**
212+
* A {@link VanishState} that is visible with the following defaults:
213+
* <ul>
214+
* <li>{@link VanishState#invisible()} = {@code false}</li>
215+
* <li>{@link VanishState#affectsMonsterSpawning()} = {@code true}</li>
216+
* <li>{@link VanishState#untargetable()} = {@code false}</li>
217+
* <li>{@link VanishState#createsSounds()} = {@code true}</li>
218+
* <li>{@link VanishState#createsParticles()} = {@code true}</li>
219+
* </ul>
220+
*
221+
* @return A newly created visible {@link VanishState}
222+
*/
223+
VanishState unvanished();
224+
}
225+
}

src/main/java/org/spongepowered/api/entity/Entity.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.spongepowered.api.data.value.ListValue;
3434
import org.spongepowered.api.data.value.SetValue;
3535
import org.spongepowered.api.data.value.Value;
36+
import org.spongepowered.api.effect.VanishState;
3637
import org.spongepowered.api.event.cause.entity.damage.source.DamageSource;
3738
import org.spongepowered.api.projectile.source.EntityProjectileSource;
3839
import org.spongepowered.api.util.AABB;
@@ -75,6 +76,7 @@
7576
*
7677
* <p>Blocks and items (when they are in inventories) are not entities.</p>
7778
*/
79+
@SuppressWarnings("deprecation")
7880
@DoNotStore
7981
public interface Entity extends Identifiable, HoverEventSource<HoverEvent.ShowEntity>, Locatable, EntityProjectileSource, Sound.Emitter,
8082
SerializableDataHolder.Mutable, RandomProvider {
@@ -325,8 +327,8 @@ default Collection<? extends Entity> nearbyEntities(final double distance, final
325327
*/
326328
default boolean canSee(final Entity entity) {
327329
Objects.requireNonNull(entity, "Entity cannot be null");
328-
final Optional<Boolean> optional = entity.get(Keys.VANISH);
329-
return !optional.isPresent() || !optional.get();
330+
final Optional<VanishState> optional = entity.get(Keys.VANISH_STATE);
331+
return !optional.map(VanishState::invisible).orElse(false);
330332
}
331333

332334
/**
@@ -606,10 +608,20 @@ default SetValue.Mutable<String> scoreboardTags() {
606608
return this.requireValue(Keys.SCOREBOARD_TAGS).asMutable();
607609
}
608610

611+
/**
612+
* {@link Keys#VANISH_STATE}
613+
*
614+
* @return The current vanish state of the entity
615+
*/
616+
default Value.Mutable<VanishState> vanishState() {
617+
return this.requireValue(Keys.VANISH_STATE).asMutable();
618+
}
619+
609620
/**
610621
* {@link Keys#VANISH}
611622
*
612623
* @return Whether the entity is vanished
624+
* @deprecated Use {@link #vanishState() VanishState} instead
613625
*/
614626
default Value.Mutable<Boolean> vanish() {
615627
return this.requireValue(Keys.VANISH).asMutable();
@@ -619,6 +631,7 @@ default Value.Mutable<Boolean> vanish() {
619631
* {@link Keys#VANISH_IGNORES_COLLISION}
620632
*
621633
* @return Whether the entity ignores collision with other entities
634+
* @deprecated Use {@link #vanishState()} instead
622635
*/
623636
default Value.Mutable<Boolean> vanishIgnoresCollision() {
624637
return this.requireValue(Keys.VANISH_IGNORES_COLLISION).asMutable();
@@ -628,6 +641,7 @@ default Value.Mutable<Boolean> vanishIgnoresCollision() {
628641
* {@link Keys#VANISH_PREVENTS_TARGETING}
629642
*
630643
* @return Whether the entity can be targeted for attack by another entity
644+
* @deprecated Use {@link #vanishState()} instead
631645
*/
632646
default Value.Mutable<Boolean> vanishPreventsTargeting() {
633647
return this.requireValue(Keys.VANISH_PREVENTS_TARGETING).asMutable();

src/main/java/org/spongepowered/api/entity/living/player/User.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.spongepowered.api.data.Keys;
3131
import org.spongepowered.api.data.value.MapValue;
3232
import org.spongepowered.api.data.value.Value;
33+
import org.spongepowered.api.effect.VanishState;
3334
import org.spongepowered.api.entity.Entity;
3435
import org.spongepowered.api.entity.Tamer;
3536
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
@@ -198,11 +199,16 @@ default Value.Mutable<Boolean> invisible() {
198199
return this.requireValue(Keys.IS_INVISIBLE).asMutable();
199200
}
200201

202+
default Value.Mutable<VanishState> vanishState() {
203+
return this.requireValue(Keys.VANISH_STATE).asMutable();
204+
}
205+
201206
/**
202207
* {@link Keys#VANISH}
203208
*
204209
* @return Whether the user is vanished
205210
*/
211+
@Deprecated
206212
default Value.Mutable<Boolean> vanish() {
207213
return this.requireValue(Keys.VANISH).asMutable();
208214
}
@@ -212,6 +218,7 @@ default Value.Mutable<Boolean> vanish() {
212218
*
213219
* @return Whether the user ignores collision with other entities
214220
*/
221+
@Deprecated
215222
default Value.Mutable<Boolean> vanishIgnoresCollision() {
216223
return this.requireValue(Keys.VANISH_IGNORES_COLLISION).asMutable();
217224
}
@@ -221,6 +228,7 @@ default Value.Mutable<Boolean> vanishIgnoresCollision() {
221228
*
222229
* @return Whether the user can be targeted for attack by another entity
223230
*/
231+
@Deprecated
224232
default Value.Mutable<Boolean> vanishPreventsTargeting() {
225233
return this.requireValue(Keys.VANISH_PREVENTS_TARGETING).asMutable();
226234
}

0 commit comments

Comments
 (0)