Skip to content

Commit

Permalink
clean up remainders loader, update for new transfer api
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed Nov 5, 2022
1 parent 2a5ad12 commit ec0c5bc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_base_version=1.19
minecraft_version=1.19
yarn_mappings=1.19+build.1
loader_version=0.14.7
minecraft_version=1.19.2
yarn_mappings=1.19.2+build.28
loader_version=0.14.10
# Mod Properties
mod_version=2.0.16
mod_version=2.0.17
maven_group=wraith
archives_base_name=wraith-alloy-forgery

# Dependencies
fabric_version=0.57.0+1.19
fabric_version=0.64.0+1.19.2

# https://maven.shedaniel.me/me/shedaniel/RoughlyEnoughItems-fabric/
rei_version=9.0.472
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import wraith.alloyforgery.AlloyForgeScreenHandler;
import wraith.alloyforgery.AlloyForgery;
Expand Down Expand Up @@ -154,7 +153,7 @@ public void tick() {

world.updateComparators(pos, getCachedState().getBlock());

if (!verifyMultiblock()) {
if (!this.verifyMultiblock()) {
this.currentSmeltTime = 0;

final var currentState = world.getBlockState(pos);
Expand All @@ -164,13 +163,13 @@ public void tick() {
return;
}

if (!getFuelStack().isEmpty()) {
final var fuelStack = getFuelStack();
if (!this.getFuelStack().isEmpty()) {
final var fuelStack = this.getFuelStack();
final var fuelDefinition = ForgeFuelRegistry.getFuelForItem(fuelStack.getItem());

if (fuelDefinition != ForgeFuelRegistry.ForgeFuelDefinition.EMPTY && canAddFuel(fuelDefinition.fuel())) {

if (!ItemOps.emptyAwareDecrement(getFuelStack())) {
if (!ItemOps.emptyAwareDecrement(this.getFuelStack())) {
setStack(11, fuelDefinition.hasReturnType() ? new ItemStack(fuelDefinition.returnType()) : ItemStack.EMPTY);
}

Expand All @@ -193,28 +192,28 @@ public void tick() {
}

if (!this.isEmpty()) {
final var recipeOptional = world.getRecipeManager().getFirstMatch(AlloyForgeRecipe.Type.INSTANCE, this, world);
final var recipeOptional = this.world.getRecipeManager().getFirstMatch(AlloyForgeRecipe.Type.INSTANCE, this, world);

if (recipeOptional.isEmpty()) {
this.currentSmeltTime = 0;
} else {
final var recipe = recipeOptional.get();
if (recipe.getMinForgeTier() > forgeDefinition.forgeTier()) {
if (recipe.getMinForgeTier() > this.forgeDefinition.forgeTier()) {
this.currentSmeltTime = 0;
return;
}

final var outputStack = this.getStack(10);
final var recipeOutput = recipe.getOutput(forgeDefinition.forgeTier());
final var recipeOutput = recipe.getOutput(this.forgeDefinition.forgeTier());

if (!outputStack.isEmpty() && !ItemOps.canStack(outputStack, recipeOutput)) {
this.currentSmeltTime = 0;
return;
}

if (this.currentSmeltTime < forgeDefinition.maxSmeltTime()) {
if (this.currentSmeltTime < this.forgeDefinition.maxSmeltTime()) {

final float fuelRequirement = recipe.getFuelPerTick() * forgeDefinition.speedMultiplier();
final float fuelRequirement = recipe.getFuelPerTick() * this.forgeDefinition.speedMultiplier();
if (this.fuel - fuelRequirement < 0) {
this.currentSmeltTime = 0;
return;
Expand All @@ -227,7 +226,7 @@ public void tick() {
AlloyForgery.FORGE_PARTICLES.spawn(world, Vec3d.of(pos), facing);
}
} else {
var remainderList = recipe.attemptToGetRemainders(this);
var remainderList = recipe.gatherRemainders(this);

recipe.craft(this);

Expand All @@ -247,7 +246,7 @@ public void tick() {
}
}

public void handleForgingRemainders(DefaultedList<ItemStack> remainderList) {
private void handleForgingRemainders(DefaultedList<ItemStack> remainderList) {
for (int i = 0; i < remainderList.size(); ++i) {
var inputStack = this.getStack(i);
var remainderStack = remainderList.get(i);
Expand All @@ -265,22 +264,20 @@ public void handleForgingRemainders(DefaultedList<ItemStack> remainderList) {
var insertStack = remainderStack.copy();
insertStack.setCount(excess);

if(!attemptToInsertIntoHopper(insertStack)){
if(!this.attemptToInsertIntoHopper(insertStack)){
var frontForgePos = pos.offset(getCachedState().get(ForgeControllerBlock.FACING));

world.playSound(null, frontForgePos.getX(), frontForgePos.getY(), frontForgePos.getZ(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 1.0F, 0.2F);

ItemScatterer.spawn(world, frontForgePos.getX(), frontForgePos.getY(), frontForgePos.getZ(), insertStack);
}
}

this.setStack(i, remainderStack);
} else {
if(!attemptToInsertIntoHopper(remainderStack)){
if(!this.attemptToInsertIntoHopper(remainderStack)){
var frontForgePos = pos.offset(getCachedState().get(ForgeControllerBlock.FACING));

world.playSound(null, frontForgePos.getX(), frontForgePos.getY(), frontForgePos.getZ(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 1.0F, 0.2F);

ItemScatterer.spawn(world, frontForgePos.getX(), frontForgePos.getY(), frontForgePos.getZ(), remainderStack);
}
}
Expand All @@ -289,7 +286,7 @@ public void handleForgingRemainders(DefaultedList<ItemStack> remainderList) {
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean attemptToInsertIntoHopper(ItemStack remainderStack){
private boolean attemptToInsertIntoHopper(ItemStack remainderStack){
if (remainderStack.isEmpty()) return true;

HopperBlockEntity blockEntity = null;
Expand Down Expand Up @@ -351,7 +348,7 @@ public boolean verifyMultiblock() {
return true;
}

public static ImmutableList<BlockPos> generateMultiblockPositions(BlockPos controllerPos, Direction controllerFacing) {
private static ImmutableList<BlockPos> generateMultiblockPositions(BlockPos controllerPos, Direction controllerFacing) {
final List<BlockPos> posses = new ArrayList<>();
final BlockPos center = controllerPos.offset(controllerFacing.getOpposite());

Expand Down Expand Up @@ -444,6 +441,11 @@ protected boolean canInsert(FluidVariant variant) {
protected boolean canExtract(FluidVariant variant) {
return false;
}

@Override
public Iterator<StorageView<FluidVariant>> iterator() {
return InsertionOnlyStorage.super.iterator();
}
}

public static class Type extends BlockEntityType<ForgeControllerBlockEntity> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager mana
prepared.forEach((identifier, jsonElement) -> {
try {
if(jsonElement instanceof JsonObject jsonObject){
if(!jsonObject.has("remainders")){
throw new JsonSyntaxException("The Global Remainders file seems to be missing the needed remainders field.");
}

var remainders = new HashMap<Item, ItemStack>();

for (var remainderEntry : jsonObject.getAsJsonObject("remainders").entrySet()) {
for (var remainderEntry : JsonHelper.getObject(jsonObject, "remainders").entrySet()) {
var item = JsonHelper.asItem(new JsonPrimitive(remainderEntry.getKey()), remainderEntry.getKey());

if (remainderEntry.getValue().isJsonObject()) {
Expand All @@ -53,10 +49,10 @@ protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager mana

AlloyForgeRecipe.addRemainders(remainders);
} else {
throw new JsonSyntaxException("JsonElement wasn't a JsonObject meaning it is malformed");
throw new JsonSyntaxException("Expected alloy forge remainders definition to be a json object");
}
} catch (IllegalArgumentException | JsonParseException exception) {
LOGGER.error("[AlloyForgerRemainders]: Parsing error loading recipe {}", identifier, exception);
LOGGER.error("[AlloyForgeRemainders]: Parsing error loading recipe {}", identifier, exception);
}
});
}
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/wraith/alloyforgery/recipe/AlloyForgeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class AlloyForgeRecipe implements Recipe<Inventory> {

private static final Map<Item, ItemStack> GLOBAL_RECIPE_REMAINDER = new HashMap<>();
private static final Map<Item, ItemStack> GLOBAL_REMAINDERS = new HashMap<>();

private static final List<Integer> INPUT_SLOT_INDICES = IntStream.rangeClosed(0, 9).boxed().toList();

Expand Down Expand Up @@ -84,7 +84,7 @@ public void finishRecipe(PendingRecipeData pendingData) {
}

public static void addRemainders(Map<Item, ItemStack> remainders){
GLOBAL_RECIPE_REMAINDER.putAll(remainders);
GLOBAL_REMAINDERS.putAll(remainders);
}

@Override
Expand Down Expand Up @@ -157,22 +157,20 @@ public Map<Ingredient, Integer> getIngredientsMap() {

@Override
public ItemStack craft(Inventory inventory) {
tryBind(inventory).forEach(inventory::removeStack);

this.tryBind(inventory).forEach(inventory::removeStack);
return ItemStack.EMPTY;
}

@Nullable
public DefaultedList<ItemStack> attemptToGetRemainders(Inventory inventory) {
public DefaultedList<ItemStack> gatherRemainders(Inventory inventory) {
final var remainders = DefaultedList.ofSize(inventory.size(), ItemStack.EMPTY);

final var owoRemainders = RecipeRemainderStorage.has(this.getId()) ? RecipeRemainderStorage.get(this.getId()) : Map.<Item, ItemStack>of();

if(owoRemainders.isEmpty() && GLOBAL_RECIPE_REMAINDER.isEmpty()) return null;
if(owoRemainders.isEmpty() && GLOBAL_REMAINDERS.isEmpty()) return null;

var setAnyRemainders = false;

for (int i : tryBind(inventory).keySet()) {
for (int i : this.tryBind(inventory).keySet()) {
var item = inventory.getStack(i).getItem();

if (!owoRemainders.isEmpty()) {
Expand All @@ -181,8 +179,8 @@ public DefaultedList<ItemStack> attemptToGetRemainders(Inventory inventory) {
remainders.set(i, owoRemainders.get(item).copy());

setAnyRemainders = true;
} else if(GLOBAL_RECIPE_REMAINDER.containsKey(item)){
remainders.set(i, GLOBAL_RECIPE_REMAINDER.get(item).copy());
} else if(GLOBAL_REMAINDERS.containsKey(item)){
remainders.set(i, GLOBAL_REMAINDERS.get(item).copy());

setAnyRemainders = true;
}
Expand Down

0 comments on commit ec0c5bc

Please sign in to comment.