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

@@ -25,7 +25,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@@ -983,11 +982,9 @@ public class CarpetEventServer
}
@Deprecated
public static final Map<EntityType<? extends Entity>, Event> ENTITY_LOAD= new HashMap<>()
{{
EntityType.byString("zombie");
Registry.ENTITY_TYPE.forEach(et -> {
put(et, new Event(getEntityLoadEventName(et), 1, true, false)
public static final Map<EntityType<? extends Entity>, Event> ENTITY_LOAD = Registry.ENTITY_TYPE
.stream()
.map(et -> Map.entry(et, new Event(getEntityLoadEventName(et), 1, true, false)
{
@Override
public void onEntityAction(Entity entity, boolean created)
@@ -997,32 +994,25 @@ public class CarpetEventServer
() -> CarpetServer.minecraft_server.createCommandSourceStack().withLevel((ServerLevel) entity.level).withPermission(CarpetSettings.runPermissionLevel)
);
}
});
});
}};
})).collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
public static String getEntityHandlerEventName(EntityType<? extends Entity> et)
{
return "entity_handler_" + ValueConversions.of(Registry.ENTITY_TYPE.getKey(et)).getString();
}
public static final Map<EntityType<? extends Entity>, Event> ENTITY_HANDLER= new HashMap<>()
{{
EntityType.byString("zombie");
Registry.ENTITY_TYPE.forEach(et -> {
put(et, new Event(getEntityHandlerEventName(et), 2, true, false)
{
public static final Map<EntityType<? extends Entity>, Event> ENTITY_HANDLER = Registry.ENTITY_TYPE
.stream()
.map(et -> Map.entry(et, new Event(getEntityHandlerEventName(et), 2, true, false) {
@Override
public void onEntityAction(Entity entity, boolean created)
{
public void onEntityAction(Entity entity, boolean created) {
handler.call(
() -> Arrays.asList(new EntityValue(entity), BooleanValue.of(created)),
() -> CarpetServer.minecraft_server.createCommandSourceStack().withLevel((ServerLevel) entity.level).withPermission(CarpetSettings.runPermissionLevel)
);
}
});
});
}};
}))
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
// on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls
@@ -21,12 +21,12 @@ import carpet.script.value.ScreenValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import carpet.script.value.ValueConversions;
import com.google.common.collect.Sets;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.core.HolderSet;
@@ -336,7 +336,7 @@ public class Inventories {
if (lv.size() > inventoryLocator.offset()+1)
amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset()+1)).getLong();
// not enough
if (((amount == 1) && (!inventoryLocator.inventory().hasAnyOf(Sets.newHashSet(searchItem.getItem()))))
if (((amount == 1) && (!inventoryLocator.inventory().hasAnyOf(Set.of(searchItem.getItem()))))
|| (inventoryLocator.inventory().countItem(searchItem.getItem()) < amount)) return Value.FALSE;
for (int i = 0, maxi = inventoryLocator.inventory().getContainerSize(); i < maxi; i++)
{
@@ -134,11 +134,11 @@ public class WorldAccess {
DIRECTION_MAP.put("x", Direction.EAST);
}
private final static Map<String, TicketType<?>> ticketTypes = new HashMap<String, TicketType<?>>(){{
put("portal", TicketType.PORTAL);
put("teleport", TicketType.POST_TELEPORT);
put("unknown", TicketType.UNKNOWN); // unknown
}};
private static final Map<String, TicketType<?>> ticketTypes = Map.of(
"portal", TicketType.PORTAL,
"teleport", TicketType.POST_TELEPORT,
"unknown", TicketType.UNKNOWN
);
// dummy entity for dummy requirements in the loot tables (see snowball)
private static FallingBlockEntity DUMMY_ENTITY = null;
private static Value booleanStateTest(
@@ -515,7 +515,7 @@ public class FileArgument
{
try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8))
{
return new JsonParser().parse(new JsonReader(reader));
return JsonParser.parseReader(reader);
}
catch (JsonParseException e)
{
@@ -3,10 +3,10 @@ package carpet.script.language;
import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.LazyValue;
import carpet.script.api.Auxiliary;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.script.utils.ScarpetJsonDeserializer;
import carpet.script.value.BooleanValue;
import carpet.script.value.ContainerValueInterface;
import carpet.script.value.LContainerValue;
@@ -341,7 +341,7 @@ public class DataStructures {
expression.addUnaryFunction("encode_json", v -> StringValue.of(v.toJson().toString()));
expression.addUnaryFunction("decode_json", v -> {
try {
return new ScarpetJsonDeserializer().deserialize(new JsonParser().parse(v.getString()), null, null);
return Auxiliary.GSON.fromJson(v.getString(), Value.class);
} catch (JsonParseException jpe){
throw new ThrowStatement("Invalid json string: " + v.getString(), Throwables.JSON_ERROR);
}
@@ -15,7 +15,6 @@ import carpet.script.EntityEventsGroup;
import carpet.script.argument.Vector3Argument;
import carpet.script.exception.InternalExpressionException;
import carpet.script.utils.InputValidator;
import com.google.common.collect.Sets;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
@@ -302,25 +301,25 @@ public class EntityValue extends Value
EntityClassDescriptor(EntityType<?> type, Predicate<? super Entity> predicate, Stream<EntityType<?>> types)
{
this(type, predicate, types.collect(Collectors.toList()));
this(type, predicate, types.toList());
}
EntityClassDescriptor(EntityTypeTest<Entity, ?> type, Predicate<? super Entity> predicate, Stream<EntityType<?>> types)
{
this(type, predicate, types.collect(Collectors.toList()));
this(type, predicate, types.toList());
}
public final static Map<String, EntityClassDescriptor> byName = new HashMap<String, EntityClassDescriptor>() {{
List<EntityType<?>> allTypes = Registry.ENTITY_TYPE.stream().collect(Collectors.toList());
public final static Map<String, EntityClassDescriptor> byName = new HashMap<>() {{
List<EntityType<?>> allTypes = Registry.ENTITY_TYPE.stream().toList();
// nonliving types
Set<EntityType<?>> projectiles = Sets.newHashSet(
Set<EntityType<?>> projectiles = Set.of(
EntityType.ARROW, EntityType.DRAGON_FIREBALL, EntityType.FIREWORK_ROCKET,
EntityType.FIREBALL, EntityType.LLAMA_SPIT, EntityType.SMALL_FIREBALL,
EntityType.SNOWBALL, EntityType.SPECTRAL_ARROW, EntityType.EGG,
EntityType.ENDER_PEARL, EntityType.EXPERIENCE_BOTTLE, EntityType.POTION,
EntityType.TRIDENT, EntityType.WITHER_SKULL, EntityType.FISHING_BOBBER, EntityType.SHULKER_BULLET
);
Set<EntityType<?>> deads = Sets.newHashSet(
Set<EntityType<?>> deads = Set.of(
EntityType.AREA_EFFECT_CLOUD, EntityType.MARKER, EntityType.BOAT, EntityType.END_CRYSTAL,
EntityType.EVOKER_FANGS, EntityType.EXPERIENCE_ORB, EntityType.EYE_OF_ENDER,
EntityType.FALLING_BLOCK, EntityType.ITEM, EntityType.ITEM_FRAME, EntityType.GLOW_ITEM_FRAME,
@@ -328,28 +327,28 @@ public class EntityValue extends Value
EntityType.TNT, EntityType.ARMOR_STAND, EntityType.CHEST_BOAT
);
Set<EntityType<?>> minecarts = Sets.newHashSet(
Set<EntityType<?>> minecarts = Set.of(
EntityType.MINECART, EntityType.CHEST_MINECART, EntityType.COMMAND_BLOCK_MINECART,
EntityType.FURNACE_MINECART, EntityType.HOPPER_MINECART,
EntityType.SPAWNER_MINECART, EntityType.TNT_MINECART
);
// living mob groups - non-defeault
Set<EntityType<?>> undeads = Sets.newHashSet(
Set<EntityType<?>> undeads = Set.of(
EntityType.STRAY, EntityType.SKELETON, EntityType.WITHER_SKELETON,
EntityType.ZOMBIE, EntityType.DROWNED, EntityType.ZOMBIE_VILLAGER,
EntityType.ZOMBIE_HORSE, EntityType.SKELETON_HORSE, EntityType.PHANTOM,
EntityType.WITHER, EntityType.ZOGLIN, EntityType.HUSK, EntityType.ZOMBIFIED_PIGLIN
);
Set<EntityType<?>> arthropods = Sets.newHashSet(
Set<EntityType<?>> arthropods = Set.of(
EntityType.BEE, EntityType.ENDERMITE, EntityType.SILVERFISH, EntityType.SPIDER,
EntityType.CAVE_SPIDER
);
Set<EntityType<?>> aquatique = Sets.newHashSet(
Set<EntityType<?>> aquatique = Set.of(
EntityType.GUARDIAN, EntityType.TURTLE, EntityType.COD, EntityType.DOLPHIN, EntityType.PUFFERFISH,
EntityType.SALMON, EntityType.SQUID, EntityType.TROPICAL_FISH
);
Set<EntityType<?>> illagers = Sets.newHashSet(
Set<EntityType<?>> illagers = Set.of(
EntityType.PILLAGER, EntityType.ILLUSIONER, EntityType.VINDICATOR, EntityType.EVOKER,
EntityType.RAVAGER, EntityType.WITCH
);
@@ -516,7 +516,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
}
try
{
CollectionTag<?> targetList = (CollectionTag) target;
CollectionTag<?> targetList = (CollectionTag<?>) target;
if (!targetList.addTag(index < 0 ? targetList.size() + index + 1 : index, newElement.copy()))
return false;
modified = true;
@@ -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)