entity load handler and rework of handling of events.
This commit is contained in:
1 parent
51f34643db
commit
7ccd46433a
15 files changed
+413
-299
No files matched your search
@@ -34,7 +34,6 @@ import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.registry.RegistryKey;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraft.world.dimension.DimensionType;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -62,11 +61,36 @@ public class CarpetEventServer
|
||||
{
|
||||
public final String host;
|
||||
public final FunctionValue function;
|
||||
public final List<Value> parametrizedArgs;
|
||||
|
||||
public Callback(String host, FunctionValue function)
|
||||
public Callback(String host, FunctionValue function, List<Value> parametrizedArgs)
|
||||
{
|
||||
this.host = host;
|
||||
this.function = function;
|
||||
this.parametrizedArgs = parametrizedArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in entity events
|
||||
* @param asSource - entity command source
|
||||
* @param runtimeArgs = options
|
||||
*/
|
||||
public boolean execute(ServerCommandSource asSource, List<Value> runtimeArgs)
|
||||
{
|
||||
//!CarpetServer.scriptServer.runas(source, call.host, call.function, argv)
|
||||
//CarpetServer.scriptServer.runas(source, call.host, call.function, argv)
|
||||
if (this.parametrizedArgs == null || this.parametrizedArgs.isEmpty())
|
||||
return CarpetServer.scriptServer.runas(
|
||||
asSource.withLevel(CarpetSettings.runPermissionLevel),
|
||||
host, function, runtimeArgs);
|
||||
else
|
||||
{
|
||||
List<Value> combinedArgs = new ArrayList<>(runtimeArgs);
|
||||
combinedArgs.addAll(this.parametrizedArgs);
|
||||
return CarpetServer.scriptServer.runas(
|
||||
asSource.withLevel(CarpetSettings.runPermissionLevel),
|
||||
host, function, combinedArgs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,14 +102,13 @@ public class CarpetEventServer
|
||||
|
||||
public static class ScheduledCall extends Callback
|
||||
{
|
||||
public final List<Value> args;
|
||||
|
||||
private final CarpetContext ctx;
|
||||
public long dueTime;
|
||||
|
||||
public ScheduledCall(CarpetContext context, FunctionValue function, List<Value> args, long dueTime)
|
||||
{
|
||||
super(context.host.getName(), function);
|
||||
this.args = args;
|
||||
super(context.host.getName(), function, args);
|
||||
this.ctx = context;
|
||||
this.dueTime = dueTime;
|
||||
}
|
||||
@@ -95,34 +118,10 @@ public class CarpetEventServer
|
||||
*/
|
||||
public void execute()
|
||||
{
|
||||
CarpetServer.scriptServer.runas(ctx.origin, ctx.s, host, function, lazify(args));
|
||||
CarpetServer.scriptServer.runas(ctx.origin, ctx.s, host, function, parametrizedArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in entity events
|
||||
* @param asSource - entity command source
|
||||
* @param args = options
|
||||
*/
|
||||
public void execute(ServerCommandSource asSource, List<Value> args)
|
||||
{
|
||||
if (this.args == null || this.args.isEmpty())
|
||||
CarpetServer.scriptServer.runas(
|
||||
ctx.origin, asSource.withLevel(CarpetSettings.runPermissionLevel),
|
||||
host, function, lazify(args));
|
||||
else
|
||||
{
|
||||
List<Value> combinedArgs = new ArrayList<>();
|
||||
combinedArgs.addAll(args);
|
||||
combinedArgs.addAll(this.args);
|
||||
CarpetServer.scriptServer.runas(
|
||||
ctx.origin, asSource.withLevel(CarpetSettings.runPermissionLevel),
|
||||
host, function, lazify(combinedArgs));
|
||||
}
|
||||
}
|
||||
private List<LazyValue> lazify(List<Value> args)
|
||||
{
|
||||
return args.stream().map(v -> (LazyValue) (c, t) -> v ).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class CallbackList
|
||||
@@ -137,18 +136,17 @@ public class CarpetEventServer
|
||||
this.reqArgs = reqArgs;
|
||||
}
|
||||
|
||||
public void call(Supplier<List<LazyValue>> argumentSupplier, Supplier<ServerCommandSource> cmdSourceSupplier)
|
||||
public void call(Supplier<List<Value>> argumentSupplier, Supplier<ServerCommandSource> cmdSourceSupplier)
|
||||
{
|
||||
if (callList.size() > 0)
|
||||
{
|
||||
List<LazyValue> argv = argumentSupplier.get(); // empty for onTickDone
|
||||
List<Value> argv = argumentSupplier.get(); // empty for onTickDone
|
||||
ServerCommandSource source = cmdSourceSupplier.get();
|
||||
assert argv.size() == reqArgs;
|
||||
List<Callback> fails = new ArrayList<>();
|
||||
for (Callback call: callList)
|
||||
{
|
||||
if (!CarpetServer.scriptServer.runas(source, call.host, call.function, argv))
|
||||
fails.add(call);
|
||||
if (!call.execute(source, argv)) fails.add(call);
|
||||
}
|
||||
for (Callback call : fails) callList.remove(call);
|
||||
}
|
||||
@@ -178,19 +176,19 @@ public class CarpetEventServer
|
||||
//remove duplicates
|
||||
|
||||
removeEventCall(hostName, udf.getString());
|
||||
callList.add(new Callback(hostName, udf));
|
||||
callList.add(new Callback(hostName, udf, null));
|
||||
return true;
|
||||
}
|
||||
public boolean addEventCallDirect(ScriptHost host, FunctionValue function)
|
||||
public boolean addEventCallInternal(ScriptHost host, FunctionValue function, List<Value> args)
|
||||
{
|
||||
if (function == null || function.getArguments().size() != reqArgs)
|
||||
if (function == null || (function.getArguments().size() - (args==null?0:args.size())) != reqArgs)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//all clear
|
||||
//remove duplicates
|
||||
removeEventCall(host.getName(), function.getString());
|
||||
callList.add(new Callback(host.getName(), function));
|
||||
callList.add(new Callback(host.getName(), function, args));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -259,8 +257,8 @@ public class CarpetEventServer
|
||||
{
|
||||
ChunkPos pos = chunk.getPos();
|
||||
return Arrays.asList(
|
||||
((c, t) -> new NumericValue(pos.x << 4)),
|
||||
((c, t) -> new NumericValue(pos.z << 4))
|
||||
(new NumericValue(pos.x << 4)),
|
||||
(new NumericValue(pos.z << 4))
|
||||
);
|
||||
}, () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world)
|
||||
);
|
||||
@@ -272,7 +270,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_DEPLOYS_ELYTRA = new Event("player_deploys_elytra", 1, false)
|
||||
@@ -280,7 +278,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_WAKES_UP = new Event("player_wakes_up", 1, false)
|
||||
@@ -288,7 +286,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_RIDES = new Event("player_rides", 5, false)
|
||||
@@ -297,11 +295,11 @@ public class CarpetEventServer
|
||||
public void onMountControls(ServerPlayerEntity player, float strafeSpeed, float forwardSpeed, boolean jumping, boolean sneaking)
|
||||
{
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new NumericValue(forwardSpeed)),
|
||||
((c, t) -> new NumericValue(strafeSpeed)),
|
||||
((c, t) -> new NumericValue(jumping)),
|
||||
((c, t) -> new NumericValue(sneaking))
|
||||
(new EntityValue(player)),
|
||||
(new NumericValue(forwardSpeed)),
|
||||
(new NumericValue(strafeSpeed)),
|
||||
(new NumericValue(jumping)),
|
||||
(new NumericValue(sneaking))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -314,9 +312,9 @@ public class CarpetEventServer
|
||||
{
|
||||
//ItemStack itemstack = player.getStackInHand(enumhand);
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> ListValue.fromItemStack(itemstack)),
|
||||
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand"))
|
||||
(new EntityValue(player)),
|
||||
(ListValue.fromItemStack(itemstack)),
|
||||
(new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand"))
|
||||
);
|
||||
}, player::getCommandSource);
|
||||
}
|
||||
@@ -329,9 +327,9 @@ public class CarpetEventServer
|
||||
handler.call( () ->
|
||||
{
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)),
|
||||
((c, t) -> new StringValue(facing.getName()))
|
||||
(new EntityValue(player)),
|
||||
(new BlockValue(null, player.getServerWorld(), blockpos)),
|
||||
(new StringValue(facing.getName()))
|
||||
);
|
||||
}, player::getCommandSource);
|
||||
}
|
||||
@@ -348,12 +346,12 @@ public class CarpetEventServer
|
||||
Direction enumfacing = hitRes.getSide();
|
||||
Vec3d vec3d = hitRes.getPos().subtract(blockpos.getX(), blockpos.getY(), blockpos.getZ());
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> ListValue.fromItemStack(itemstack)),
|
||||
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
|
||||
((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)),
|
||||
((c, t) -> new StringValue(enumfacing.getName())),
|
||||
((c, t) -> ListValue.of(
|
||||
(new EntityValue(player)),
|
||||
(ListValue.fromItemStack(itemstack)),
|
||||
(new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
|
||||
(new BlockValue(null, player.getServerWorld(), blockpos)),
|
||||
(new StringValue(enumfacing.getName())),
|
||||
(ListValue.of(
|
||||
new NumericValue(vec3d.x),
|
||||
new NumericValue(vec3d.y),
|
||||
new NumericValue(vec3d.z)
|
||||
@@ -373,11 +371,11 @@ public class CarpetEventServer
|
||||
Direction enumfacing = hitRes.getSide();
|
||||
Vec3d vec3d = hitRes.getPos().subtract(blockpos.getX(), blockpos.getY(), blockpos.getZ());
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
|
||||
((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)),
|
||||
((c, t) -> new StringValue(enumfacing.getName())),
|
||||
((c, t) -> ListValue.of(
|
||||
(new EntityValue(player)),
|
||||
(new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
|
||||
(new BlockValue(null, player.getServerWorld(), blockpos)),
|
||||
(new StringValue(enumfacing.getName())),
|
||||
(ListValue.of(
|
||||
new NumericValue(vec3d.x),
|
||||
new NumericValue(vec3d.y),
|
||||
new NumericValue(vec3d.z)
|
||||
@@ -392,10 +390,10 @@ public class CarpetEventServer
|
||||
public void onBlockPlaced(ServerPlayerEntity player, BlockPos pos, Hand enumhand, ItemStack itemstack)
|
||||
{
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> ListValue.fromItemStack(itemstack)),
|
||||
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
|
||||
((c, t) -> new BlockValue(null, player.getServerWorld(), pos))
|
||||
(new EntityValue(player)),
|
||||
(ListValue.fromItemStack(itemstack)),
|
||||
(new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
|
||||
(new BlockValue(null, player.getServerWorld(), pos))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -405,8 +403,8 @@ public class CarpetEventServer
|
||||
public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS)
|
||||
{
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new BlockValue(previousBS, player.getServerWorld(), pos))
|
||||
(new EntityValue(player)),
|
||||
(new BlockValue(previousBS, player.getServerWorld(), pos))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -416,9 +414,9 @@ public class CarpetEventServer
|
||||
public void onEntityHandAction(ServerPlayerEntity player, Entity entity, Hand enumhand)
|
||||
{
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new EntityValue(entity)),
|
||||
((c, t) -> new StringValue(enumhand==Hand.MAIN_HAND?"mainhand":"offhand"))
|
||||
(new EntityValue(player)),
|
||||
(new EntityValue(entity)),
|
||||
(new StringValue(enumhand==Hand.MAIN_HAND?"mainhand":"offhand"))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -430,8 +428,8 @@ public class CarpetEventServer
|
||||
{
|
||||
//ItemStack itemstack = player.getStackInHand(enumhand);
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> ListValue.fromItemStack(itemstack))
|
||||
(new EntityValue(player)),
|
||||
(ListValue.fromItemStack(itemstack))
|
||||
);
|
||||
}, player::getCommandSource);
|
||||
}
|
||||
@@ -443,8 +441,8 @@ public class CarpetEventServer
|
||||
public void onEntityHandAction(ServerPlayerEntity player, Entity entity, Hand enumhand)
|
||||
{
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new EntityValue(entity))
|
||||
(new EntityValue(player)),
|
||||
(new EntityValue(entity))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -453,7 +451,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_STOPS_SNEAKING = new Event("player_stops_sneaking", 1, false)
|
||||
@@ -461,7 +459,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_STARTS_SPRINTING = new Event("player_starts_sprinting", 1, false)
|
||||
@@ -469,7 +467,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_STOPS_SPRINTING = new Event("player_stops_sprinting", 1, false)
|
||||
@@ -477,7 +475,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -490,9 +488,9 @@ public class CarpetEventServer
|
||||
handler.call( () ->
|
||||
{
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> ListValue.fromItemStack(itemstack)),
|
||||
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand"))
|
||||
(new EntityValue(player)),
|
||||
(ListValue.fromItemStack(itemstack)),
|
||||
(new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand"))
|
||||
);
|
||||
}, player::getCommandSource);
|
||||
}
|
||||
@@ -506,9 +504,9 @@ public class CarpetEventServer
|
||||
handler.call( () ->
|
||||
{
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> ListValue.fromItemStack(itemstack)),
|
||||
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand"))
|
||||
(new EntityValue(player)),
|
||||
(ListValue.fromItemStack(itemstack)),
|
||||
(new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand"))
|
||||
);
|
||||
}, player::getCommandSource);
|
||||
}
|
||||
@@ -518,7 +516,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_DROPS_STACK = new Event("player_drops_stack", 1, false)
|
||||
@@ -526,7 +524,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_CHOOSES_RECIPE = new Event("player_chooses_recipe", 3, false)
|
||||
@@ -537,9 +535,9 @@ public class CarpetEventServer
|
||||
handler.call( () ->
|
||||
{
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new StringValue(NBTSerializableValue.nameFromRegistryId(recipe))),
|
||||
((c, t) -> new NumericValue(fullStack))
|
||||
(new EntityValue(player)),
|
||||
(new StringValue(NBTSerializableValue.nameFromRegistryId(recipe))),
|
||||
(new NumericValue(fullStack))
|
||||
);
|
||||
}, player::getCommandSource);
|
||||
}
|
||||
@@ -553,9 +551,9 @@ public class CarpetEventServer
|
||||
handler.call( () ->
|
||||
{
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new NumericValue(from)),
|
||||
((c, t) -> new NumericValue(to))
|
||||
(new EntityValue(player)),
|
||||
(new NumericValue(from)),
|
||||
(new NumericValue(to))
|
||||
);
|
||||
}, player::getCommandSource);
|
||||
}
|
||||
@@ -565,7 +563,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_TAKES_DAMAGE = new Event("player_takes_damage", 4, false)
|
||||
@@ -576,10 +574,10 @@ public class CarpetEventServer
|
||||
handler.call( () ->
|
||||
{
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(target)),
|
||||
((c, t) -> new NumericValue(amount)),
|
||||
((c, t) ->new StringValue(source.getName())),
|
||||
((c, t) -> source.getAttacker()==null?Value.NULL:new EntityValue(source.getAttacker()))
|
||||
( new EntityValue(target)),
|
||||
( new NumericValue(amount)),
|
||||
(new StringValue(source.getName())),
|
||||
( source.getAttacker()==null?Value.NULL:new EntityValue(source.getAttacker()))
|
||||
);
|
||||
}, target::getCommandSource);
|
||||
}
|
||||
@@ -592,9 +590,9 @@ public class CarpetEventServer
|
||||
handler.call( () ->
|
||||
{
|
||||
return Arrays.asList(
|
||||
((c, t) -> new EntityValue(source.getAttacker())),
|
||||
((c, t) -> new NumericValue(amount)),
|
||||
((c, t) -> new EntityValue(target))
|
||||
(new EntityValue(source.getAttacker())),
|
||||
(new NumericValue(amount)),
|
||||
(new EntityValue(target))
|
||||
);
|
||||
}, () -> source.getAttacker().getCommandSource());
|
||||
}
|
||||
@@ -604,8 +602,8 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onEntityHandAction(ServerPlayerEntity player, Entity entity, Hand enumhand) {
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new EntityValue(entity))
|
||||
(new EntityValue(player)),
|
||||
(new EntityValue(entity))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -615,7 +613,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_RESPAWNS = new Event("player_respawns", 1, false)
|
||||
@@ -623,7 +621,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_CHANGES_DIMENSION = new Event("player_changes_dimension", 5, false)
|
||||
@@ -638,11 +636,11 @@ public class CarpetEventServer
|
||||
Value toDimStr = new StringValue(NBTSerializableValue.nameFromRegistryId(dimTo.getValue()));
|
||||
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> fromValue),
|
||||
((c, t) -> fromDimStr),
|
||||
((c, t) -> toValue),
|
||||
((c, t) -> toDimStr)
|
||||
(new EntityValue(player)),
|
||||
(fromValue),
|
||||
(fromDimStr),
|
||||
(toValue),
|
||||
(toDimStr)
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -650,7 +648,7 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onPlayerEvent(ServerPlayerEntity player)
|
||||
{
|
||||
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
|
||||
handler.call( () -> Collections.singletonList((new EntityValue(player))), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
public static final Event PLAYER_DISCONNECTS = new Event("player_disconnects", 2, false) {
|
||||
@@ -658,8 +656,8 @@ public class CarpetEventServer
|
||||
public void onPlayerMessage(ServerPlayerEntity player, String message)
|
||||
{
|
||||
handler.call( () -> Arrays.asList(
|
||||
((c, t) -> new EntityValue(player)),
|
||||
((c, t) -> new StringValue(message))
|
||||
(new EntityValue(player)),
|
||||
(new StringValue(message))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -680,10 +678,10 @@ public class CarpetEventServer
|
||||
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))
|
||||
(new EntityValue(player)),
|
||||
(new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.STAT_TYPE.getId(stat.getType())))),
|
||||
(new StringValue(NBTSerializableValue.nameFromRegistryId(id))),
|
||||
(new NumericValue(amount))
|
||||
), player::getCommandSource);
|
||||
}
|
||||
};
|
||||
@@ -694,23 +692,30 @@ public class CarpetEventServer
|
||||
{
|
||||
handler.call(
|
||||
() -> Arrays.asList(
|
||||
((c, t) -> new BlockValue(null, world, pos)),
|
||||
((c, t) -> flag>0?Value.TRUE:Value.FALSE)
|
||||
(new BlockValue(null, world, pos)),
|
||||
(flag>0?Value.TRUE:Value.FALSE)
|
||||
), () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
public static String getLoadEvent(EntityType<? extends Entity> et)
|
||||
{
|
||||
return "entity_loaded_" + Registry.ENTITY_TYPE.getId(et);
|
||||
}
|
||||
|
||||
public static final Map<EntityType<?>, Event> ENTITY_LOAD= new HashMap<EntityType<?>, Event>() {{
|
||||
public static final Map<EntityType<? extends Entity>, Event> ENTITY_LOAD= new HashMap<EntityType<? extends Entity>, Event>() {{
|
||||
EntityType.get("zombie");
|
||||
Registry.ENTITY_TYPE.forEach(et -> {
|
||||
put(et, new Event("entity_loaded_" + Registry.ENTITY_TYPE.getId(et), 1, true, false)
|
||||
put(et, new Event(getLoadEvent(et), 1, true, false)
|
||||
{
|
||||
@Override
|
||||
public void onEntityAction(Entity entity)
|
||||
{
|
||||
super.onEntityAction(entity);
|
||||
handler.call(
|
||||
() -> Collections.singletonList((new EntityValue(entity))),
|
||||
() -> CarpetServer.minecraft_server.getCommandSource().withWorld((ServerWorld) entity.world).withLevel(CarpetSettings.runPermissionLevel)
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -808,12 +813,13 @@ public class CarpetEventServer
|
||||
return added;
|
||||
}
|
||||
|
||||
public boolean addEventDirectly(String event, ScriptHost host, FunctionValue function)
|
||||
public void addEventDirectly(String event, ScriptHost host, FunctionValue function, List<Value> args)
|
||||
{
|
||||
Event ev = Event.byName.get(event);
|
||||
if (!canAddEvent(ev, host))
|
||||
throw new InternalExpressionException("Global event "+event+" can only be added to apps with global scope");
|
||||
return ev.handler.addEventCallDirect(host, function);
|
||||
boolean success = ev.handler.addEventCallInternal(host, function, args);
|
||||
if (!success) throw new InternalExpressionException("Global event "+event+" requires "+ev.handler.reqArgs+", not "+(function.getNumParams()-((args==null)?0:args.size())));
|
||||
}
|
||||
|
||||
private boolean canAddEvent(Event event, ScriptHost host)
|
||||
@@ -836,6 +842,16 @@ public class CarpetEventServer
|
||||
Messenger.m(source, "gi Removed event: " + funName + " from "+event);
|
||||
return true;
|
||||
}
|
||||
public boolean removeEventDirectly(String event, ScriptHost host)
|
||||
{
|
||||
|
||||
if (!Event.byName.containsKey(event))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Event.byName.get(event).handler.removeAllCalls(host.getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
public void removeEventDirectly(String event, ScriptHost host, String funName)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user