Improve REI Plugin
All checks were successful
EnergonRelics/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-08-03 13:00:08 -04:00
parent 9ec92ee4d9
commit 2ca8870775
9 changed files with 96 additions and 29 deletions

View File

@ -1,9 +1,12 @@
package com.thebrokenrail.energonrelics.block.entity.infuser; package com.thebrokenrail.energonrelics.block.entity.infuser;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -13,6 +16,9 @@ import net.minecraft.world.explosion.Explosion;
interface InfuserAction { interface InfuserAction {
void run(World world, BlockPos pos); void run(World world, BlockPos pos);
@Environment(EnvType.CLIENT)
InfuserOutputItem display();
class ItemAction implements InfuserAction { class ItemAction implements InfuserAction {
final ItemStack stack; final ItemStack stack;
@ -28,6 +34,12 @@ interface InfuserAction {
public void run(World world, BlockPos pos) { public void run(World world, BlockPos pos) {
Block.dropStack(world, pos, stack); Block.dropStack(world, pos, stack);
} }
@Environment(EnvType.CLIENT)
@Override
public InfuserOutputItem display() {
return new InfuserOutputItem(true, stack);
}
} }
class ExplosionAction implements InfuserAction { class ExplosionAction implements InfuserAction {
@ -36,6 +48,12 @@ interface InfuserAction {
world.setBlockState(pos, Blocks.AIR.getDefaultState()); world.setBlockState(pos, Blocks.AIR.getDefaultState());
world.createExplosion(null, pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, 3f, true, Explosion.DestructionType.DESTROY); world.createExplosion(null, pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, 3f, true, Explosion.DestructionType.DESTROY);
} }
@Environment(EnvType.CLIENT)
@Override
public InfuserOutputItem display() {
return new InfuserOutputItem(false, new ItemStack(Items.TNT));
}
} }
class ParticleAction implements InfuserAction { class ParticleAction implements InfuserAction {
@ -43,5 +61,11 @@ interface InfuserAction {
public void run(World world, BlockPos pos) { public void run(World world, BlockPos pos) {
((ServerWorld) world).spawnParticles(ParticleTypes.SMOKE, pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, 38, 0.31f, 0.31f, 0.31f, 0f); ((ServerWorld) world).spawnParticles(ParticleTypes.SMOKE, pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, 38, 0.31f, 0.31f, 0.31f, 0f);
} }
@Environment(EnvType.CLIENT)
@Override
public InfuserOutputItem display() {
return new InfuserOutputItem(false, new ItemStack(Items.FIRE_CHARGE));
}
} }
} }

View File

@ -1,7 +1,8 @@
package com.thebrokenrail.energonrelics.block.entity.infuser; package com.thebrokenrail.energonrelics.block.entity.infuser;
import com.thebrokenrail.energonrelics.util.WeightedList; import com.thebrokenrail.energonrelics.util.WeightedList;
import net.minecraft.item.ItemStack; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -39,18 +40,18 @@ public class InfuserEntry {
weightedList.pick(world.random).run(world, pos); weightedList.pick(world.random).run(world, pos);
} }
public List<ItemStack> getOutputItems(boolean isFail) { @Environment(EnvType.CLIENT)
public List<InfuserOutputItem> getOutputItems(boolean isFail) {
InfuserAction[] actionList = isFail ? fail : success; InfuserAction[] actionList = isFail ? fail : success;
List<ItemStack> list = new ArrayList<>(); List<InfuserOutputItem> list = new ArrayList<>();
for (InfuserAction action : actionList) { for (InfuserAction action : actionList) {
if (action instanceof InfuserAction.ItemAction) { list.add(action.display());
list.add(((InfuserAction.ItemAction) action).stack);
}
} }
return list; return list;
} }
@Environment(EnvType.CLIENT)
public double getSingleChance(boolean isFail) { public double getSingleChance(boolean isFail) {
return (1d / (double) success.length) * (isFail ? 1d - successChance : successChance); return (1d / (double) (isFail ? fail.length : success.length)) * (isFail ? 1d - successChance : successChance);
} }
} }

View File

@ -0,0 +1,16 @@
package com.thebrokenrail.energonrelics.block.entity.infuser;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.item.ItemStack;
@Environment(EnvType.CLIENT)
public class InfuserOutputItem {
public final boolean outputsItem;
public final ItemStack stack;
public InfuserOutputItem(boolean outputsItem, ItemStack item) {
this.outputsItem = outputsItem;
this.stack = item;
}
}

View File

