From a7ae41a9df7409229aed11a72fccdffd1c887685 Mon Sep 17 00:00:00 2001 From: gnembon Date: Thu, 18 Jul 2019 09:31:42 -0300 Subject: [PATCH 1/6] spawn and properties --- .../java/carpet/script/CarpetExpression.java | 80 ++++++++++++++++++- src/main/java/carpet/script/Expression.java | 8 +- .../java/carpet/script/value/BlockValue.java | 9 ++- .../java/carpet/script/value/EntityValue.java | 56 +++++++------ 4 files changed, 119 insertions(+), 34 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 042c0ebc..71f6aa01 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -35,8 +35,11 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.LightningEntity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.SpawnType; import net.minecraft.entity.decoration.ArmorStandEntity; +import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; @@ -53,6 +56,8 @@ import net.minecraft.sound.SoundCategory; import net.minecraft.state.StateFactory; import net.minecraft.state.property.Property; import net.minecraft.text.LiteralText; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.Clearable; import net.minecraft.util.Identifier; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.EntityHitResult; @@ -73,6 +78,7 @@ import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -599,7 +605,7 @@ public class CarpetExpression throw new InternalExpressionException("Block requires at least one parameter"); } CompoundTag tag = BlockValue.fromParams(cc, lv, 0, true).block.getData(); - if (tag.isEmpty()) + if (tag == null) return (c_, t_) -> Value.NULL; Value retval = new NBTSerializableValue(tag); return (c_, t_) -> retval; @@ -783,7 +789,8 @@ public class CarpetExpression return (c_, t_) -> Value.FALSE; CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates; BlockPos targetPos = targetLocator.block.getPos(); - cc.s.getWorld().setBlockState(targetPos, sourceBlockState, 2); + Clearable.clear(world.getBlockEntity(targetPos)); + world.setBlockState(targetPos, sourceBlockState, 2); if ( data != null) { @@ -935,15 +942,28 @@ public class CarpetExpression { BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext) c, lv, 0); BlockState state = locator.block.getBlockState(); + if (lv.size() <= locator.offset) + throw new InternalExpressionException("property requires to specify a property to query"); String tag = lv.get(locator.offset).evalValue(c).getString(); StateFactory states = state.getBlock().getStateFactory(); Property property = states.getProperty(tag); if (property == null) return LazyValue.NULL; - Value retval = new StringValue(state.get(property).toString()); + Value retval = new StringValue(state.get(property).toString().toLowerCase(Locale.ROOT)); return (_c, _t) -> retval; }); + this.expr.addLazyFunction("block_properties", -1, (c, t, lv) -> + { + BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext) c, lv, 0); + BlockState state = locator.block.getBlockState(); + StateFactory states = state.getBlock().getStateFactory(); + Value res = ListValue.wrap(states.getProperties().stream().map( + p -> new StringValue(p.getName())).collect(Collectors.toList()) + ); + return (_c, _t) -> res; + }); + /*this.expr.addLazyFunction("trace_block", -1, (c, t, lv) -> { Value entityValue = lv.get(0).evalValue(c); if (!(entityValue instanceof EntityValue)) @@ -1597,6 +1617,60 @@ public class CarpetExpression return (cc, tt) -> finalVar; }); + this.expr.addLazyFunction("spawn", -1, (c, t, lv) -> { + CarpetContext cc = (CarpetContext)c; + if (lv.size() < 2) + throw new InternalExpressionException("spawn function takes mob name, and position to spawn"); + String entityString = lv.get(0).evalValue(c).getString(); + Identifier entityId; + try + { + entityId = Identifier.fromCommandInput(new StringReader(entityString)); + EntityType type = Registry.ENTITY_TYPE.getOrEmpty(entityId).orElse(null); + if (type == null || !type.isSummonable()) + return LazyValue.NULL; + } + catch (CommandSyntaxException exception) + { + return LazyValue.NULL; + } + + BlockValue.VectorLocator position = BlockValue.locateVec(cc, lv, 1); + if (position.fromBlock) + position.vec = position.vec.subtract(0, 0.5, 0); + CompoundTag tag = new CompoundTag(); + boolean hasTag = false; + if (lv.size() > position.offset) + { + hasTag = true; + tag = new NBTSerializableValue(lv.get(position.offset).evalValue(c).getString()).getTag(); + } + tag.putString("id", entityId.toString()); + Vec3d vec3d = position.vec; + + if (EntityType.getId(EntityType.LIGHTNING_BOLT).equals(entityId)) { + LightningEntity lightningEntity_1 = new LightningEntity(cc.s.getWorld(), vec3d.x, vec3d.y, vec3d.z, false); + cc.s.getWorld().addLightning(lightningEntity_1); + cc.s.sendFeedback(new TranslatableText("commands.summon.success", lightningEntity_1.getDisplayName()), true); + return LazyValue.NULL; + } else { + ServerWorld serverWorld = cc.s.getWorld(); + Entity entity_1 = EntityType.loadEntityWithPassengers(tag, serverWorld, (entity_1x) -> { + entity_1x.setPositionAndAngles(vec3d.x, vec3d.y, vec3d.z, entity_1x.yaw, entity_1x.pitch); + return !serverWorld.method_18768(entity_1x) ? null : entity_1x; + }); + if (entity_1 == null) { + return LazyValue.NULL; + } else { + if (!hasTag && entity_1 instanceof MobEntity) { + ((MobEntity)entity_1).initialize(serverWorld, serverWorld.getLocalDifficulty(new BlockPos(entity_1)), SpawnType.COMMAND, null, null); + } + Value res = new EntityValue(entity_1); + return (_c, _t) -> res; + } + } + }); + this.expr.addLazyFunction("entity_id", 1, (c, t, lv) -> { Value who = lv.get(0).evalValue(c); diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 88ac9cac..2d2f01fb 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -691,10 +691,10 @@ public class Expression implements Cloneable { throw exit; } - catch (Exception exc) - { - throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage()); - } + //catch (Exception exc) + //{ + // throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage()); + //} } }); } diff --git a/src/main/java/carpet/script/value/BlockValue.java b/src/main/java/carpet/script/value/BlockValue.java index 8473a460..460abe00 100644 --- a/src/main/java/carpet/script/value/BlockValue.java +++ b/src/main/java/carpet/script/value/BlockValue.java @@ -78,7 +78,7 @@ public class BlockValue extends Value Value v1 = params.get(0 + offset).evalValue(c); if (v1 instanceof BlockValue) { - return new VectorLocator(new Vec3d(((BlockValue) v1).getPos()).add(0.5,0.5,0.5), 1+offset); + return (new VectorLocator(new Vec3d(((BlockValue) v1).getPos()).add(0.5,0.5,0.5), 1+offset)).fromBlock(); } if (v1 instanceof ListValue) { @@ -270,6 +270,7 @@ public class BlockValue extends Value public int offset; public double yaw; public double pitch; + public boolean fromBlock; VectorLocator(Vec3d v, int o) { vec = v; @@ -284,6 +285,12 @@ public class BlockValue extends Value yaw = y; pitch = p; } + + public VectorLocator fromBlock() + { + fromBlock = true; + return this; + } } public enum SpecificDirection { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 2b73e5fa..a0ea7f9e 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -4,6 +4,7 @@ import carpet.fakes.MobEntityInterface; import carpet.script.exception.InternalExpressionException; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.client.network.packet.EntityPositionS2CPacket; import net.minecraft.client.network.packet.EntityVelocityUpdateS2CPacket; import net.minecraft.command.EntitySelector; import net.minecraft.command.EntitySelectorReader; @@ -26,6 +27,7 @@ import net.minecraft.nbt.Tag; import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -356,6 +358,21 @@ public class EntityValue extends Value featureModifiers.get(what).accept(entity, toWhat); } + private static void updatePosition(Entity e) + { + if (e instanceof ServerPlayerEntity) + ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + else + ((ServerWorld)e.getEntityWorld()).method_14178().sendToNearbyPlayers(e,new EntityPositionS2CPacket(e)); + } + + private static void updateVelocity(Entity e) + { + ((ServerWorld)e.getEntityWorld()).method_14178().sendToNearbyPlayers(e, new EntityVelocityUpdateS2CPacket(e)); + } + + + private static Map> featureModifiers = new HashMap>() {{ put("remove", (entity, value) -> entity.remove()); put("health", (e, v) -> { if (e instanceof LivingEntity) ((LivingEntity) e).setHealth((float) NumericValue.asNumber(v).getDouble()); }); @@ -375,8 +392,7 @@ public class EntityValue extends Value e.yaw = (float) NumericValue.asNumber(coords.get(3)).getDouble(); e.prevYaw = e.yaw; e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("pos", (e, v) -> { @@ -389,43 +405,37 @@ public class EntityValue extends Value e.y = NumericValue.asNumber(coords.get(1)).getDouble(); e.z = NumericValue.asNumber(coords.get(2)).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("x", (e, v) -> { e.x = NumericValue.asNumber(v).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("y", (e, v) -> { e.y = NumericValue.asNumber(v).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("z", (e, v) -> { e.z = NumericValue.asNumber(v).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("pitch", (e, v) -> { e.pitch = (float) NumericValue.asNumber(v).getDouble(); e.prevPitch = e.pitch; - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("yaw", (e, v) -> { e.yaw = (float) NumericValue.asNumber(v).getDouble(); e.prevYaw = e.yaw; - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); //"look" //"turn" @@ -442,8 +452,7 @@ public class EntityValue extends Value e.y += NumericValue.asNumber(coords.get(1)).getDouble(); e.z += NumericValue.asNumber(coords.get(2)).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.y, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("motion", (e, v) -> @@ -458,29 +467,25 @@ public class EntityValue extends Value NumericValue.asNumber(coords.get(1)).getDouble(), NumericValue.asNumber(coords.get(2)).getDouble() ); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("motion_x", (e, v) -> { Vec3d velocity = e.getVelocity(); e.setVelocity(NumericValue.asNumber(v).getDouble(), velocity.y, velocity.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("motion_y", (e, v) -> { Vec3d velocity = e.getVelocity(); e.setVelocity(velocity.x, NumericValue.asNumber(v).getDouble(), velocity.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("motion_z", (e, v) -> { Vec3d velocity = e.getVelocity(); e.setVelocity(velocity.x, velocity.y, NumericValue.asNumber(v).getDouble()); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("accelerate", (e, v) -> @@ -495,8 +500,7 @@ public class EntityValue extends Value NumericValue.asNumber(coords.get(1)).getDouble(), NumericValue.asNumber(coords.get(2)).getDouble() ); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("custom_name", (e, v) -> { From 239e7006a5dd907768afc7bb92dbffeab0fc60a7 Mon Sep 17 00:00:00 2001 From: gnembon Date: Fri, 19 Jul 2019 09:45:15 -0300 Subject: [PATCH 2/6] reworked script host, working on nbttags --- .../java/carpet/commands/ScriptCommand.java | 19 ++++-- .../java/carpet/script/CarpetEventServer.java | 10 +++ .../java/carpet/script/CarpetExpression.java | 1 + .../carpet/script/CarpetScriptServer.java | 64 +++++++++++-------- src/main/java/carpet/script/Expression.java | 10 +-- src/main/java/carpet/script/ScriptHost.java | 50 ++++++++++++--- .../java/carpet/script/value/EntityValue.java | 8 +-- .../carpet/script/value/LazyListValue.java | 6 ++ .../java/carpet/script/value/ListValue.java | 11 ++++ .../script/value/NBTSerializableValue.java | 42 ++++++++++++ src/main/java/carpet/script/value/Value.java | 5 ++ 11 files changed, 175 insertions(+), 51 deletions(-) diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index dd9034bf..b38efe75 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -14,6 +14,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.block.Block; @@ -24,6 +25,7 @@ import net.minecraft.command.arguments.BlockStateArgument; import net.minecraft.command.arguments.BlockStateArgumentType; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Clearable; import net.minecraft.util.math.BlockPos; import net.minecraft.server.world.ServerWorld; @@ -206,9 +208,17 @@ public class ScriptCommand suggests( (cc, bb) -> suggestMatching(CarpetServer.scriptServer.listAvailableModules(),bb)). executes((cc) -> { - boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package"), false); + boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package"), true); return success?1:0; - }) + }). + then(literal("global"). + executes((cc) -> + { + boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package"), false); + return success?1:0; + } + ) + ) ); LiteralArgumentBuilder f = literal("unload").requires( (player) -> player.hasPermissionLevel(2) ). then(argument("package", StringArgumentType.word()). @@ -267,8 +277,8 @@ public class ScriptCommand try { String name = StringArgumentType.getString(context, "package").toLowerCase(Locale.ROOT); - return CarpetServer.scriptServer.modules.getOrDefault(name, CarpetServer.scriptServer.globalHost); - + ScriptHost parentHost = CarpetServer.scriptServer.modules.getOrDefault(name, CarpetServer.scriptServer.globalHost); + return parentHost.retrieveForExecution(context.getSource()); } catch (IllegalArgumentException ignored) { @@ -277,7 +287,6 @@ public class ScriptCommand } private static Collection suggestFunctionCalls(CommandContext c) { - ServerCommandSource s = c.getSource(); ScriptHost host = getHost(c); return host.getPublicFunctions(); } diff --git a/src/main/java/carpet/script/CarpetEventServer.java b/src/main/java/carpet/script/CarpetEventServer.java index 52145df9..4fcf43d4 100644 --- a/src/main/java/carpet/script/CarpetEventServer.java +++ b/src/main/java/carpet/script/CarpetEventServer.java @@ -122,6 +122,11 @@ public class CarpetEventServer { callList.removeIf((c)-> c.udf.equalsIgnoreCase(callName) && ( hostName == null || c.host.equalsIgnoreCase(hostName) ) ); } + + public void removeAllCalls(String hostName) + { + callList.removeIf((c)-> c.host.equals(hostName)); + } } public Map eventHandlers = new HashMap<>(); @@ -194,6 +199,11 @@ public class CarpetEventServer return true; } + public void removeAllHostEvents(String hostName) + { + eventHandlers.forEach((e, a) -> a.removeAllCalls(hostName)); + } + private Callback decodeCallback(String funName) { Pattern find = Pattern.compile("(\\w+)\\(from (\\w+)\\)"); diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 71f6aa01..3fc0300c 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -45,6 +45,7 @@ import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; import net.minecraft.particle.ParticleEffect; import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.server.MinecraftServer; diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index b736b15a..716870a2 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -44,7 +44,7 @@ public class CarpetScriptServer public CarpetScriptServer() { - globalHost = createMinecraftScriptHost(null, null); + globalHost = createMinecraftScriptHost(null, null, false, null); events = new CarpetEventServer(); modules = new HashMap<>(); tickStart = 0L; @@ -112,12 +112,35 @@ public class CarpetScriptServer } - private static ScriptHost createMinecraftScriptHost(String name, ModuleInterface code) + private ScriptHost createMinecraftScriptHost(String name, ModuleInterface module, boolean perPlayer, ServerCommandSource source) { - ScriptHost host = new ScriptHost(name); + ScriptHost host = new ScriptHost(name, module, perPlayer ); host.globalVariables.put("_x", (c, t) -> Value.ZERO); host.globalVariables.put("_y", (c, t) -> Value.ZERO); host.globalVariables.put("_z", (c, t) -> Value.ZERO); + // parse code and convert to expression + if (module != null) + { + try + { + String code = module.getCode(); + if (code == null) + { + Messenger.m(source, "r Unable to load the package - not found"); + return null; + } + setChatErrorSnooper(source); + CarpetExpression ex = new CarpetExpression(code, source, new BlockPos(0, 0, 0)); + ex.getExpr().asAModule(); + ex.scriptRunCommand(host, new BlockPos(source.getPosition())); + } + catch (CarpetExpressionException e) + { + Messenger.m(source, "r Exception white evaluating expression at " + new BlockPos(source.getPosition()) + ": " + e.getMessage()); + resetErrorSnooper(); + return null; + } + } return host; } @@ -126,7 +149,12 @@ public class CarpetScriptServer //TODO add per player modules to support player actions better on a server name = name.toLowerCase(Locale.ROOT); ModuleInterface module = getModule(name); - ScriptHost newHost = createMinecraftScriptHost(name, module); + ScriptHost newHost = createMinecraftScriptHost(name, module, perPlayer, source); + if (newHost == null) + { + Messenger.m(source, "r Failed to add a module"); + return false; + } if (module == null) { Messenger.m(source, "r Unable to locate the package, but created empty host "+name+" instead"); @@ -134,27 +162,12 @@ public class CarpetScriptServer return true; } String code = module.getCode(); - if (code == null) + if (module.getCode() == null) { Messenger.m(source, "r Unable to load the package - not found"); return false; } - // parse code and convert to expression - // fill functions etc. - // possibly add a command - try - { - setChatErrorSnooper(source); - CarpetExpression ex = new CarpetExpression(module.getCode(), source, new BlockPos(0, 0, 0)); - ex.getExpr().asAModule(); - ex.scriptRunCommand(newHost, new BlockPos(source.getPosition())); - } - catch (CarpetExpressionException e) - { - Messenger.m(source, "r Exception white evaluating expression at "+new BlockPos(source.getPosition())+": "+e.getMessage()); - resetErrorSnooper(); - return false; - } + modules.put(name, newHost); addEvents(source, name); @@ -206,7 +219,7 @@ public class CarpetScriptServer requires((player) -> modules.containsKey(hostName)). executes( (c) -> { - Messenger.m(c.getSource(), "gi "+modules.get(hostName).call(c.getSource(),"__command", null, "")); + Messenger.m(c.getSource(), "gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call(c.getSource(),"__command", null, "")); return 1; }); @@ -218,7 +231,7 @@ public class CarpetScriptServer executes( (c) -> { Messenger.m( c.getSource(), - "gi "+modules.get(hostName).call( + "gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call( c.getSource(), function, null, @@ -231,7 +244,7 @@ public class CarpetScriptServer executes( (c) -> { Messenger.m( c.getSource(), - "gi "+modules.get(hostName).call( + "gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call( c.getSource(), function, null, @@ -304,6 +317,7 @@ public class CarpetScriptServer return false; } // stop all events associated with name + events.removeAllHostEvents(name); modules.remove(name); CarpetServer.settingsManager.notifyPlayersCommandsChanged(); Messenger.m(source, "w Removed host "+name); @@ -319,7 +333,7 @@ public class CarpetScriptServer { ScriptHost host = globalHost; if (hostname != null) - host = modules.get(hostname); + host = modules.get(hostname).retrieveForExecution(source); try { host.callUDF(origin, source, host.globalFunctions.get(udf_name), argv); diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 2d2f01fb..a6f24aee 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -1894,15 +1894,7 @@ public class Expression implements Cloneable addBinaryFunction("get", (v1, v2) -> { - if (v1 instanceof LazyListValue || !(v1 instanceof ListValue)) - throw new InternalExpressionException("First argument of get should be a list"); - List items = ((ListValue)v1).getItems(); - long index = NumericValue.asNumber(v2).getLong(); - int numitems = items.size(); - long range = abs(index)/numitems; - index += (range+2)*numitems; - index = index % numitems; - return items.get((int)index); + return v1.getElementAt(v2); }); //Deprecated, use "get" instead diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index 58651f55..b032bae4 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -8,7 +8,9 @@ import carpet.script.exception.InvalidCallbackException; import carpet.script.value.NumericValue; import carpet.script.value.StringValue; import carpet.script.value.Value; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.math.BlockPos; import java.math.BigInteger; @@ -23,22 +25,21 @@ import static java.lang.Math.max; public class ScriptHost { - - public final Map globalFunctions = new HashMap<>(); - - public final Map globalVariables = new HashMap<>(); + public final Map userHosts = new HashMap<>(); + public Map globalFunctions = new HashMap<>(); + public Map globalVariables = new HashMap<>(); private String name; public String getName() {return name;} - private ModuleInterface myCode; + private final ModuleInterface myCode; + public final boolean perUser; - ScriptHost(String name) { this(name, null);} - - ScriptHost(String name, ModuleInterface code) + ScriptHost(String name, ModuleInterface code, boolean perUser) { this.name = name; this.myCode = code; + this.perUser = perUser; globalVariables.put("euler", (c, t) -> Expression.euler); globalVariables.put("pi", (c, t) -> Expression.PI); globalVariables.put("null", (c, t) -> Value.NULL); @@ -51,6 +52,37 @@ public class ScriptHost globalVariables.put("_a", (c, t) -> Value.ZERO); } + public ScriptHost retrieveForExecution(String /*Nullable*/ user) + { + if (!perUser) + return this; + ScriptHost userHost = userHosts.get(user); + if (userHost != null) + return userHost; + userHost = new ScriptHost(this.name, myCode, false); + userHost.globalVariables.putAll(this.globalVariables); + userHost.globalFunctions.putAll(this.globalFunctions); + userHosts.put(user, userHost); + return userHost; + } + + public ScriptHost retrieveForExecution(ServerCommandSource source) + { + if (!perUser) + return this; + try + { + ServerPlayerEntity player = source.getPlayer(); + CarpetSettings.LOG.error("Retrieving "+player.getName().getString()+" host for "+getName()); + return retrieveForExecution(player.getName().getString()); + } + catch (CommandSyntaxException e) + { + CarpetSettings.LOG.error("Retrieving global host for "+getName()); + return retrieveForExecution((String)null); + } + } + public Expression getExpressionForFunction(String name) { return globalFunctions.get(name).getExpression(); @@ -190,4 +222,6 @@ public class ScriptHost CarpetSettings.LOG.error("Callback failed: "+e.getMessage()); } } + + } diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index a0ea7f9e..235203fe 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -313,7 +313,7 @@ public class EntityValue extends Value put("nbt",(e, a) -> { CompoundTag nbttagcompound = e.toTag((new CompoundTag())); if (a==null) - return new StringValue(nbttagcompound.toString()); + return new NBTSerializableValue(nbttagcompound);//StringValue(nbttagcompound.toString()); NbtPathArgumentType.NbtPath path; try { @@ -330,11 +330,11 @@ public class EntityValue extends Value if (tags.size()==0) return Value.NULL; if (tags.size()==1) - return new StringValue(tags.get(0).toText().getString()); - return ListValue.wrap(tags.stream().map((t) -> new StringValue(t.toText().getString())).collect(Collectors.toList())); + return new NBTSerializableValue((CompoundTag) tags.get(0)); + return ListValue.wrap(tags.stream().map(t -> new NBTSerializableValue((CompoundTag) t)).collect(Collectors.toList())); } catch (CommandSyntaxException ignored) { } - return new StringValue(res); + return Value.NULL; }); }}; private static Req assertEntityArgType(Class klass, Value arg) diff --git a/src/main/java/carpet/script/value/LazyListValue.java b/src/main/java/carpet/script/value/LazyListValue.java index be416f18..8bf16cc2 100644 --- a/src/main/java/carpet/script/value/LazyListValue.java +++ b/src/main/java/carpet/script/value/LazyListValue.java @@ -106,4 +106,10 @@ public abstract class LazyListValue extends ListValue implements Iterator return false; } + @Override + public Value getElementAt(Value v2) + { + throw new InternalExpressionException("get element can only be obtained for regular lists"); + } + } diff --git a/src/main/java/carpet/script/value/ListValue.java b/src/main/java/carpet/script/value/ListValue.java index fefb8a7f..ab7a47f6 100644 --- a/src/main/java/carpet/script/value/ListValue.java +++ b/src/main/java/carpet/script/value/ListValue.java @@ -316,6 +316,17 @@ public class ListValue extends Value return (double)items.size(); } + @Override + public Value getElementAt(Value value) + { + long index = NumericValue.asNumber(value).getLong(); + int numitems = items.size(); + long range = abs(index)/numitems; + index += (range+2)*numitems; + index = index % numitems; + return items.get((int)index); + } + } diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index 14d72f80..2abf9e45 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -15,6 +15,7 @@ import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.StringNbtReader; +import net.minecraft.nbt.Tag; import net.minecraft.util.math.BlockPos; import java.util.HashMap; @@ -22,6 +23,8 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; +import static java.lang.Math.abs; + public class NBTSerializableValue extends Value { private String nbtString = null; @@ -195,4 +198,43 @@ public class NBTSerializableValue extends Value offset = o; } } + + @Override + public Value getElementAt(Value value) + { + String path = value.getString(); + + return Value.NULL; + /* + CompoundTag nbttagcompound = e.toTag((new CompoundTag())); + if (a==null) + return new NBTSerializableValue(nbttagcompound);//StringValue(nbttagcompound.toString()); + NbtPathArgumentType.NbtPath path; + try + { + path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(a.getString())); + } + catch (CommandSyntaxException exc) + { + throw new InternalExpressionException("Incorrect path: "+a.getString()); + } + String res = null; + try + { + List tags = path.get(nbttagcompound); + if (tags.size()==0) + return Value.NULL; + if (tags.size()==1) + return new NBTSerializableValue(tags.get(0)); + return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList())); + } + catch (CommandSyntaxException ignored) { } + return Value.NULL; + */ + /*int numitems = items.size(); + long range = abs(index)/numitems; + index += (range+2)*numitems; + index = index % numitems; + return items.get((int)index); */ + } } diff --git a/src/main/java/carpet/script/value/Value.java b/src/main/java/carpet/script/value/Value.java index 24a3d26c..454b5871 100644 --- a/src/main/java/carpet/script/value/Value.java +++ b/src/main/java/carpet/script/value/Value.java @@ -172,4 +172,9 @@ public abstract class Value implements Comparable, Cloneable { return (long)readNumber(); } + + public Value getElementAt(Value v2) + { + throw new InternalExpressionException("get element can only be obtained for lists and nbt values"); + } } From eee23e4ba60d30e614d0db1ba3bd785e0424da66 Mon Sep 17 00:00:00 2001 From: gnembon Date: Fri, 19 Jul 2019 17:47:08 -0300 Subject: [PATCH 3/6] nbt contd --- .../java/carpet/script/CarpetExpression.java | 6 +- src/main/java/carpet/script/Expression.java | 7 +- .../java/carpet/script/value/EntityValue.java | 4 +- .../script/value/NBTSerializableValue.java | 82 ++++++++++++------- 4 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 3fc0300c..be10cdfc 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -1162,9 +1162,9 @@ public class CarpetExpression { Value nbtValue = lv.get(inventoryLocator.offset+3).evalValue(c); if (nbtValue instanceof NBTSerializableValue) - nbt = ((NBTSerializableValue)nbtValue).getTag(); + nbt = ((NBTSerializableValue)nbtValue).getCompoundTag(); else - nbt = new NBTSerializableValue(nbtValue.getString()).getTag(); + nbt = new NBTSerializableValue(nbtValue.getString()).getCompoundTag(); } ItemStackArgument newitem = NBTSerializableValue.parseItem( lv.get(inventoryLocator.offset+2).evalValue(c).getString(), @@ -1644,7 +1644,7 @@ public class CarpetExpression if (lv.size() > position.offset) { hasTag = true; - tag = new NBTSerializableValue(lv.get(position.offset).evalValue(c).getString()).getTag(); + tag = new NBTSerializableValue(lv.get(position.offset).evalValue(c).getString()).getCompoundTag(); } tag.putString("id", entityId.toString()); Vec3d vec3d = position.vec; diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index a6f24aee..2b064a22 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -1892,10 +1892,9 @@ public class Expression implements Cloneable return LazyListValue.range(from, to, step); }); - addBinaryFunction("get", (v1, v2) -> - { - return v1.getElementAt(v2); - }); + addUnaryFunction("is_list", value -> (value instanceof ListValue)? Value.TRUE : Value.FALSE); + + addBinaryFunction("get", (v1, v2) -> v1.getElementAt(v2)); //Deprecated, use "get" instead addBinaryFunction("element", (v1, v2) -> diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 235203fe..037850ba 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -330,8 +330,8 @@ public class EntityValue extends Value if (tags.size()==0) return Value.NULL; if (tags.size()==1) - return new NBTSerializableValue((CompoundTag) tags.get(0)); - return ListValue.wrap(tags.stream().map(t -> new NBTSerializableValue((CompoundTag) t)).collect(Collectors.toList())); + return NBTSerializableValue.decodeTag(tags.get(0)); + return ListValue.wrap(tags.stream().map(t -> NBTSerializableValue.decodeTag(t)).collect(Collectors.toList())); } catch (CommandSyntaxException ignored) { } return Value.NULL; diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index 2abf9e45..0d4a6b9a 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -8,11 +8,13 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.block.entity.HopperBlockEntity; import net.minecraft.command.arguments.ItemStackArgument; import net.minecraft.command.arguments.ItemStringReader; +import net.minecraft.command.arguments.NbtPathArgumentType; import net.minecraft.entity.Entity; import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.AbstractNumberTag; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.StringNbtReader; import net.minecraft.nbt.Tag; @@ -22,13 +24,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Supplier; +import java.util.stream.Collectors; import static java.lang.Math.abs; public class NBTSerializableValue extends Value { private String nbtString = null; - private CompoundTag nbtTag = null; + private Tag nbtTag = null; private Supplier nbtSupplier = null; public NBTSerializableValue(ItemStack stack) @@ -51,7 +54,7 @@ public class NBTSerializableValue extends Value }; } - public NBTSerializableValue(CompoundTag tag) + public NBTSerializableValue(Tag tag) { nbtTag = tag; } @@ -155,7 +158,17 @@ public class NBTSerializableValue extends Value return slot; } - public CompoundTag getTag() + public static Value decodeTag(Tag t) + { + if (t instanceof CompoundTag) + return new NBTSerializableValue(t); + if (t instanceof AbstractNumberTag) + return new NumericValue(((AbstractNumberTag) t).getDouble()); + // more can be done here + return new StringValue(t.asString()); + } + + public Tag getTag() { if (nbtTag == null) nbtTag = nbtSupplier.get(); @@ -184,6 +197,18 @@ public class NBTSerializableValue extends Value return true; } + public CompoundTag getCompoundTag() + { + try + { + return (CompoundTag) getTag(); + } + catch (ClassCastException e) + { + throw new InternalExpressionException(getString()+" is not a valid compound tag"); + } + } + public static class InventoryLocator { public Object owner; @@ -202,35 +227,32 @@ public class NBTSerializableValue extends Value @Override public Value getElementAt(Value value) { - String path = value.getString(); + String pathString = value.getString(); + //return Value.NULL; + + NbtPathArgumentType.NbtPath path; + try + { + path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(pathString)); + } + catch (CommandSyntaxException exc) + { + throw new InternalExpressionException("Incorrect nbt path: "+pathString); + } + String res = null; + try + { + List tags = path.get(getTag()); + if (tags.size()==0) + return Value.NULL; + if (tags.size()==1) + return new NBTSerializableValue(tags.get(0)); + return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList())); + } + catch (CommandSyntaxException ignored) { } return Value.NULL; - /* - CompoundTag nbttagcompound = e.toTag((new CompoundTag())); - if (a==null) - return new NBTSerializableValue(nbttagcompound);//StringValue(nbttagcompound.toString()); - NbtPathArgumentType.NbtPath path; - try - { - path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(a.getString())); - } - catch (CommandSyntaxException exc) - { - throw new InternalExpressionException("Incorrect path: "+a.getString()); - } - String res = null; - try - { - List tags = path.get(nbttagcompound); - if (tags.size()==0) - return Value.NULL; - if (tags.size()==1) - return new NBTSerializableValue(tags.get(0)); - return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList())); - } - catch (CommandSyntaxException ignored) { } - return Value.NULL; - */ + /*int numitems = items.size(); long range = abs(index)/numitems; index += (range+2)*numitems; From 3c96345a605176d12433303a36b6fd919578cfa7 Mon Sep 17 00:00:00 2001 From: gnembon Date: Sat, 20 Jul 2019 18:53:17 -0300 Subject: [PATCH 4/6] tracking and tracing --- src/main/java/carpet/CarpetServer.java | 4 + .../java/carpet/commands/MobAICommand.java | 42 +++++++++ .../carpet/mixins/VillagerEntity_aiMixin.java | 80 ++++++++++++++++ .../java/carpet/script/CarpetExpression.java | 45 ++------- src/main/java/carpet/script/Expression.java | 85 +++++------------ src/main/java/carpet/script/ScriptHost.java | 2 - .../java/carpet/script/value/BlockValue.java | 6 ++ .../java/carpet/script/value/EntityValue.java | 85 ++++++++++++++--- .../script/value/FunctionSignatureValue.java | 8 +- .../java/carpet/script/value/GlobalValue.java | 8 +- .../carpet/script/value/LazyListValue.java | 5 + .../java/carpet/script/value/ListValue.java | 5 + .../script/value/NBTSerializableValue.java | 39 +++++--- .../java/carpet/script/value/NullValue.java | 6 ++ .../carpet/script/value/NumericValue.java | 6 ++ .../java/carpet/script/value/StringValue.java | 6 ++ src/main/java/carpet/script/value/Value.java | 2 + .../java/carpet/settings/CarpetSettings.java | 3 + src/main/java/carpet/utils/MobAI.java | 93 +++++++++++++++++++ src/main/resources/carpet.mixins.json | 2 + 20 files changed, 404 insertions(+), 128 deletions(-) create mode 100644 src/main/java/carpet/commands/MobAICommand.java create mode 100644 src/main/java/carpet/mixins/VillagerEntity_aiMixin.java create mode 100644 src/main/java/carpet/utils/MobAI.java diff --git a/src/main/java/carpet/CarpetServer.java b/src/main/java/carpet/CarpetServer.java index c5aa40a0..f54a66f3 100644 --- a/src/main/java/carpet/CarpetServer.java +++ b/src/main/java/carpet/CarpetServer.java @@ -11,6 +11,7 @@ import carpet.commands.DistanceCommand; import carpet.commands.DrawCommand; import carpet.commands.InfoCommand; import carpet.commands.LogCommand; +import carpet.commands.MobAICommand; import carpet.commands.PerimeterInfoCommand; import carpet.commands.PlayerCommand; import carpet.commands.ScriptCommand; @@ -22,6 +23,7 @@ import carpet.script.CarpetScriptServer; import carpet.settings.CarpetSettings; import carpet.settings.SettingsManager; import carpet.utils.HUDController; +import carpet.utils.MobAI; import com.mojang.brigadier.CommandDispatcher; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.MinecraftServer; @@ -59,6 +61,7 @@ public class CarpetServer // static for now - easier to handle all around the co e.onServerLoaded(server); }); scriptServer = new CarpetScriptServer(); + MobAI.resetTrackers(); } public static void tick(MinecraftServer server) @@ -88,6 +91,7 @@ public class CarpetServer // static for now - easier to handle all around the co PerimeterInfoCommand.register(dispatcher); DrawCommand.register(dispatcher); ScriptCommand.register(dispatcher); + MobAICommand.register(dispatcher); extensions.forEach(e -> e.registerCommands(dispatcher)); //TestCommand.register(dispatcher); } diff --git a/src/main/java/carpet/commands/MobAICommand.java b/src/main/java/carpet/commands/MobAICommand.java new file mode 100644 index 00000000..f099ac4c --- /dev/null +++ b/src/main/java/carpet/commands/MobAICommand.java @@ -0,0 +1,42 @@ +package carpet.commands; + +import carpet.settings.CarpetSettings; +import carpet.utils.MobAI; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import net.minecraft.command.arguments.EntitySummonArgumentType; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.util.registry.Registry; + +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; +import static net.minecraft.server.command.CommandSource.suggestMatching; + +public class MobAICommand +{ + public static void register(CommandDispatcher dispatcher) + { + LiteralArgumentBuilder command = literal("track"). + requires((player) -> CarpetSettings.commandTrackAI). + then(argument("entity type", EntitySummonArgumentType.entitySummon()). + + suggests( (c, b) -> suggestMatching(MobAI.availbleTypes(), b)). + then(literal("clear").executes( (c) -> + { + MobAI.clearTracking(Registry.ENTITY_TYPE.get(EntitySummonArgumentType.getEntitySummon(c, "entity type"))); + return 1; + } + )). + then(argument("aspect", StringArgumentType.word()). + suggests( (c, b) -> suggestMatching(MobAI.availableFor(Registry.ENTITY_TYPE.get(EntitySummonArgumentType.getEntitySummon(c, "entity type"))),b)). + executes( (c) -> { + MobAI.startTracking( + Registry.ENTITY_TYPE.get(EntitySummonArgumentType.getEntitySummon(c, "entity type")), + MobAI.TrackingType.byName(StringArgumentType.getString(c, "aspect")) + ); + return 1; + }))); + dispatcher.register(command); + } +} diff --git a/src/main/java/carpet/mixins/VillagerEntity_aiMixin.java b/src/main/java/carpet/mixins/VillagerEntity_aiMixin.java new file mode 100644 index 00000000..637667b7 --- /dev/null +++ b/src/main/java/carpet/mixins/VillagerEntity_aiMixin.java @@ -0,0 +1,80 @@ +package carpet.mixins; + +import carpet.utils.Messenger; +import carpet.utils.MobAI; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.ai.brain.Activity; +import net.minecraft.entity.ai.brain.MemoryModuleType; +import net.minecraft.entity.passive.AbstractTraderEntity; +import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.particle.ParticleTypes; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Timestamp; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.Optional; + +@Mixin(VillagerEntity.class) +public abstract class VillagerEntity_aiMixin extends AbstractTraderEntity +{ + @Shadow protected abstract boolean hasSeenGolemRecently(long long_1); + + @Inject(method = "tick", at = @At("HEAD")) + private void ontick(CallbackInfo ci) + { + if (MobAI.isTracking(this, MobAI.TrackingType.IRON_GOLEM_SPAWNING) && !getEntityWorld().isClient()) + { + long time; + Optional optional_1 = this.brain.getOptionalMemory(MemoryModuleType.GOLEM_LAST_SEEN_TIME); + if (!optional_1.isPresent()) { + time = 0; + } else { + Long long_2 = optional_1.get(); + time = long_2+600 - getEntityWorld().getTime(); + } + boolean recentlySeen = hasSeenGolemRecently(getEntityWorld().getTime()); + Optional optional_11 = this.brain.getOptionalMemory(MemoryModuleType.LAST_SLEPT); + Optional optional_22 = this.brain.getOptionalMemory(MemoryModuleType.LAST_WORKED_AT_POI); + boolean work = false; + boolean sleep = false; + boolean panic = this.brain.hasActivity(Activity.PANIC); + long currentTime = this.world.getTime(); + if (optional_11.isPresent()) { + sleep = (currentTime - optional_11.get().getTime()) < 24000L; + } + if (optional_22.isPresent()) { + work = (currentTime - optional_22.get().getTime()) < 36000L; + } + + this.setCustomName(Messenger.c( + (sleep?"eb ":"fb ")+"\u263d ", + (work?"eb ":"fb ")+"\u2692 ",//"\u26CF ", + (panic?"lb ":"fb ")+"\u2623 ",//"\u2622 \u2620 \u26A1 ", + (recentlySeen?"rb ":"lb ")+time )); + this.setCustomNameVisible(true); + } + } + + @Inject(method = "summonGolem", at = @At( + value = "INVOKE", + target = "Lnet/minecraft/util/math/Box;expand(DDD)Lnet/minecraft/util/math/Box;", + shift = At.Shift.AFTER + )) + private void particleIt(long long_1, int int_1, CallbackInfo ci) + { + if (MobAI.isTracking(this, MobAI.TrackingType.IRON_GOLEM_SPAWNING) && !getEntityWorld().isClient()) + { + ((ServerWorld) getEntityWorld()).spawnParticles(ParticleTypes.BARRIER, x, y+3, z, 1, 0.1, 0.1, 0.1, 0.0); + } + } + + public VillagerEntity_aiMixin(EntityType entityType_1, World world_1) + { + super(entityType_1, world_1); + } +} diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index be10cdfc..2b9b227e 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -679,7 +679,6 @@ public class CarpetExpression this.expr.addLazyFunction("blast_resistance", -1, (c, t, lv) -> genericStateTest(c, "blast_resistance", lv, (s, p, w) -> new NumericValue(s.getBlock().getBlastResistance()))); - // Deprecated this.expr.addLazyFunction("top", -1, (c, t, lv) -> { String type = lv.get(0).evalValue(c).getString().toLowerCase(Locale.ROOT); @@ -693,20 +692,10 @@ public class CarpetExpression case "surface": htype = Heightmap.Type.WORLD_SURFACE; break; default: throw new InternalExpressionException("Unknown heightmap type: "+type); } - int x; - int z; - Value v1 = lv.get(1).evalValue(c); - if (v1 instanceof BlockValue) - { - BlockPos inpos = ((BlockValue)v1).getPos(); - x = inpos.getX(); - z = inpos.getZ(); - } - else - { - x = (int) NumericValue.asNumber(lv.get(1).evalValue(c)).getLong(); - z = (int) NumericValue.asNumber(lv.get(2).evalValue(c)).getLong(); - } + BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext)c, lv, 1); + BlockPos pos = locator.block.getPos(); + int x = pos.getX(); + int z = pos.getZ(); Value retval = new NumericValue(((CarpetContext)c).s.getWorld().getChunk(x >> 4, z >> 4).sampleHeightmap(htype, x & 15, z & 15) + 1); return (c_, t_) -> retval; //BlockPos pos = new BlockPos(x,y,z); @@ -965,24 +954,6 @@ public class CarpetExpression return (_c, _t) -> res; }); - /*this.expr.addLazyFunction("trace_block", -1, (c, t, lv) -> { - Value entityValue = lv.get(0).evalValue(c); - if (!(entityValue instanceof EntityValue)) - throw new InternalExpressionException("First argument of trace_block should be an entity"); - Entity e = ((EntityValue) entityValue).getEntity(); - HitResult hitres = Tracer.traceEntityLook(e, 20); - switch (hitres.getType()) - { - case MISS: - return (_c, _t) -> Value.NULL; - case BLOCK: - return (_c, _t) -> new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() ); - case ENTITY: - return (_c, _t) -> new EntityValue(((EntityHitResult)hitres).getEntity()); - } - return LazyValue.NULL; - });*/ - this.expr.addLazyFunction("set_biome", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0); @@ -1736,7 +1707,6 @@ public class CarpetExpression return (c_, t_) -> ListValue.wrap(retlist); }); - this.expr.addLazyFunction("query", -1, (c, t, lv) -> { if (lv.size()<2) { @@ -2516,15 +2486,16 @@ public class CarpetExpression this.expr.addLazyFunction("print", 1, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; + Value res = lv.get(0).evalValue(c); if (s.getEntity() instanceof PlayerEntity) { - Messenger.m((PlayerEntity) s.getEntity(), "w "+ lv.get(0).evalValue(c).getString()); + Messenger.m((PlayerEntity) s.getEntity(), "w "+ res.getString()); } else { - Messenger.m(s, "w "+ lv.get(0).evalValue(c).getString()); + Messenger.m(s, "w "+ res.getString()); } - return lv.get(0); // pass through for variables + return (_c, _t) -> res; // pass through for variables }); diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 2b064a22..1f0720f2 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -498,21 +498,9 @@ public class Expression implements Cloneable Value.assertNotNull(v); return lazyfun.apply(c, t, v); } - catch (InternalExpressionException exc) + catch (RuntimeException exc) { - throw new ExpressionException(e, token, exc.getMessage()); - } - catch (ArithmeticException exc) - { - throw new ExpressionException(e, token, "Your math is wrong, "+exc.getMessage()); - } - catch (ExitStatement exit) - { - throw exit; - } - catch (Exception exc) - { - throw new ExpressionException(e, token, "Error while evaluating expression: "+exc.getMessage()); + throw handleCodeException(exc, e, token); } } }); @@ -532,21 +520,9 @@ public class Expression implements Cloneable Value.assertNotNull(v1, v2); return lazyfun.apply(c, type, e, t, v1, v2); } - catch (InternalExpressionException exc) // might not actually throw it + catch (RuntimeException exc) { - throw new ExpressionException(e, t, exc.getMessage()); - } - catch (ArithmeticException exc) - { - throw new ExpressionException(e, t, "Your math is wrong, "+exc.getMessage()); - } - catch (ExitStatement exit) - { - throw exit; - } - catch (Exception exc) - { - throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage()); + throw handleCodeException(exc, e, t); } } }); @@ -565,26 +541,27 @@ public class Expression implements Cloneable Value.assertNotNull(v1, v2); return lazyfun.apply(c, t, v1, v2); } - catch (InternalExpressionException exc) + catch (RuntimeException exc) { - throw new ExpressionException(e, token, exc.getMessage()); - } - catch (ArithmeticException exc) - { - throw new ExpressionException(e, token, "Your math is wrong, "+exc.getMessage()); - } - catch (ExitStatement exit) - { - throw exit; - } - catch (Exception exc) - { - throw new ExpressionException(e, token, "Error while evaluating expression: "+exc.getMessage()); + throw handleCodeException(exc, e, token); } } }); } + private RuntimeException handleCodeException(RuntimeException exc, Expression e, Tokenizer.Token token) + { + if (exc instanceof ExitStatement) + return exc; + if (exc instanceof InternalExpressionException) + return new ExpressionException(e, token, exc.getMessage()); + if (exc instanceof ArithmeticException) + return new ExpressionException(e, token, "Your math is wrong, "+exc.getMessage()); + if (exc instanceof ExpressionException) + return exc; + return new ExpressionException(e, token, "Error while evaluating expression: "+exc.getMessage()); + } + private void addUnaryOperator(String surface, boolean leftAssoc, Function fun) { operators.put(surface+"u", new AbstractUnaryOperator(precedence.get("unary+-!"), leftAssoc) @@ -679,22 +656,10 @@ public class Expression implements Cloneable { return fun.apply(c, i, lazyParams); } - catch (InternalExpressionException exc) + catch (RuntimeException exc) { - throw new ExpressionException(e, t, exc.getMessage()); + throw handleCodeException(exc, e, t); } - catch (ArithmeticException exc) - { - throw new ExpressionException(e, t, "Your math is wrong, "+exc.getMessage()); - } - catch (ExitStatement exit) - { - throw exit; - } - //catch (Exception exc) - //{ - // throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage()); - //} } }); } @@ -770,7 +735,7 @@ public class Expression implements Cloneable { throw new ExpressionException(function_context, t, "Your math is wrong, "+exc.getMessage()); } - catch (ExitStatement exit) + catch (ExitStatement | ExpressionException exit) { throw exit; } @@ -1060,7 +1025,7 @@ public class Expression implements Cloneable Value ret = lv.get(lv.size() - 1).evalValue(c); return (cc, tt) -> ret; } - return (cc, tt) -> Value.ZERO; + return (cc, tt) -> Value.NULL; }); } @@ -1892,8 +1857,6 @@ public class Expression implements Cloneable return LazyListValue.range(from, to, step); }); - addUnaryFunction("is_list", value -> (value instanceof ListValue)? Value.TRUE : Value.FALSE); - addBinaryFunction("get", (v1, v2) -> v1.getElementAt(v2)); //Deprecated, use "get" instead @@ -2512,6 +2475,8 @@ public class Expression implements Cloneable } }); + addUnaryFunction("type", v -> new StringValue(v.getTypeString())); + addUnaryFunction("length", v -> new NumericValue(v.length())); addLazyFunction("rand", 1, (c, t, lv) -> { Value argument = lv.get(0).evalValue(c); diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index b032bae4..a73f2cb0 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -73,12 +73,10 @@ public class ScriptHost try { ServerPlayerEntity player = source.getPlayer(); - CarpetSettings.LOG.error("Retrieving "+player.getName().getString()+" host for "+getName()); return retrieveForExecution(player.getName().getString()); } catch (CommandSyntaxException e) { - CarpetSettings.LOG.error("Retrieving global host for "+getName()); return retrieveForExecution((String)null); } } diff --git a/src/main/java/carpet/script/value/BlockValue.java b/src/main/java/carpet/script/value/BlockValue.java index 460abe00..11073e6b 100644 --- a/src/main/java/carpet/script/value/BlockValue.java +++ b/src/main/java/carpet/script/value/BlockValue.java @@ -241,6 +241,12 @@ public class BlockValue extends Value return this != NULL && !getBlockState().isAir(); } + @Override + public String getTypeString() + { + return "block"; + } + @Override public Value clone() { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 037850ba..8ba290a6 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -1,6 +1,7 @@ package carpet.script.value; import carpet.fakes.MobEntityInterface; +import carpet.helpers.Tracer; import carpet.script.exception.InternalExpressionException; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; @@ -30,6 +31,9 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.hit.EntityHitResult; +import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.math.Vec3d; @@ -124,6 +128,12 @@ public class EntityValue extends Value return this.get(what, null); } + @Override + public String getTypeString() + { + return "entity"; + } + public static Pair, Predicate> getPredicate(String who) { Pair, Predicate> res = entityPredicates.get(who); @@ -310,20 +320,65 @@ public class EntityValue extends Value return new StringValue(Direction.getEntityFacingOrder(e)[index].asString()); }); + put("trace", (e, a) -> + { + float reach = 4.5f; + boolean entities = true; + boolean liquids = false; + + if (a!=null) + { + if (!(a instanceof ListValue)) + { + reach = (float) NumericValue.asNumber(a).getDouble(); + } + else + { + List args = ((ListValue) a).getItems(); + if (args.size()==0) + throw new InternalExpressionException("trace needs more arguments"); + reach = (float) NumericValue.asNumber(args.get(0)).getDouble(); + if (args.size() > 1) + { + entities = false; + for (int i = 1; i < args.size(); i++) + { + String what = args.get(i).getString(); + if (what.equalsIgnoreCase("entities")) + entities = true; + else if (what.equalsIgnoreCase("blocks")) {} + else if (what.equalsIgnoreCase("liquids")) + liquids = true; + else throw new InternalExpressionException("Incorrect tracing: "+what); + } + } + } + } + else if (e instanceof ServerPlayerEntity && ((ServerPlayerEntity) e).interactionManager.isCreative()) + { + reach = 5.0f; + } + + HitResult hitres; + if (entities) + hitres = Tracer.rayTrace(e, 1, reach, liquids); + else + hitres = Tracer.rayTraceBlocks(e, 1, reach, liquids); + + switch (hitres.getType()) + { + case MISS: return Value.NULL; + case BLOCK: return new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() ); + case ENTITY: return new EntityValue(((EntityHitResult)hitres).getEntity()); + } + return Value.NULL; + }); + put("nbt",(e, a) -> { CompoundTag nbttagcompound = e.toTag((new CompoundTag())); if (a==null) return new NBTSerializableValue(nbttagcompound);//StringValue(nbttagcompound.toString()); - NbtPathArgumentType.NbtPath path; - try - { - path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(a.getString())); - } - catch (CommandSyntaxException exc) - { - throw new InternalExpressionException("Incorrect path: "+a.getString()); - } - String res = null; + NbtPathArgumentType.NbtPath path = NBTSerializableValue.cachePath(a.getString()); try { List tags = path.get(nbttagcompound); @@ -331,7 +386,7 @@ public class EntityValue extends Value return Value.NULL; if (tags.size()==1) return NBTSerializableValue.decodeTag(tags.get(0)); - return ListValue.wrap(tags.stream().map(t -> NBTSerializableValue.decodeTag(t)).collect(Collectors.toList())); + return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList())); } catch (CommandSyntaxException ignored) { } return Value.NULL; @@ -504,9 +559,13 @@ public class EntityValue extends Value }); put("custom_name", (e, v) -> { - String name = v.getString(); - if (name.isEmpty()) + if (v instanceof NullValue) + { + e.setCustomNameVisible(false); e.setCustomName(null); + return; + } + e.setCustomNameVisible(true); e.setCustomName(new LiteralText(v.getString())); }); put("dismount", (e, v) -> e.stopRiding() ); diff --git a/src/main/java/carpet/script/value/FunctionSignatureValue.java b/src/main/java/carpet/script/value/FunctionSignatureValue.java index 2fefaf05..b54274ab 100644 --- a/src/main/java/carpet/script/value/FunctionSignatureValue.java +++ b/src/main/java/carpet/script/value/FunctionSignatureValue.java @@ -1,7 +1,7 @@ package carpet.script.value; import carpet.script.exception.ExpressionException; -import carpet.script.value.Value; +import carpet.script.exception.InternalExpressionException; import java.util.List; @@ -23,6 +23,12 @@ public class FunctionSignatureValue extends Value throw new ExpressionException("Function "+identifier+" is not defined yet"); } + @Override + public String getTypeString() + { + throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct."); + } + @Override public Value clone() { diff --git a/src/main/java/carpet/script/value/GlobalValue.java b/src/main/java/carpet/script/value/GlobalValue.java index ae51b0e3..fb2f3f71 100644 --- a/src/main/java/carpet/script/value/GlobalValue.java +++ b/src/main/java/carpet/script/value/GlobalValue.java @@ -1,6 +1,6 @@ package carpet.script.value; -import carpet.script.value.Value; +import carpet.script.exception.InternalExpressionException; public class GlobalValue extends Value { @@ -21,4 +21,10 @@ public class GlobalValue extends Value { return false; } + + @Override + public String getTypeString() + { + throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct."); + } } diff --git a/src/main/java/carpet/script/value/LazyListValue.java b/src/main/java/carpet/script/value/LazyListValue.java index 8bf16cc2..accf40a5 100644 --- a/src/main/java/carpet/script/value/LazyListValue.java +++ b/src/main/java/carpet/script/value/LazyListValue.java @@ -111,5 +111,10 @@ public abstract class LazyListValue extends ListValue implements Iterator { throw new InternalExpressionException("get element can only be obtained for regular lists"); } + @Override + public String getTypeString() + { + return "iterator"; + } } diff --git a/src/main/java/carpet/script/value/ListValue.java b/src/main/java/carpet/script/value/ListValue.java index ab7a47f6..f7ac3662 100644 --- a/src/main/java/carpet/script/value/ListValue.java +++ b/src/main/java/carpet/script/value/ListValue.java @@ -326,6 +326,11 @@ public class ListValue extends Value index = index % numitems; return items.get((int)index); } + @Override + public String getTypeString() + { + return "list"; + } diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index 0d4a6b9a..f3e822c5 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -26,8 +26,6 @@ import java.util.Map; import java.util.function.Supplier; import java.util.stream.Collectors; -import static java.lang.Math.abs; - public class NBTSerializableValue extends Value { private String nbtString = null; @@ -224,31 +222,38 @@ public class NBTSerializableValue extends Value } } - @Override - public Value getElementAt(Value value) + private static Map pathCache = new HashMap<>(); + public static NbtPathArgumentType.NbtPath cachePath(String arg) { - String pathString = value.getString(); - - //return Value.NULL; - - NbtPathArgumentType.NbtPath path; + NbtPathArgumentType.NbtPath res = pathCache.get(arg); + if (res != null) + return res; try { - path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(pathString)); + res = NbtPathArgumentType.nbtPath().method_9362(new StringReader(arg)); } catch (CommandSyntaxException exc) { - throw new InternalExpressionException("Incorrect nbt path: "+pathString); + throw new InternalExpressionException("Incorrect nbt path: "+arg); } - String res = null; + if (pathCache.size() > 1024) + pathCache.clear(); + pathCache.put(arg, res); + return res; + } + + @Override + public Value getElementAt(Value value) + { + NbtPathArgumentType.NbtPath path = cachePath(value.getString()); try { List tags = path.get(getTag()); if (tags.size()==0) return Value.NULL; if (tags.size()==1) - return new NBTSerializableValue(tags.get(0)); - return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList())); + return NBTSerializableValue.decodeTag(tags.get(0)); + return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList())); } catch (CommandSyntaxException ignored) { } return Value.NULL; @@ -259,4 +264,10 @@ public class NBTSerializableValue extends Value index = index % numitems; return items.get((int)index); */ } + + @Override + public String getTypeString() + { + return "nbt"; + } } diff --git a/src/main/java/carpet/script/value/NullValue.java b/src/main/java/carpet/script/value/NullValue.java index f7d9858b..a749a40b 100644 --- a/src/main/java/carpet/script/value/NullValue.java +++ b/src/main/java/carpet/script/value/NullValue.java @@ -38,4 +38,10 @@ public class NullValue extends NumericValue // TODO check nonsingleton code { return o instanceof NullValue ? 0 : -1; } + + @Override + public String getTypeString() + { + return "null"; + } } diff --git a/src/main/java/carpet/script/value/NumericValue.java b/src/main/java/carpet/script/value/NumericValue.java index ec7bc9b2..ddc3cbb8 100644 --- a/src/main/java/carpet/script/value/NumericValue.java +++ b/src/main/java/carpet/script/value/NumericValue.java @@ -167,4 +167,10 @@ public class NumericValue extends Value return getLong(); } + @Override + public String getTypeString() + { + return "number"; + } + } diff --git a/src/main/java/carpet/script/value/StringValue.java b/src/main/java/carpet/script/value/StringValue.java index 650d6211..3db05b75 100644 --- a/src/main/java/carpet/script/value/StringValue.java +++ b/src/main/java/carpet/script/value/StringValue.java @@ -24,4 +24,10 @@ public class StringValue extends Value { this.str = str; } + + @Override + public String getTypeString() + { + return "number"; + } } diff --git a/src/main/java/carpet/script/value/Value.java b/src/main/java/carpet/script/value/Value.java index 454b5871..35c94827 100644 --- a/src/main/java/carpet/script/value/Value.java +++ b/src/main/java/carpet/script/value/Value.java @@ -177,4 +177,6 @@ public abstract class Value implements Comparable, Cloneable { throw new InternalExpressionException("get element can only be obtained for lists and nbt values"); } + + public abstract String getTypeString(); } diff --git a/src/main/java/carpet/settings/CarpetSettings.java b/src/main/java/carpet/settings/CarpetSettings.java index 6b2138be..842498e3 100644 --- a/src/main/java/carpet/settings/CarpetSettings.java +++ b/src/main/java/carpet/settings/CarpetSettings.java @@ -211,6 +211,9 @@ public class CarpetSettings @Rule(desc = "Enables /player command to control/spawn players", category = COMMAND) public static boolean commandPlayer = true; + @Rule(desc = "Allows to track mobs AI via /track command", category = COMMAND) + public static boolean commandTrackAI = true; + @Rule(desc = "Placing carpets may issue carpet commands for non-op players", category = SURVIVAL) public static boolean carpets = false; diff --git a/src/main/java/carpet/utils/MobAI.java b/src/main/java/carpet/utils/MobAI.java new file mode 100644 index 00000000..76f94401 --- /dev/null +++ b/src/main/java/carpet/utils/MobAI.java @@ -0,0 +1,93 @@ +package carpet.utils; + +import carpet.CarpetServer; +import com.google.common.collect.Sets; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.registry.Registry; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class MobAI +{ + private static Map> aiTrackers = new HashMap<>(); + + public static void resetTrackers() + { + aiTrackers.clear(); + } + + public static boolean isTracking(Entity e, TrackingType type) + { + Set currentTrackers = aiTrackers.get(e.getType()); + if (currentTrackers == null) + return false; + return currentTrackers.contains(type); + } + + public static void clearTracking(EntityType etype) + { + aiTrackers.remove(etype); + for(ServerWorld world : CarpetServer.minecraft_server.getWorlds() ) + { + for (Entity e: world.getEntities(etype, Entity::hasCustomName)) + { + e.setCustomNameVisible(false); + e.setCustomName(null); + } + } + } + + public static void startTracking(EntityType e, TrackingType type) + { + aiTrackers.putIfAbsent(e,Sets.newHashSet()); + aiTrackers.get(e).add(type); + } + + public static List availbleTypes() + { + Set types = new HashSet<>(); + for (TrackingType type: TrackingType.values()) + { + types.addAll(type.types); + } + return types.stream().map(t -> Registry.ENTITY_TYPE.getId(t).getPath()).collect(Collectors.toList()); + } + + public static List availableFor(EntityType entityType) + { + Set availableOptions = new HashSet<>(); + for (TrackingType type: TrackingType.values()) + for (EntityType etype: type.types) + if (etype == entityType) + availableOptions.add(type); + return availableOptions.stream().map(t -> t.name).collect(Collectors.toList()); + } + + public enum TrackingType + { + IRON_GOLEM_SPAWNING("iron_golem_spawning", Sets.newHashSet(EntityType.VILLAGER)); + public Set types; + public String name; + TrackingType(String name, Set applicableTypes) + { + this.name = name; + types = applicableTypes; + } + + public static TrackingType byName(String aspect) + { + for (TrackingType type: values()) + if (type.name.equalsIgnoreCase(aspect)) + return type; + return null; + } + } + +} diff --git a/src/main/resources/carpet.mixins.json b/src/main/resources/carpet.mixins.json index 2b96c42e..e137da04 100644 --- a/src/main/resources/carpet.mixins.json +++ b/src/main/resources/carpet.mixins.json @@ -27,6 +27,8 @@ "ServerWorld_unloadedEntityMixin", "MobEntity_leashMixin", + "VillagerEntity_aiMixin", + "ServerWorld_fakePlayersMixin", "ServerPlayNetworkHandler_coreMixin", From 34bfe2e99687bfb59ee04db6dbc45090a4641500 Mon Sep 17 00:00:00 2001 From: gnembon Date: Sat, 20 Jul 2019 19:05:11 -0300 Subject: [PATCH 5/6] host tweaks --- .../java/carpet/commands/ScriptCommand.java | 2 +- .../carpet/script/CarpetScriptServer.java | 30 ++++++++----------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index b38efe75..033a6dc1 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -211,7 +211,7 @@ public class ScriptCommand boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package"), true); return success?1:0; }). - then(literal("global"). + then(literal("shared"). executes((cc) -> { boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package"), false); diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index 716870a2..142bfd6f 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -56,6 +56,17 @@ public class CarpetScriptServer ModuleInterface getModule(String name) { + File folder = CarpetServer.minecraft_server.getLevelStorage().resolveFile( + CarpetServer.minecraft_server.getLevelName(), "scripts"); + File[] listOfFiles = folder.listFiles(); + if (listOfFiles != null) + for (File script : listOfFiles) + { + if (script.getName().equalsIgnoreCase(name+".sc")) + { + return new FileModule(script); + } + } for (ModuleInterface moduleData : bundledModuleData) { if (moduleData.getName().equalsIgnoreCase(name)) @@ -63,19 +74,6 @@ public class CarpetScriptServer return moduleData; } } - - File folder = CarpetServer.minecraft_server.getLevelStorage().resolveFile( - CarpetServer.minecraft_server.getLevelName(), "scripts"); - File[] listOfFiles = folder.listFiles(); - if (listOfFiles == null) - return null; - for (File script : listOfFiles) - { - if (script.getName().equalsIgnoreCase(name+".sc")) - { - return new FileModule(script); - } - } return null; } @@ -84,8 +82,7 @@ public class CarpetScriptServer List moduleNames = new ArrayList<>(); for (ModuleInterface mi: bundledModuleData) { - if (!modules.containsKey(mi.getName())) - moduleNames.add(mi.getName()); + moduleNames.add(mi.getName()); } File folder = CarpetServer.minecraft_server.getLevelStorage().resolveFile( CarpetServer.minecraft_server.getLevelName(), "scripts"); @@ -97,8 +94,7 @@ public class CarpetScriptServer if (script.getName().endsWith(".sc")) { String name = script.getName().replaceFirst("\\.sc","").toLowerCase(Locale.ROOT); - if (!modules.containsKey(name)) - moduleNames.add(name); + moduleNames.add(name); } } return moduleNames; From 64aca650cf5461dc95dbd622d80dd1d11273fe75 Mon Sep 17 00:00:00 2001 From: gnembon Date: Sat, 20 Jul 2019 19:14:03 -0300 Subject: [PATCH 6/6] host tweaks --- src/main/java/carpet/script/value/NBTSerializableValue.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index f3e822c5..efb616ee 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -257,12 +257,6 @@ public class NBTSerializableValue extends Value } catch (CommandSyntaxException ignored) { } return Value.NULL; - - /*int numitems = items.size(); - long range = abs(index)/numitems; - index += (range+2)*numitems; - index = index % numitems; - return items.get((int)index); */ } @Override