Skip to content

Commit 54f7a87

Browse files
MrHell228Faithcaio
authored andcommitted
Add ForwardingViewer
1 parent 446aa5e commit 54f7a87

File tree

3 files changed

+167
-31
lines changed

3 files changed

+167
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 net.kyori.adventure.audience.ForwardingAudience;
28+
import org.spongepowered.api.Sponge;
29+
import org.spongepowered.api.entity.Entity;
30+
import org.spongepowered.api.world.World;
31+
import org.spongepowered.math.vector.Vector3i;
32+
33+
import java.util.Collection;
34+
import java.util.function.Supplier;
35+
36+
/**
37+
* ForwardingViewer represents some group of {@link Viewer}s
38+
*/
39+
public interface ForwardingViewer extends Viewer, ForwardingAudience {
40+
41+
@Override
42+
Iterable<? extends Viewer> audiences();
43+
44+
/**
45+
* Creates {@link ForwardingViewer} from provided viewers supplier.
46+
*
47+
* @param viewersSupplier The viewers supplier
48+
* @return The forwarding viewer
49+
*/
50+
static ForwardingViewer of(final Supplier<? extends Iterable<? extends Viewer>> viewersSupplier) {
51+
return Sponge.game().factoryProvider().provide(Factory.class).of(viewersSupplier);
52+
}
53+
54+
/**
55+
* Creates {@link ForwardingViewer} from provided viewers.
56+
*
57+
* @param viewers The viewers
58+
* @return The forwarding viewer
59+
*/
60+
static ForwardingViewer of(final Collection<? extends Viewer> viewers) {
61+
return Sponge.game().factoryProvider().provide(Factory.class).of(viewers);
62+
}
63+
64+
/**
65+
* Creates {@link ForwardingViewer} from provided viewers.
66+
*
67+
* @param viewers The viewers
68+
* @return The forwarding viewer
69+
*/
70+
static ForwardingViewer of(final Viewer... viewers) {
71+
return Sponge.game().factoryProvider().provide(Factory.class).of(viewers);
72+
}
73+
74+
/**
75+
* Creates {@link ForwardingViewer} that will only affect viewers
76+
* in the given area of the given world.
77+
*
78+
* @param world The world
79+
* @param center The center of the area
80+
* @param radius The radius of the area
81+
* @return The forwarding viewer
82+
*/
83+
static ForwardingViewer allAround(final World<?, ?> world, final Vector3i center, final int radius) {
84+
return Sponge.game().factoryProvider().provide(Factory.class).allAround(world, center, radius);
85+
}
86+
87+
/**
88+
* Creates {@link ForwardingViewer} that will only affect viewers
89+
* in the given radius from the given entity.
90+
*
91+
* @param entity The entity
92+
* @param radius The radius
93+
* @return The forwarding viewer
94+
*/
95+
static ForwardingViewer allAround(final Entity entity, final int radius) {
96+
return Sponge.game().factoryProvider().provide(Factory.class).allAround(entity, radius);
97+
}
98+
99+
100+
interface Factory {
101+
102+
ForwardingViewer of(Supplier<? extends Iterable<? extends Viewer>> viewersSupplier);
103+
104+
ForwardingViewer of(Collection<? extends Viewer> viewers);
105+
106+
ForwardingViewer of(Viewer... viewers);
107+
108+
ForwardingViewer allAround(World<?, ?> world, Vector3i center, int radius);
109+
110+
ForwardingViewer allAround(Entity entity, int radius);
111+
}
112+
}

src/main/java/org/spongepowered/api/effect/Viewer.java

+52-26
Original file line numberDiff line numberDiff line change
@@ -41,54 +41,52 @@
4141

