two more events, and nbt cleanups

This commit is contained in:
gnembon committed 2019-10-06 22:06:38 -03:00
1 parent dab9c5debe
commit 2167c929d5
10 files changed
+462 -207

No files matched your search

+262 -164
View File
@@ -7,13 +7,10 @@ import carpet.script.value.ListValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import carpet.settings.CarpetSettings;
import net.minecraft.block.BlockState;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
@@ -21,9 +18,6 @@ import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import java.util.ArrayList;
import java.util.Arrays;
@@ -36,10 +30,11 @@ import java.util.Map;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class CarpetEventServer
{
public List<ScheduledCall> scheduledCalls = new LinkedList<>();
public static class Callback
{
public String host;
@@ -146,9 +141,262 @@ public class CarpetEventServer
}
}
public Map<String, CallbackList> eventHandlers = new HashMap<>();
public enum Event
{
TICK("tick", new CallbackList(0))
{
@Override
public void onTick()
{
handler.call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
}
},
NETHER_TICK("tick_nether",new CallbackList(0))
{
@Override
public void onTick()
{
handler.call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
}
},
ENDER_TICK("tick_ender",new CallbackList(0))
{
@Override
public void onTick()
{
handler.call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
}
},
PLAYER_JUMPS("player_jumps", new CallbackList(1))
{
@Override
public void onPlayerEvent(ServerPlayerEntity player)
{
handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
},
PLAYER_DEPLOYS_ELYTRA("player_deploys_elytra",new CallbackList(1))
{
@Override
public void onPlayerEvent(ServerPlayerEntity player)
{
handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
},
PLAYER_WAKES_UP("player_wakes_up",new CallbackList(1))
{
@Override
public void onPlayerEvent(ServerPlayerEntity player)
{
handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
},
PLAYER_RIDES("player_rides",new CallbackList(5))
{
@Override
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))
), player::getCommandSource);
}
},
PLAYER_USES_ITEM("player_uses_item",new CallbackList(3))
{
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
{
handler.call( () ->
{
//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"))
);
}, player::getCommandSource);
}
},
PLAYER_CLICKS_BLOCK("player_clicks_block",new CallbackList(3))
{
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
{
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"))
);
}, player::getCommandSource);
}
},
PLAYER_RIGHT_CLICKS_BLOCK("player_right_clicks_block",new CallbackList(6))
{
@Override
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)//ItemStack itemstack, Hand enumhand, BlockPos blockpos, Direction enumfacing, Vec3d vec3d)
{
handler.call( () ->
{
ItemStack itemstack = player.getStackInHand(enumhand);
BlockPos blockpos = hitRes.getBlockPos();
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 NumericValue(vec3d.x),
new NumericValue(vec3d.y),
new NumericValue(vec3d.z)
))
);
}, player::getCommandSource);
}
},
PLAYER_BREAK_BLOCK("player_breaks_block",new CallbackList(2))
{
@Override
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))
), player::getCommandSource);
}
},
PLAYER_INERACTSW_WITH_ENTITY("player_interacts_with_entity",new CallbackList(3))
{
@Override
public void onEntityAction(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"))
), player::getCommandSource);
}
},
PLAYER_ATTACKS_ENTITY("player_attacks_entity",new CallbackList(2))
{
@Override
public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand)
{
handler.call( () -> Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new EntityValue(entity))
), player::getCommandSource);
}
},
PLAYER_STARTS_SNEAKING("player_starts_sneaking",new CallbackList(1))
{
@Override
public void onPlayerEvent(ServerPlayerEntity player)
{
handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
},
PLAYER_STOPS_SNEAKING("player_stops_sneaking",new CallbackList(1))
{
@Override
public void onPlayerEvent(ServerPlayerEntity player)
{
handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
},
PLAYER_STARTS_SPRINTING("player_starts_sprinting",new CallbackList(1))
{
@Override
public void onPlayerEvent(ServerPlayerEntity player)
{
handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
},
PLAYER_STOPS_SPRINTING("player_stops_sprinting",new CallbackList(1))
{
@Override
public void onPlayerEvent(ServerPlayerEntity player)
{
handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
},
public List<ScheduledCall> scheduledCalls = new LinkedList<>();
PLAYER_RELEASED_ITEM("player_released_item",new CallbackList(3))
{
@Override
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
{
// this.getStackInHand(this.getActiveHand()), this.activeItemStack)
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"))
);
}, player::getCommandSource);
}
},
PLAYER_FINISHED_USING_ITEM("player_finished_using_item",new CallbackList(3))
{
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
{
// this.getStackInHand(this.getActiveHand()), this.activeItemStack)
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"))
);
}, player::getCommandSource);
}
};
// on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls
public String name;
public static Map<String, Event> byName = new HashMap<String, Event>(){{
for (Event e: Event.values())
{
put(e.name, e);
}
}};
public CallbackList handler;
Event(String name, CallbackList eventHandler)
{
this.name = name;
this.handler = eventHandler;
}
public boolean isNeeded()
{
return handler.callList.size() > 0;
}
//stubs for calls just to ease calls in vanilla code so they don't need to deal with scarpet value types
public void onTick() { }
public void onPlayerEvent(ServerPlayerEntity player) { }
public void onMountControls(ServerPlayerEntity player, float strafeSpeed, float forwardSpeed, boolean jumping, boolean sneaking) { }
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) { }
public void onBlockAction(ServerPlayerEntity player, BlockPos blockpos, Direction facing) { }
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes) { }
public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS) { }
public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand) { }
}
public CarpetEventServer()
{
for (Event e: Event.values())
e.handler.callList.clear();
}
public void tick()
{
@@ -175,50 +423,28 @@ public class CarpetEventServer
scheduledCalls.add(new ScheduledCall(context, function, args, due));
}
public CarpetEventServer()
{
eventHandlers.put("tick",new CallbackList(0));
eventHandlers.put("tick_nether",new CallbackList(0));
eventHandlers.put("tick_ender",new CallbackList(0));
eventHandlers.put("player_jumps",new CallbackList(1));
eventHandlers.put("player_deploys_elytra",new CallbackList(1));
eventHandlers.put("player_wakes_up",new CallbackList(1));
eventHandlers.put("player_rides",new CallbackList(5));
eventHandlers.put("player_uses_item",new CallbackList(3));
eventHandlers.put("player_clicks_block",new CallbackList(3));
eventHandlers.put("player_right_clicks_block",new CallbackList(6));
eventHandlers.put("player_breaks_block",new CallbackList(2));
eventHandlers.put("player_interacts_with_entity",new CallbackList(3));
eventHandlers.put("player_attacks_entity",new CallbackList(2));
eventHandlers.put("player_starts_sneaking",new CallbackList(1));
eventHandlers.put("player_stops_sneaking",new CallbackList(1));
eventHandlers.put("player_starts_sprinting",new CallbackList(1));
eventHandlers.put("player_stops_sprinting",new CallbackList(1));
}
public boolean addEvent(String event, String host, String funName)
{
if (!eventHandlers.containsKey(event))
if (!Event.byName.containsKey(event))
{
return false;
}
return eventHandlers.get(event).addEventCall(host, funName);
return Event.byName.get(event).handler.addEventCall(host, funName);
}
public boolean removeEvent(String event, String funName)
{
if (!eventHandlers.containsKey(event))
if (!Event.byName.containsKey(event))
return false;
Callback callback= decodeCallback(funName);
eventHandlers.get(event).removeEventCall(callback.host, callback.udf);
Event.byName.get(event).handler.removeEventCall(callback.host, callback.udf);
return true;
}
public void removeAllHostEvents(String hostName)
{
eventHandlers.forEach((e, a) -> a.removeAllCalls(hostName));
Event.byName.values().forEach((e) -> e.handler.removeAllCalls(hostName));
}
private Callback decodeCallback(String funName)
@@ -250,131 +476,6 @@ public class CarpetEventServer
return new ScheduledCall(cc, function, lazyArgs, 0);
}
public void onTick()
{
eventHandlers.get("tick").call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
eventHandlers.get("tick_nether").call(Collections::emptyList, () ->
CarpetServer.minecraft_server.getCommandSource().withWorld(CarpetServer.minecraft_server.getWorld(DimensionType.THE_NETHER)));
eventHandlers.get("tick_ender").call(Collections::emptyList, () ->
CarpetServer.minecraft_server.getCommandSource().withWorld(CarpetServer.minecraft_server.getWorld(DimensionType.THE_END)));
}
public void onPlayerJumped(PlayerEntity player)
{
eventHandlers.get("player_jumps").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public void onElytraDeploy(ServerPlayerEntity player)
{
eventHandlers.get("player_deploys_elytra").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public void onOutOfBed(ServerPlayerEntity player)
{
eventHandlers.get("player_wakes_up").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public void onMountControls(PlayerEntity player, float strafeSpeed, float forwardSpeed, boolean jumping, boolean sneaking)
{
eventHandlers.get("player_rides").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))
), player::getCommandSource);
}
public void onRightClick(ServerPlayerEntity player, Hand enumhand)
{
eventHandlers.get("player_uses_item").call( () ->
{
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"))
);
}, player::getCommandSource);
}
public void onBlockClicked(ServerPlayerEntity player, BlockPos blockpos, Direction facing)
{
eventHandlers.get("player_clicks_block").call( () -> Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)),
((c, t) -> new StringValue(facing.getName()))
), player::getCommandSource);
}
public void onRightClickBlock(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)//ItemStack itemstack, Hand enumhand, BlockPos blockpos, Direction enumfacing, Vec3d vec3d)
{
eventHandlers.get("player_right_clicks_block").call( () ->
{
ItemStack itemstack = player.getStackInHand(enumhand);
BlockPos blockpos = hitRes.getBlockPos();
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 NumericValue(vec3d.x),
new NumericValue(vec3d.y),
new NumericValue(vec3d.z)
))
);
}, player::getCommandSource);
}
public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS)
{
eventHandlers.get("player_breaks_block").call( () -> Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new BlockValue(previousBS, player.getServerWorld(), pos))
), player::getCommandSource);
}
public void onEntityInteracted(ServerPlayerEntity player, Entity entity, Hand enumhand)
{
eventHandlers.get("player_interacts_with_entity").call( () -> Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new EntityValue(entity)),
((c, t) -> new StringValue(enumhand==Hand.MAIN_HAND?"mainhand":"offhand"))
), player::getCommandSource);
}
public void onEntityAttacked(ServerPlayerEntity player, Entity entity)
{
eventHandlers.get("player_attacks_entity").call( () -> Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new EntityValue(entity))
), player::getCommandSource);
}
public void onStartSneaking(ServerPlayerEntity player)
{
eventHandlers.get("player_starts_sneaking").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public void onStopSneaking(ServerPlayerEntity player)
{
eventHandlers.get("player_stops_sneaking").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public void onStartSprinting(ServerPlayerEntity player)
{
eventHandlers.get("player_starts_sprinting").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public void onStopSprinting(ServerPlayerEntity player)
{
eventHandlers.get("player_stops_sprinting").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
public ScheduledCall makeDeathCall(CarpetContext cc, String function, List<Value> extraArgs)
{
return makeEventCall(cc, function, extraArgs, 2);
@@ -413,7 +514,4 @@ public class CarpetEventServer
(c, t) -> source.getAttacker()==null?Value.NULL:new EntityValue(source.getAttacker())
));
}
}
@@ -347,7 +347,7 @@ public class CarpetExpression
* <h1>Blocks / World API</h1>
* <div style="padding-left: 20px; border-radius: 5px 45px; border:1px solid grey;">
* <h2>Specifying blocks</h2>
* <h3><code>block(x, y, z), block(state)</code></h3>
* <h3><code>block(x, y, z), block(l(x,y,z)), block(state)</code></h3>
* <p>Returns either a block from specified location, or block with a specific state
* (as used by <code>/setblock</code> command), so allowing for block properties, block entity data etc.
* Blocks otherwise can be referenced everywhere by its simple string name, but its only used in its default state</p>
@@ -1189,6 +1189,8 @@ public class CarpetExpression
Value nbtValue = lv.get(inventoryLocator.offset+3).evalValue(c);
if (nbtValue instanceof NBTSerializableValue)
nbt = ((NBTSerializableValue)nbtValue).getCompoundTag();
else if (nbtValue instanceof NullValue)
nbt = null;
else
nbt = new NBTSerializableValue(nbtValue.getString()).getCompoundTag();
}
@@ -1251,7 +1253,7 @@ public class CarpetExpression
if (inventoryLocator == null)
return (_c, _t) -> Value.NULL;
if (lv.size() <= inventoryLocator.offset)
throw new InternalExpressionException("inventory_set requires at least an item to be removed");
throw new InternalExpressionException("inventory_remove requires at least an item to be removed");
ItemStackArgument searchItem = NBTSerializableValue.parseItem(lv.get(inventoryLocator.offset).evalValue(c).getString());
int amount = 1;
if (lv.size() > inventoryLocator.offset+1)
@@ -1540,10 +1542,9 @@ public class CarpetExpression
* that corresponds to the path. For specification of <code>path</code> attribute, consult
* vanilla <code>/data get entity</code> command.</p>
* <p>Note that calls to <code>nbt</code> are considerably more expensive comparing to other
* calls in Minecraft API, and should only be used when there is no other option. Also returned
* NBT is just a string to any retrieval of information post can currently only be done with matching
* operator <code>~</code>. With time we are hoping not to support the <code>'nbt'</code> call better,
* but rather to fill the API, so that <code>'nbt'</code> calls are not needed</p>
* calls in Minecraft API, and should only be used when there is no other option.A returned value is of type <code>nbt</code>,
* which can be further manipulated with nbt type objects via <code>get, put, has, delete</code>, so try to use API calls
* first</p>
* <h2>Entity Modification</h2>
* <p>Like with entity querying, entity modifications happen through one function. Most position and movements
* modifications don't work currently on players as their position is controlled by clients.</p>
@@ -1724,8 +1725,12 @@ public class CarpetExpression
boolean hasTag = false;
if (lv.size() > position.offset)
{
hasTag = true;
tag = new NBTSerializableValue(lv.get(position.offset).evalValue(c).getString()).getCompoundTag();
NBTSerializableValue v = NBTSerializableValue.parseString(lv.get(position.offset).evalValue(c).getString());
if (v != null)
{
hasTag = true;
tag = v.getCompoundTag();
}
}
tag.putString("id", entityId.toString());
Vec3d vec3d = position.vec;
@@ -2355,8 +2360,7 @@ public class CarpetExpression
* <h2>System function</h2>
* <h3><code>nbt(expr)</code></h3>
* <p>Treats the argument as a nbt serializable string and returns its nbt value.
* In case nbt is not in a correct nbt compound tag format, the execution of a script will stop with an error,
* whenever that tag is used.</p>
* In case nbt is not in a correct nbt compound tag format, it will return <code>null</code> value.</p>
* <h3><code>print(expr)</code></h3>
* <p>Displays the result of the expression to the chat. Overrides default <code>scarpet</code> behaviour of
* sending everyting to stderr.</p>
@@ -2376,11 +2380,11 @@ public class CarpetExpression
* loop(1000,tick(100)) // runs the game twice as slow for 1000 ticks
* </pre>
* <h3><code>current_dimension()</code></h3>
* <p>Returns current dimension that scripts run in.</p>
* <p>Returns current dimension that the script runs in.</p>
* <h3><code>in_dimension(smth, expr)</code></h3>
* <p>Evaluates the expression <code>expr</code> with different dimension execution context. <code>smth</code> can
* be an entity, world-localized block, so not <code>block('stone')</code>, or a string representing a dimension like:
* <code>'nether'</code>, <code>'end'</code> or <code>'overworld'</code>.</p>
* <code>'nether'</code>, <code>'the_nether'</code>, <code>'end'</code> or <code>'overworld'</code>, etc.</p>
* <h3><code>schedule(delay, function, args...)</code></h3>
* <p>Schedules a user defined function to run with a specified <code>delay</code> ticks of delay.
* Scheduled functions run at the end of the tick, and they will run in order they were scheduled.</p>
@@ -2677,7 +2681,9 @@ public class CarpetExpression
Value v = lv.get(0).evalValue(c);
if (v instanceof NBTSerializableValue)
return (cc, tt) -> v;
Value ret = new NBTSerializableValue(v.getString());
Value ret = NBTSerializableValue.parseString(v.getString());
if (ret == null)
return LazyValue.NULL;
return (cc, tt) -> ret;
});
@@ -200,7 +200,7 @@ public class CarpetScriptServer
if (!fun.startsWith("__on_"))
continue;
String event = fun.replaceFirst("__on_","");
if (!events.eventHandlers.containsKey(event))
if (!CarpetEventServer.Event.byName.containsKey(event))
continue;
events.addEvent(event, hostName, fun);
}
@@ -27,6 +27,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class NBTSerializableValue extends Value implements ContainerValueInterface
@@ -55,6 +57,22 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
};
}
public static NBTSerializableValue parseString(String nbtString)
{
Tag tag;
try
{
tag = (new StringNbtReader(new StringReader(nbtString))).parseTag();
}
catch (CommandSyntaxException e)
{
return null;
}
NBTSerializableValue value = new NBTSerializableValue(tag);
value.nbtString = nbtString;
return value;
}
public NBTSerializableValue(Tag tag)
{
nbtTag = tag;
@@ -261,10 +279,15 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
}
private Tag modify_insert(int index, NbtPathArgumentType.NbtPath nbtPath, Tag newElement)
{
return modify_insert(index, nbtPath, newElement, this.getTag().copy());
}
private Tag modify_insert(int index, NbtPathArgumentType.NbtPath nbtPath, Tag newElement, Tag currentTag)
{
Collection<Tag> targets;
Tag currentTag = getTag().copy();
try
{
targets = nbtPath.putIfAbsent(currentTag, ListTag::new);
@@ -284,7 +307,8 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
try
{
AbstractListTag<?> targetList = (AbstractListTag) target;
targetList.addTag(index < 0 ? targetList.size() + index + 1 : index, newElement.copy());
if (!targetList.addTag(index < 0 ? targetList.size() + index + 1 : index, newElement.copy()))
return null;
modified = true;
}
catch (IndexOutOfBoundsException ignored)
@@ -320,6 +344,35 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
private Tag modify_replace(NbtPathArgumentType.NbtPath nbtPath, Tag replacement) //nbtPathArgumentType$NbtPath_1, list_1)
{
Tag originalTag = getTag().copy();
String pathText = nbtPath.toString();
if (pathText.endsWith("]")) // workaround for array replacement or item in the array replacement
{
if (nbtPath.remove(originalTag)==0)
return null;
Pattern pattern = Pattern.compile("\\[[^\\[]*]$");
Matcher matcher = pattern.matcher(pathText);
if (!matcher.find()) // malformed path
{
return null;
}
String arrAccess = matcher.group();
int pos;
if (arrAccess.length()==2) // we just removed entire array
pos = 0;
else
{
try
{
pos = Integer.parseInt(arrAccess.substring(1, arrAccess.length() - 1));
}
catch (NumberFormatException e)
{
return null;
}
}
NbtPathArgumentType.NbtPath newPath = cachePath(pathText.substring(0, pathText.length()-arrAccess.length()));
return modify_insert(pos,newPath,replacement, originalTag);
}
try
{
nbtPath.put(originalTag, () -> replacement);