Skip to content

Commit

Permalink
The Crushing Wheel
Browse files Browse the repository at this point in the history
- Crushing wheels can now apply recipes to contained items
- Added a new generic recipe type for processing
- Improved Crushing Wheel model
- Improved gui models of large wheels
- Improved gui models of harvesters and drills
- Added several crushing recipes
- Fixed Water wheel updating rotation speeds incosistently
  • Loading branch information
simibubi committed Aug 22, 2019
1 parent 911363c commit efcdd3c
Show file tree
Hide file tree
Showing 68 changed files with 2,059 additions and 275 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/simibubi/create/AllBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.simibubi.create.modules.contraptions.generators.MotorBlock;
import com.simibubi.create.modules.contraptions.generators.WaterWheelBlock;
import com.simibubi.create.modules.contraptions.receivers.CrushingWheelBlock;
import com.simibubi.create.modules.contraptions.receivers.CrushingWheelControllerBlock;
import com.simibubi.create.modules.contraptions.receivers.DrillBlock;
import com.simibubi.create.modules.contraptions.receivers.HarvesterBlock;
import com.simibubi.create.modules.contraptions.receivers.TurntableBlock;
Expand Down Expand Up @@ -66,6 +67,7 @@ public enum AllBlocks {
TURNTABLE(new TurntableBlock()),
HALF_AXIS(new HalfAxisBlock()),
CRUSHING_WHEEL(new CrushingWheelBlock()),
CRUSHING_WHEEL_CONTROLLER(new CrushingWheelControllerBlock()),

MECHANICAL_PISTON(new MechanicalPistonBlock(false)),
STICKY_MECHANICAL_PISTON(new MechanicalPistonBlock(true)),
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/com/simibubi/create/AllRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,48 @@

import java.util.function.Supplier;

import com.simibubi.create.modules.contraptions.base.ProcessingRecipeSerializer;
import com.simibubi.create.modules.contraptions.receivers.CrushingRecipe;
import com.simibubi.create.modules.curiosities.placementHandgun.BuilderGunUpgradeRecipe;

import net.minecraft.inventory.IInventory;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
import net.minecraftforge.event.RegistryEvent;

public enum AllRecipes {

Placement_Handgun_Upgrade(BuilderGunUpgradeRecipe.Serializer::new),

Placement_Handgun_Upgrade(BuilderGunUpgradeRecipe.Serializer::new, IRecipeType.CRAFTING),

Crushing(() -> {
return new ProcessingRecipeSerializer<>(CrushingRecipe::new);
}, Types.CRUSHING),

;

public static class Types {
public static IRecipeType<CrushingRecipe> CRUSHING = register("crushing");

static <T extends IRecipe<?>> IRecipeType<T> register(final String key) {
return Registry.register(Registry.RECIPE_TYPE, new ResourceLocation(key), new IRecipeType<T>() {
public String toString() {
return key;
}
});
}
}

public IRecipeSerializer<?> serializer;
public Supplier<IRecipeSerializer<?>> supplier;
public IRecipeType<? extends IRecipe<? extends IInventory>> type;

private AllRecipes(Supplier<IRecipeSerializer<?>> supplier) {
private AllRecipes(Supplier<IRecipeSerializer<?>> supplier,
IRecipeType<? extends IRecipe<? extends IInventory>> type) {
this.supplier = supplier;
this.type = type;
}

public static void register(RegistryEvent.Register<IRecipeSerializer<?>> event) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/simibubi/create/AllTileEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.simibubi.create.modules.contraptions.generators.MotorTileEntity;
import com.simibubi.create.modules.contraptions.generators.MotorTileEntityRenderer;
import com.simibubi.create.modules.contraptions.generators.WaterWheelTileEntity;
import com.simibubi.create.modules.contraptions.receivers.CrushingWheelControllerTileEntity;
import com.simibubi.create.modules.contraptions.receivers.CrushingWheelTileEntity;
import com.simibubi.create.modules.contraptions.receivers.DrillTileEntity;
import com.simibubi.create.modules.contraptions.receivers.TurntableTileEntity;
Expand Down Expand Up @@ -53,6 +54,7 @@ public enum AllTileEntities {
MECHANICAL_PISTON(MechanicalPistonTileEntity::new, AllBlocks.MECHANICAL_PISTON, AllBlocks.STICKY_MECHANICAL_PISTON),
DRILL(DrillTileEntity::new, AllBlocks.DRILL),
CRUSHING_WHEEL(CrushingWheelTileEntity::new, AllBlocks.CRUSHING_WHEEL),
CRUSHING_WHEEL_CONTROLLER(CrushingWheelControllerTileEntity::new, AllBlocks.CRUSHING_WHEEL_CONTROLLER),
WATER_WHEEL(WaterWheelTileEntity::new, AllBlocks.WATER_WHEEL),

;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.simibubi.create.modules.contraptions.base;

import java.util.ArrayList;
import java.util.List;

import com.simibubi.create.AllRecipes;

import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;

public abstract class ProcessingRecipe<T extends IInventory> implements IRecipe<T> {
protected final List<Ingredient> ingredients;
protected final List<StochasticOutput> results;
private final IRecipeType<?> type;
private final IRecipeSerializer<?> serializer;
protected final ResourceLocation id;
protected final String group;
protected final int processingDuration;

public ProcessingRecipe(AllRecipes recipeType, ResourceLocation id, String group,
List<Ingredient> ingredients, List<StochasticOutput> results, int processingDuration) {
this.type = recipeType.type;
this.serializer = recipeType.serializer;
this.id = id;
this.group = group;
this.ingredients = ingredients;
this.results = results;
this.processingDuration = processingDuration;
}

@Override
public NonNullList<Ingredient> getIngredients() {
NonNullList<Ingredient> nonnulllist = NonNullList.create();
nonnulllist.addAll(this.ingredients);
return nonnulllist;
}

public int getProcessingDuration() {
return processingDuration;
}

public List<StochasticOutput> getAllResults() {
return results;
}

public List<ItemStack> rollResults() {
List<ItemStack> stacks = new ArrayList<>();
for (StochasticOutput output : results) {
ItemStack stack = output.rollOutput();
if (!stack.isEmpty())
stacks.add(stack);
}
return stacks;
}

@Override
public ItemStack getCraftingResult(T inv) {
return getRecipeOutput();
}

@Override
public boolean canFit(int width, int height) {
return true;
}

@Override
public ItemStack getRecipeOutput() {
return results.isEmpty() ? ItemStack.EMPTY : results.get(0).getStack();
}

@Override
public ResourceLocation getId() {
return id;
}

@Override
public IRecipeSerializer<?> getSerializer() {
return serializer;
}

@Override
public String getGroup() {
return group;
}

@Override
public IRecipeType<?> getType() {
return type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.simibubi.create.modules.contraptions.base;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;

public class ProcessingRecipeSerializer<T extends ProcessingRecipe<?>>
extends net.minecraftforge.registries.ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<T> {

protected final IRecipeFactory<T> factory;

public ProcessingRecipeSerializer(IRecipeFactory<T> factory) {
this.factory = factory;
}

@SuppressWarnings("deprecation")
public T read(ResourceLocation recipeId, JsonObject json) {
String s = JSONUtils.getString(json, "group", "");

List<Ingredient> ingredients = new ArrayList<>();
for (JsonElement e : JSONUtils.getJsonArray(json, "ingredients")) {
ingredients.add(Ingredient.deserialize(e));
}

List<StochasticOutput> results = new ArrayList<>();
for (JsonElement e : JSONUtils.getJsonArray(json, "results")) {
String s1 = JSONUtils.getString(e.getAsJsonObject().get("item"), "item");
int i = JSONUtils.getInt(e.getAsJsonObject().get("count"), "count");
float chance = 1;
if (JSONUtils.hasField((JsonObject) e, "chance"))
chance = JSONUtils.getFloat(e.getAsJsonObject().get("chance"), "chance");
ItemStack itemstack = new ItemStack(Registry.ITEM.getOrDefault(new ResourceLocation(s1)), i);
results.add(new StochasticOutput(itemstack, chance));
}

int duration = JSONUtils.getInt(json, "processingTime");

return this.factory.create(recipeId, s, ingredients, results, duration);
}

public T read(ResourceLocation recipeId, PacketBuffer buffer) {
String s = buffer.readString(32767);

List<Ingredient> ingredients = new ArrayList<>();
for (int i = 0; i < buffer.readInt(); i++)
ingredients.add(Ingredient.read(buffer));

List<StochasticOutput> results = new ArrayList<>();
for (int i = 0; i < buffer.readInt(); i++)
results.add(StochasticOutput.read(buffer));

int duration = buffer.readInt();

return this.factory.create(recipeId, s, ingredients, results, duration);
}

public void write(PacketBuffer buffer, T recipe) {
buffer.writeString(recipe.group);

buffer.writeInt(recipe.ingredients.size());
recipe.ingredients.forEach(i -> i.write(buffer));

buffer.writeInt(recipe.results.size());
recipe.results.forEach(i -> i.write(buffer));

buffer.writeInt(recipe.processingDuration);
}

public interface IRecipeFactory<T extends ProcessingRecipe<?>> {
T create(ResourceLocation id, String group, List<Ingredient> ingredients, List<StochasticOutput> results, int duration);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.simibubi.create.modules.contraptions.base;

import java.util.Random;

import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;

public class StochasticOutput {

private static Random r = new Random();
private ItemStack stack;
private float chance;

public StochasticOutput(ItemStack stack, float chance) {
this.stack = stack;
this.chance = chance;
}

public ItemStack getStack() {
return stack;
}

public float getChance() {
return chance;
}

public ItemStack rollOutput() {
int outputAmount = stack.getCount();
for (int roll = 0; roll < stack.getCount(); roll++)
if (r.nextFloat() > chance)
outputAmount--;
return outputAmount > 0 ? new ItemStack(stack.getItem(), outputAmount) : ItemStack.EMPTY;
}

public void write(PacketBuffer buf) {
buf.writeItemStack(getStack());
buf.writeFloat(getChance());
}

public static StochasticOutput read(PacketBuffer buf) {
return new StochasticOutput(buf.readItemStack(), buf.readFloat());
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package com.simibubi.create.modules.contraptions.generators;

import static net.minecraft.util.Direction.DOWN;
import static net.minecraft.util.Direction.EAST;
import static net.minecraft.util.Direction.NORTH;
import static net.minecraft.util.Direction.SOUTH;
import static net.minecraft.util.Direction.UP;
import static net.minecraft.util.Direction.WEST;

import com.simibubi.create.AllBlocks;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.modules.contraptions.base.HorizontalKineticBlock;

import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -47,7 +39,7 @@ public BlockRenderLayer getRenderLayer() {
protected boolean hasStaticPart() {
return false;
}

@Override
public boolean isValidPosition(BlockState state, IWorldReader worldIn, BlockPos pos) {
for (Direction direction : Direction.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public void updateSpeed() {
speed += i;

if (this.speed != speed) {
RotationPropagator.handleRemoved(world, pos, this);
this.setSpeed(speed);
hasFlows = speed != 0;
notifyBlockUpdate();
RotationPropagator.handleRemoved(world, pos, this);
RotationPropagator.handleAdded(world, pos, this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.simibubi.create.modules.contraptions.receivers;

import java.util.List;

import com.simibubi.create.AllRecipes;
import com.simibubi.create.modules.contraptions.base.ProcessingRecipe;
import com.simibubi.create.modules.contraptions.base.StochasticOutput;
import com.simibubi.create.modules.contraptions.receivers.CrushingWheelControllerTileEntity.Inventory;

import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

public class CrushingRecipe extends ProcessingRecipe<CrushingWheelControllerTileEntity.Inventory> {

public CrushingRecipe(ResourceLocation id, String group, List<Ingredient> ingredients,
List<StochasticOutput> results, int processingDuration) {
super(AllRecipes.Crushing, id, group, ingredients, results, processingDuration);
}

@Override
public boolean matches(Inventory inv, World worldIn) {
if (inv.isEmpty())
return false;
return ingredients.get(0).test(inv.getStackInSlot(0));
}

}
Loading

0 comments on commit efcdd3c

Please sign in to comment.