Skip to content

Commit

Permalink
Few cleanups over the code and use more Java 9+ features
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Feb 26, 2023
1 parent 98fd5dc commit 1589e9a
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 276 deletions.
Expand Up @@ -174,7 +174,6 @@
import java.util.concurrent.ForkJoinPool;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -566,10 +565,10 @@ public Property<?> load(net.minecraft.world.level.block.state.properties.Propert
return new BooleanProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else if (state instanceof DirectionProperty) {
return new DirectionalProperty(state.getName(),
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).collect(Collectors.toList()));
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.EnumProperty) {
return new EnumProperty(state.getName(),
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).collect(Collectors.toList()));
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.IntegerProperty) {
return new IntegerProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else {
Expand Down
Expand Up @@ -175,7 +175,6 @@
import java.util.concurrent.ForkJoinPool;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -558,10 +557,10 @@ public Property<?> load(net.minecraft.world.level.block.state.properties.Propert
return new BooleanProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else if (state instanceof DirectionProperty) {
return new DirectionalProperty(state.getName(),
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).collect(Collectors.toList()));
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.EnumProperty) {
return new EnumProperty(state.getName(),
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).collect(Collectors.toList()));
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.IntegerProperty) {
return new IntegerProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else {
Expand Down
Expand Up @@ -174,7 +174,6 @@
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -558,10 +557,10 @@ public Property<?> load(net.minecraft.world.level.block.state.properties.Propert
return new BooleanProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else if (state instanceof DirectionProperty) {
return new DirectionalProperty(state.getName(),
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).collect(Collectors.toList()));
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.EnumProperty) {
return new EnumProperty(state.getName(),
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).collect(Collectors.toList()));
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.IntegerProperty) {
return new IntegerProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else {
Expand Down
Expand Up @@ -174,7 +174,6 @@
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -558,10 +557,10 @@ public Property<?> load(net.minecraft.world.level.block.state.properties.Propert
return new BooleanProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else if (state instanceof DirectionProperty) {
return new DirectionalProperty(state.getName(),
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).collect(Collectors.toList()));
(List<Direction>) state.getPossibleValues().stream().map(e -> Direction.valueOf(((StringRepresentable) e).getSerializedName().toUpperCase(Locale.ROOT))).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.EnumProperty) {
return new EnumProperty(state.getName(),
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).collect(Collectors.toList()));
(List<String>) state.getPossibleValues().stream().map(e -> ((StringRepresentable) e).getSerializedName()).toList());
} else if (state instanceof net.minecraft.world.level.block.state.properties.IntegerProperty) {
return new IntegerProperty(state.getName(), ImmutableList.copyOf(state.getPossibleValues()));
} else {
Expand Down
Expand Up @@ -32,7 +32,6 @@
import java.util.Map;

@Deprecated
@SuppressWarnings("deprecation")
public class CommandsManagerRegistration extends CommandRegistration {

protected CommandsManager<?> commands;
Expand Down
Expand Up @@ -84,7 +84,7 @@ public static PermissionsResolverManager getInstance() {
private YAMLProcessor config;
private final List<Class<? extends PermissionsResolver>> enabledResolvers = new ArrayList<>();

@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked" })
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[] {
PluginPermissionsResolver.class,
PermissionsExResolver.class,
Expand Down
Expand Up @@ -371,7 +371,7 @@ private void copyDefaultConfig(InputStream input, File actual, String name) {
}

private String rebuildArguments(String commandLabel, String[] args) {
int plSep = commandLabel.indexOf(":");
int plSep = commandLabel.indexOf(':');
if (plSep >= 0 && plSep < commandLabel.length() + 1) {
commandLabel = commandLabel.substring(plSep + 1);
}
Expand Down Expand Up @@ -555,7 +555,7 @@ public void onAsyncTabComplete(com.destroystokyo.paper.event.server.AsyncTabComp
if (owner != WorldEditPlugin.this) {
return;
}
int plSep = label.indexOf(":");
int plSep = label.indexOf(':');
if (plSep >= 0 && plSep < label.length() + 1) {
label = label.substring(plSep + 1);
buffer = "/" + buffer.substring(plSep + 2);
Expand Down
Expand Up @@ -33,30 +33,28 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

public class CLIBlockRegistry extends BundledBlockRegistry {

private Property<?> createProperty(String type, String key, List<String> values) {
switch (type) {
case "int": {
List<Integer> fixedValues = values.stream().map(Integer::parseInt).collect(Collectors.toList());
case "int" -> {
List<Integer> fixedValues = values.stream().map(Integer::parseInt).toList();
return new IntegerProperty(key, fixedValues);
}
case "bool": {
List<Boolean> fixedValues = values.stream().map(Boolean::parseBoolean).collect(Collectors.toList());
case "bool" -> {
List<Boolean> fixedValues = values.stream().map(Boolean::parseBoolean).toList();
return new BooleanProperty(key, fixedValues);
}
case "enum": {
case "enum" -> {
return new EnumProperty(key, values);
}
case "direction": {
List<Direction> fixedValues = values.stream().map(String::toUpperCase).map(Direction::valueOf).collect(Collectors.toList());
case "direction" -> {
List<Direction> fixedValues = values.stream().map(String::toUpperCase).map(Direction::valueOf).toList();
return new DirectionalProperty(key, fixedValues);
}
default:
throw new RuntimeException("Failed to create property");
default -> throw new RuntimeException("Failed to create property");
}
}

Expand Down
Expand Up @@ -172,7 +172,7 @@ public void setNbtData(CompoundTag rootTag) {
try {
spawnDataTag = NBTUtils.getChildTag(values, "SpawnData", CompoundTag.class);
mobType = spawnDataTag.getString("id");
if (mobType.equals("")) {
if (mobType.isEmpty()) {
throw new InvalidFormatException("No spawn id.");
}
this.mobType = mobType;
Expand Down
Expand Up @@ -61,7 +61,7 @@ public boolean containsKey(String key) {
return linTag.value().containsKey(key);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "rawtypes" })
@Override
public Map<String, Tag<?, ?>> getValue() {
return ImmutableMap.copyOf(Maps.transformValues(
Expand Down
3 changes: 1 addition & 2 deletions worldedit-core/src/main/java/com/sk89q/jnbt/ListTag.java
Expand Up @@ -38,7 +38,6 @@
import java.util.List;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

/**
Expand All @@ -57,7 +56,7 @@ public final class ListTag<EV, E extends LinTag<EV>> extends Tag<Object, LinList
public ListTag(Class<? extends Tag<EV, E>> type, List<? extends Tag<EV, E>> value) {
this(LinListTag.of(
LinTagType.fromId(LinTagId.fromId(NBTUtils.getTypeCode(type))),
value.stream().map(Tag::toLinTag).collect(Collectors.toList())
value.stream().map(Tag::toLinTag).toList()
));
}

Expand Down
Expand Up @@ -352,7 +352,7 @@ private List<TracingExtent> getActiveTracingExtents() {
}
return tracingExtents.stream()
.filter(TracingExtent::isActive)
.collect(Collectors.toList());
.toList();
}

/**
Expand Down Expand Up @@ -946,7 +946,7 @@ private void dumpTracingInformation() {
for (BlockVector3 loc : touchedLocations) {
List<TracingExtent> stack = tracingExtents.stream()
.filter(it -> it.getTouchedLocations().contains(loc))
.collect(Collectors.toList());
.toList();
boolean anyFailed = stack.stream()
.anyMatch(it -> it.getFailedActions().containsKey(loc));
if (anyFailed && stacks.add(stack)) {
Expand Down
Expand Up @@ -52,9 +52,9 @@ public Generator(Collection<? extends Command> subCommands) {
}

public Command.Condition build() {
final List<Command.Condition> conditions = subCommands.stream().map(Command::getCondition).collect(Collectors.toList());
final List<Optional<PermissionCondition>> permConds = conditions.stream().map(c -> c.as(PermissionCondition.class)).collect(Collectors.toList());
if (permConds.stream().anyMatch(o -> !o.isPresent())) {
final List<Command.Condition> conditions = subCommands.stream().map(Command::getCondition).toList();
final List<Optional<PermissionCondition>> permConds = conditions.stream().map(c -> c.as(PermissionCondition.class)).toList();
if (permConds.stream().anyMatch(Optional::isEmpty)) {
// if any sub-command doesn't require permissions, then this command doesn't require permissions
return new PermissionCondition(ImmutableSet.of());
}
Expand Down
Expand Up @@ -88,26 +88,23 @@ public static Stream<String> getBlockPropertySuggestions(String blockType, Strin
// only property, no value yet
final List<? extends Property<?>> matchingProps = propertyMap.entrySet().stream()
.filter(p -> !matchedProperties.contains(p.getKey()) && p.getKey().startsWith(matchProp))
.map(Map.Entry::getValue).collect(Collectors.toList());
switch (matchingProps.size()) {
case 0:
return propertyMap.keySet().stream().filter(p -> !matchedProperties.contains(p)).map(prop ->
lastValidInput + prop + "=");
case 1:
return matchingProps.get(0).getValues().stream().map(val ->
lastValidInput + matchingProps.get(0).getName() + "="
+ val.toString().toLowerCase(Locale.ROOT));
default:
return matchingProps.stream().map(p -> lastValidInput + p.getName() + "=");
}
.map(Map.Entry::getValue).toList();
return switch (matchingProps.size()) {
case 0 -> propertyMap.keySet().stream().filter(p -> !matchedProperties.contains(p)).map(prop ->
lastValidInput + prop + "=");
case 1 -> matchingProps.get(0).getValues().stream().map(val ->
lastValidInput + matchingProps.get(0).getName() + "="
+ val.toString().toLowerCase(Locale.ROOT));
default -> matchingProps.stream().map(p -> lastValidInput + p.getName() + "=");
};
} else {
Property<?> prop = propertyMap.get(matchProp);
if (prop == null) {
return propertyMap.keySet().stream().map(p -> lastValidInput + p);
}
final List<String> values = prop.getValues().stream().map(v -> v.toString().toLowerCase(Locale.ROOT)).collect(Collectors.toList());
final List<String> values = prop.getValues().stream().map(v -> v.toString().toLowerCase(Locale.ROOT)).toList();
String matchVal = propVal[1].toLowerCase(Locale.ROOT);
List<String> matchingVals = values.stream().filter(val -> val.startsWith(matchVal)).collect(Collectors.toList());
List<String> matchingVals = values.stream().filter(val -> val.startsWith(matchVal)).toList();
if (matchingVals.isEmpty()) {
return values.stream().map(val -> lastValidInput + prop.getName() + "=" + val);
} else {
Expand Down
Expand Up @@ -85,7 +85,7 @@ public MaskFactory(WorldEdit worldEdit) {
public List<String> getSuggestions(String input, ParserContext context) {
final String[] split = input.split(" ");
if (split.length > 1) {
String prev = input.substring(0, input.lastIndexOf(" ")) + " ";
String prev = input.substring(0, input.lastIndexOf(' ')) + " ";
return super.getSuggestions(split[split.length - 1], context).stream()
.map(s -> prev + s)
.collect(Collectors.toList());
Expand Down

0 comments on commit 1589e9a

Please sign in to comment.