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);
}
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;
}
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);
}
}
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;
}
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));
}
}
Aggregations