Use newer Java features in places where it makes sense and more (#1009)

This commit is contained in:
altrisi authored and GitHub committed 2021-08-03 07:32:40 -03:00
1 parent 28cb136c6b
commit 7fb5e47da5
34 files changed
+338 -399

No files matched your search

@@ -18,7 +18,6 @@ import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.command.CommandException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.WorldSavePath;
import org.apache.commons.lang3.tuple.Triple;
import java.io.BufferedReader;
import java.io.BufferedWriter;
@@ -55,6 +54,7 @@ public class AppStoreManager
*/
private static String scarpetRepoLink = "https://api.github.com/repos/gnembon/scarpet/contents/programs/";
private static record AppInfo(String name, String url, StoreNode source) {}
public static class ScarpetAppStoreValidator extends Validator<String>
{
@@ -253,23 +253,23 @@ public class AppStoreManager
public static int downloadScript(ServerCommandSource source, String path)
{
Triple<String, String, StoreNode> nodeInfo = getFileNode(path);
AppInfo nodeInfo = getFileNode(path);
return downloadScript(source, path, nodeInfo, false);
}
private static int downloadScript(ServerCommandSource source, String path, Triple<String, String, StoreNode> nodeInfo, boolean useTrash)
private static int downloadScript(ServerCommandSource source, String path, AppInfo nodeInfo, boolean useTrash)
{
String code;
try
{
code = getStringFromStream(nodeInfo.getMiddle());
code = getStringFromStream(nodeInfo.url());
}
catch (IOException e)
{
throw new CommandException(Messenger.c("rb Failed to obtain app file content: "+e.getMessage()));
}
if (!saveScriptToFile(source, path, nodeInfo.getLeft(), code, false, useTrash)) return 0;
boolean success = CarpetServer.scriptServer.addScriptHost(source, nodeInfo.getLeft().replaceFirst("\\.sc$", ""), null, true, false, false, nodeInfo.getRight());
if (!saveScriptToFile(source, path, nodeInfo.name(), code, false, useTrash)) return 0;
boolean success = CarpetServer.scriptServer.addScriptHost(source, nodeInfo.name().replaceFirst("\\.sc$", ""), null, true, false, false, nodeInfo.source());
return success?1:0;
}
@@ -278,12 +278,12 @@ public class AppStoreManager
* @param appPath The user inputted path to the scarpet script
* @return Pair of app file name and content
*/
public static Triple<String, String, StoreNode> getFileNode(String appPath)
public static AppInfo getFileNode(String appPath)
{
return getFileNodeFrom(APP_STORE_ROOT, appPath);
}
public static Triple<String, String, StoreNode> getFileNodeFrom(StoreNode start, String appPath)
public static AppInfo getFileNodeFrom(StoreNode start, String appPath)
{
String[] path = appPath.split("/");
StoreNode appKiosk = start;
@@ -293,7 +293,7 @@ public class AppStoreManager
appKiosk = appKiosk.drillDown(pathElement);
String appName = path[path.length-1];
appKiosk.getValue(appName);
return Triple.of(appName, appKiosk.getValue(appName), appKiosk);
return new AppInfo(appName, appKiosk.getValue(appName), appKiosk);
}
catch (IOException e)
{
@@ -390,8 +390,8 @@ public class AppStoreManager
if (original.matches("^https?://.*$")) // We've got a full url here: Just use it
return original;
if (original.charAt(0) == '/') // We've got an absolute path: Use app store root
return getFileNode(original.substring(1)).getMiddle();
return getFileNodeFrom(storeSource, original).getMiddle(); // Relative path: Use download location
return getFileNode(original.substring(1)).url();
return getFileNodeFrom(storeSource, original).url(); // Relative path: Use download location
}
public static void addResource(CarpetScriptHost carpetScriptHost, StoreNode storeSource, Value resource)
@@ -461,7 +461,7 @@ public class AppStoreManager
throw new InternalExpressionException("App resource tried to leave script reserved space");
try
{
downloadScript(carpetScriptHost.responsibleSource, target, Triple.of(target, contentUrl, getNewStoreNode(storeSource, source, contentUrl)), true);
downloadScript(carpetScriptHost.responsibleSource, target, new AppInfo(target, contentUrl, getNewStoreNode(storeSource, source, contentUrl)), true);
}
catch (CommandException e)
{
@@ -2,7 +2,6 @@ package carpet.script.utils;
import carpet.CarpetSettings;
import carpet.network.ServerNetworkHandler;
import carpet.script.CarpetContext;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
@@ -16,8 +15,6 @@ import carpet.script.value.MapValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -45,7 +42,6 @@ import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import net.minecraft.util.registry.RegistryKey;
import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList;
import java.util.Collection;
@@ -60,11 +56,14 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.Map.entry;
public class ShapeDispatcher
{
private static final Map<String, ParticleEffect> particleCache = new HashMap<>();
public static record ShapeWithConfig(ExpiringShape shape, Map<String, Value> config) {}
public static Pair<ExpiringShape,Map<String, Value>> fromFunctionArgs(
public static ShapeWithConfig fromFunctionArgs(
MinecraftServer server, ServerWorld world,
List<Value> lv,
Set<ServerPlayerEntity> playerSet
@@ -114,10 +113,10 @@ public class ShapeDispatcher
}
params.remove("player");
}
return Pair.of(ShapeDispatcher.create(server, shapeType, params), params);
return new ShapeWithConfig(ShapeDispatcher.create(server, shapeType, params), params);
}
public static void sendShape(Collection<ServerPlayerEntity> players, List<Pair<ExpiringShape,Map<String, Value>>> shapes)
public static void sendShape(Collection<ServerPlayerEntity> players, List<ShapeWithConfig> shapes)
{
List<ServerPlayerEntity> clientPlayers = new ArrayList<>();
List<ServerPlayerEntity> alternativePlayers = new ArrayList<>();
@@ -137,9 +136,9 @@ public class ShapeDispatcher
NbtList tag = new NbtList();
int tagcount = 0;
for (Pair<ExpiringShape, Map<String, Value>> s : shapes)
for (ShapeWithConfig s : shapes)
{
tag.add(ExpiringShape.toTag(s.getRight())); // 4000 shapes limit boxes
tag.add(ExpiringShape.toTag(s.config())); // 4000 shapes limit boxes
if (tagcount++>1000)
{
tagcount = 0;
@@ -154,7 +153,7 @@ public class ShapeDispatcher
if (!alternativePlayers.isEmpty())
{
List<Consumer<ServerPlayerEntity>> alternatives = new ArrayList<>();
shapes.forEach(s -> alternatives.add(s.getLeft().alternative()));
shapes.forEach(s -> alternatives.add(s.shape().alternative()));
alternativePlayers.forEach(p -> alternatives.forEach( a -> a.accept(p)));
}
}
@@ -405,8 +404,8 @@ public class ShapeDispatcher
}
// list of params that need to be there
private final Set<String> required = ImmutableSet.of("duration", "shape", "dim");
private final Map<String, Value> optional = ImmutableMap.of(
private final Set<String> required = Set.of("duration", "shape", "dim");
private final Map<String, Value> optional = Map.of(
"color", new NumericValue(-1),
"follow", new NumericValue(-1),
"line", new NumericValue(2.0),
@@ -425,20 +424,19 @@ public class ShapeDispatcher
public static class DisplayedText extends ExpiringShape
{
private final Set<String> required = ImmutableSet.of("pos", "text");
private final Map<String, Value> optional = ImmutableMap.<String, Value>builder().
put("facing", new StringValue("player")).
put("raise", new NumericValue(0)).
put("tilt", new NumericValue(0)).
put("lean", new NumericValue(0)).
put("turn", new NumericValue(0)).
put("indent", new NumericValue(0)).
put("height", new NumericValue(0)).
put("align", new StringValue("center")).
put("size", new NumericValue(10)).
put("value", Value.NULL).
put("doublesided", new NumericValue(0)).
build();
private final Set<String> required = Set.of("pos", "text");
private final Map<String, Value> optional = Map.ofEntries(
entry("facing", new StringValue("player")),
entry("raise", new NumericValue(0)),
entry("tilt", new NumericValue(0)),
entry("lean", new NumericValue(0)),
entry("turn", new NumericValue(0)),
entry("indent", new NumericValue(0)),
entry("height", new NumericValue(0)),
entry("align", new StringValue("center")),
entry("size", new NumericValue(10)),
entry("value", Value.NULL),
entry("doublesided", new NumericValue(0)));
@Override
protected Set<String> requiredParams() { return Sets.union(super.requiredParams(), required); }
@Override
@@ -552,8 +550,8 @@ public class ShapeDispatcher
public static class Box extends ExpiringShape
{
private final Set<String> required = ImmutableSet.of("from", "to");
private final Map<String, Value> optional = ImmutableMap.of();
private final Set<String> required = Set.of("from", "to");
private final Map<String, Value> optional = Map.of();
@Override
protected Set<String> requiredParams() { return Sets.union(super.requiredParams(), required); }
@Override
@@ -630,8 +628,8 @@ public class ShapeDispatcher
public static class Line extends ExpiringShape
{
private final Set<String> required = ImmutableSet.of("from", "to");
private final Map<String, Value> optional = ImmutableMap.of();
private final Set<String> required = Set.of("from", "to");
private final Map<String, Value> optional = Map.of();
@Override
protected Set<String> requiredParams() { return Sets.union(super.requiredParams(), required); }
@Override
@@ -683,8 +681,8 @@ public class ShapeDispatcher
public static class Sphere extends ExpiringShape
{
private final Set<String> required = ImmutableSet.of("center", "radius");
private final Map<String, Value> optional = ImmutableMap.of("level", Value.ZERO);
private final Set<String> required = Set.of("center", "radius");
private final Map<String, Value> optional = Map.of("level", Value.ZERO);
@Override
protected Set<String> requiredParams() { return Sets.union(super.requiredParams(), required); }
@Override
@@ -755,8 +753,8 @@ public class ShapeDispatcher
}
public static class Cylinder extends ExpiringShape
{
private final Set<String> required = ImmutableSet.of("center", "radius");
private final Map<String, Value> optional = ImmutableMap.of(
private final Set<String> required = Set.of("center", "radius");
private final Map<String, Value> optional = Map.of(
"level", Value.ZERO,
"height", Value.ZERO,
"axis", new StringValue("y")
@@ -913,6 +911,7 @@ public class ShapeDispatcher
@Override
public NbtElement toTag(Value value) { return NbtString.of(value.getString()); }
@Override
public Value decode(NbtElement tag) { return new StringValue(tag.asString()); }
}
public static class TextParam extends StringParam
@@ -1149,6 +1148,7 @@ public class ShapeDispatcher
throw new InternalExpressionException("'"+p.id+"' requires a triple, block or entity to indicate position");
}
@Override
public Value decode(NbtElement tag)
{
NbtList ctag = (NbtList)tag;
@@ -1187,6 +1187,7 @@ public class ShapeDispatcher
return ListValue.wrap(points);
}
@Override
public Value decode(NbtElement tag)
{
NbtList ltag = (NbtList)tag;
@@ -1228,6 +1229,7 @@ public class ShapeDispatcher
super(id);
}
@Override
public Value decode(NbtElement tag) { return new NumericValue(((NbtInt)tag).intValue()); }
@Override
public NbtElement toTag(Value value) { return NbtInt.of(NumericValue.asNumber(value, id).getInt()); }
@@ -1254,6 +1256,7 @@ public class ShapeDispatcher
return new NumericValue(player.getId());
}
@Override
public Value decode(NbtElement tag) { return new NumericValue(((NbtInt)tag).intValue()); }
}
@@ -38,6 +38,7 @@ public class SimplexNoiseSampler extends PerlinNoiseSampler {
return g;
}
@Override
public double sample2d(double x, double y) {
x = x/2;
y = y/2;
@@ -75,6 +76,7 @@ public class SimplexNoiseSampler extends PerlinNoiseSampler {
return 35.0D * (aa + ab + ac)+0.5;
}
@Override
public double sample3d(double d, double e, double f) {
d = d/2;
e = e/2;
@@ -122,6 +122,7 @@ public class SnoopyCommandSource extends ServerCommandSource
return new SnoopyCommandSource(output, position, rotation, world, level, simpleName, name, server, entity, consumer, entityAnchor, error, chatOutput);
}
@Override
public ServerCommandSource mergeConsumers(ResultConsumer<ServerCommandSource> consumer, BinaryOperator<ResultConsumer<ServerCommandSource>> binaryOperator)
{
ResultConsumer<ServerCommandSource> resultConsumer = binaryOperator.apply(this.resultConsumer, consumer);
@@ -133,7 +133,7 @@ public class SystemInfo {
put("java_system_cpu_load", c -> {
OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
OperatingSystemMXBean.class);
return new NumericValue(osBean.getSystemCpuLoad());
return new NumericValue(osBean.getCpuLoad());
});
put("java_process_cpu_load", c -> {
OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
@@ -1,7 +1,6 @@
package carpet.script.utils;
import carpet.fakes.MinecraftServerInterface;
import com.google.common.collect.ImmutableList;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.network.packet.s2c.play.ChunkDataS2CPacket;
@@ -93,7 +92,7 @@ public class WorldTools
boolean bl = generatorOptions.isDebugWorld();
long l = generatorOptions.getSeed();
long m = BiomeAccess.hashSeed(l);
List<Spawner> list = ImmutableList.of();
List<Spawner> list = List.of();
SimpleRegistry<DimensionOptions> simpleRegistry = generatorOptions.getDimensions();
DimensionOptions dimensionOptions = simpleRegistry.get(DimensionOptions.OVERWORLD);
ChunkGenerator chunkGenerator2;