4242
/**
4343
* A Viewer is something that sees effects.
44-
* The Viewer class contains methods for spawning particles and playing sound
45-
* effects.
44+
* E.g. the Viewer class contains methods for spawning particles and playing
45+
* sound effects.
4646
*/
4747
public interface Viewer extends Audience {
4848

4949
/**
50-
* Sends the effect of being in a particular Vanilla world environment, such as the Nether,
50+
* Sends the effect of being in a particular world environment, such as the Nether,
5151
* as an effect to the viewer.
5252
*
53-
* <p>For example, specifying {@link WorldTypes#THE_NETHER} will create a red skybox and
54-
* red hazy fog on the vanilla minecraft client</p>
53+
* <p>For example, specifying {@link WorldTypes#THE_NETHER} will create an empty skybox
54+
* and hazy fog on the vanilla minecraft client</p>
5555
*
5656
* @param worldType The world type
5757
*/
5858
void sendWorldType(WorldType worldType);
5959

6060
/**
61-
* Spawn a {@link ParticleEffect} at a given position.
62-
* All players within a default radius around the position will see the
63-
* particles.
61+
* Spawn the given {@link ParticleEffect} at the given position.
6462
*
6563
* @param particleEffect The particle effect to spawn
66-
* @param position The position at which to spawn the particle effect
64+
* @param position The position
6765
*/
6866
default void spawnParticles(final ParticleEffect particleEffect, final Vector3d position) {
69-
this.spawnParticles(Objects.requireNonNull(particleEffect, "particleEffect"), Objects.requireNonNull(position, "position"), Integer.MAX_VALUE);
67+
Objects.requireNonNull(position, "position");
68+
this.spawnParticles(particleEffect, position.x(), position.y(), position.z());
7069
}
7170

7271
/**
73-
* Spawn a {@link ParticleEffect} at a given position.
74-
* All players within a given radius around the position will see the
75-
* particles.
72+
* Spawn the given {@link ParticleEffect} at the given position.
7673
*
7774
* @param particleEffect The particle effect to spawn
78-
* @param position The position at which to spawn the particle effect
79-
* @param radius The radius around the position where the particles can be
80-
* seen by players
75+
* @param x The x position
76+
* @param y The y position
77+
* @param z The z position
8178
*/
82-
void spawnParticles(ParticleEffect particleEffect, Vector3d position, int radius);
79+
void spawnParticles(ParticleEffect particleEffect, double x, double y, double z);
8380

8481
/**
85-
* Plays a sound.
82+
* Plays the given {@link Sound} at the given position.
8683
*
87-
* @param sound the sound
88-
* @param pos the position to play the sound at
84+
* @param sound The sound
85+
* @param position The position
8986
*/
90-
default void playSound(final @NonNull Sound sound, final Vector3d pos) {
91-
this.playSound(sound, pos.x(), pos.y(), pos.z());
87+
default void playSound(final @NonNull Sound sound, final Vector3d position) {
88+
Objects.requireNonNull(position, "position");
89+
this.playSound(sound, position.x(), position.y(), position.z());
9290
}
9391

9492
/**
@@ -98,16 +96,44 @@ default void playSound(final @NonNull Sound sound, final Vector3d pos) {
9896
* position will cancel the currently playing one.
9997
*
10098
* @param position The position
101-
* @param musicDiscType The music disc
99+
* @param musicDisc The music disc
100+
*/
101+
default void playMusicDisc(final Vector3i position, final MusicDisc musicDisc) {
102+
Objects.requireNonNull(position, "position");
103+
this.playMusicDisc(position.x(), position.y(), position.z(), musicDisc);
104+
}
105+
106+
/**
107+
* Plays the given {@link MusicDisc} at the given position. The benefit of playing
108+
* {@link MusicDisc} instead of a {@link SoundType} allows you to stop them through
109+
* the {@link #stopMusicDisc(Vector3i)}. Playing a new {@link MusicDisc} at the same
110+
* position will cancel the currently playing one.
111+
*
112+
* @param x The x position
113+
* @param y The y position
114+
* @param z The z position
115+
* @param musicDisc The music disc
102116
*/
103-
void playMusicDisc(Vector3i position, MusicDisc musicDiscType);
117+
void playMusicDisc(int x, int y, int z, MusicDisc musicDisc);
104118

105119
/**
106120
* Stops the {@link MusicDisc} that is playing at the given position.
107121
*
108122
* @param position The position
109123
*/
110-
void stopMusicDisc(Vector3i position);
124+
default void stopMusicDisc(final Vector3i position) {
125+
Objects.requireNonNull(position, "position");
126+
this.stopMusicDisc(position.x(), position.y(), position.z());
127+
}
128+
129+
/**
130+
* Stops the {@link MusicDisc} that is playing at the given position.
131+
*
132+
* @param x The x position
133+
* @param y The y position
134+
* @param z The z position
135+
*/
136+
void stopMusicDisc(int x, int y, int z);
111137

112138
/**
113139
* Sends a client-only block change.
@@ -119,7 +145,7 @@ default void playSound(final @NonNull Sound sound, final Vector3d pos) {
119145
*/
120146
default void sendBlockChange(final Vector3i position, final BlockState state) {
121147
Objects.requireNonNull(position, "position");
122-
this.sendBlockChange(position.x(), position.y(), position.z(), Objects.requireNonNull(state, "state"));
148+
this.sendBlockChange(position.x(), position.y(), position.z(), state);
123149
}
124150

125151
/**

src/main/java/org/spongepowered/api/world/World.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
*/
2525
package org.spongepowered.api.world;
2626

27-
import net.kyori.adventure.audience.Audience;
28-
import net.kyori.adventure.audience.ForwardingAudience;
2927
import org.spongepowered.api.Server;
28+
import org.spongepowered.api.effect.ForwardingViewer;
3029
import org.spongepowered.api.effect.Viewer;
3130
import org.spongepowered.api.entity.Entity;
3231
import org.spongepowered.api.entity.living.player.Player;
@@ -51,12 +50,11 @@
5150
*/
5251
@DoNotStore
5352
public interface World<W extends World<W, L>, L extends Location<W, L>> extends
54-
ForwardingAudience,
53+
ForwardingViewer,
5554
WorldLike<W>,
5655
LocationCreator<W, L>,
5756
PhysicsAwareMutableBlockVolume<W>,
5857
ContextSource,
59-
Viewer,
6058
ArchetypeVolumeCreator,
6159
WeatherUniverse,
6260
RegistryHolder {
@@ -92,7 +90,7 @@ default W world() {
9290
Collection<? extends Player> players();
9391

9492
@Override
95-
default Iterable<? extends Audience> audiences() {
93+
default Iterable<? extends Viewer> audiences() {
9694
return this.players();
9795
}
9896

0 commit comments

Comments
 (0)