Skip to content

Commit

Permalink
manual testing 1
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Aug 7, 2024
1 parent 43fb9ec commit 0cb22e0
Show file tree
Hide file tree
Showing 16 changed files with 364 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ modmenu_version=7.0.1
clothconfig_version=11.1.106

# Mod Properties
mod_version= a0.5.0
mod_version= 0.5.0-alpha
maven_group=com.megatrex4.ukrainian_dlight
archives_base_name=ukrainian-delight
46 changes: 30 additions & 16 deletions src/main/java/com/megatrex4/ukrainian_dlight/UkrainianDelight.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@
import com.megatrex4.ukrainian_dlight.block.ModBlocks;
import com.megatrex4.ukrainian_dlight.block.entity.ModBlockEntities;
import com.megatrex4.ukrainian_dlight.config.ModConfig;
//import com.megatrex4.ukrainian_dlight.config.UkrainianDelightConfig;

import com.megatrex4.ukrainian_dlight.initialize.Initialise;
import com.megatrex4.ukrainian_dlight.item.ModItemGroups;
import com.megatrex4.ukrainian_dlight.item.ModItems;
import com.megatrex4.ukrainian_dlight.item.ToolTipHelper;
import com.megatrex4.ukrainian_dlight.manual.ManualData;
import com.megatrex4.ukrainian_dlight.manual.ManualDataInitializer;
import com.megatrex4.ukrainian_dlight.manual.ManualItem;
import com.megatrex4.ukrainian_dlight.manual.ManualReloadListener;
import com.megatrex4.ukrainian_dlight.networking.ModMessages;
import com.megatrex4.ukrainian_dlight.recipe.ModRecipes;
import com.megatrex4.ukrainian_dlight.screen.ModScreenHandlers;
import com.megatrex4.ukrainian_dlight.screen.ModScreens;
import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.resource.ResourceType;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand All @@ -42,24 +51,29 @@ public static MutableText i18n(String key, Object... args) {

@Override
public void onInitialize() {
try {
RegistryKey<DamageType> GLASS_DAMAGE;

RegistryKey<DamageType> GLASS_DAMAGE;
FoodJarBlocks.registerFoodBlocks();
DrinkBottleBlock.registerDrinkBlock();
ModBlocks.registerModBlocks();
ModItems.registerModItems();
ModItemGroups.registerItemGroups();
ToolTipHelper.registerTooltip();
ModBlockEntities.registerBlockEntities();
ModScreenHandlers.registerModScreenHandlers();
ModMessages.registerS2CPackets();
ModRecipes.registerRecipes();
ModScreens.registerScreens();
ModConfig.loadConfig();
Initialise.register();

FoodJarBlocks.registerFoodBlocks();
DrinkBottleBlock.registerDrinkBlock();
ModBlocks.registerModBlocks();
ModItems.registerModItems();
ModItemGroups.registerItemGroups();
ToolTipHelper.registerTooltip();
ModBlockEntities.registerBlockEntities();
ModScreenHandlers.registerModScreenHandlers();
ModMessages.registerS2CPackets();
ModRecipes.registerRecipes();
ModScreens.registerScreens();
ModConfig.loadConfig();
Initialise.register();
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new ManualReloadListener());

LOGGER.info("Hello Fabric world it's " + MOD_ID + "!");
LOGGER.info("Hello Fabric world it's " + MOD_ID + "!");
} catch (Exception e) {
LOGGER.error("Mod initialization failed!", e);
}
}

