use of net.silentchaos512.gear.api.part.IGearPart in project Silent-Gear by SilentChaos512.
the class PartData method read.
@Nullable
public static PartData read(CompoundTag tags) {
ResourceLocation id = SilentGear.getIdWithDefaultNamespace(tags.getString(NBT_ID));
if (id == null)
return null;
IGearPart part = PartManager.get(id);
if (part == null)
return null;
ItemStack craftingItem = ItemStack.of(tags.getCompound("Item"));
return of(part, craftingItem);
}
use of net.silentchaos512.gear.api.part.IGearPart in project Silent-Gear by SilentChaos512.
the class PartManager method handlePartSyncPacket.
public static void handlePartSyncPacket(SyncGearPartsPacket packet, Supplier<NetworkEvent.Context> context) {
synchronized (MAP) {
Map<ResourceLocation, IGearPart> oldParts = ImmutableMap.copyOf(MAP);
MAP.clear();
for (IGearPart part : packet.getParts()) {
part.retainData(oldParts.get(part.getId()));
MAP.put(part.getId(), part);
}
SilentGear.LOGGER.info("Read {} parts from server", MAP.size());
}
context.get().setPacketHandled(true);
}
use of net.silentchaos512.gear.api.part.IGearPart in project Silent-Gear by SilentChaos512.
the class PartManager method onResourceManagerReload.
@Override
public void onResourceManagerReload(ResourceManager resourceManager) {
Gson gson = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create();
Collection<ResourceLocation> resources = getAllResources(resourceManager);
if (resources.isEmpty())
return;
synchronized (MAP) {
MAP.clear();
ERROR_LIST.clear();
SilentGear.LOGGER.info(MARKER, "Reloading part files");
for (ResourceLocation id : resources) {
String path = id.getPath().substring(DATA_PATH.length() + 1, id.getPath().length() - ".json".length());
ResourceLocation name = new ResourceLocation(id.getNamespace(), path);
String packName = "ERROR";
try (Resource iresource = resourceManager.getResource(id)) {
packName = iresource.getSourceName();
if (SilentGear.LOGGER.isTraceEnabled()) {
SilentGear.LOGGER.trace(MARKER, "Found likely part file: {}, trying to read as part {}", id, name);
}
JsonObject json = GsonHelper.fromJson(gson, IOUtils.toString(iresource.getInputStream(), StandardCharsets.UTF_8), JsonObject.class);
if (json == null) {
SilentGear.LOGGER.error(MARKER, "Could not load part {} as it's null or empty", name);
} else if (!CraftingHelper.processConditions(json, "conditions")) {
SilentGear.LOGGER.info("Skipping loading gear part {} as it's conditions were not met", name);
} else {
IGearPart part = PartSerializers.deserialize(name, json);
if (part instanceof AbstractGearPart) {
((AbstractGearPart) part).packName = iresource.getSourceName();
}
addPart(part);
highestMainPartTier = Math.max(highestMainPartTier, part.getTier());
}
} catch (IllegalArgumentException | JsonParseException ex) {
SilentGear.LOGGER.error(MARKER, "Parsing error loading gear part {}", name, ex);
ERROR_LIST.add(String.format("%s (%s)", name, packName));
} catch (IOException ex) {
SilentGear.LOGGER.error(MARKER, "Could not read gear part {}", name, ex);
ERROR_LIST.add(String.format("%s (%s)", name, packName));
}
}
SilentGear.LOGGER.info(MARKER, "Registered {} parts", MAP.size());
}
}
use of net.silentchaos512.gear.api.part.IGearPart in project Silent-Gear by SilentChaos512.
the class PartSerializers method write.
@SuppressWarnings("unchecked")
public static <T extends IGearPart> void write(T part, FriendlyByteBuf buffer) {
ResourceLocation id = part.getId();
ResourceLocation type = part.getSerializer().getName();
log(() -> "write " + id + " (type " + type + ")");
buffer.writeResourceLocation(id);
buffer.writeResourceLocation(type);
IPartSerializer<T> serializer = (IPartSerializer<T>) part.getSerializer();
serializer.write(buffer, part);
}
use of net.silentchaos512.gear.api.part.IGearPart in project Silent-Gear by SilentChaos512.
the class GearData method hasPartOfType.
/**
* Check if the gear item has at least one part of the given type.
*
* @param stack The gear item
* @param type The part type
* @return True if and only if the construction parts include a part of the given type
*/
public static boolean hasPartOfType(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);
String key = nbt.getString("ID");
IGearPart part = PartManager.get(key);
if (part != null && part.getType() == type) {
return true;
}
}
return false;
}
Aggregations