warning cleanup
This commit is contained in:
1 parent
5ab4a9e442
commit
277872b33c
106 files changed
+4495
-4345
No files matched your search
@@ -2,7 +2,7 @@ package carpet.script.argument;
|
||||
|
||||
public abstract class Argument
|
||||
{
|
||||
public int offset;
|
||||
public final int offset;
|
||||
|
||||
protected Argument(int offset)
|
||||
{
|
||||
|
||||
@@ -14,53 +14,55 @@ import java.util.NoSuchElementException;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class BlockArgument extends Argument
|
||||
{
|
||||
public final BlockValue block;
|
||||
public final String replacement;
|
||||
@Nullable public final String replacement;
|
||||
|
||||
private BlockArgument(final BlockValue b, final int o)
|
||||
private BlockArgument(BlockValue b, int o)
|
||||
{
|
||||
super(o);
|
||||
block = b;
|
||||
replacement = null;
|
||||
}
|
||||
|
||||
private BlockArgument(final BlockValue b, final int o, final String replacement)
|
||||
private BlockArgument(BlockValue b, int o, @Nullable String replacement)
|
||||
{
|
||||
super(o);
|
||||
block = b;
|
||||
this.replacement = replacement;
|
||||
}
|
||||
|
||||
public static BlockArgument findIn(final CarpetContext c, final List<Value> params, final int offset)
|
||||
public static BlockArgument findIn(CarpetContext c, List<Value> params, int offset)
|
||||
{
|
||||
return findIn(c, params, offset, false, false, false);
|
||||
}
|
||||
|
||||
public static BlockArgument findIn(final CarpetContext c, final List<Value> params, final int offset, final boolean acceptString)
|
||||
public static BlockArgument findIn(CarpetContext c, List<Value> params, int offset, boolean acceptString)
|
||||
{
|
||||
return findIn(c, params, offset, acceptString, false, false);
|
||||
}
|
||||
|
||||
public static BlockArgument findIn(final CarpetContext c, final List<Value> params, final int offset, final boolean acceptString, final boolean optional, final boolean anyString)
|
||||
public static BlockArgument findIn(CarpetContext c, List<Value> params, int offset, boolean acceptString, boolean optional, boolean anyString)
|
||||
{
|
||||
return findIn(c, params.listIterator(offset), offset, acceptString, optional, anyString);
|
||||
}
|
||||
|
||||
public static BlockArgument findIn(final CarpetContext c, final Iterator<Value> params, final int offset, final boolean acceptString, final boolean optional, final boolean anyString)
|
||||
public static BlockArgument findIn(CarpetContext c, Iterator<Value> params, int offset, boolean acceptString, boolean optional, boolean anyString)
|
||||
{
|
||||
try
|
||||
{
|
||||
final Value v1 = params.next();
|
||||
Value v1 = params.next();
|
||||
//add conditional from string name
|
||||
if (optional && v1.isNull())
|
||||
{
|
||||
return new BlockArgument(null, 1 + offset);
|
||||
return new MissingBlockArgument(1 + offset, null);
|
||||
}
|
||||
if (anyString && v1 instanceof StringValue)
|
||||
{
|
||||
return new BlockArgument(null, 1 + offset, v1.getString());
|
||||
return new MissingBlockArgument(1 + offset, v1.getString());
|
||||
}
|
||||
if (acceptString && v1 instanceof StringValue)
|
||||
{
|
||||
@@ -70,28 +72,26 @@ public class BlockArgument extends Argument
|
||||
{
|
||||
return new BlockArgument(((BlockValue) v1), 1 + offset);
|
||||
}
|
||||
final BlockPos pos = c.origin();
|
||||
BlockPos pos = c.origin();
|
||||
if (v1 instanceof ListValue)
|
||||
{
|
||||
final List<Value> args = ((ListValue) v1).getItems();
|
||||
final int xpos = (int) NumericValue.asNumber(args.get(0)).getLong();
|
||||
final int ypos = (int) NumericValue.asNumber(args.get(1)).getLong();
|
||||
final int zpos = (int) NumericValue.asNumber(args.get(2)).getLong();
|
||||
List<Value> args = ((ListValue) v1).getItems();
|
||||
int xpos = (int) NumericValue.asNumber(args.get(0)).getLong();
|
||||
int ypos = (int) NumericValue.asNumber(args.get(1)).getLong();
|
||||
int zpos = (int) NumericValue.asNumber(args.get(2)).getLong();
|
||||
|
||||
return new BlockArgument(
|
||||
new BlockValue(
|
||||
null,
|
||||
c.level(),
|
||||
new BlockPos(pos.getX() + xpos, pos.getY() + ypos, pos.getZ() + zpos)
|
||||
),
|
||||
1 + offset);
|
||||
}
|
||||
final int xpos = (int) NumericValue.asNumber(v1).getLong();
|
||||
final int ypos = (int) NumericValue.asNumber(params.next()).getLong();
|
||||
final int zpos = (int) NumericValue.asNumber(params.next()).getLong();
|
||||
int xpos = (int) NumericValue.asNumber(v1).getLong();
|
||||
int ypos = (int) NumericValue.asNumber(params.next()).getLong();
|
||||
int zpos = (int) NumericValue.asNumber(params.next()).getLong();
|
||||
return new BlockArgument(
|
||||
new BlockValue(
|
||||
null,
|
||||
c.level(),
|
||||
new BlockPos(pos.getX() + xpos, pos.getY() + ypos, pos.getZ() + zpos)
|
||||
),
|
||||
@@ -104,7 +104,15 @@ public class BlockArgument extends Argument
|
||||
}
|
||||
}
|
||||
|
||||
private static InternalExpressionException handleError(final boolean optional, final boolean acceptString)
|
||||
public static class MissingBlockArgument extends BlockArgument
|
||||
{
|
||||
public MissingBlockArgument(int o, @Nullable String replacement)
|
||||
{
|
||||
super(BlockValue.NONE, o, replacement);
|
||||
}
|
||||
}
|
||||
|
||||
private static InternalExpressionException handleError(boolean optional, boolean acceptString)
|
||||
{
|
||||
String message = "Block-type argument should be defined either by three coordinates (a triple or by three arguments), or a block value";
|
||||
if (acceptString)
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.minecraft.nbt.TagTypes;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
@@ -50,15 +51,15 @@ import java.util.stream.Stream;
|
||||
|
||||
public class FileArgument
|
||||
{
|
||||
public String resource;
|
||||
public Type type;
|
||||
public String zipContainer;
|
||||
public boolean isFolder;
|
||||
public boolean isShared;
|
||||
public Reason reason;
|
||||
public final String resource;
|
||||
public final Type type;
|
||||
public final String zipContainer;
|
||||
public final boolean isFolder;
|
||||
public final boolean isShared;
|
||||
public final Reason reason;
|
||||
private FileSystem zfs;
|
||||
private Path zipPath;
|
||||
private ScriptHost host;
|
||||
private final ScriptHost host;
|
||||
|
||||
public static final Object writeIOSync = new Object();
|
||||
|
||||
@@ -70,7 +71,7 @@ public class FileArgument
|
||||
{
|
||||
zfs.close();
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new InternalExpressionException("Unable to close zip container: " + zipContainer);
|
||||
}
|
||||
@@ -90,9 +91,9 @@ public class FileArgument
|
||||
private final String id;
|
||||
private final String extension;
|
||||
|
||||
public static Map<String, Type> of = Arrays.stream(values()).collect(Collectors.toMap(t -> t.id, t -> t));
|
||||
private static final Map<String, Type> of = Arrays.stream(values()).collect(Collectors.toMap(t -> t.id, t -> t));
|
||||
|
||||
Type(final String id, final String extension)
|
||||
Type(String id, String extension)
|
||||
{
|
||||
this.id = id;
|
||||
this.extension = extension;
|
||||
@@ -104,7 +105,7 @@ public class FileArgument
|
||||
READ, CREATE, DELETE
|
||||
}
|
||||
|
||||
public FileArgument(final String resource, final Type type, final String zipContainer, final boolean isFolder, final boolean isShared, final Reason reason, final ScriptHost host)
|
||||
public FileArgument(@Nullable String resource, Type type, @Nullable String zipContainer, boolean isFolder, boolean isShared, Reason reason, ScriptHost host)
|
||||
{
|
||||
this.resource = resource;
|
||||
this.type = type;
|
||||
@@ -120,24 +121,24 @@ public class FileArgument
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "path: " + resource + " zip: " + zipContainer + " type: " + type.id + " folder: " + isFolder + " shared: " + isShared + " reason: " + reason.toString();
|
||||
return "path: " + resource + " zip: " + zipContainer + " type: " + type.id + " folder: " + isFolder + " shared: " + isShared + " reason: " + reason;
|
||||
}
|
||||
|
||||
public static FileArgument from(final Context context, final List<Value> lv, final boolean isFolder, final Reason reason)
|
||||
public static FileArgument from(Context context, List<Value> lv, boolean isFolder, Reason reason)
|
||||
{
|
||||
if (lv.size() < 2)
|
||||
{
|
||||
throw new InternalExpressionException("File functions require path and type as first two arguments");
|
||||
}
|
||||
final String origtype = lv.get(1).getString().toLowerCase(Locale.ROOT);
|
||||
final boolean shared = origtype.startsWith("shared_");
|
||||
final String typeString = shared ? origtype.substring(7) : origtype; //len(shared_)
|
||||
final Type type = Type.of.get(typeString);
|
||||
final Pair<String, String> resource = recognizeResource(lv.get(0).getString(), isFolder, type);
|
||||
String origtype = lv.get(1).getString().toLowerCase(Locale.ROOT);
|
||||
boolean shared = origtype.startsWith("shared_");
|
||||
String typeString = shared ? origtype.substring(7) : origtype; //len(shared_)
|
||||
Type type = Type.of.get(typeString);
|
||||
if (type == null)
|
||||
{
|
||||
throw new InternalExpressionException("Unsupported file type: " + origtype);
|
||||
}
|
||||
Pair<String, String> resource = recognizeResource(lv.get(0).getString(), isFolder, type);
|
||||
if (type == Type.FOLDER && !isFolder)
|
||||
{
|
||||
throw new InternalExpressionException("Folder types are no supported for this IO function");
|
||||
@@ -146,21 +147,21 @@ public class FileArgument
|
||||
|
||||
}
|
||||
|
||||
public static FileArgument resourceFromPath(final ScriptHost host, final String path, final Reason reason, final boolean shared)
|
||||
public static FileArgument resourceFromPath(ScriptHost host, String path, Reason reason, boolean shared)
|
||||
{
|
||||
final Pair<String, String> resource = recognizeResource(path, false, Type.ANY);
|
||||
Pair<String, String> resource = recognizeResource(path, false, Type.ANY);
|
||||
return new FileArgument(resource.getLeft(), Type.ANY, resource.getRight(), false, shared, reason, host);
|
||||
}
|
||||
|
||||
public static Pair<String, String> recognizeResource(final String origfile, final boolean isFolder, final Type type)
|
||||
public static Pair<String, String> recognizeResource(String origfile, boolean isFolder, Type type)
|
||||
{
|
||||
final String[] pathElements = origfile.split("[/\\\\]+");
|
||||
final List<String> path = new ArrayList<>();
|
||||
String[] pathElements = origfile.split("[/\\\\]+");
|
||||
List<String> path = new ArrayList<>();
|
||||
String zipPath = null;
|
||||
for (int i = 0; i < pathElements.length; i++)
|
||||
{
|
||||
String token = pathElements[i];
|
||||
final boolean isZip = token.endsWith(".zip") && (isFolder || (i < pathElements.length - 1));
|
||||
boolean isZip = token.endsWith(".zip") && (isFolder || (i < pathElements.length - 1));
|
||||
if (zipPath != null && isZip)
|
||||
{
|
||||
throw new InternalExpressionException(token + " indicates zip access in an already zipped location " + zipPath);
|
||||
@@ -196,12 +197,13 @@ public class FileArgument
|
||||
return Pair.of(String.join("/", path), zipPath);
|
||||
}
|
||||
|
||||
private Path resolve(final String suffix)
|
||||
private Path resolve(String suffix)
|
||||
{
|
||||
return host.resolveScriptFile(suffix);
|
||||
}
|
||||
|
||||
private Path toPath(final Module module)
|
||||
@Nullable
|
||||
private Path toPath(@Nullable Module module)
|
||||
{
|
||||
if (!isShared && module == null)
|
||||
{
|
||||
@@ -215,7 +217,7 @@ public class FileArgument
|
||||
{
|
||||
if (zfs == null)
|
||||
{
|
||||
final Map<String, String> env = new HashMap<>();
|
||||
Map<String, String> env = new HashMap<>();
|
||||
if (reason == Reason.CREATE)
|
||||
{
|
||||
env.put("create", "true");
|
||||
@@ -233,7 +235,7 @@ public class FileArgument
|
||||
}
|
||||
zfs = FileSystems.newFileSystem(URI.create("jar:" + zipPath.toUri()), env);
|
||||
}
|
||||
catch (final FileSystemNotFoundException | IOException e)
|
||||
catch (FileSystemNotFoundException | IOException e)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("Exception when opening zip file", e);
|
||||
throw new ThrowStatement("Unable to open zip file: " + zipContainer, Throwables.IO_EXCEPTION);
|
||||
@@ -243,7 +245,8 @@ public class FileArgument
|
||||
}
|
||||
}
|
||||
|
||||
private Path moduleRootPath(final Module module)
|
||||
@Nullable
|
||||
private Path moduleRootPath(@Nullable Module module)
|
||||
{
|
||||
return !isShared && module == null
|
||||
? null
|
||||
@@ -255,7 +258,7 @@ public class FileArgument
|
||||
return (isShared ? "shared/" : "") + (zipContainer != null ? zipContainer + "/" : "") + resource + type.extension;
|
||||
}
|
||||
|
||||
private String getDescriptor(final Module module, final String res)
|
||||
private String getDescriptor(@Nullable Module module, @Nullable String res)
|
||||
{
|
||||
if (isShared)
|
||||
{
|
||||
@@ -269,13 +272,13 @@ public class FileArgument
|
||||
}
|
||||
|
||||
|
||||
public boolean findPathAndApply(final Module module, final Consumer<Path> action)
|
||||
public boolean findPathAndApply(Module module, Consumer<Path> action)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
final Path dataFile = toPath(module);//, resourceName, supportedTypes.get(type), isShared);
|
||||
Path dataFile = toPath(module);//, resourceName, supportedTypes.get(type), isShared);
|
||||
if (dataFile == null)
|
||||
{
|
||||
return false;
|
||||
@@ -291,14 +294,15 @@ public class FileArgument
|
||||
return true;
|
||||
}
|
||||
|
||||
public Stream<Path> listFiles(final Module module)
|
||||
@Nullable
|
||||
public Stream<Path> listFiles(Module module)
|
||||
{
|
||||
final Path dir = toPath(module);
|
||||
Path dir = toPath(module);
|
||||
if (dir == null || !Files.exists(dir))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
final String ext = type.extension;
|
||||
String ext = type.extension;
|
||||
try
|
||||
{
|
||||
return Files.list(dir).filter(path -> (type == Type.FOLDER)
|
||||
@@ -306,16 +310,17 @@ public class FileArgument
|
||||
: (Files.isRegularFile(path) && path.toString().endsWith(ext))
|
||||
);
|
||||
}
|
||||
catch (final IOException ignored)
|
||||
catch (IOException ignored)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Stream<String> listFolder(final Module module)
|
||||
@Nullable
|
||||
public Stream<String> listFolder(Module module)
|
||||
{
|
||||
Stream<String> strings;
|
||||
try (final Stream<Path> result = listFiles(module))
|
||||
try (Stream<Path> result = listFiles(module))
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
@@ -323,12 +328,12 @@ public class FileArgument
|
||||
{
|
||||
return null;
|
||||
}
|
||||
final Path rootPath = moduleRootPath(module);
|
||||
Path rootPath = moduleRootPath(module);
|
||||
if (rootPath == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
final String zipComponent = (zipContainer != null) ? rootPath.relativize(zipPath).toString() : null;
|
||||
String zipComponent = (zipContainer != null) ? rootPath.relativize(zipPath).toString() : null;
|
||||
// need to evaluate the stream before exiting try-with-resources else there'll be no data to stream
|
||||
strings = (zipContainer == null)
|
||||
? result.map(p -> rootPath.relativize(p).toString().replaceAll("[\\\\/]+", "/")).toList().stream()
|
||||
@@ -345,7 +350,7 @@ public class FileArgument
|
||||
: strings.map(FilenameUtils::removeExtension);
|
||||
}
|
||||
|
||||
private void createPaths(final Path file)
|
||||
private void createPaths(Path file)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -357,29 +362,29 @@ public class FileArgument
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("IOException when creating paths", e);
|
||||
throw new ThrowStatement("Unable to create paths for " + file, Throwables.IO_EXCEPTION);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean appendToTextFile(final Module module, final List<String> message)
|
||||
public boolean appendToTextFile(Module module, List<String> message)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
final Path dataFile = toPath(module);
|
||||
Path dataFile = toPath(module);
|
||||
if (dataFile == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
createPaths(dataFile);
|
||||
final OutputStream out = Files.newOutputStream(dataFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
|
||||
try (final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)))
|
||||
OutputStream out = Files.newOutputStream(dataFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
|
||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)))
|
||||
{
|
||||
for (final String line : message)
|
||||
for (String line : message)
|
||||
{
|
||||
writer.append(line);
|
||||
if (type == Type.TEXT)
|
||||
@@ -390,7 +395,7 @@ public class FileArgument
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("IOException when appending to text file", e);
|
||||
throw new ThrowStatement("Error when writing to the file: " + e, Throwables.IO_EXCEPTION);
|
||||
@@ -402,13 +407,14 @@ public class FileArgument
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tag getNbtData(final Module module) // aka getData
|
||||
@Nullable
|
||||
public Tag getNbtData(Module module) // aka getData
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
final Path dataFile = toPath(module);
|
||||
Path dataFile = toPath(module);
|
||||
if (dataFile == null || !Files.exists(dataFile))
|
||||
{
|
||||
return null;
|
||||
@@ -424,29 +430,30 @@ public class FileArgument
|
||||
|
||||
//copied private method from net.minecraft.nbt.NbtIo.read()
|
||||
// to read non-compound tags - these won't be compressed
|
||||
public static Tag readTag(final Path path)
|
||||
@Nullable
|
||||
public static Tag readTag(Path path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return NbtIo.readCompressed(Files.newInputStream(path));
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
// Copy of NbtIo.read(File) because that's now client-side only
|
||||
if (!Files.exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try (final DataInputStream in = new DataInputStream(new BufferedInputStream(Files.newInputStream(path))))
|
||||
try (DataInputStream in = new DataInputStream(new BufferedInputStream(Files.newInputStream(path))))
|
||||
{
|
||||
return NbtIo.read(in);
|
||||
}
|
||||
catch (final IOException ioException)
|
||||
catch (IOException ioException)
|
||||
{
|
||||
// not compressed compound tag neither uncompressed compound tag - trying any type of a tag
|
||||
try (final DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(Files.newInputStream(path))))
|
||||
try (DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(Files.newInputStream(path))))
|
||||
{
|
||||
final byte b = dataInputStream.readByte();
|
||||
byte b = dataInputStream.readByte();
|
||||
if (b == 0)
|
||||
{
|
||||
return null;
|
||||
@@ -457,7 +464,7 @@ public class FileArgument
|
||||
return TagTypes.getType(b).load(dataInputStream, 0, NbtAccounter.UNLIMITED);
|
||||
}
|
||||
}
|
||||
catch (final IOException secondIO)
|
||||
catch (IOException secondIO)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("IOException when trying to read nbt file, something may have gone wrong with the fs", e);
|
||||
CarpetScriptServer.LOG.warn("", ioException);
|
||||
@@ -466,19 +473,19 @@ public class FileArgument
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (final ReportedException e)
|
||||
catch (ReportedException e)
|
||||
{
|
||||
throw new ThrowStatement("Error when reading NBT file " + path.toString(), Throwables.NBT_ERROR);
|
||||
throw new ThrowStatement("Error when reading NBT file " + path, Throwables.NBT_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean saveNbtData(final Module module, final Tag tag) // aka saveData
|
||||
public boolean saveNbtData(Module module, Tag tag) // aka saveData
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
final Path dataFile = toPath(module);
|
||||
Path dataFile = toPath(module);
|
||||
if (dataFile == null)
|
||||
{
|
||||
return false;
|
||||
@@ -494,9 +501,9 @@ public class FileArgument
|
||||
}
|
||||
|
||||
//copied private method from net.minecraft.nbt.NbtIo.write() and client method safe_write
|
||||
public static boolean writeTagDisk(final Tag tag, Path path, final boolean zipped)
|
||||
public static boolean writeTagDisk(Tag tag, Path path, boolean zipped)
|
||||
{
|
||||
final Path original = path;
|
||||
Path original = path;
|
||||
try
|
||||
{
|
||||
if (!zipped)
|
||||
@@ -511,7 +518,7 @@ public class FileArgument
|
||||
}
|
||||
else
|
||||
{
|
||||
try (final DataOutputStream dataOutputStream = new DataOutputStream(Files.newOutputStream(path)))
|
||||
try (DataOutputStream dataOutputStream = new DataOutputStream(Files.newOutputStream(path)))
|
||||
{
|
||||
dataOutputStream.writeByte(tag.getId());
|
||||
if (tag.getId() != 0)
|
||||
@@ -528,20 +535,20 @@ public class FileArgument
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("IO Exception when writing nbt file", e);
|
||||
throw new ThrowStatement("Unable to write tag to " + original, Throwables.IO_EXCEPTION);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean dropExistingFile(final Module module)
|
||||
public boolean dropExistingFile(Module module)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
final Path dataFile = toPath(module);
|
||||
Path dataFile = toPath(module);
|
||||
if (dataFile == null)
|
||||
{
|
||||
return false;
|
||||
@@ -549,7 +556,7 @@ public class FileArgument
|
||||
return Files.deleteIfExists(dataFile);
|
||||
}
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("IOException when removing file", e);
|
||||
throw new ThrowStatement("Error while removing file: " + getDisplayPath(), Throwables.IO_EXCEPTION);
|
||||
@@ -560,13 +567,14 @@ public class FileArgument
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> listFile(final Module module)
|
||||
@Nullable
|
||||
public List<String> listFile(Module module)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
final Path dataFile = toPath(module);
|
||||
Path dataFile = toPath(module);
|
||||
if (dataFile == null)
|
||||
{
|
||||
return null;
|
||||
@@ -584,14 +592,14 @@ public class FileArgument
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> listFileContent(final Path filePath)
|
||||
public static List<String> listFileContent(Path filePath)
|
||||
{
|
||||
try (final BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8))
|
||||
try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8))
|
||||
{
|
||||
final List<String> result = new ArrayList<>();
|
||||
List<String> result = new ArrayList<>();
|
||||
for (; ; )
|
||||
{
|
||||
final String line = reader.readLine();
|
||||
String line = reader.readLine();
|
||||
if (line == null)
|
||||
{
|
||||
break;
|
||||
@@ -600,20 +608,21 @@ public class FileArgument
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("IOException when reading text file", e);
|
||||
throw new ThrowStatement("Failed to read text file " + filePath, Throwables.IO_EXCEPTION);
|
||||
}
|
||||
}
|
||||
|
||||
public JsonElement readJsonFile(final Module module)
|
||||
@Nullable
|
||||
public JsonElement readJsonFile(Module module)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (writeIOSync)
|
||||
{
|
||||
final Path dataFile = toPath(module);
|
||||
Path dataFile = toPath(module);
|
||||
if (dataFile == null || !Files.exists(dataFile))
|
||||
{
|
||||
return null;
|
||||
@@ -627,13 +636,13 @@ public class FileArgument
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonElement readJsonContent(final Path filePath)
|
||||
public static JsonElement readJsonContent(Path filePath)
|
||||
{
|
||||
try (final BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8))
|
||||
try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8))
|
||||
{
|
||||
return JsonParser.parseReader(reader);
|
||||
}
|
||||
catch (final JsonParseException e)
|
||||
catch (JsonParseException e)
|
||||
{
|
||||
Throwable exc = e;
|
||||
if (e.getCause() != null)
|
||||
@@ -645,7 +654,7 @@ public class FileArgument
|
||||
StringValue.of("path"), StringValue.of(filePath.toString())
|
||||
)), Throwables.JSON_ERROR);
|
||||
}
|
||||
catch (final IOException e)
|
||||
catch (IOException e)
|
||||
{
|
||||
CarpetScriptServer.LOG.warn("IOException when reading JSON file", e);
|
||||
throw new ThrowStatement("Failed to read json file content " + filePath, Throwables.IO_EXCEPTION);
|
||||
|
||||
@@ -10,16 +10,17 @@ import carpet.script.value.ListValue;
|
||||
import carpet.script.value.Value;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class FunctionArgument extends Argument
|
||||
{
|
||||
public FunctionValue function;
|
||||
public List<Value> args;
|
||||
public final FunctionValue function;
|
||||
public final List<Value> args;
|
||||
|
||||
private FunctionArgument(final FunctionValue function, final int offset, final List<Value> args)
|
||||
private FunctionArgument(@Nullable FunctionValue function, int offset, List<Value> args)
|
||||
{
|
||||
super(offset);
|
||||
this.function = function;
|
||||
@@ -38,12 +39,12 @@ public class FunctionArgument extends Argument
|
||||
* @return argument data
|
||||
*/
|
||||
public static FunctionArgument findIn(
|
||||
final Context c,
|
||||
final Module module,
|
||||
final List<Value> params,
|
||||
final int offset,
|
||||
final boolean allowNone,
|
||||
final boolean checkArgs)
|
||||
Context c,
|
||||
Module module,
|
||||
List<Value> params,
|
||||
int offset,
|
||||
boolean allowNone,
|
||||
boolean checkArgs)
|
||||
{
|
||||
Value functionValue = params.get(offset);
|
||||
if (functionValue.isNull())
|
||||
@@ -56,14 +57,14 @@ public class FunctionArgument extends Argument
|
||||
}
|
||||
if (!(functionValue instanceof FunctionValue))
|
||||
{
|
||||
final String name = functionValue.getString();
|
||||
String name = functionValue.getString();
|
||||
functionValue = c.host.getAssertFunction(module, name);
|
||||
}
|
||||
final FunctionValue fun = (FunctionValue) functionValue;
|
||||
final int argsize = fun.getArguments().size();
|
||||
FunctionValue fun = (FunctionValue) functionValue;
|
||||
int argsize = fun.getArguments().size();
|
||||
if (checkArgs)
|
||||
{
|
||||
final int extraargs = params.size() - argsize - offset - 1;
|
||||
int extraargs = params.size() - argsize - offset - 1;
|
||||
if (extraargs < 0)
|
||||
{
|
||||
throw new InternalExpressionException("Function " + fun.getPrettyString() + " requires at least " + fun.getArguments().size() + " arguments");
|
||||
@@ -73,7 +74,7 @@ public class FunctionArgument extends Argument
|
||||
throw new InternalExpressionException("Function " + fun.getPrettyString() + " requires " + fun.getArguments().size() + " arguments");
|
||||
}
|
||||
}
|
||||
final List<Value> lvargs = new ArrayList<>();
|
||||
List<Value> lvargs = new ArrayList<>();
|
||||
for (int i = offset + 1, mx = params.size(); i < mx; i++)
|
||||
{
|
||||
lvargs.add(params.get(i));
|
||||
@@ -81,27 +82,27 @@ public class FunctionArgument extends Argument
|
||||
return new FunctionArgument(fun, offset + 1 + argsize, lvargs);
|
||||
}
|
||||
|
||||
public static FunctionArgument fromCommandSpec(final ScriptHost host, Value funSpec) throws CommandSyntaxException
|
||||
public static FunctionArgument fromCommandSpec(ScriptHost host, Value funSpec) throws CommandSyntaxException
|
||||
{
|
||||
final FunctionValue function;
|
||||
FunctionValue function;
|
||||
List<Value> args = Collections.emptyList();
|
||||
if (!(funSpec instanceof ListValue))
|
||||
{
|
||||
funSpec = ListValue.of(funSpec);
|
||||
}
|
||||
final List<Value> params = ((ListValue) funSpec).getItems();
|
||||
List<Value> params = ((ListValue) funSpec).getItems();
|
||||
if (params.isEmpty())
|
||||
{
|
||||
throw CommandArgument.error("Function has empty spec");
|
||||
}
|
||||
final Value first = params.get(0);
|
||||
Value first = params.get(0);
|
||||
if (first instanceof FunctionValue)
|
||||
{
|
||||
function = (FunctionValue) first;
|
||||
}
|
||||
else
|
||||
{
|
||||
final String name = first.getString();
|
||||
String name = first.getString();
|
||||
function = host.getFunction(name);
|
||||
if (function == null)
|
||||
{
|
||||
|
||||
@@ -14,15 +14,18 @@ import java.util.NoSuchElementException;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class Vector3Argument extends Argument
|
||||
{
|
||||
public Vec3 vec;
|
||||
public final double yaw;
|
||||
public final double pitch;
|
||||
public boolean fromBlock = false;
|
||||
@Nullable
|
||||
public Entity entity = null;
|
||||
|
||||
private Vector3Argument(final Vec3 v, final int o)
|
||||
private Vector3Argument(Vec3 v, int o)
|
||||
{
|
||||
super(o);
|
||||
this.vec = v;
|
||||
@@ -30,7 +33,7 @@ public class Vector3Argument extends Argument
|
||||
this.pitch = 0.0D;
|
||||
}
|
||||
|
||||
private Vector3Argument(final Vec3 v, final int o, final double y, final double p)
|
||||
private Vector3Argument(Vec3 v, int o, double y, double p)
|
||||
{
|
||||
super(o);
|
||||
this.vec = v;
|
||||
@@ -44,40 +47,40 @@ public class Vector3Argument extends Argument
|
||||
return this;
|
||||
}
|
||||
|
||||
private Vector3Argument withEntity(final Entity e)
|
||||
private Vector3Argument withEntity(Entity e)
|
||||
{
|
||||
entity = e;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Vector3Argument findIn(final List<Value> params, final int offset)
|
||||
public static Vector3Argument findIn(List<Value> params, int offset)
|
||||
{
|
||||
return findIn(params, offset, false, false);
|
||||
}
|
||||
|
||||
public static Vector3Argument findIn(final List<Value> params, final int offset, final boolean optionalDirection, final boolean optionalEntity)
|
||||
public static Vector3Argument findIn(List<Value> params, int offset, boolean optionalDirection, boolean optionalEntity)
|
||||
{
|
||||
return findIn(params.listIterator(offset), offset, optionalDirection, optionalEntity);
|
||||
}
|
||||
|
||||
public static Vector3Argument findIn(final Iterator<Value> params, final int offset, final boolean optionalDirection, final boolean optionalEntity)
|
||||
public static Vector3Argument findIn(Iterator<Value> params, int offset, boolean optionalDirection, boolean optionalEntity)
|
||||
{
|
||||
try
|
||||
{
|
||||
final Value v1 = params.next();
|
||||
Value v1 = params.next();
|
||||
if (v1 instanceof BlockValue)
|
||||
{
|
||||
return (new Vector3Argument(Vec3.atCenterOf(((BlockValue) v1).getPos()), 1 + offset)).fromBlock();
|
||||
}
|
||||
if (optionalEntity && v1 instanceof EntityValue)
|
||||
{
|
||||
final Entity e = ((EntityValue) v1).getEntity();
|
||||
Entity e = ((EntityValue) v1).getEntity();
|
||||
return new Vector3Argument(e.position(), 1 + offset).withEntity(e);
|
||||
}
|
||||
if (v1 instanceof ListValue)
|
||||
{
|
||||
final List<Value> args = ((ListValue) v1).getItems();
|
||||
final Vec3 pos = new Vec3(
|
||||
List<Value> args = ((ListValue) v1).getItems();
|
||||
Vec3 pos = new Vec3(
|
||||
NumericValue.asNumber(args.get(0)).getDouble(),
|
||||
NumericValue.asNumber(args.get(1)).getDouble(),
|
||||
NumericValue.asNumber(args.get(2)).getDouble());
|
||||
@@ -90,7 +93,7 @@ public class Vector3Argument extends Argument
|
||||
}
|
||||
return new Vector3Argument(pos, offset + 1, yaw, pitch);
|
||||
}
|
||||
final Vec3 pos = new Vec3(
|
||||
Vec3 pos = new Vec3(
|
||||
NumericValue.asNumber(v1).getDouble(),
|
||||
NumericValue.asNumber(params.next()).getDouble(),
|
||||
NumericValue.asNumber(params.next()).getDouble());
|
||||
@@ -106,7 +109,7 @@ public class Vector3Argument extends Argument
|
||||
|
||||
return new Vector3Argument(pos, offset + eatenLength, yaw, pitch);
|
||||
}
|
||||
catch (final IndexOutOfBoundsException | NoSuchElementException e)
|
||||
catch (IndexOutOfBoundsException | NoSuchElementException e)
|
||||
{
|
||||
throw new InternalExpressionException("Position argument should be defined either by three coordinates (a triple or by three arguments), or a positioned block value");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user