better entity events, untested

This commit is contained in:
gnembon committed 2019-07-25 14:10:01 -03:00
1 parent 32dc6b17e8
commit 6505afdfdb
6 files changed
+100 -25

No files matched your search

@@ -6,6 +6,7 @@ import carpet.script.value.EntityValue;
import carpet.script.value.ListValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import carpet.settings.CarpetSettings;
import net.minecraft.block.BlockState;
import net.minecraft.nbt.CompoundTag;
@@ -34,6 +35,7 @@ import java.util.Map;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class CarpetEventServer
{
@@ -78,7 +80,15 @@ public class CarpetEventServer
public void execute(List<LazyValue> args)
{
CarpetServer.scriptServer.runas(context_origin, context_source, host, udf, args);
if (this.args == null)
CarpetServer.scriptServer.runas(context_origin, context_source, host, udf, args);
else
{
List<LazyValue> combinedArgs = new ArrayList<>();
combinedArgs.addAll(args);
combinedArgs.addAll(this.args);
CarpetServer.scriptServer.runas(context_origin, context_source, host, udf, combinedArgs);
}
}
}
@@ -221,6 +231,24 @@ public class CarpetEventServer
return new Callback(null, funName);
}
private ScheduledCall makeEventCall(CarpetContext cc, String function, List<Value> extraArgs, int argCount)
{
UserDefinedFunction udf = cc.host.globalFunctions.get(function);
if (udf == null || udf.getArguments().size() != argCount)
{
// call won't match arguments
return null;
}
List<LazyValue> lazyArgs = null;
if (extraArgs != null)
{
lazyArgs = new ArrayList<>();
for (Value v : extraArgs)
lazyArgs.add( (c, t) -> v);
}
return new ScheduledCall(cc, function, lazyArgs, 0);
}
public void onTick()
{
eventHandlers.get("tick").call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
@@ -354,19 +382,23 @@ public class CarpetEventServer
eventHandlers.get("player_stops_sprinting").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public ScheduledCall makeDeathCall(CarpetContext cc, String function)
public ScheduledCall makeDeathCall(CarpetContext cc, String function, List<Value> extraArgs)
{
UserDefinedFunction udf = cc.host.globalFunctions.get(function);
if (udf == null || udf.getArguments().size() != 2)
{
// call won't match arguments
return null;
}
return new ScheduledCall(cc, function, null, 0);
return makeEventCall(cc, function, extraArgs, 2);
}
public ScheduledCall makeRemovedCall(CarpetContext cc, String function, List<Value> extraArgs)
{
return makeEventCall(cc, function, extraArgs, 1);
}
public void onEntityDeath(ScheduledCall call, Entity e, String reason)
{
call.execute( Arrays.asList( (c, t)-> new EntityValue(e), (c, t)-> new StringValue(reason)));
}
public void onEntityRemoved(ScheduledCall removeCall, Entity entity)
{
removeCall.execute(Collections.singletonList((c, t) -> new EntityValue(entity)));
}
}