Merge branch 'master' into item-api

This commit is contained in:
TheMode 2021-04-10 19:37:36 +02:00
commit be2d1db9e0
7 changed files with 43 additions and 27 deletions

View File

@ -6,7 +6,7 @@ import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
import java.util.function.Function;
import java.util.function.UnaryOperator;
@SuppressWarnings("rawtypes")
public class ArgumentEnum<E extends Enum> extends Argument<E> {
@ -61,9 +61,9 @@ public class ArgumentEnum<E extends Enum> extends Argument<E> {
LOWER_CASED(name -> name.toLowerCase(Locale.ROOT)),
UPPER_CASED(name -> name.toUpperCase(Locale.ROOT));
private final Function<String, String> formatter;
private final UnaryOperator<String> formatter;
Format(Function<String, String> formatter) {
Format(@NotNull UnaryOperator<String> formatter) {
this.formatter = formatter;
}
}

View File

@ -20,27 +20,27 @@ public class MeleeAttackGoal extends GoalSelector {
private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK));
private long lastHit;
private final double range;
private final int delay;
private final TimeUnit timeUnit;
private final int range;
private boolean stop;
private Entity cachedTarget;
/**
* @param entityCreature the entity to add the goal to
* @param delay the delay between each attacks
* @param range the allowed range the entity can attack others.
* @param delay the delay between each attacks
* @param timeUnit the unit of the delay
*/
public MeleeAttackGoal(@NotNull EntityCreature entityCreature, int delay, int range, @NotNull TimeUnit timeUnit) {
public MeleeAttackGoal(@NotNull EntityCreature entityCreature, double range, int delay, @NotNull TimeUnit timeUnit) {
super(entityCreature);
this.delay = delay;
this.range = range;
this.delay = delay;
this.timeUnit = timeUnit;
}
public Cooldown getCooldown() {
public @NotNull Cooldown getCooldown() {
return this.cooldown;
}

View File

@ -157,6 +157,31 @@ public interface EquipmentHandler {
throw new IllegalStateException("Something weird happened");
}
default void setEquipment(@NotNull EntityEquipmentPacket.Slot slot, @NotNull ItemStack itemStack) {
switch (slot) {
case MAIN_HAND:
setItemInMainHand(itemStack);
break;
case OFF_HAND:
setItemInOffHand(itemStack);
break;
case HELMET:
setHelmet(itemStack);
break;
case CHESTPLATE:
setChestplate(itemStack);
break;
case LEGGINGS:
setLeggings(itemStack);
break;
case BOOTS:
setBoots(itemStack);
break;
default:
throw new IllegalStateException("Something weird happened");
}
}
/**
* Sends a specific equipment to viewers.
*

View File

@ -18,9 +18,7 @@ import net.minestom.server.utils.Utils;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.cache.CacheablePacket;
import net.minestom.server.utils.cache.TemporaryCache;
import net.minestom.server.utils.cache.TemporaryPacketCache;
import net.minestom.server.utils.cache.TimedBuffer;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull;
@ -35,7 +33,7 @@ import java.util.concurrent.TimeUnit;
public class ChunkDataPacket implements ServerPacket, CacheablePacket {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryPacketCache(5, TimeUnit.MINUTES);
private static final TemporaryPacketCache CACHE = new TemporaryPacketCache(5, TimeUnit.MINUTES);
public boolean fullChunk;
public Biome[] biomes;
@ -233,9 +231,8 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
return ServerPacketIdentifier.CHUNK_DATA;
}
@NotNull
@Override
public TemporaryCache<TimedBuffer> getCache() {
public @NotNull TemporaryPacketCache getCache() {
return CACHE;
}

View File

@ -5,9 +5,7 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.cache.CacheablePacket;
import net.minestom.server.utils.cache.TemporaryCache;
import net.minestom.server.utils.cache.TemporaryPacketCache;
import net.minestom.server.utils.cache.TimedBuffer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -18,7 +16,7 @@ import java.util.concurrent.TimeUnit;
public class UpdateLightPacket implements ServerPacket, CacheablePacket {
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryPacketCache(5, TimeUnit.MINUTES);
private static final TemporaryPacketCache CACHE = new TemporaryPacketCache(5, TimeUnit.MINUTES);
public int chunkX;
public int chunkZ;
@ -128,9 +126,8 @@ public class UpdateLightPacket implements ServerPacket, CacheablePacket {
return ServerPacketIdentifier.UPDATE_LIGHT;
}
@NotNull
@Override
public TemporaryCache<TimedBuffer> getCache() {
public @NotNull TemporaryPacketCache getCache() {
return CACHE;
}

View File

@ -192,7 +192,7 @@ public final class PacketUtils {
final boolean compression = compressionThreshold > 0;
if (compression) {
// Dummy varint
// Dummy var-int
final int packetLengthIndex = Utils.writeEmptyVarIntHeader(buffer);
final int dataLengthIndex = Utils.writeEmptyVarIntHeader(buffer);
@ -226,13 +226,13 @@ public final class PacketUtils {
} else {
// No compression
// Write dummy varint
// Write dummy var-int
final int index = Utils.writeEmptyVarIntHeader(buffer);
// Write packet id + payload
writePacket(buffer, serverPacket);
// Rewrite dummy varint to packet length
// Rewrite dummy var-int to packet length
final int afterIndex = buffer.writerIndex();
final int packetSize = (afterIndex - index) - Utils.VARINT_HEADER_SIZE;
Utils.overrideVarIntHeader(buffer, index, packetSize);

View File

@ -24,8 +24,7 @@ public interface CacheablePacket {
*
* @return the temporary packet cache
*/
@NotNull
TemporaryCache<TimedBuffer> getCache();
@NotNull TemporaryPacketCache getCache();
/**
* Gets the identifier of this packet.
@ -34,8 +33,7 @@ public interface CacheablePacket {
*
* @return this packet identifier, null to prevent caching
*/
@Nullable
UUID getIdentifier();
@Nullable UUID getIdentifier();
/**
* Gets the last time this packet changed.
@ -44,8 +42,7 @@ public interface CacheablePacket {
*/
long getTimestamp();
@Nullable
static FramedPacket getCache(@NotNull ServerPacket serverPacket) {
static @Nullable FramedPacket getCache(@NotNull ServerPacket serverPacket) {
if (!(serverPacket instanceof CacheablePacket))
return null;