minecraft-source/src/net/minecraft/advancements/critereon/InventoryChangeTrigger.java

121 lines
5.3 KiB
Java

package net.minecraft.advancements.critereon;
import java.util.Iterator;
import net.minecraft.world.item.ItemStack;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
public class InventoryChangeTrigger extends SimpleCriterionTrigger<TriggerInstance> {
private static final ResourceLocation ID;
@Override
public ResourceLocation getId() {
return InventoryChangeTrigger.ID;
}
@Override
public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
final JsonObject jsonObject2 = GsonHelper.getAsJsonObject(jsonObject, "slots", new JsonObject());
final MinMaxBounds.Ints d5 = MinMaxBounds.Ints.fromJson(jsonObject2.get("occupied"));
final MinMaxBounds.Ints d6 = MinMaxBounds.Ints.fromJson(jsonObject2.get("full"));
final MinMaxBounds.Ints d7 = MinMaxBounds.Ints.fromJson(jsonObject2.get("empty"));
final ItemPredicate[] arr8 = ItemPredicate.fromJsonArray(jsonObject.get("items"));
return new TriggerInstance(d5, d6, d7, arr8);
}
public void trigger(final ServerPlayer xe, final Inventory ayi) {
this.trigger(xe.getAdvancements(), a -> a.matches(ayi));
}
static {
ID = new ResourceLocation("inventory_changed");
}
public static class TriggerInstance extends AbstractCriterionTriggerInstance {
private final MinMaxBounds.Ints slotsOccupied;
private final MinMaxBounds.Ints slotsFull;
private final MinMaxBounds.Ints slotsEmpty;
private final ItemPredicate[] predicates;
public TriggerInstance(final MinMaxBounds.Ints d1, final MinMaxBounds.Ints d2, final MinMaxBounds.Ints d3, final ItemPredicate[] arr) {
super(InventoryChangeTrigger.ID);
this.slotsOccupied = d1;
this.slotsFull = d2;
this.slotsEmpty = d3;
this.predicates = arr;
}
public static TriggerInstance hasItem(final ItemPredicate... arr) {
return new TriggerInstance(MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY, arr);
}
public static TriggerInstance hasItem(final ItemLike... arr) {
final ItemPredicate[] arr2 = new ItemPredicate[arr.length];
for (int integer3 = 0; integer3 < arr.length; ++integer3) {
arr2[integer3] = new ItemPredicate(null, arr[integer3].asItem(), MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY, EnchantmentPredicate.NONE, EnchantmentPredicate.NONE, null, NbtPredicate.ANY);
}
return hasItem(arr2);
}
@Override
public JsonElement serializeToJson() {
final JsonObject jsonObject2 = new JsonObject();
if (!this.slotsOccupied.isAny() || !this.slotsFull.isAny() || !this.slotsEmpty.isAny()) {
final JsonObject jsonObject3 = new JsonObject();
jsonObject3.add("occupied", this.slotsOccupied.serializeToJson());
jsonObject3.add("full", this.slotsFull.serializeToJson());
jsonObject3.add("empty", this.slotsEmpty.serializeToJson());
jsonObject2.add("slots", jsonObject3);
}
if (this.predicates.length > 0) {
final JsonArray jsonArray3 = new JsonArray();
for (final ItemPredicate bj7 : this.predicates) {
jsonArray3.add(bj7.serializeToJson());
}
jsonObject2.add("items", jsonArray3);
}
return jsonObject2;
}
public boolean matches(final Inventory ayi) {
int integer3 = 0;
int integer4 = 0;
int integer5 = 0;
final List<ItemPredicate> list6 = Lists.<ItemPredicate>newArrayList(this.predicates);
for (int integer6 = 0; integer6 < ayi.getContainerSize(); ++integer6) {
final ItemStack ben8 = ayi.getItem(integer6);
if (ben8.isEmpty()) {
++integer4;
}
else {
++integer5;
if (ben8.getCount() >= ben8.getMaxStackSize()) {
++integer3;
}
final Iterator<ItemPredicate> iterator9 = list6.iterator();
while (iterator9.hasNext()) {
final ItemPredicate bj10 = iterator9.next();
if (bj10.matches(ben8)) {
iterator9.remove();
}
}
}
}
return this.slotsFull.matches(integer3) && this.slotsEmpty.matches(integer4) && this.slotsOccupied.matches(integer5) && list6.isEmpty();
}
}
}