Search in sources :

Example 51 with PartType

use of net.silentchaos512.gear.api.part.PartType in project Silent-Gear by SilentChaos512.

the class GearData method addOrReplacePart.

public static Optional<PartData> addOrReplacePart(ItemStack gear, PartData part) {
    PartType partType = part.getType();
    PartDataList parts = getConstructionParts(gear);
    List<PartData> partsOfType = parts.getPartsOfType(partType);
    PartData removedPart = null;
    if (!partsOfType.isEmpty() && partsOfType.size() >= partType.getMaxPerItem(GearHelper.getType(gear))) {
        removedPart = partsOfType.get(0);
        parts.remove(removedPart);
        removedPart.onRemoveFromGear(gear);
    }
    parts.add(part);
    writeConstructionParts(gear, parts);
    return Optional.ofNullable(removedPart);
}
Also used : PartType(net.silentchaos512.gear.api.part.PartType) PartDataList(net.silentchaos512.gear.api.part.PartDataList) PartData(net.silentchaos512.gear.gear.part.PartData) IPartData(net.silentchaos512.gear.api.part.IPartData)

Example 52 with PartType

use of net.silentchaos512.gear.api.part.PartType in project Silent-Gear by SilentChaos512.

the class GearData method removeFirstPartOfType.

public static boolean removeFirstPartOfType(ItemStack gear, PartType type) {
    PartDataList parts = getConstructionParts(gear);
    List<PartData> partsOfType = new ArrayList<>(parts.getPartsOfType(type));
    if (!partsOfType.isEmpty()) {
        PartData removed = partsOfType.remove(0);
        parts.remove(removed);
        writeConstructionParts(gear, parts);
        removed.onRemoveFromGear(gear);
        return true;
    }
    return false;
}
Also used : PartDataList(net.silentchaos512.gear.api.part.PartDataList) PartData(net.silentchaos512.gear.gear.part.PartData) IPartData(net.silentchaos512.gear.api.part.IPartData)

Example 53 with PartType

use of net.silentchaos512.gear.api.part.PartType in project Silent-Gear by SilentChaos512.

the class GearData method removeExcessParts.

public static void removeExcessParts(ItemStack gear, PartType partType) {
    // Mostly just to correct https://github.com/SilentChaos512/Silent-Gear/issues/242
    PartDataList parts = getConstructionParts(gear);
    List<PartData> partsOfType = new ArrayList<>(parts.getPartsOfType(partType));
    int maxCount = partType.getMaxPerItem(GearHelper.getType(gear));
    int removed = 0;
    while (partsOfType.size() > maxCount) {
        PartData toRemove = partsOfType.get(0);
        partsOfType.remove(toRemove);
        parts.remove(toRemove);
        toRemove.onRemoveFromGear(gear);
        ++removed;
        SilentGear.LOGGER.debug("Removed excess part '{}' from '{}'", toRemove.getDisplayName(gear).getString(), gear.getHoverName().getString());
    }
    if (removed > 0) {
        writeConstructionParts(gear, parts);
    }
}
Also used : PartDataList(net.silentchaos512.gear.api.part.PartDataList) PartData(net.silentchaos512.gear.gear.part.PartData) IPartData(net.silentchaos512.gear.api.part.IPartData)

Example 54 with PartType

use of net.silentchaos512.gear.api.part.PartType in project Silent-Gear by SilentChaos512.

the class GearData method getPartOfType.

/**
 * Gets the first part in the construction parts list that is of the given type.
 *
 * @param stack The gear item
 * @param type  The part type
 * @return The first part of the given type, or null if there is none
 */
@Nullable
public static PartData getPartOfType(ItemStack stack, PartType type) {
    CompoundTag tags = getData(stack, NBT_ROOT_CONSTRUCTION);
    ListTag tagList = tags.getList(NBT_CONSTRUCTION_PARTS, Tag.TAG_COMPOUND);
    for (int i = 0; i < tagList.size(); ++i) {
        CompoundTag nbt = tagList.getCompound(i);
        PartData part = PartData.read(nbt);
        if (part != null && part.getType() == type) {
            return part;
        }
    }
    return null;
}
Also used : PartData(net.silentchaos512.gear.gear.part.PartData) IPartData(net.silentchaos512.gear.api.part.IPartData) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) Nullable(javax.annotation.Nullable)

Example 55 with PartType

use of net.silentchaos512.gear.api.part.PartType in project Silent-Gear by SilentChaos512.

the class ModKitItem method onCycleKeyPress.

@Override
public void onCycleKeyPress(ItemStack stack, KeyPressOnItemPacket.Type direction) {
    PartType selected = getSelectedType(stack);
    List<PartType> types = getRemovableTypes();
    if (types.isEmpty())
        return;
    if (selected == PartType.NONE) {
        if (direction == KeyPressOnItemPacket.Type.CYCLE_BACK) {
            setSelectedType(stack, types.get(types.size() - 1));
        } else if (direction == KeyPressOnItemPacket.Type.CYCLE_NEXT) {
            setSelectedType(stack, types.get(0));
        }
    } else {
        int index = types.indexOf(selected) + direction.direction;
        if (index < 0)
            index = types.size() - 1;
        if (index >= types.size())
            index = 0;
        setSelectedType(stack, types.get(index));
    }
}
Also used : PartType(net.silentchaos512.gear.api.part.PartType)

Aggregations

PartType (net.silentchaos512.gear.api.part.PartType)37 ItemStack (net.minecraft.world.item.ItemStack)14 PartData (net.silentchaos512.gear.gear.part.PartData)14 StatInstance (net.silentchaos512.gear.api.stats.StatInstance)13 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)10 ICoreItem (net.silentchaos512.gear.api.item.ICoreItem)10 TextComponent (net.minecraft.network.chat.TextComponent)9 ResourceLocation (net.minecraft.resources.ResourceLocation)9 GearType (net.silentchaos512.gear.api.item.GearType)9 PartDataList (net.silentchaos512.gear.api.part.PartDataList)9 ItemStat (net.silentchaos512.gear.api.stats.ItemStat)9 Component (net.minecraft.network.chat.Component)8 IMaterialInstance (net.silentchaos512.gear.api.material.IMaterialInstance)8 IPartData (net.silentchaos512.gear.api.part.IPartData)8 MaterialInstance (net.silentchaos512.gear.gear.material.MaterialInstance)8 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)7 StatGearKey (net.silentchaos512.gear.api.util.StatGearKey)7 ArrayList (java.util.ArrayList)5 Collectors (java.util.stream.Collectors)5 MutableComponent (net.minecraft.network.chat.MutableComponent)5