Skip to content

Commit

Permalink
Merge pull request #88 from OneLiteFeatherNET/feature/cleanup
Browse files Browse the repository at this point in the history
Cleanup code base
  • Loading branch information
TheMeinerLP authored Nov 3, 2024
2 parents 769abf8 + 6031c8c commit 2c4e9e7
Show file tree
Hide file tree
Showing 112 changed files with 441 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.entity.Player;
import net.minestom.server.network.ConnectionState;

import java.util.Collection;
import java.util.List;

public class PlayersCommand extends Command {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public TestCommand2() {
var argA = ArgumentType.String("a");
var argB = ArgumentType.String("b");

addSyntax((sender, context) -> {
sender.sendMessage("a only");
}, argA);
addSyntax((sender, context) -> {
sender.sendMessage("a and b");
}, argB, argA);
addSyntax((sender, context) -> sender.sendMessage("a only"), argA);
addSyntax((sender, context) -> sender.sendMessage("a and b"), argB, argA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@State(Scope.Benchmark)
public class TagReadBenchmark {
static final Tag<String> TAG = Tag.String("key");
static final String VALUE = "value";

@Param({"false", "true"})
public boolean present;
Expand All @@ -30,14 +31,14 @@ public class TagReadBenchmark {
public void setup() {
// Tag benchmark
this.tagHandler = TagHandler.newHandler();
if (present) tagHandler.setTag(TAG, "value");
if (present) tagHandler.setTag(TAG, VALUE);
secondTag = Tag.String("key");
// Concurrent map benchmark
map = new HashMap<>();
if (present) map.put("key", "value");
if (present) map.put("key", VALUE);
// Hash map benchmark
concurrentMap = new ConcurrentHashMap<>();
if (present) concurrentMap.put("key", "value");
if (present) concurrentMap.put("key", VALUE);
}

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@State(Scope.Benchmark)
public class TagWriteBenchmark {
static final Tag<String> TAG = Tag.String("key");
static final String VALUE = "value";

TagHandler tagHandler;
Tag<String> secondTag;
Expand All @@ -26,38 +27,38 @@ public class TagWriteBenchmark {
public void setup() {
// Tag benchmark
this.tagHandler = TagHandler.newHandler();
tagHandler.setTag(TAG, "value");
tagHandler.setTag(TAG, VALUE);
secondTag = Tag.String("key");
// Concurrent map benchmark
map = new HashMap<>();
map.put("key", "value");
map.put("key", VALUE);
// Hash map benchmark
concurrentMap = new ConcurrentHashMap<>();
concurrentMap.put("key", "value");
concurrentMap.put("key", VALUE);
}

@Benchmark
public void writeConstantTag() {
tagHandler.setTag(TAG, "value");
tagHandler.setTag(TAG, VALUE);
}

@Benchmark
public void writeDifferentTag() {
tagHandler.setTag(secondTag, "value");
tagHandler.setTag(secondTag, VALUE);
}

@Benchmark
public void writeNewTag() {
tagHandler.setTag(Tag.String("key"), "value");
tagHandler.setTag(Tag.String("key"), VALUE);
}

@Benchmark
public void writeConcurrentMap() {
concurrentMap.put("key", "value");
concurrentMap.put("key", VALUE);
}

@Benchmark
public void writeMap() {
map.put("key", "value");
map.put("key", VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public record BoundingBox(Vec relativeStart, Vec relativeEnd) implements Shape {
private static final BoundingBox SNEAKING = new BoundingBox(0.6, 1.5, 0.6);
private static final BoundingBox SMALL = new BoundingBox(0.6, 0.6, 0.6);

final static BoundingBox ZERO = new BoundingBox(Vec.ZERO, Vec.ZERO);
static final BoundingBox ZERO = new BoundingBox(Vec.ZERO, Vec.ZERO);

public BoundingBox(double width, double height, double depth, Point offset) {
this(Vec.fromPoint(offset), new Vec(width, height, depth).add(offset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.function.Function;
import java.util.function.Predicate;

@ApiStatus.Internal
public final class CollisionUtils {
Expand Down Expand Up @@ -51,7 +51,7 @@ public static PhysicsResult handlePhysics(@NotNull Entity entity, @NotNull Vec e
* This is used to extend the search radius for entities we collide with
* For players this is (0.3^2 + 0.3^2 + 1.8^2) ^ (1/3) ~= 1.51
*/
public static @NotNull Collection<EntityCollisionResult> checkEntityCollisions(@NotNull Instance instance, @NotNull BoundingBox boundingBox, @NotNull Point pos, @NotNull Vec velocity, double extendRadius, @NotNull Function<Entity, Boolean> entityFilter, @Nullable PhysicsResult physicsResult) {
public static @NotNull Collection<EntityCollisionResult> checkEntityCollisions(@NotNull Instance instance, @NotNull BoundingBox boundingBox, @NotNull Point pos, @NotNull Vec velocity, double extendRadius, @NotNull Predicate<Entity> entityFilter, @Nullable PhysicsResult physicsResult) {
return EntityCollision.checkCollision(instance, boundingBox, pos, velocity, extendRadius, entityFilter, physicsResult);
}

Expand All @@ -65,7 +65,7 @@ public static PhysicsResult handlePhysics(@NotNull Entity entity, @NotNull Vec e
* @param physicsResult optional physics result
* @return the entity collision results
*/
public static @NotNull Collection<EntityCollisionResult> checkEntityCollisions(@NotNull Entity entity, @NotNull Vec velocity, double extendRadius, @NotNull Function<Entity, Boolean> entityFilter, @Nullable PhysicsResult physicsResult) {
public static @NotNull Collection<EntityCollisionResult> checkEntityCollisions(@NotNull Entity entity, @NotNull Vec velocity, double extendRadius, @NotNull Predicate<Entity> entityFilter, @Nullable PhysicsResult physicsResult) {
return EntityCollision.checkCollision(entity.getInstance(), entity.getBoundingBox(), entity.getPosition(), velocity, extendRadius, entityFilter, physicsResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

final class EntityCollision {
static @NotNull List<EntityCollisionResult> checkCollision(@NotNull Instance instance, @NotNull BoundingBox boundingBox, @NotNull Point point, @NotNull Vec entityVelocity, double extendRadius, @NotNull Function<Entity, Boolean> entityFilter, @Nullable PhysicsResult physicsResult) {
static @NotNull List<EntityCollisionResult> checkCollision(@NotNull Instance instance, @NotNull BoundingBox boundingBox, @NotNull Point point, @NotNull Vec entityVelocity, double extendRadius, @NotNull Predicate<Entity> entityFilter, @Nullable PhysicsResult physicsResult) {
double minimumRes = physicsResult != null ? physicsResult.res().res : Double.MAX_VALUE;

List<EntityCollisionResult> result = new ArrayList<>();
Expand All @@ -24,7 +24,7 @@ final class EntityCollision {
for (Entity e : instance.getNearbyEntities(point, extendRadius + maxDistance + projectileDistance)) {
SweepResult sweepResult = new SweepResult(minimumRes, 0, 0, 0, null, 0, 0, 0, 0, 0, 0);

if (!entityFilter.apply(e)) continue;
if (!entityFilter.test(e)) continue;
if (!e.hasEntityCollision()) continue;

// Overlapping with entity, math can't be done we return the entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.instance.block.BlockFace;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Represents the result of a collision with an entity
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/net/minestom/server/command/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
* Manager used to register {@link Command commands}.
Expand Down Expand Up @@ -117,10 +122,8 @@ public boolean commandExists(@NotNull String commandName) {
final ExecutableCommand executable = parsedCommand.executable();
final ExecutableCommand.Result executeResult = executable.execute(sender);
final CommandResult result = resultConverter(executable, executeResult, command);
if (result.getType() == CommandResult.Type.UNKNOWN) {
if (unknownCommandCallback != null) {
this.unknownCommandCallback.apply(sender, command);
}
if (result.getType() == CommandResult.Type.UNKNOWN && unknownCommandCallback != null) {
this.unknownCommandCallback.apply(sender, command);
}
return result;
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/minestom/server/command/ConsoleSender.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package net.minestom.server.command;

import java.util.UUID;

import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.pointer.Pointers;
import net.kyori.adventure.text.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
public class Command {

private final static Logger LOGGER = LoggerFactory.getLogger(Command.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Command.class);

private final String name;
private final String[] aliases;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Represents a syntax in {@link Command}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minestom.server.command.builder.parser.ValidSyntaxHolder;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,20 @@ private static EntityFinder parseStructure(@NotNull CommandSender sender,
final String structureData = structure.substring(1, structure.length() - 1);
//System.out.println("structure data: " + structureData);

String currentArgument = "";
StringBuilder currentArgument = new StringBuilder();
for (int i = 0; i < structureData.length(); i++) {
final char c = structureData.charAt(i);
if (c == '=') {

// Replace all unnecessary spaces
currentArgument = currentArgument.trim();
String argument = currentArgument.toString().trim();

if (!VALID_ARGUMENTS.contains(currentArgument))
throw new ArgumentSyntaxException("Argument name '" + currentArgument + "' does not exist", input, INVALID_ARGUMENT_NAME);
if (!VALID_ARGUMENTS.contains(argument))
throw new ArgumentSyntaxException("Argument name '" + argument + "' does not exist", input, INVALID_ARGUMENT_NAME);

i = parseArgument(sender, entityFinder, currentArgument, input, structureData, i);
currentArgument = ""; // Reset current argument
i = parseArgument(sender, entityFinder, argument, input, structureData, i);
currentArgument.setLength(0); // Reset current argument
} else {
currentArgument += c;
currentArgument.append(c);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ record Noop(BinaryTag content) implements DataPredicate {

}

public static final BinaryTagSerializer<DataPredicate> NBT_TYPE = new BinaryTagSerializer<DataPredicate>() {
BinaryTagSerializer<DataPredicate> NBT_TYPE = new BinaryTagSerializer<>() {
@Override
public @NotNull BinaryTag write(@NotNull DataPredicate value) {
return ((Noop) value).content;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/minestom/server/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
* <p>
* To create your own entity you probably want to extend {@link LivingEntity} or {@link EntityCreature} instead.
*/
@SuppressWarnings("java:S3252")
public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, EventHandler<EntityEvent>, Taggable,
PermissionHandler, HoverEventSource<ShowEntity>, Sound.Emitter, Shape, AcquirableSource<Entity> {
private static final AtomicInteger LAST_ENTITY_ID = new AtomicInteger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.ai.TargetSelector;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private double computeValue(double base) {
result *= (1.0f + modifier.amount());
}

return Math.clamp(result, getAttribute().minValue(), getAttribute().maxValue());
return Math.clamp(result, attribute().minValue(), attribute().maxValue());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/minestom/server/entity/damage/Damage.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public Damage(@NotNull DynamicRegistry.Key<DamageType> type, @Nullable Entity so
* @return the sound to play when the given entity is hurt by this damage type. Can be null if no sound should play
*/
public @Nullable SoundEvent getSound(@NotNull LivingEntity entity) {
if (entity instanceof Player) {
return getPlayerSound((Player) entity);
if (entity instanceof Player player) {
return getPlayerSound(player);
}
return getGenericSound(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minestom.server.entity.MetadataHolder;
import net.minestom.server.network.NetworkBuffer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class FrogMeta extends AnimalMeta {
public static final byte OFFSET = AnimalMeta.MAX_OFFSET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum Spell {
DISAPPEAR,
BLINDNESS;

private final static Spell[] VALUES = values();
private static final Spell[] VALUES = values();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public enum State {
SEMI_PUFFED,
FULLY_PUFFED;

private final static State[] VALUES = values();
private static final State[] VALUES = values();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.minestom.server.entity.pathfinding.followers;

import net.minestom.server.collision.CollisionUtils;
import net.minestom.server.collision.PhysicsResult;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.minestom.server.entity.pathfinding.followers;

import net.minestom.server.collision.CollisionUtils;
import net.minestom.server.collision.PhysicsResult;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
Expand Down Expand Up @@ -45,10 +44,9 @@ public void moveTowards(@NotNull Point direction, double speed, @NotNull Point l
}

var instance = entity.getInstance();
if (instance != null)
if (instance.getBlock(position).isLiquid()) {
speed *= WATER_SPEED_MULTIPLIER;
}
if (instance != null && instance.getBlock(position).isLiquid()) {
speed *= WATER_SPEED_MULTIPLIER;
}

final double radians = Math.atan2(dz, dx);
final double speedX = Math.cos(radians) * speed;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/minestom/server/event/EventBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public <M extends E> FilteredBuilder<E, T> map(@NotNull Class<M> eventType,
final var copy = Map.copyOf(mapped);
final var eventTypes = copy.keySet();

Map<Class<? extends Event>, Consumer<E>> consumers = new HashMap<>(eventTypes.size());
Map<Class<? extends Event>, Consumer<E>> consumers = HashMap.newHashMap(eventTypes.size());
for (var eventType : eventTypes) {
final var consumer = copy.get(eventType);
consumers.put(eventType, event -> {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/minestom/server/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ protected Builder(Class<T> eventType) {
@Override
public @NotNull Result run(@NotNull T event) {
// Event cancellation
if (ignoreCancelled && event instanceof CancellableEvent &&
((CancellableEvent) event).isCancelled()) {
if (ignoreCancelled && event instanceof CancellableEvent cancellableEvent && cancellableEvent.isCancelled()) {
return Result.INVALID;
}
// Expiration predicate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.minestom.server.event.player;

import net.minestom.server.coordinate.BlockVec;
import net.minestom.server.coordinate.Point;
import net.minestom.server.entity.Player;
import net.minestom.server.event.trait.BlockEvent;
import net.minestom.server.event.trait.CancellableEvent;
Expand Down
Loading

0 comments on commit 2c4e9e7

Please sign in to comment.