Skip to content

Commit

Permalink
Merge pull request #32 from CottonMC/rei
Browse files Browse the repository at this point in the history
Add REI plugin
  • Loading branch information
falseresync authored Jun 11, 2019
2 parents b46eb59 + 51ebc0a commit 3d29bd9
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 1 deletion.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ repositories {
name = "BuildCraft" //For LibBlockAttributes
url = "https://mod-buildcraft.com/maven"
}
maven {
// For REI's dependencies
name = 'CurseForge'
url = 'https://minecraft.curseforge.com/api/maven'
}
}

dependencies {
Expand All @@ -31,6 +36,7 @@ dependencies {
modCompile ("io.github.cottonmc:cotton:0.7.3+1.14.2-SNAPSHOT") { transitive = false }
modCompile ("io.github.cottonmc:cotton-energy:1.4.0+1.14.2-SNAPSHOT") { transitive = false }
modCompile ("io.github.cottonmc:cotton-resources:1.2.0+1.14.2-SNAPSHOT") { transitive = false }
modCompile "me.shedaniel:RoughlyEnoughItems:2.9.1+build.116"

include ("alexill.mc.lib:libblockattributes:0.4.2") { transitive = false }
include ("io.github.cottonmc:cotton-energy:1.4.0") { transitive = false }
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/vivatech/rei/BaseRecipeCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package vivatech.rei;

import com.google.common.collect.ImmutableList;
import me.shedaniel.rei.api.RecipeCategory;
import me.shedaniel.rei.api.RecipeDisplay;
import me.shedaniel.rei.gui.widget.RecipeBaseWidget;
import me.shedaniel.rei.gui.widget.Widget;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.recipe.Recipe;
import net.minecraft.util.Identifier;
import vivatech.rei.widget.CottonSlotWidget;
import vivatech.rei.widget.ProgressBarWidget;

import java.awt.Rectangle;
import java.util.List;
import java.util.function.Supplier;

abstract class BaseRecipeCategory implements RecipeCategory<RecipeDisplay<Recipe<?>>> {
private final Identifier id;
private final String translationKey;

BaseRecipeCategory(Identifier id, String translationKey) {
this.id = id;
this.translationKey = translationKey;
}

@Override
public Identifier getIdentifier() {
return id;
}

@Override
public String getCategoryName() {
return I18n.translate(translationKey);
}

@Override
public List<Widget> setupDisplay(Supplier<RecipeDisplay<Recipe<?>>> recipeDisplaySupplier, Rectangle bounds) {
RecipeDisplay<?> display = recipeDisplaySupplier.get();
int x = (int) bounds.getCenterX();
int y = (int) bounds.getCenterY() - 9;

return ImmutableList.of(
new RecipeBaseWidget(bounds),
new ProgressBarWidget(x - 18 - 5, y, 40, 18, 200),
new CottonSlotWidget(x - 2 * 18 - 9, y, display.getInput().get(0), true, true),
CottonSlotWidget.createBig(x + 2 * 18 - 9, y, display.getOutput(), true, true)
);
}
}
17 changes: 17 additions & 0 deletions src/main/java/vivatech/rei/CrushingCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package vivatech.rei;

import me.shedaniel.rei.api.Renderable;
import me.shedaniel.rei.api.Renderer;
import net.minecraft.item.ItemStack;
import vivatech.init.VivatechBlocks;

final class CrushingCategory extends BaseRecipeCategory {
CrushingCategory() {
super(VivatechREIPlugin.CRUSHING, "gui.vivatech.crushing");
}

@Override
public Renderer getIcon() {
return Renderable.fromItemStack(new ItemStack(VivatechBlocks.CRUSHER));
}
}
52 changes: 52 additions & 0 deletions src/main/java/vivatech/rei/CrushingDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package vivatech.rei;

import me.shedaniel.rei.api.RecipeDisplay;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import vivatech.recipe.CrushingRecipe;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

final class CrushingDisplay implements RecipeDisplay<CrushingRecipe> {
private final CrushingRecipe recipe;
private final List<List<ItemStack>> input;
private final List<ItemStack> output;
// private final List<ItemStack> bonusStacks;

CrushingDisplay(CrushingRecipe recipe) {
this.recipe = recipe;
input = recipe.getPreviewInputs().stream()
.map(ingredient -> Arrays.asList(ingredient.getStackArray()))
.collect(Collectors.toList());
output = Collections.singletonList(recipe.getOutput());
//bonusStacks = LootUtils.getAllStacks(recipe.getBonusLootTable());
}

@Override
public Optional<CrushingRecipe> getRecipe() {
return Optional.of(recipe);
}

@Override
public List<List<ItemStack>> getInput() {
return input;
}

@Override
public List<ItemStack> getOutput() {
return output;
}

@Override
public Identifier getRecipeCategory() {
return VivatechREIPlugin.CRUSHING;
}

/*public List<ItemStack> getBonusStacks() {
return bonusStacks;
}*/
}
17 changes: 17 additions & 0 deletions src/main/java/vivatech/rei/PressingCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package vivatech.rei;

import me.shedaniel.rei.api.Renderable;
import me.shedaniel.rei.api.Renderer;
import net.minecraft.item.ItemStack;
import vivatech.init.VivatechBlocks;

final class PressingCategory extends BaseRecipeCategory {
PressingCategory() {
super(VivatechREIPlugin.PRESSING, "gui.vivatech.pressing");
}

@Override
public Renderer getIcon() {
return Renderable.fromItemStack(new ItemStack(VivatechBlocks.PRESS));
}
}
52 changes: 52 additions & 0 deletions src/main/java/vivatech/rei/PressingDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package vivatech.rei;

import com.google.common.collect.ImmutableList;
import me.shedaniel.rei.api.RecipeDisplay;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import vivatech.recipe.PressingRecipe;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

final class PressingDisplay implements RecipeDisplay<PressingRecipe> {
private final PressingRecipe recipe;
private final List<List<ItemStack>> input;
private final List<ItemStack> output;

PressingDisplay(PressingRecipe recipe) {
this.recipe = recipe;
input = recipe.getPreviewInputs().stream()
.map(ingredient -> Arrays.asList(ingredient.getStackArray()))
.collect(Collectors.toList());

output = ImmutableList.of(recipe.getOutput());
}

@Override
public Optional<PressingRecipe> getRecipe() {
return Optional.of(recipe);
}

@Override
public List<List<ItemStack>> getInput() {
return input;
}

@Override
public List<ItemStack> getOutput() {
return output;
}

@Override
public List<List<ItemStack>> getRequiredItems() {
return input;
}

@Override
public Identifier getRecipeCategory() {
return VivatechREIPlugin.PRESSING;
}
}
36 changes: 36 additions & 0 deletions src/main/java/vivatech/rei/VivatechREIPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package vivatech.rei;

import me.shedaniel.rei.api.REIPluginEntry;
import me.shedaniel.rei.api.RecipeHelper;
import net.minecraft.recipe.Recipe;
import net.minecraft.util.Identifier;
import vivatech.recipe.CrushingRecipe;
import vivatech.recipe.PressingRecipe;

public final class VivatechREIPlugin implements REIPluginEntry {
public static final Identifier ID = new Identifier("vivatech", "rei_plugin");
public static final Identifier CRUSHING = new Identifier("vivatech", "crushing");
public static final Identifier PRESSING = new Identifier("vivatech", "pressing");

@Override
public Identifier getPluginIdentifier() {
return ID;
}

@Override
public void registerPluginCategories(RecipeHelper recipeHelper) {
recipeHelper.registerCategory(new CrushingCategory());
recipeHelper.registerCategory(new PressingCategory());
}

@Override
public void registerRecipeDisplays(RecipeHelper recipeHelper) {
for (Recipe<?> recipe : recipeHelper.getAllSortedRecipes()) {
if (recipe instanceof PressingRecipe) {
recipeHelper.registerDisplay(PRESSING, new PressingDisplay((PressingRecipe) recipe));
} else if (recipe instanceof CrushingRecipe) {
recipeHelper.registerDisplay(CRUSHING, new CrushingDisplay((CrushingRecipe) recipe));
}
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/vivatech/rei/widget/CottonSlotWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package vivatech.rei.widget;

import io.github.cottonmc.cotton.gui.EmptyInventory;
import io.github.cottonmc.cotton.gui.widget.WItemSlot;
import me.shedaniel.rei.gui.widget.SlotWidget;
import net.minecraft.item.ItemStack;

import java.util.List;

/**
* A slot widget that uses Cotton's {@link WItemSlot} for rendering.
*/
public final class CottonSlotWidget extends SlotWidget {
private final WItemSlot renderSlot;
private final int x;
private final int y;

private CottonSlotWidget(int x, int y, List<ItemStack> itemList, boolean showToolTips, boolean clickToMoreRecipes, WItemSlot renderSlot) {
super(x, y, itemList, false, showToolTips, clickToMoreRecipes);
this.renderSlot = renderSlot;
this.x = x;
this.y = y;
}

public CottonSlotWidget(int x, int y, List<ItemStack> itemList, boolean showToolTips, boolean clickToMoreRecipes) {
this(x, y, itemList, showToolTips, clickToMoreRecipes, WItemSlot.of(EmptyInventory.INSTANCE, 0));
}

public static CottonSlotWidget createBig(int x, int y, List<ItemStack> itemList, boolean showToolTips, boolean clickToMoreRecipes) {
return new CottonSlotWidget(x, y, itemList, showToolTips, clickToMoreRecipes, WItemSlot.outputOf(EmptyInventory.INSTANCE, 0));
}

@Override
public void render(int mouseX, int mouseY, float delta) {
renderSlot.paintBackground(x, y);
super.render(mouseX, mouseY, delta);
}
}
56 changes: 56 additions & 0 deletions src/main/java/vivatech/rei/widget/ProgressBarWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package vivatech.rei.widget;

import io.github.cottonmc.cotton.gui.widget.WBar;
import me.shedaniel.rei.gui.widget.Widget;
import net.minecraft.client.gui.Element;
import net.minecraft.client.render.GuiLighting;
import net.minecraft.container.PropertyDelegate;
import vivatech.VivatechClient;

import java.util.Collections;
import java.util.List;

/**
* A progress bar widget that uses Cotton's {@link WBar} for drawing.
*/
public final class ProgressBarWidget extends Widget {
private final int x;
private final int y;
private final WBar renderBar;

public ProgressBarWidget(int x, int y, int width, int height, int maxProgress) {
this.x = x;
this.y = y;
PropertyDelegate renderProperties = new PropertyDelegate() {
@Override
public int get(int i) {
if (i == 0)
return ((int) System.currentTimeMillis() / 10) % maxProgress;
else
return maxProgress;
}

@Override
public void set(int i, int value) {
}

@Override
public int size() {
return 2;
}
};
renderBar = new RenderWBar(VivatechClient.PROGRESS_BAR_BG, VivatechClient.PROGRESS_BAR, 0, 1, WBar.Direction.RIGHT, renderProperties);
renderBar.setSize(width, height);
}

@Override
public void render(int mouseX, int mouseY, float delta) {
GuiLighting.disable();
renderBar.paintBackground(x, y);
}

@Override
public List<? extends Element> children() {
return Collections.emptyList();
}
}
15 changes: 15 additions & 0 deletions src/main/java/vivatech/rei/widget/RenderWBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package vivatech.rei.widget;

import io.github.cottonmc.cotton.gui.widget.WBar;
import net.minecraft.container.PropertyDelegate;
import net.minecraft.util.Identifier;

/**
* A {@link WBar} subclass that sets the property delegate through the constructor.
*/
final class RenderWBar extends WBar {
RenderWBar(Identifier bg, Identifier bar, int field, int maxfield, Direction dir, PropertyDelegate properties) {
super(bg, bar, field, maxfield, dir);
this.properties = properties;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/assets/vivatech/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"block.vivatech.electric_furnace": "Electric Furnace",
"block.vivatech.energy_bank": "Energy Bank",
"block.vivatech.press": "Press",
"item.vivatech.battery": "Battery"
"item.vivatech.battery": "Battery",
"gui.vivatech.crushing": "Crushing",
"gui.vivatech.pressing": "Pressing"
}
3 changes: 3 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
],
"client": [
"vivatech.VivatechClient"
],
"rei_plugins": [
"vivatech.rei.VivatechREIPlugin"
]
},

Expand Down

0 comments on commit 3d29bd9

Please sign in to comment.