Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Butterfly data can now be edited via JSON #86

Merged
merged 3 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

### 2.1.0 (2024-01-28)
- Butterfly data can now be edited via JSON files

### 2.0.9 (2024-01-26)
- Fixed butterfly net achievements not working

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod_name=Butterfly Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=All Rights Reserved
# The mod version. See https://semver.org/
mod_version=2.0.9
mod_version=2.1.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public int getPageCount() {
public FormattedText getPageRaw(int page) {
if (pages != null) {
int butterflyIndex = pages.getInt((page - 1) / 2);
ButterflyData.Entry entry = ButterflyData.getEntry(butterflyIndex);
ButterflyData entry = ButterflyData.getEntry(butterflyIndex);
if (entry != null) {
// Butterfly name
MutableComponent component = Component.translatable("entity.butterflies." + entry.entityId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.bokmcdok.butterflies.event.level;

import com.bokmcdok.butterflies.ButterfliesMod;
import com.bokmcdok.butterflies.world.ButterflyData;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.logging.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraftforge.event.level.LevelEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

/**
* Listens for events on loading a level.
*/
@Mod.EventBusSubscriber(modid = ButterfliesMod.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class LevelEventListener {


// GSON build that can parse Butterfly entries.
private static final Gson GSON = (new GsonBuilder()).registerTypeAdapter(ButterflyData.class, new ButterflyData.Serializer()).create();

// Logger for reporting errors.
private static final Logger LOGGER = LogUtils.getLogger();

/**
* Load butterfly mod-specific data when a level loads.
* @param event The level load event.
*/
@SubscribeEvent
public static void onLevelLoad(LevelEvent.Load event) {
LOGGER.debug("LevelEvent.Load");

// Get the resource manager.
ResourceManager resourceManager = null;
if (event.getLevel().isClientSide()) {
resourceManager = Minecraft.getInstance().getResourceManager();
} else {
MinecraftServer server = event.getLevel().getServer();
if (server != null) {
resourceManager = server.getResourceManager();
}
}

if (resourceManager == null) {
LOGGER.error("Failed to get Resource Manager");
} else {
// Get the butterfly JSON files
Map<ResourceLocation, Resource> resourceMap =
resourceManager.listResources(ButterfliesMod.MODID, (x) -> x.getPath().endsWith(".json"));

// Parse each one and generate the data.
for (ResourceLocation location : resourceMap.keySet()) {
try {
Resource resource = resourceMap.get(location);
BufferedReader reader = resource.openAsReader();
ButterflyData butterflyData = GSON.fromJson(reader, ButterflyData.class);
ButterflyData.addButterfly(butterflyData);
} catch (IOException e) {
LOGGER.error("Failed to load butterfly data: [" + location.toString() + "]", e);
}
}
}
}
}
Loading