camera update and task clearing
This commit is contained in:
1 parent
62529a352c
commit
a4e6e7cb10
14 files changed
+545
-336
No files matched your search
@@ -5,6 +5,7 @@ import carpet.script.value.BlockValue;
|
||||
import carpet.script.value.EntityValue;
|
||||
import carpet.script.value.FunctionValue;
|
||||
import carpet.script.value.ListValue;
|
||||
import carpet.script.value.NBTSerializableValue;
|
||||
import carpet.script.value.NumericValue;
|
||||
import carpet.script.value.StringValue;
|
||||
import carpet.script.value.Value;
|
||||
@@ -13,11 +14,15 @@ import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.stat.Stat;
|
||||
import net.minecraft.stat.Stats;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.hit.BlockHitResult;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.dimension.DimensionType;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
@@ -25,10 +30,12 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -403,7 +410,32 @@ public class CarpetEventServer
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
},
|
||||
STATISTICS("statistic", new CallbackList(4))
|
||||
{
|
||||
private <T> Identifier getStatId(Stat<T> stat)
|
||||
{
|
||||
return stat.getType().getRegistry().getId(stat.getValue());
|
||||
}
|
||||
private Set<Identifier> skippedStats = new HashSet<Identifier>(){{
|
||||
add(Stats.TIME_SINCE_DEATH);
|
||||
add(Stats.TIME_SINCE_REST);
|
||||
add(Stats.PLAY_ONE_MINUTE);
|
||||
}};
|
||||
@Override
|
||||
public void onPlayerStatistic(ServerPlayerEntity player, Stat<?> stat, int amount)
|
||||
{
|
||||
Identifier id = getStatId(stat);
|
||||
if (skippedStats.contains(id)) return;
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.STAT_TYPE.getId(stat.getType())))),
|
||||
((c, t) -> new StringValue(NBTSerializableValue.nameFromRegistryId(id))),
|
||||
((c, t) -> new NumericValue(amount))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
// on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls
|
||||
|
||||
@@ -427,6 +459,7 @@ public class CarpetEventServer
|
||||
//stubs for calls just to ease calls in vanilla code so they don't need to deal with scarpet value types
|
||||
public void onTick() { }
|
||||
public void onPlayerEvent(ServerPlayerEntity player) { }
|
||||
public void onPlayerStatistic(ServerPlayerEntity player, Stat<?> stat, int amount) { }
|
||||
public void onMountControls(ServerPlayerEntity player, float strafeSpeed, float forwardSpeed, boolean jumping, boolean sneaking) { }
|
||||
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) { }
|
||||
public void onBlockAction(ServerPlayerEntity player, BlockPos blockpos, Direction facing) { }
|
||||
|
||||
@@ -3157,7 +3157,7 @@ public class CarpetExpression
|
||||
String file = null;
|
||||
if (lv.size()>1)
|
||||
{
|
||||
String origfile = lv.get(0).evalValue(c).getString();
|
||||
String origfile = lv.get(1).evalValue(c).getString();
|
||||
file = origfile.toLowerCase(Locale.ROOT).replaceAll("[^A-Za-z0-9]", "");
|
||||
if (file.isEmpty())
|
||||
{
|
||||
|
||||
@@ -20,6 +20,7 @@ import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -217,6 +218,35 @@ public class CarpetScriptHost extends ScriptHost
|
||||
super.onClose();
|
||||
if (this.saveTimeout > 0)
|
||||
dumpState();
|
||||
|
||||
FunctionValue closing = globalFunctions.get("__on_close");
|
||||
if (closing != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
callUDF(BlockPos.ORIGIN, CarpetServer.minecraft_server.getCommandSource(), closing, Collections.emptyList());
|
||||
}
|
||||
catch (InvalidCallbackException ignored)
|
||||
{
|
||||
}
|
||||
}
|
||||
userHosts.forEach((key, value) ->
|
||||
{
|
||||
FunctionValue userClosing = value.globalFunctions.get("__on_close");
|
||||
if (userClosing != null)
|
||||
{
|
||||
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(key);
|
||||
ServerCommandSource source = (player != null)?player.getCommandSource():CarpetServer.minecraft_server.getCommandSource();
|
||||
try
|
||||
{
|
||||
((CarpetScriptHost) value).callUDF(BlockPos.ORIGIN, source, userClosing, Collections.emptyList());
|
||||
}
|
||||
catch (InvalidCallbackException ignored)
|
||||
{
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
String markerName = ExpressionInspector.MARKER_STRING+"_"+((getName()==null)?"":getName());
|
||||
for (ServerWorld world : CarpetServer.minecraft_server.getWorlds())
|
||||
{
|
||||
@@ -225,6 +255,7 @@ public class CarpetScriptHost extends ScriptHost
|
||||
e.remove();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void dumpState()
|
||||
@@ -239,9 +270,9 @@ public class CarpetScriptHost extends ScriptHost
|
||||
|
||||
public Tag getGlobalState(String file)
|
||||
{
|
||||
if (getName() == null || myCode.isInternal()) return null;
|
||||
if (getName() == null ) return null;
|
||||
if (file != null)
|
||||
myCode.getData(file);
|
||||
return myCode.getData(file);
|
||||
if (parent == null)
|
||||
return globalState;
|
||||
return ((CarpetScriptHost)parent).globalState;
|
||||
@@ -249,7 +280,7 @@ public class CarpetScriptHost extends ScriptHost
|
||||
|
||||
public void setGlobalState(Tag tag, String file)
|
||||
{
|
||||
if (getName() == null || myCode.isInternal()) return;
|
||||
if (getName() == null ) return;
|
||||
|
||||
if (file!= null)
|
||||
{
|
||||
|
||||
@@ -43,7 +43,7 @@ public class CarpetScriptServer
|
||||
private Set<String> holyMoly;
|
||||
public CarpetEventServer events;
|
||||
|
||||
public static List<ModuleInterface> bundledModuleData = new ArrayList<ModuleInterface>(){{
|
||||
private static List<ModuleInterface> bundledModuleData = new ArrayList<ModuleInterface>(){{
|
||||
add(new BundledModule("camera"));
|
||||
add(new BundledModule("event_test"));
|
||||
}};
|
||||
@@ -72,7 +72,7 @@ public class CarpetScriptServer
|
||||
|
||||
}
|
||||
|
||||
ModuleInterface getModule(String name)
|
||||
private ModuleInterface getModule(String name)
|
||||
{
|
||||
File folder = CarpetServer.minecraft_server.getLevelStorage().resolveFile(
|
||||
CarpetServer.minecraft_server.getLevelName(), "scripts");
|
||||
@@ -218,25 +218,6 @@ public class CarpetScriptServer
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private void addEvents(ServerCommandSource source, String hostName)
|
||||
{
|
||||
if (1+2 == 3) throw new RuntimeException("This should run when code is evaluated");
|
||||
ScriptHost host = modules.get(hostName);
|
||||
if (host == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (String fun : host.globalFunctions.keySet())
|
||||
{
|
||||
if (!fun.startsWith("__on_"))
|
||||
continue;
|
||||
String event = fun.replaceFirst("__on_","");
|
||||
if (!CarpetEventServer.Event.byName.containsKey(event))
|
||||
continue;
|
||||
events.addEvent(event, hostName, fun);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void addCommand(ServerCommandSource source, String hostName)
|
||||
{
|
||||
@@ -261,7 +242,9 @@ public class CarpetScriptServer
|
||||
requires((player) -> modules.containsKey(hostName)).
|
||||
executes( (c) ->
|
||||
{
|
||||
Messenger.m(c.getSource(), "gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call(c.getSource(),"__command", null, ""));
|
||||
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
|
||||
call(c.getSource(),"__command", null, "");
|
||||
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
|
||||
return 1;
|
||||
});
|
||||
|
||||
@@ -271,28 +254,16 @@ public class CarpetScriptServer
|
||||
then(literal(function).
|
||||
requires((player) -> modules.containsKey(hostName) && modules.get(hostName).getPublicFunctions().contains(function)).
|
||||
executes( (c) -> {
|
||||
Messenger.m(
|
||||
c.getSource(),
|
||||
"gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call(
|
||||
c.getSource(),
|
||||
function,
|
||||
null,
|
||||
""
|
||||
)
|
||||
);
|
||||
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
|
||||
call(c.getSource(), function,null,"");
|
||||
if (!response.isEmpty()) Messenger.m(c.getSource(),"gi "+response);
|
||||
return 1;
|
||||
}).
|
||||
then(argument("args...", StringArgumentType.greedyString()).
|
||||
executes( (c) -> {
|
||||
Messenger.m(
|
||||
c.getSource(),
|
||||
"gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call(
|
||||
c.getSource(),
|
||||
function,
|
||||
null,
|
||||
StringArgumentType.getString(c, "args...")
|
||||
)
|
||||
);
|
||||
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
|
||||
call(c.getSource(), function,null, StringArgumentType.getString(c, "args..."));
|
||||
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
|
||||
return 1;
|
||||
})));
|
||||
}
|
||||
|
||||
@@ -3002,22 +3002,9 @@ public class Expression implements Cloneable
|
||||
});
|
||||
|
||||
addLazyFunctionWithDelegation("task", -1, (c, t, expr, tok, lv) -> {
|
||||
Value queue = Value.NULL;
|
||||
Value funcDesc;
|
||||
if (lv.size()==1)
|
||||
{
|
||||
funcDesc = lv.get(0).evalValue(c);
|
||||
}
|
||||
else if (lv.size() == 2)
|
||||
{
|
||||
queue = lv.get(0).evalValue(c);
|
||||
funcDesc = lv.get(1).evalValue(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InternalExpressionException("'task' takes one or two parameters");
|
||||
}
|
||||
FunctionValue fun;
|
||||
if (lv.size() == 0)
|
||||
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
|
||||
Value funcDesc = lv.get(0).evalValue(c);
|
||||
if (!(funcDesc instanceof FunctionValue))
|
||||
{
|
||||
String name = funcDesc.getString();
|
||||
@@ -3025,10 +3012,20 @@ public class Expression implements Cloneable
|
||||
if (funcDesc == null)
|
||||
throw new InternalExpressionException("Function "+name+" is not defined yet");
|
||||
}
|
||||
fun = (FunctionValue)funcDesc;
|
||||
if (fun.getArguments().size() > 0)
|
||||
throw new InternalExpressionException("tasks can only accept functions that don't take any arguments");
|
||||
ThreadValue thread = new ThreadValue(queue, fun, expr, tok, c);
|
||||
FunctionValue fun = (FunctionValue)funcDesc;
|
||||
int extraargs = lv.size() - fun.getArguments().size();
|
||||
if (extraargs != 1 && extraargs != 2)
|
||||
{
|
||||
throw new InternalExpressionException("Function takes "+fun.getArguments().size()+" arguments.");
|
||||
}
|
||||
List<LazyValue> lvargs = new ArrayList<>();
|
||||
for (int i=0; i< fun.getArguments().size(); i++)
|
||||
{
|
||||
lvargs.add(lv.get(i+1));
|
||||
}
|
||||
Value queue = Value.NULL;
|
||||
if (extraargs == 2) queue = lv.get(lv.size()-1).evalValue(c);
|
||||
ThreadValue thread = new ThreadValue(queue, fun, expr, tok, c, lvargs);
|
||||
Thread.yield();
|
||||
return (cc, tt) -> thread;
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class ScriptHost
|
||||
{
|
||||
private final Map<String, ScriptHost> userHosts = new Object2ObjectOpenHashMap<>();
|
||||
protected final Map<String, ScriptHost> userHosts = new Object2ObjectOpenHashMap<>();
|
||||
public Map<String, FunctionValue> globalFunctions = new Object2ObjectOpenHashMap<>();
|
||||
public Map<String, LazyValue> globalVariables = new Object2ObjectOpenHashMap<>();
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class FileModule implements ModuleInterface
|
||||
}
|
||||
|
||||
//copied private method from net.minecraft.nbt.NbtIo.read()
|
||||
private static Tag read(File file)
|
||||
public static Tag read(File file)
|
||||
{
|
||||
try (DataInputStream dataInput_1 = new DataInputStream(new FileInputStream(file)))
|
||||
{
|
||||
@@ -80,7 +80,7 @@ public class FileModule implements ModuleInterface
|
||||
}
|
||||
|
||||
//copied private method from net.minecraft.nbt.NbtIo.write() and client method safe_write
|
||||
private static void write(Tag tag_1, File file)
|
||||
public static void write(Tag tag_1, File file)
|
||||
{
|
||||
File file_2 = new File(file.getAbsolutePath() + "_tmp");
|
||||
if (file_2.exists()) file_2.delete();
|
||||
@@ -101,23 +101,4 @@ public class FileModule implements ModuleInterface
|
||||
if (file.exists()) file.delete();
|
||||
if (!file.exists()) file_2.renameTo(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag getData(String file)
|
||||
{
|
||||
File dataFile =CarpetServer.minecraft_server.getLevelStorage().resolveFile(
|
||||
CarpetServer.minecraft_server.getLevelName(), "scripts/"+name+".data"+(file==null?"":"."+file)+".nbt");
|
||||
if (!(dataFile.isFile()))
|
||||
return null;
|
||||
return read(dataFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveData(String file, Tag globalState)
|
||||
{
|
||||
File dataFile =CarpetServer.minecraft_server.getLevelStorage().resolveFile(
|
||||
CarpetServer.minecraft_server.getLevelName(), "scripts/"+name+".data"+(file==null?"":"."+file)+".nbt");
|
||||
write(globalState, dataFile);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,44 @@
|
||||
package carpet.script.bundled;
|
||||
|
||||
import carpet.CarpetServer;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static carpet.script.bundled.FileModule.read;
|
||||
import static carpet.script.bundled.FileModule.write;
|
||||
|
||||
public interface ModuleInterface
|
||||
{
|
||||
String getName();
|
||||
String getCode();
|
||||
default boolean isInternal() {return true; }
|
||||
default Tag getData(String file) {return null; }
|
||||
default void saveData(String file, Tag globalState) {}
|
||||
|
||||
default Tag getData(String file)
|
||||
{
|
||||
File dataFile = CarpetServer.minecraft_server.getLevelStorage().resolveFile(
|
||||
CarpetServer.minecraft_server.getLevelName(), "scripts/"+getName()+".data"+(file==null?"":"."+file)+".nbt");
|
||||
if (!Files.exists(dataFile.toPath()) || !(dataFile.isFile())) return null;
|
||||
return read(dataFile);
|
||||
}
|
||||
|
||||
default void saveData(String file, Tag globalState)
|
||||
{
|
||||
File dataFile =CarpetServer.minecraft_server.getLevelStorage().resolveFile(
|
||||
CarpetServer.minecraft_server.getLevelName(), "scripts/"+getName()+".data"+(file==null?"":"."+file)+".nbt");
|
||||
if (!Files.exists(dataFile.toPath().getParent()))
|
||||
{
|
||||
try
|
||||
{
|
||||
Files.createDirectory(dataFile.toPath().getParent());
|
||||
}
|
||||
catch (IOException ignored)
|
||||
{
|
||||
}
|
||||
}
|
||||
write(globalState, dataFile);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,12 @@ package carpet.script.value;
|
||||
|
||||
import carpet.script.Context;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.LazyValue;
|
||||
import carpet.script.Tokenizer;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -17,18 +18,14 @@ public class ThreadValue extends Value
|
||||
{
|
||||
private CompletableFuture<Value> taskFuture;
|
||||
private long id;
|
||||
private final Object lock;
|
||||
private static long sequence = 0L;
|
||||
private static final Map<Value,ThreadPoolExecutor> executorServices = new HashMap<>();
|
||||
|
||||
public ThreadValue(Value pool, FunctionValue function, Expression expr, Tokenizer.Token token, Context ctx)
|
||||
public ThreadValue(Value pool, FunctionValue function, Expression expr, Tokenizer.Token token, Context ctx, List<LazyValue> args)
|
||||
{
|
||||
id = sequence++;
|
||||
lock = new Object();
|
||||
taskFuture = CompletableFuture.supplyAsync(
|
||||
() -> { synchronized (lock) {
|
||||
return function.lazyEval(ctx, Context.NONE, expr, token, Collections.emptyList()).evalValue(ctx);
|
||||
} },
|
||||
() -> function.lazyEval(ctx, Context.NONE, expr, token, args).evalValue(ctx),
|
||||
executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool())
|
||||
);
|
||||
Thread.yield();
|
||||
@@ -63,17 +60,6 @@ public class ThreadValue extends Value
|
||||
return taskFuture.isDone();
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
if (!taskFuture.isDone())
|
||||
{
|
||||
taskFuture.complete(Value.NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user