Search in sources :

Example 1 with IGearPart

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);
}
Also used : IGearPart(net.silentchaos512.gear.api.part.IGearPart) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack) Nullable(javax.annotation.Nullable)

Example 2 with IGearPart

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);
}
Also used : IGearPart(net.silentchaos512.gear.api.part.IGearPart) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 3 with IGearPart

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());
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Resource(net.minecraft.server.packs.resources.Resource) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) IGearPart(net.silentchaos512.gear.api.part.IGearPart) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 4 with IGearPart

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);
}
Also used : IPartSerializer(net.silentchaos512.gear.api.part.IPartSerializer) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 5 with IGearPart

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;
}
Also used : IGearPart(net.silentchaos512.gear.api.part.IGearPart) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

IGearPart (net.silentchaos512.gear.api.part.IGearPart)8 ResourceLocation (net.minecraft.resources.ResourceLocation)5 TextComponent (net.minecraft.network.chat.TextComponent)2 ItemStack (net.minecraft.world.item.ItemStack)2 PartData (net.silentchaos512.gear.gear.part.PartData)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ListTag (net.minecraft.nbt.ListTag)1 Component (net.minecraft.network.chat.Component)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 Resource (net.minecraft.server.packs.resources.Resource)1 ICoreItem (net.silentchaos512.gear.api.item.ICoreItem)1 IPartData (net.silentchaos512.gear.api.part.IPartData)1 IPartSerializer (net.silentchaos512.gear.api.part.IPartSerializer)1 PartType (net.silentchaos512.gear.api.part.PartType)1