@ -61,6 +61,9 @@ public class InfuserRegistry {
add(Items.COAL, new InfuserEntry(340, 0.2d, new InfuserAction[]{new InfuserAction.ItemAction(Items.DIAMOND)}, new InfuserAction[]{new InfuserAction.ExplosionAction()})); add(Items.COAL, new InfuserEntry(340, 0.2d, new InfuserAction[]{new InfuserAction.ItemAction(Items.DIAMOND)}, new InfuserAction[]{new InfuserAction.ExplosionAction()}));
add(EnergonRelics.VERIDIUM_INGOT_ITEM, new InfuserEntry(240, 0.25d, new InfuserAction[]{new InfuserAction.ItemAction(EnergonRelics.VERIDIUM_ORB_ITEM)}, new InfuserAction[]{new InfuserAction.ParticleAction(), new InfuserAction.ParticleAction(), new InfuserAction.ExplosionAction()})); add(EnergonRelics.VERIDIUM_INGOT_ITEM, new InfuserEntry(240, 0.25d, new InfuserAction[]{new InfuserAction.ItemAction(EnergonRelics.VERIDIUM_ORB_ITEM)}, new InfuserAction[]{new InfuserAction.ParticleAction(), new InfuserAction.ParticleAction(), new InfuserAction.ExplosionAction()}));
add(Items.GOLD_NUGGET, new InfuserEntry(260, 0.3d, new InfuserAction[]{new InfuserAction.ItemAction(Items.GOLD_INGOT)}, new InfuserAction[]{new InfuserAction.ItemAction(Items.IRON_NUGGET), new InfuserAction.ParticleAction()}));
add(Items.IRON_NUGGET, new InfuserEntry(260, 0.3d, new InfuserAction[]{new InfuserAction.ItemAction(Items.IRON_INGOT)}, new InfuserAction[]{new InfuserAction.ParticleAction()}));
} }
private static void addTransform(Item[] items, long cost, double successChance) { private static void addTransform(Item[] items, long cost, double successChance) {

View File

@ -2,7 +2,9 @@ package com.thebrokenrail.energonrelics.client.rei;
import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserEntry; import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserEntry;
import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserOutputItem;
import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserRegistry; import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserRegistry;
import com.thebrokenrail.energonrelics.util.BooleanIterator;
import me.shedaniel.rei.api.EntryStack; import me.shedaniel.rei.api.EntryStack;
import me.shedaniel.rei.api.RecipeHelper; import me.shedaniel.rei.api.RecipeHelper;
import me.shedaniel.rei.api.plugins.REIPluginV0; import me.shedaniel.rei.api.plugins.REIPluginV0;
@ -32,14 +34,12 @@ public class EnergonRelicsPlugin implements REIPluginV0 {
@Override @Override
public void registerRecipeDisplays(RecipeHelper recipeHelper) { public void registerRecipeDisplays(RecipeHelper recipeHelper) {
for (Map.Entry<Item, InfuserEntry> entry : InfuserRegistry.entrySet()) { for (Map.Entry<Item, InfuserEntry> entry : InfuserRegistry.entrySet()) {
List<ItemStack> success = entry.getValue().getOutputItems(false); BooleanIterator.run(isFail -> {
if (success.size() > 0) { List<InfuserOutputItem> list = entry.getValue().getOutputItems(isFail);
recipeHelper.registerDisplay(new InfuserDisplay(new ItemStack(entry.getKey()), success, entry.getValue().getSingleChance(false), entry.getValue().cost)); for (InfuserOutputItem item : list) {
} recipeHelper.registerDisplay(new InfuserDisplay(new ItemStack(entry.getKey()), item, entry.getValue().getSingleChance(isFail), entry.getValue().cost));
List<ItemStack> fail = entry.getValue().getOutputItems(true); }
if (fail.size() > 0) { });
recipeHelper.registerDisplay(new InfuserDisplay(new ItemStack(entry.getKey()), fail, entry.getValue().getSingleChance(true), entry.getValue().cost));
}
} }
} }

View File

@ -11,8 +11,10 @@ import me.shedaniel.rei.gui.widget.Widget;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.minecraft.client.resource.language.I18n; import net.minecraft.client.resource.language.I18n;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,12 +27,21 @@ public class InfuserCategory implements RecipeCategory<InfuserDisplay> {
Point startPoint = new Point(bounds.getCenterX() - 41, bounds.y + 10); Point startPoint = new Point(bounds.getCenterX() - 41, bounds.y + 10);
List<Widget> widgets = new ArrayList<>(); List<Widget> widgets = new ArrayList<>();
widgets.add(Widgets.createRecipeBase(bounds)); widgets.add(Widgets.createRecipeBase(bounds));
widgets.add(Widgets.createResultSlotBackground(new Point(startPoint.x + 61, startPoint.y + 26)));
widgets.add(Widgets.createLabel(new Point(bounds.x + bounds.width / 2, bounds.y + 9), new TranslatableText("category.rei." + EnergonRelics.NAMESPACE + ".infuser.chance", new DecimalFormat("###.##").format(display.successChance * 100d))).noShadow().centered().color(0xFF404040, 0xFFBBBBBB)); widgets.add(Widgets.createLabel(new Point(bounds.x + bounds.width / 2, bounds.y + 9), new TranslatableText("category.rei." + EnergonRelics.NAMESPACE + ".infuser.chance", new DecimalFormat("###.##").format(display.successChance * 100d))).noShadow().centered().color(0xFF404040, 0xFFBBBBBB));
widgets.add(Widgets.createLabel(new Point(bounds.x + bounds.width / 2, bounds.y + 18), new TranslatableText("category.rei." + EnergonRelics.NAMESPACE + ".infuser.cost", display.cost)).noShadow().centered().color(0xFF404040, 0xFFBBBBBB)); widgets.add(Widgets.createLabel(new Point(bounds.x + bounds.width / 2, bounds.y + 18), new TranslatableText("category.rei." + EnergonRelics.NAMESPACE + ".infuser.cost", display.cost)).noShadow().centered().color(0xFF404040, 0xFFBBBBBB));
widgets.add(Widgets.createArrow(new Point(startPoint.x + 24, startPoint.y + 25)).animationDurationTicks(HardcodedConfig.INFUSER_TIME)); widgets.add(Widgets.createArrow(new Point(startPoint.x + 24, startPoint.y + 25)).animationDurationTicks(HardcodedConfig.INFUSER_TIME));
widgets.add(Widgets.createSlot(new Point(startPoint.x + 1, startPoint.y + 26)).entries(display.getInputEntries().get(0)).markInput()); widgets.add(Widgets.createSlot(new Point(startPoint.x + 1, startPoint.y + 26)).entries(display.getInputEntries().get(0)).markInput());
widgets.add(Widgets.createSlot(new Point(startPoint.x + 61, startPoint.y + 26)).entries(display.getOutputEntries()).disableBackground().markOutput());
int outputX = startPoint.x + 61;
int outputY = startPoint.y + 26;
if (display.outputsItem) {
widgets.add(Widgets.createResultSlotBackground(new Point(startPoint.x + 61, startPoint.y + 26)));
widgets.add(Widgets.createSlot(new Point(outputX, outputY)).entries(display.getOutputEntries()).disableBackground().markOutput());
} else {
Identifier outputID = Registry.ITEM.getId(display.getOutputEntries().get(0).getItem());
Text text = new TranslatableText("category.rei." + EnergonRelics.NAMESPACE + ".infuser.display_item." + outputID.getNamespace() + "." + outputID.getPath());
widgets.add(Widgets.createLabel(new Point(outputX - 7, outputY + 3), text).noShadow().leftAligned().color(0xFF404040, 0xFFBBBBBB));
}
return widgets; return widgets;
} }

View File

@ -1,32 +1,28 @@
package com.thebrokenrail.energonrelics.client.rei; package com.thebrokenrail.energonrelics.client.rei;
import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserOutputItem;
import me.shedaniel.rei.api.EntryStack; import me.shedaniel.rei.api.EntryStack;
import me.shedaniel.rei.api.RecipeDisplay; import me.shedaniel.rei.api.RecipeDisplay;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class InfuserDisplay implements RecipeDisplay { public class InfuserDisplay implements RecipeDisplay {
private final EntryStack input; private final EntryStack input;
private final List<EntryStack> output; private final EntryStack output;
public final boolean outputsItem;
public final double successChance; public final double successChance;
public final long cost; public final long cost;
public InfuserDisplay(ItemStack input, List<ItemStack> output, double successChance, long cost) { public InfuserDisplay(ItemStack input, InfuserOutputItem output, double successChance, long cost) {
this.input = EntryStack.create(input); this.input = EntryStack.create(input);
List<EntryStack> newList = new ArrayList<>(); this.output = EntryStack.create(output.stack.copy());
for (ItemStack outputStack : output) { outputsItem = output.outputsItem;
newList.add(EntryStack.create(outputStack));
}
this.output = newList;
this.successChance = successChance; this.successChance = successChance;
this.cost = cost; this.cost = cost;
} }
@ -38,7 +34,7 @@ public class InfuserDisplay implements RecipeDisplay {
@Override @Override
public List<EntryStack> getOutputEntries() { public List<EntryStack> getOutputEntries() {
return output; return Collections.singletonList(output);
} }
@Override @Override

View File

@ -0,0 +1,14 @@
package com.thebrokenrail.energonrelics.util;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import java.util.function.Consumer;
@Environment(EnvType.CLIENT)
public class BooleanIterator {
public static void run(Consumer<Boolean> function) {
function.accept(false);
function.accept(true);
}
}

View File

@ -57,5 +57,7 @@
"block.energonrelics.energy_projector": "Energy Projector", "block.energonrelics.energy_projector": "Energy Projector",
"block.energonrelics.energy_beam": "Energy Beam", "block.energonrelics.energy_beam": "Energy Beam",
"block.energonrelics.energy_portal": "Energy Portal", "block.energonrelics.energy_portal": "Energy Portal",
"block.energonrelics.energized_obsidian": "Energized Obsidian" "block.energonrelics.energized_obsidian": "Energized Obsidian",
"category.rei.energonrelics.infuser.display_item.minecraft.tnt": "Explosion",
"category.rei.energonrelics.infuser.display_item.minecraft.fire_charge": "Nothing"
} }