Skip to content

Commit

Permalink
Compass (#244)
Browse files Browse the repository at this point in the history
* init

* move compass to ldlib

* Update settings.gradle

* compass for test

* data gen

* data gen compass sections

* done

* Update GTMachines.java
  • Loading branch information
Yefancy authored Aug 1, 2023
1 parent 19ee22a commit 75eec7c
Show file tree
Hide file tree
Showing 473 changed files with 13,750 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.gregtechceu.gtceu.api.gui.compass;

import com.gregtechceu.gtceu.api.gui.GuiTextures;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.integration.GTRecipeWidget;
import com.lowdragmc.lowdraglib.gui.compass.component.RecipeComponent;
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Recipe;

/**
* @author KilaBash
* @date 2023/7/30
* @implNote GTRecipeViewCreator
*/
public class GTRecipeViewCreator implements RecipeComponent.RecipeViewCreator {
@Override
public ItemStack getWorkstation(Recipe<?> recipe) {
if (recipe instanceof GTRecipe gtRecipe) {
if (gtRecipe.recipeType.getIconSupplier() != null) {
return gtRecipe.recipeType.getIconSupplier().get();
}
}
return new ItemStack(Items.BARRIER);
}

@Override
public WidgetGroup getViewWidget(Recipe<?> recipe) {
if (recipe instanceof GTRecipe gtRecipe) {
var widget = new GTRecipeWidget(gtRecipe);
widget.addSelfPosition(4, 4);
var recipeGroup = new WidgetGroup(0, 0, widget.getSize().width + 8, widget.getSize().height + 8);
recipeGroup.setBackground(GuiTextures.BACKGROUND);
recipeGroup.addWidget(widget);
return recipeGroup;

}
return new WidgetGroup();
}

@Override
public boolean test(Recipe<?> recipe) {
return recipe instanceof GTRecipe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class GTRecipeType implements RecipeType<GTRecipe> {
private GTRecipeType smallRecipeMap;
@Setter
@Getter
@Nullable
private Supplier<ItemStack> iconSupplier;
@Nullable
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.gregtechceu.gtceu.api.machine.MachineDefinition;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.RecipeCondition;
import com.gregtechceu.gtceu.api.registry.registrate.CompassNode;
import com.gregtechceu.gtceu.api.registry.registrate.CompassSection;
import com.gregtechceu.gtceu.api.registry.registrate.GTRegistrate;
import com.gregtechceu.gtceu.api.sound.SoundEntry;
import com.gregtechceu.gtceu.api.data.chemical.Element;
Expand Down Expand Up @@ -39,6 +41,8 @@ public final class GTRegistries {
public static final GTRegistry.String<RecipeCapability<?>> RECIPE_CAPABILITIES = new GTRegistry.String<>(GTCEu.id("recipe_capability"));
public static final GTRegistry.String<Class<? extends RecipeCondition>> RECIPE_CONDITIONS = new GTRegistry.String<>(GTCEu.id("recipe_condition"));
public static final GTRegistry.RL<SoundEntry> SOUNDS = new GTRegistry.RL<>(GTCEu.id("sound"));
public static final GTRegistry.RL<CompassSection> COMPASS_SECTIONS = new GTRegistry.RL<>(GTCEu.id("compass_section"));
public static final GTRegistry.RL<CompassNode> COMPASS_NODES = new GTRegistry.RL<>(GTCEu.id("compass_node"));
public static final GTRegistry.RL<BedrockFluidDefinition> BEDROCK_FLUID_DEFINITIONS = new GTRegistry.RL<>(GTCEu.id("bedrock_fluid"));
public static final GTRegistry.RL<GTOreFeatureEntry> ORE_VEINS = new GTRegistry.RL<>(GTCEu.id("ore_vein"));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package com.gregtechceu.gtceu.api.registry.registrate;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.utils.FormattingUtil;
import com.lowdragmc.lowdraglib.gui.texture.IGuiTexture;
import com.lowdragmc.lowdraglib.gui.texture.ItemStackTexture;
import com.lowdragmc.lowdraglib.json.SimpleIGuiTextureJsonUtils;
import com.lowdragmc.lowdraglib.utils.Position;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.Registry;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.DataProvider;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
* @author KilaBash
* @date 2023/7/31
* @implNote CompassSectionBuilder
*/
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@Accessors(fluent = true, chain = true)
public class CompassNode {
@Getter
private final ResourceLocation sectionID;
@Getter
private final ResourceLocation nodeID;
@Setter
private ResourceLocation page;
@Setter
private int size = 24;
@Setter @Nullable // null - auto layout
private Position position = null;
private final Set<ResourceLocation> preNodes = new HashSet<>();
private final List<Supplier<? extends Item>> items = new ArrayList<>();
@Setter @Nullable
private Supplier<IGuiTexture> icon = null;
@Setter @Getter
private String lang;

private CompassNode(ResourceLocation sectionID, String nodeID) {
this.sectionID = sectionID;
lang = FormattingUtil.toEnglishName(nodeID);
this.nodeID = GTCEu.id(sectionID.getPath() + "/" + nodeID);
this.page = this.nodeID;
}

public static CompassNode getOrCreate(ResourceLocation sectionID, String nodeID) {
var exist = GTRegistries.COMPASS_NODES.get(GTCEu.id(sectionID.getPath() + "/" + nodeID));
return exist == null ? new CompassNode(sectionID, nodeID).register() : exist;
}

public static CompassNode getOrCreate(CompassSection section, String nodeID) {
return getOrCreate(section.sectionID(), nodeID);
}

public static CompassNode getOrCreate(CompassSection section, Supplier<? extends Item> item) {
return getOrCreate(section.sectionID(), Registry.ITEM.getKey(item.get()).getPath()).addItem(item);
}

private CompassNode register() {
GTRegistries.COMPASS_NODES.register(nodeID, this);
return this;
}

public String getUnlocalizedKey() {
return nodeID.toLanguageKey("compass.node");
}

public CompassNode position(int x, int y) {
this.position = new Position(x, y);
return this;
}

public CompassNode addPreNode(ResourceLocation... nodeID) {
preNodes.addAll(Arrays.stream(nodeID).toList());
return this;
}

public CompassNode addPreNode(CompassNode... node) {
preNodes.addAll(Arrays.stream(node).map(CompassNode::nodeID).toList());
return this;
}

public CompassNode addItem(Supplier<? extends Item> item) {
items.add(item);
return this;
}

public static class CompassNodeProvider implements DataProvider {
private final DataGenerator generator;
private final Predicate<ResourceLocation> existingHelper;

public CompassNodeProvider(DataGenerator generator, Predicate<ResourceLocation> existingHelper) {
this.generator = generator;
this.existingHelper = existingHelper;
}

@Override
public void run(CachedOutput cache) {
generate(generator.getOutputFolder(), cache);
}

@Override
public String getName() {
return "GTCEU's Compass Nodes";
}

public void generate(Path path, CachedOutput cache) {
path = path.resolve("assets/" + GTCEu.MOD_ID);
Map<ResourceLocation, List<CompassNode>> nodesNOPosition = new HashMap<>();
try {
for (var node : GTRegistries.COMPASS_NODES) {
if (node.position == null) {
nodesNOPosition.computeIfAbsent(node.sectionID, k -> new ArrayList<>()).add(node);
} else {
genNodeData(path, cache, node);
}
}
for (List<CompassNode> nodes : nodesNOPosition.values()) {
var size = nodes.size();
var row = (int) Math.ceil(Math.sqrt(size));
for (int i = 0; i < row; i++) {
boolean done = false;
for (int j = 0; j < row; j++) {
int index = i * row + j;
if (index < nodes.size()) {
var node = nodes.get(index);
node.position = new Position(-(row * 50) + 50 * j, 50 * i);
genNodeData(path, cache, node);
} else {
done = true;
break;
}
}
if (done) break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

private void genNodeData(Path path, CachedOutput cache, CompassNode node) throws IOException {
if (node.position == null) return;
var resourcePath = "compass/nodes/" + node.nodeID.getPath() + ".json";
if (existingHelper.test(GTCEu.id(resourcePath))) return;

JsonObject json = new JsonObject();
json.addProperty("section", node.sectionID.toString());
json.addProperty("page", node.page.toString());
if (node.icon == null) {
if (!node.items.isEmpty()) {
node.icon = () -> new ItemStackTexture(node.items.get(0).get());
} else {
node.icon = () -> IGuiTexture.EMPTY;
}
}
json.add("button_texture",SimpleIGuiTextureJsonUtils.toJson(node.icon.get()));
if (node.size != 24) {
json.addProperty("size", node.size);
}
var pos = new JsonArray();
pos.add(node.position.x);
pos.add(node.position.y);
json.add("position", pos);
if (!node.preNodes.isEmpty()) {
var pre = new JsonArray();
for (var preNode : node.preNodes) {
pre.add(preNode.toString());
}
json.add("pre_nodes", pre);
}
if (!node.items.isEmpty()) {
var items = new JsonArray();
for (var item : node.items) {
items.add(Registry.ITEM.getKey(item.get()).toString());
}
json.add("items", items);
}

DataProvider.saveStable(cache, json, path.resolve(resourcePath));
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.gregtechceu.gtceu.api.registry.registrate;

import com.google.gson.JsonObject;
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.utils.FormattingUtil;
import com.lowdragmc.lowdraglib.gui.texture.IGuiTexture;
import com.lowdragmc.lowdraglib.json.SimpleIGuiTextureJsonUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.DataProvider;
import net.minecraft.resources.ResourceLocation;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
import java.nio.file.Path;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
* @author KilaBash
* @date 2023/7/31
* @implNote CompassSectionBuilder
*/
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@Accessors(fluent = true, chain = true)
public class CompassSection {
@Getter
private final ResourceLocation sectionID;
@Setter
private Supplier<IGuiTexture> icon = () -> IGuiTexture.EMPTY;
@Setter
private Supplier<IGuiTexture> background = () -> IGuiTexture.EMPTY;
@Setter
private int priority = 99;
@Setter @Getter
private String lang;

private CompassSection(String section) {
this.sectionID = GTCEu.id(section);
lang = FormattingUtil.toEnglishName(section);
}

public static CompassSection create(String section) {
return new CompassSection(section);
}

public CompassSection register() {
GTRegistries.COMPASS_SECTIONS.register(sectionID, this);
return this;
}

public String getUnlocalizedKey() {
return sectionID.toLanguageKey("compass.section");
}

public static class CompassSectionProvider implements DataProvider {
private final DataGenerator generator;
private final Predicate<ResourceLocation> existingHelper;

public CompassSectionProvider(DataGenerator generator, Predicate<ResourceLocation> existingHelper) {
this.generator = generator;
this.existingHelper = existingHelper;
}

@Override
public void run(CachedOutput cache) {
generate(generator.getOutputFolder(), cache);
}

@Override
public String getName() {
return "GTCEU's Compass Sections";
}

public void generate(Path path, CachedOutput cache) {
path = path.resolve("assets/" + GTCEu.MOD_ID);

try {
for (var section : GTRegistries.COMPASS_SECTIONS) {
var resourcePath = "compass/sections/" + section.sectionID.getPath() + ".json";
if (existingHelper.test(GTCEu.id(resourcePath))) {
continue;
}
JsonObject json = new JsonObject();
json.add("button_texture",SimpleIGuiTextureJsonUtils.toJson(section.icon.get()));
json.add("background_texture",SimpleIGuiTextureJsonUtils.toJson(section.background.get()));
json.addProperty("priority", section.priority);
DataProvider.saveStable(cache, json, path.resolve(resourcePath));
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

}
Loading

0 comments on commit 75eec7c

Please sign in to comment.