Cleanup + added PlayerSettingsChangeEvent

This commit is contained in:
themode 2020-11-01 00:07:15 +01:00
parent 30b9ae2ac8
commit 463e1f047f
10 changed files with 56 additions and 13 deletions

View File

@ -1477,7 +1477,7 @@ public class Player extends LivingEntity implements CommandSender {
/**
* Gets the player connection.
* <p>
* Used to send packets and get relatives stuff to the connection.
* Used to send packets and get stuff related to the connection.
*
* @return the player connection
*/

View File

@ -18,7 +18,7 @@ import java.util.function.Consumer;
* using a {@link FakePlayerController} (see {@link #getController()}).
* <p>
* You can create one using {@link #initPlayer(UUID, String, Consumer)}. Be aware that this really behave exactly like a player
* and this is a feature not a bug, you will need to check at some place if the player is a fake one or not if you want to change it.
* and this is a feature not a bug, you will need to check at some place if the player is a fake one or not (instanceof) if you want to change it.
*/
public class FakePlayer extends Player {

View File

@ -14,15 +14,18 @@ import net.minestom.server.network.packet.server.play.KeepAlivePacket;
import net.minestom.server.network.packet.server.play.PlayerPositionAndLookPacket;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.inventory.PlayerInventoryUtils;
import org.jetbrains.annotations.NotNull;
/**
* This class act as a client controller for {@link FakePlayer}.
* This class acts as a client controller for {@link FakePlayer}.
* <p>
* The main use is to simulate the receiving of {@link ClientPlayPacket}
*/
public class FakePlayerController {
private final FakePlayer fakePlayer;
public FakePlayerController(FakePlayer fakePlayer) {
public FakePlayerController(@NotNull FakePlayer fakePlayer) {
this.fakePlayer = fakePlayer;
}

View File

@ -77,6 +77,8 @@ public interface EventHandler {
/**
* Calls the specified {@link Event} with all the assigned {@link EventCallback}.
* <p>
* Events are always called in the current thread.
*
* @param eventClass the event class
* @param event the event object

View File

@ -0,0 +1,30 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import org.jetbrains.annotations.NotNull;
/**
* Called after the player signals the server that his settings has been modified.
*/
public class PlayerSettingsChangeEvent extends Event {
private final Player player;
public PlayerSettingsChangeEvent(@NotNull Player player) {
this.player = player;
}
/**
* Gets the player who changed his settings.
* <p>
* You can retrieve the new player settings with {@link Player#getSettings()}.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
}

View File

@ -482,13 +482,14 @@ public abstract class Chunk implements Viewable, DataContainer {
return Collections.unmodifiableSet(viewers);
}
@Nullable
@Override
public Data getData() {
return data;
}
@Override
public void setData(Data data) {
public void setData(@Nullable Data data) {
this.data = data;
}
@ -497,7 +498,7 @@ public abstract class Chunk implements Viewable, DataContainer {
*
* @param player the player
*/
protected void sendChunk(Player player) {
protected void sendChunk(@NotNull Player player) {
// Only send loaded chunk
if (!isLoaded())
return;
@ -540,7 +541,7 @@ public abstract class Chunk implements Viewable, DataContainer {
*
* @param player the player to update the chunk to
*/
public void sendChunkUpdate(Player player) {
public void sendChunkUpdate(@NotNull Player player) {
retrieveDataBuffer(buf -> {
final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(buf, true);
@ -570,7 +571,7 @@ public abstract class Chunk implements Viewable, DataContainer {
* @param section the section to update
* @param player the player to send the packet to
*/
public void sendChunkSectionUpdate(int section, Player player) {
public void sendChunkSectionUpdate(int section, @NotNull Player player) {
if (!PlayerUtils.isNettyClient(player))
return;
Check.argCondition(!MathUtils.isBetween(section, 0, CHUNK_SECTION_COUNT),

View File

@ -409,8 +409,8 @@ public class DynamicChunk extends Chunk {
@Override
public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) {
DynamicChunk dynamicChunk = new DynamicChunk(instance, biomes.clone(), chunkX, chunkZ);
ArrayUtils.copyToDestination(dynamicChunk.blocksStateId, blocksStateId);
ArrayUtils.copyToDestination(dynamicChunk.customBlocksId, customBlocksId);
ArrayUtils.copyToDestination(blocksStateId, dynamicChunk.blocksStateId);
ArrayUtils.copyToDestination(customBlocksId, dynamicChunk.customBlocksId);
dynamicChunk.blocksData.putAll(blocksData);
dynamicChunk.updatableBlocks.addAll(updatableBlocks);
dynamicChunk.updatableBlocksLastUpdate.putAll(updatableBlocksLastUpdate);

View File

@ -623,6 +623,7 @@ public class InstanceContainer extends Instance {
* {@link UUID} is randomized, {@link DimensionType} is passed over and the {@link StorageLocation} is null.
*
* @return an {@link InstanceContainer} with the exact same chunks as 'this'
* @see #getSrcInstance() to retrieve the "creation source" of the copied instance
*/
public synchronized InstanceContainer copy() {
InstanceContainer copiedInstance = new InstanceContainer(UUID.randomUUID(), getDimensionType(), null);
@ -649,6 +650,7 @@ public class InstanceContainer extends Instance {
* Only present if this instance has been created with {@link InstanceContainer#copy()}.
*
* @return the instance source, null if not created by a copy
* @see #copy() to create a copy of this instance with 'this' as the source
*/
@Nullable
public InstanceContainer getSrcInstance() {

View File

@ -1,6 +1,7 @@
package net.minestom.server.listener;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerSettingsChangeEvent;
import net.minestom.server.network.packet.client.play.ClientSettingsPacket;
public class SettingsListener {
@ -8,6 +9,10 @@ public class SettingsListener {
public static void listener(ClientSettingsPacket packet, Player player) {
Player.PlayerSettings settings = player.getSettings();
settings.refresh(packet.locale, packet.viewDistance, packet.chatMode, packet.chatColors, packet.displayedSkinParts, packet.mainHand);
PlayerSettingsChangeEvent playerSettingsChangeEvent = new PlayerSettingsChangeEvent(player);
player.callEvent(PlayerSettingsChangeEvent.class, playerSettingsChangeEvent);
}
}

View File

@ -32,8 +32,8 @@ public final class ArrayUtils {
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
}
public static void copyToDestination(short[] dest, short[] src) {
Check.argCondition(dest.length != src.length, "The two arrays need to have the same length");
public static void copyToDestination(short[] src, short[] dest) {
Check.argCondition(src.length != dest.length, "The two arrays need to have the same length.");
System.arraycopy(src, 0, dest, 0, src.length);
}
@ -79,7 +79,7 @@ public final class ArrayUtils {
}
/**
* Gets if two arrays share the same start until {@code length}
* Gets if two arrays share the same start until {@code length}.
*
* @param array1 the first array
* @param array2 the second array