Fix more warnings and do some small optimizations (#1356)

* Fix more warnings and do some small optimizations

* Fix more warnings in ValueConversions

* Clean up some decompiler names and fix an unnecessary transformation around commands

* Use (way) more compact data structure for slot map

* Minor tweaks
Such as map directly in the collector

* I hate vanilla javac
Revert map changes in latest commit
This commit is contained in:
altrisi authored and GitHub committed 2022-05-27 19:43:45 +02:00
1 parent e4dc080657
commit f18cfb09d2
18 files changed
+116 -139

No files matched your search

@@ -5,6 +5,8 @@ import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.utils.BlockInfo;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
@@ -55,12 +57,6 @@ public class ValueConversions
return ListValue.of(new NumericValue(pos.getX()), new NumericValue(pos.getY()), new NumericValue(pos.getZ()));
}
public static Value ofOptional(BlockPos pos)
{
if (pos == null) return Value.NULL;
return ListValue.of(new NumericValue(pos.getX()), new NumericValue(pos.getY()), new NumericValue(pos.getZ()));
}
public static Value of(Vec3 vec)
{
return ListValue.of(new NumericValue(vec.x), new NumericValue(vec.y), new NumericValue(vec.z));
@@ -275,17 +271,17 @@ public class ValueConversions
{
v = new ArrayList(((Set) v));
}
if (v instanceof List l)
if (v instanceof List<?> l)
{
if (l.isEmpty()) return ListValue.of();
Object el = l.get(0);
if (el instanceof Entity)
{
return ListValue.wrap((List<Value>) l.stream().map(o -> new EntityValue((Entity)o)).collect(Collectors.toList()));
return ListValue.wrap(l.stream().map(o -> new EntityValue((Entity)o)).collect(Collectors.toList()));
}
if (el instanceof GlobalPos)
{
return ListValue.wrap((List<Value>) l.stream().map(o -> of((GlobalPos) o)).collect(Collectors.toList()));
return ListValue.wrap(l.stream().map(o -> of((GlobalPos) o)).collect(Collectors.toList()));
}
}
return Value.NULL;
@@ -350,49 +346,53 @@ public class ValueConversions
throw new InternalExpressionException("Unknown property type: "+p.getName());
}
record SlotParam(/* Nullable */ String type, int id) {
public ListValue build() {
return ListValue.of(StringValue.of(type), new NumericValue(id));
}
}
private static final Map<Integer, ListValue> slotIdsToSlotParams = new HashMap<Integer, ListValue>() {{
private static final Int2ObjectMap<SlotParam> slotIdsToSlotParams = new Int2ObjectOpenHashMap<>() {{
int n;
//covers blocks, player hotbar and inventory, and all default inventories
for(n = 0; n < 54; ++n) {
put(n, ListValue.of(Value.NULL, NumericValue.of(n)));
put(n, new SlotParam(null, n));
}
for(n = 0; n < 27; ++n) {
put(200+n, ListValue.of(StringValue.of("enderchest"), NumericValue.of(n)));
put(200 + n, new SlotParam("enderchest", n));
}
// villager
for(n = 0; n < 8; ++n) {
put(300+n, ListValue.of(Value.NULL, NumericValue.of(n)));
put(300 + n, new SlotParam(null, n));
}
// horse, llamas, donkeys, etc.
// two first slots are for saddle and armour
for(n = 0; n < 15; ++n) {
put(500+n, ListValue.of(Value.NULL, NumericValue.of(n+2)));
put(500 + n, new SlotParam(null, n + 2));
}
Value equipment = StringValue.of("equipment");
// weapon main hand
put(98, ListValue.of(equipment, NumericValue.of(0)));
put(98, new SlotParam("equipment", 0));
// offhand
put(99, ListValue.of(equipment, NumericValue.of(5)));
put(99, new SlotParam("equipment", 5));
// feet, legs, chest, head
for(n = 0; n < 4; ++n) {
put(100+n, ListValue.of(equipment, NumericValue.of(n+1)));
put(100 + n, new SlotParam("equipment", n + 1));
}
//horse defaults saddle
put(400, ListValue.of(Value.NULL, NumericValue.of(0)));
put(400, new SlotParam(null, 0));
// armor
put(401, ListValue.of(Value.NULL, NumericValue.of(1)));
put(401, new SlotParam(null, 1));
// chest itself on the donkey is wierd - use NBT to alter that.
//hashMap.put("horse.chest", 499);
}};
public static Value ofVanillaSlotResult(int itemSlot)
{
Value ret = slotIdsToSlotParams.get(itemSlot);
if (ret == null) return ListValue.of(Value.NULL, NumericValue.of(itemSlot));
return ret;
SlotParam ret = slotIdsToSlotParams.get(itemSlot);
if (ret == null) return ListValue.of(Value.NULL, new NumericValue(itemSlot));
return ret.build();
}
public static Value ofBlockPredicate(RegistryAccess registryAccess, Predicate<BlockInWorld> blockPredicate)