Search in sources :

Example 1 with AbstractGearPart

use of net.silentchaos512.gear.gear.part.AbstractGearPart 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 2 with AbstractGearPart

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

the class TooltipHandler method onPartTooltip.

private static void onPartTooltip(ItemTooltipEvent event, ItemStack stack, PartData part) {
    if (event.getFlags().isAdvanced() && KeyTracker.isControlDown()) {
        event.getToolTip().add(new TextComponent("* Part ID: " + part.getId()).withStyle(ChatFormatting.DARK_GRAY));
        event.getToolTip().add(new TextComponent("* Part data pack: " + part.get().getPackName()).withStyle(ChatFormatting.DARK_GRAY));
    }
    if (!Config.Client.showPartTooltips.get()) {
        return;
    }
    // Type, tier
    event.getToolTip().add(TextUtil.withColor(part.getType().getDisplayName(part.getTier()), Color.AQUAMARINE));
    // Traits
    List<TraitInstance> traits = new ArrayList<>();
    for (TraitInstance traitInstance : part.getTraits()) {
        if (traitInstance.getTrait().showInTooltip(event.getFlags())) {
            traits.add(traitInstance);
        }
    }
    int numTraits = traits.size();
    int traitIndex = getTraitDisplayIndex(numTraits);
    int i = 0;
    for (TraitInstance inst : traits) {
        if (traitIndex < 0 || traitIndex == i) {
            inst.addInformation(event.getToolTip(), event.getFlags());
        }
        ++i;
    }
    // Stats
    if (KeyTracker.isControlDown()) {
        event.getToolTip().add(new TranslatableComponent("misc.silentgear.tooltip.stats").withStyle(ChatFormatting.GOLD).append(new TextComponent(" (Silent Gear)").withStyle(ChatFormatting.RESET).withStyle(ChatFormatting.ITALIC)));
        getPartStatLines(event, stack, part);
    } else {
        event.getToolTip().add(new TranslatableComponent("misc.silentgear.tooltip.ctrlForStats").withStyle(ChatFormatting.GOLD));
    }
    // Gear type blacklist?
    if (part.get() instanceof AbstractGearPart) {
        List<GearType> blacklist = ((AbstractGearPart) part.get()).getBlacklistedGearTypes();
        if (!blacklist.isEmpty()) {
            int index = (ClientTicks.ticksInGame() / 20) % blacklist.size();
            GearType gearType = blacklist.get(index);
            Component blacklistedType = gearType.getDisplayName();
            event.getToolTip().add(new TranslatableComponent("misc.silentgear.tooltip.blacklist", blacklistedType).withStyle(ChatFormatting.RED));
        }
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) AbstractGearPart(net.silentchaos512.gear.gear.part.AbstractGearPart) GearType(net.silentchaos512.gear.api.item.GearType) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) MutableComponent(net.minecraft.network.chat.MutableComponent) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent)

Example 3 with AbstractGearPart

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

the class SyncGearCraftingItemsPacket method handle.

public void handle(Supplier<NetworkEvent.Context> context) {
    SilentGear.LOGGER.debug("Correcting part crafting items");
    this.craftingItems.forEach((id, ingredient) -> {
        IGearPart part = PartManager.get(id);
        if (part instanceof AbstractGearPart) {
            ((AbstractGearPart) part).updateCraftingItems(ingredient);
        }
    });
    context.get().setPacketHandled(true);
}
Also used : AbstractGearPart(net.silentchaos512.gear.gear.part.AbstractGearPart) IGearPart(net.silentchaos512.gear.api.part.IGearPart)

Aggregations

IGearPart (net.silentchaos512.gear.api.part.IGearPart)2 AbstractGearPart (net.silentchaos512.gear.gear.part.AbstractGearPart)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 Component (net.minecraft.network.chat.Component)1 MutableComponent (net.minecraft.network.chat.MutableComponent)1 TextComponent (net.minecraft.network.chat.TextComponent)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 Resource (net.minecraft.server.packs.resources.Resource)1 GearType (net.silentchaos512.gear.api.item.GearType)1 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)1