public static void saveConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ModItemGroups {
.displayName(Text.translatable("itemgroup.ingredients_ukrainian_delight"))
.icon(() -> new ItemStack(ModItems.CUCUMBER))
.entries((displayContext, entries) -> {
entries.add(ModItems.MANUAL);
entries.add(ModBlocks.BREWING_KEG);
entries.add(ModBlocks.SALT_BAG);
entries.add(ModBlocks.SALT_BLOCK);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.megatrex4.ukrainian_dlight.item;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;

import com.megatrex4.ukrainian_dlight.manual.ManualItem;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class ModItems {
// Simple items without specifying maxCount, defaults to 64
public static final Item SALT = registerItem("salt", new ItemBuilder().build());
public static final Item YEAST = registerItem("yeast", new ItemBuilder().build());
public static final Item MANUAL = registerItem("manual", new ManualItem());

// Register items
public static Item registerItem(String name, Item item) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.megatrex4.ukrainian_dlight.manual;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.resource.Resource;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ManualData {
private final Map<String, JsonObject> manualEntries = new HashMap<>();
private final Map<String, String> categories = new HashMap<>();

public void loadManualEntries(ResourceManager resourceManager) {
String namespace = "ukrainian_delight";
String path = "manual/en_us/";

for (String noteFile : getNoteFileNames()) {
Identifier resourceId = new Identifier(namespace, path + noteFile);
try {
Optional<Resource> resourceOptional = resourceManager.getResource(resourceId);
if (resourceOptional.isPresent()) {
try (Reader reader = new InputStreamReader(resourceOptional.get().getInputStream())) {
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
String category = jsonObject.get("category").getAsString();
String noteName = jsonObject.get("header").getAsString(); // Assuming note name is present
manualEntries.put(noteName, jsonObject);
categories.put(category, noteName);
}
} else {
System.err.println("Resource not found: " + resourceId);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

private List<String> getNoteFileNames() {
// This should return a list of note file names; adjust this method based on your file structure
return List.of("example_note.json"); // Example, replace with actual file names or dynamic retrieval
}

public Map<String, String> getCategories() {
return categories;
}

public String getNoteContent(String noteName) {
JsonObject entry = manualEntries.get(noteName);
if (entry != null) {
return entry.get("text").getAsString();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.megatrex4.ukrainian_dlight.manual;

import net.minecraft.client.MinecraftClient;

public class ManualDataInitializer {

private final ManualData manualData = new ManualData();

public void initialize() {
manualData.loadManualEntries(MinecraftClient.getInstance().getResourceManager());
}

public ManualData getManualData() {
return manualData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.megatrex4.ukrainian_dlight.manual;

import net.minecraft.text.Text;

public class ManualDefine {

public static Text getCategoryName(String categoryKey) {
return Text.translatable("manual.category." + categoryKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.megatrex4.ukrainian_dlight.manual;

import com.megatrex4.ukrainian_dlight.manual.screen.ManualScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;

public class ManualItem extends Item {

public ManualItem() {
super(new Item.Settings());
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
MinecraftClient.getInstance().execute(() -> {
ClientPlayerEntity player = MinecraftClient.getInstance().player;
if (player != null) {
ManualDataInitializer dataInitializer = new ManualDataInitializer();
dataInitializer.initialize();
MinecraftClient.getInstance().setScreen(new ManualScreen(Text.of("Manual Title"), dataInitializer.getManualData()));
}
});
return ActionResult.success(context.getWorld().isClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.megatrex4.ukrainian_dlight.manual;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static com.megatrex4.ukrainian_dlight.UkrainianDelight.LOGGER;
import static com.megatrex4.ukrainian_dlight.UkrainianDelight.MOD_ID;

public class ManualReloadListener implements IdentifiableResourceReloadListener {

@Override
public Identifier getFabricId() {
return new Identifier(MOD_ID, "manual_reload_listener");
}

@Override
public CompletableFuture<Void> reload(Synchronizer synchronizer, ResourceManager resourceManager,
Profiler serverProfiler, Profiler clientProfiler,
Executor serverExecutor, Executor clientExecutor) {
return CompletableFuture.runAsync(() -> {
ManualData manualData = new ManualData();
manualData.loadManualEntries(resourceManager);
LOGGER.info(MOD_ID + "Manual entries loaded successfully!");
}, serverExecutor).thenCompose(synchronizer::whenPrepared);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.megatrex4.ukrainian_dlight.manual.screen;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import com.megatrex4.ukrainian_dlight.manual.ManualData;
import com.megatrex4.ukrainian_dlight.manual.ManualDefine;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.HashMap;
import java.util.Map;

public class ManualScreen extends Screen {
private static final Identifier TEXTURE = new Identifier(UkrainianDelight.MOD_ID, "textures/gui/manual_gui.png");
private final ManualData manualData;
private ScreenState currentState = ScreenState.CATEGORY_SELECTION;
private String selectedCategory;
private String selectedNote;
private Map<String, String> categories;
private Map<String, Integer[]> categoryPositions; // To store category positions

public ManualScreen(Text title, ManualData manualData) {
super(title);
this.manualData = manualData;
}

@Override
protected void init() {
super.init();
categories = manualData.getCategories(); // Load categories from ManualData
categoryPositions = new HashMap<>();
}

@Override
public void renderBackground(DrawContext context) {
context.drawTexture(TEXTURE, 0, 0, 0, 0, width, height);
}

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);
super.render(context, mouseX, mouseY, delta);
switch (currentState) {
case CATEGORY_SELECTION:
drawCategorySelection(context, mouseX, mouseY);
break;
case NOTE_SELECTION:
drawNoteSelection(context);
break;
case NOTE_CONTENT:
drawNoteContent(context);
break;
}
}

private void drawCategorySelection(DrawContext context, int mouseX, int mouseY) {
MatrixStack matrices = context.getMatrices();
var textRenderer = MinecraftClient.getInstance().textRenderer;
int y = 20;

for (Map.Entry<String, String> entry : categories.entrySet()) {
String category = entry.getKey();
Text categoryText = ManualDefine.getCategoryName(category);
int textWidth = textRenderer.getWidth(categoryText);
int x = (width - textWidth) / 2;

// Draw category text
textRenderer.draw(categoryText, x, y, 0xFFFFFF, true, matrices.peek().getPositionMatrix(),
MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers(),
TextRenderer.TextLayerType.NORMAL, 0x000000, 15728880);

// Store position for clickable area
categoryPositions.put(category, new Integer[]{x, y, textWidth, 10}); // 10 is the height of the text

y += 20; // Adjust as needed
}
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (currentState == ScreenState.CATEGORY_SELECTION) {
for (Map.Entry<String, Integer[]> entry : categoryPositions.entrySet()) {
Integer[] pos = entry.getValue();
String category = entry.getKey();
if (mouseX >= pos[0] && mouseX <= pos[0] + pos[2] &&
mouseY >= pos[1] && mouseY <= pos[1] + pos[3]) {
selectCategory(category);
return true;
}
}
}
return super.mouseClicked(mouseX, mouseY, button);
}

private void drawNoteSelection(DrawContext context) {
// Implement similar to drawCategorySelection but for notes
}

private void drawNoteContent(DrawContext context) {
MatrixStack matrices = context.getMatrices();
var textRenderer = MinecraftClient.getInstance().textRenderer;
if (selectedNote != null) {
String noteContent = manualData.getNoteContent(selectedNote);
Text noteText = Text.of(noteContent);
int textWidth = textRenderer.getWidth(noteText);
int x = (width - textWidth) / 2;
int y = (height - 8) / 2;
textRenderer.draw(noteText, x, y, 0xFFFFFF, true, matrices.peek().getPositionMatrix(),
MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers(),
TextRenderer.TextLayerType.NORMAL, 0x000000, 15728880);
}
}

public void selectCategory(String category) {
this.selectedCategory = category;
this.currentState = ScreenState.NOTE_SELECTION;
}

public void selectNote(String note) {
this.selectedNote = note;
this.currentState = ScreenState.NOTE_CONTENT;
}

private enum ScreenState {
CATEGORY_SELECTION,
NOTE_SELECTION,
NOTE_CONTENT
}
}
Loading

0 comments on commit 0cb22e0

Please sign in to comment.