Skip to content

Commit

Permalink
update bacterial analyzer texture, laser junction caching, fix aquati…
Browse files Browse the repository at this point in the history
…c catalyst blockentity bugs
  • Loading branch information
Thepigcat76 committed Jan 2, 2025
1 parent 55f4de9 commit c30335e
Show file tree
Hide file tree
Showing 31 changed files with 247 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ protected final void addItemHandler(int slots, int slotLimit) {
protected final void addItemHandler(int slots, BiPredicate<Integer, ItemStack> validation) {
addItemHandler(slots, 64, validation);
}

protected final void addItemHandler(int slots, UnaryOperator<Integer> slotLimit) {
addItemHandler(slots, slotLimit, (slot, itemStack) -> true);
}
Expand All @@ -164,7 +165,6 @@ protected final void addItemHandler(int slots, UnaryOperator<Integer> slotLimit,
@Override
protected void onContentsChanged(int slot) {
update();
setChanged();
onItemsChanged(slot);
invalidateCapabilities();
}
Expand All @@ -185,6 +185,35 @@ private static int getStackLimit(IItemHandler itemHandler, int slot, ItemStack s
return Math.min(itemHandler.getSlotLimit(slot), stack.getMaxStackSize());
}

public ItemStack forceExtractItem(int slot, int amount, boolean simulate) {
if (amount == 0)
return ItemStack.EMPTY;

ItemStack existing = getItemHandler().getStackInSlot(slot);

if (existing.isEmpty())
return ItemStack.EMPTY;

int toExtract = Math.min(amount, existing.getMaxStackSize());

if (existing.getCount() <= toExtract) {
if (!simulate) {
getItemStackHandler().setStackInSlot(slot, ItemStack.EMPTY);
onItemsChanged(slot);
return existing;
} else {
return existing.copy();
}
} else {
if (!simulate) {
getItemStackHandler().setStackInSlot(slot, existing.copyWithCount(existing.getCount() - toExtract));
onItemsChanged(slot);
}

return existing.copyWithCount(toExtract);
}
}

public ItemStack forceInsertItem(int slot, ItemStack stack, boolean simulate) {
if (stack.isEmpty())
return ItemStack.EMPTY;
Expand Down Expand Up @@ -239,7 +268,6 @@ protected final void addFluidTank(int capacityInMb, Predicate<FluidStack> valida
@Override
protected void onContentsChanged() {
update();
setChanged();
onFluidChanged();
}

Expand All @@ -260,7 +288,6 @@ protected final void addPowerStorage(int powerCapacity) {
@Override
public void onEnergyChanged() {
update();
setChanged();
ContainerBlockEntity.this.onPowerChanged();
}
};
Expand All @@ -271,10 +298,9 @@ protected final void addBacteriaStorage(int slots) {
this.bacteriaStorage = new BacteriaStorage(slots);
}

private void update() {
if (!level.isClientSide()) {
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
}
public void update() {
setChanged();
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
}

protected void onItemsChanged(int slot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.portingdeadmods.nautec.api.blockentities;

import com.portingdeadmods.nautec.NTConfig;
import com.portingdeadmods.nautec.Nautec;
import com.portingdeadmods.nautec.api.blocks.blockentities.LaserBlock;
import com.portingdeadmods.nautec.content.recipes.ItemTransformationRecipe;
import com.portingdeadmods.nautec.content.recipes.inputs.ItemTransformationRecipeInput;
import com.portingdeadmods.nautec.utils.ParticleUtils;
Expand All @@ -27,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public abstract class LaserBlockEntity extends ContainerBlockEntity {
protected final Object2IntMap<Direction> laserDistances;
Expand All @@ -52,9 +51,9 @@ public LaserBlockEntity(BlockEntityType<?> blockEntityType, BlockPos blockPos, B
this.purity = 0;
}

public abstract ObjectSet<Direction> getLaserInputs();
public abstract Set<Direction> getLaserInputs();

public abstract ObjectSet<Direction> getLaserOutputs();
public abstract Set<Direction> getLaserOutputs();

public boolean shouldRender(Direction direction) {
return getLaserOutputs().contains(direction) && (power > 0 || powerToTransfer > 0);
Expand Down Expand Up @@ -258,14 +257,28 @@ protected void checkConnections() {
if (laserBlockEntity.getLaserInputs().contains(direction.getOpposite())) {
Vec3i diffVec3 = worldPosition.subtract(resultPos);
int diff = diffVec3.getX() + diffVec3.getY() + diffVec3.getZ();
this.laserDistances.put(direction, Math.abs(diff));
int prevDistance = this.laserDistances.getInt(direction);
int newDistance = Math.abs(diff);
if (prevDistance != newDistance) {
this.laserDistances.put(direction, newDistance);
onLaserDistancesChanged(direction, prevDistance);
}
}
} else {
this.laserDistances.put(direction, 0);
int prevDistance = this.laserDistances.getInt(direction);
int newDistance = 0;
if (prevDistance != newDistance) {
this.laserDistances.put(direction, 0);
onLaserDistancesChanged(direction, prevDistance);
}
}
}
}

protected void onLaserDistancesChanged(Direction direction, int prevDistance) {

}

// CLIENT
public int getLaserAnimTimeDuration() {
return 80;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.portingdeadmods.nautec.content.menus;
package com.portingdeadmods.nautec.client.screen;

import com.portingdeadmods.nautec.Nautec;
import com.portingdeadmods.nautec.api.augments.Augment;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.portingdeadmods.nautec.client.screen;

public class MixerScreen {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.portingdeadmods.nautec.content.blockentities;

import com.portingdeadmods.nautec.Nautec;
import com.portingdeadmods.nautec.api.blockentities.LaserBlockEntity;
import com.portingdeadmods.nautec.capabilities.IOActions;
import com.portingdeadmods.nautec.content.blocks.AquaticCatalystBlock;
import com.portingdeadmods.nautec.content.recipes.AquaticCatalystChannelingRecipe;
import com.portingdeadmods.nautec.registries.NTBlockEntityTypes;
import com.portingdeadmods.nautec.utils.SidedCapUtils;
Expand All @@ -21,31 +19,38 @@
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.neoforged.neoforge.capabilities.BlockCapability;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static com.portingdeadmods.nautec.content.blocks.AquaticCatalystBlock.ACTIVE;
import static com.portingdeadmods.nautec.content.blocks.AquaticCatalystBlock.STAGE;

// TODO: actually consume item before starting to generate power
public class AquaticCatalystBlockEntity extends LaserBlockEntity {
private RecipeHolder<AquaticCatalystChannelingRecipe> recipe;
// cache that uses the current item
private RecipeHolder<AquaticCatalystChannelingRecipe> nextRecipeCache;
// actually need to serialize this to know the amount of AP we need to produce
private RecipeHolder<AquaticCatalystChannelingRecipe> currentRecipe;
private int amount;
private int duration;
private boolean active;
private boolean updateRecipe = true;

public AquaticCatalystBlockEntity(BlockPos blockPos, BlockState blockState) {
super(NTBlockEntityTypes.AQUATIC_CATALYST.get(), blockPos, blockState);
addItemHandler(1, (slot, stack) -> hasRecipe(stack));
}

private boolean hasRecipe(ItemStack stack) {
return level.getRecipeManager().getRecipeFor(AquaticCatalystChannelingRecipe.Type.INSTANCE, new SingleRecipeInput(stack), level).isPresent();
return getRecipeForCache(stack) != null;
}

@Override
public ObjectSet<Direction> getLaserInputs() {
public Set<Direction> getLaserInputs() {
return ObjectSet.of();
}

Expand All @@ -54,31 +59,59 @@ public int getDuration() {
}

public int getRemainingDuration() {
return recipe != null ? recipe.value().duration() - duration : 0;
return nextRecipeCache != null ? nextRecipeCache.value().duration() - duration : 0;
}

public ItemStack getProcessingItem() {
return getItemHandler().getStackInSlot(0);
}

public Optional<AquaticCatalystChannelingRecipe> getCurrentRecipe() {
return Optional.ofNullable(this.recipe).map(RecipeHolder::value);
return Optional.ofNullable(this.nextRecipeCache).map(RecipeHolder::value);
}

@Override
protected void onItemsChanged(int slot) {
super.onItemsChanged(slot);
if (updateRecipe) {
updateRecipe(slot);
}
}

@Override
protected void onLaserDistancesChanged(Direction direction, int prevDistance) {
if (updateRecipe) {
updateRecipe(0);
}
}

BlockState state = getBlockState().setValue(ACTIVE, !getItemHandler().getStackInSlot(0).isEmpty());
private void updateRecipe(int slot) {
ItemStackHandler itemStackHandler = getItemStackHandler();
ItemStack stackInSlot1 = itemStackHandler.getStackInSlot(slot);
RecipeHolder<AquaticCatalystChannelingRecipe> recipe1 = getRecipeForCache(stackInSlot1);
if (recipe1 != null) {
this.updateRecipe = false;
itemStackHandler.extractItem(slot, 1, false);
this.updateRecipe = true;
}
this.nextRecipeCache = recipe1;
setActive(this.nextRecipeCache != null);
}

public void setActive(boolean active) {
float i = (float) getItemHandler().getStackInSlot(0).getCount() / getItemHandler().getSlotLimit(0);
int stage = (int) (i * 6);
Nautec.LOGGER.debug("ratio: {}, stage: {}", i, stage);
level.setBlockAndUpdate(worldPosition, state.setValue(STAGE, stage));
level.setBlockAndUpdate(worldPosition, getBlockState()
.setValue(ACTIVE, active)
.setValue(STAGE, active ? stage : 0));
this.active = active;
}

public boolean isActive() {
return getBlockState().getValue(ACTIVE) && this.active;
}

@Override
public ObjectSet<Direction> getLaserOutputs() {
public Set<Direction> getLaserOutputs() {
Direction direction = getBlockState().getValue(BlockStateProperties.FACING);
boolean coreActive = isActive();
if (coreActive) {
Expand All @@ -91,35 +124,29 @@ public ObjectSet<Direction> getLaserOutputs() {
public void commonTick() {
super.commonTick();

ItemStack stack = getItemHandler().getStackInSlot(0);
if (!stack.isEmpty() && isActive()) {
// caching
if (recipe == null) {
Optional<RecipeHolder<AquaticCatalystChannelingRecipe>> recipe1 = level.getRecipeManager().getRecipeFor(AquaticCatalystChannelingRecipe.Type.INSTANCE, new SingleRecipeInput(stack), level);
if (recipe1.isPresent()) {
this.recipe = recipe1.get();
getItemStackHandler().setStackInSlot(0, stack.copyWithCount(stack.getCount() - 1));
} else {
return;
}
}

if (isActive() && nextRecipeCache != null) {
Direction direction = getBlockState().getValue(BlockStateProperties.FACING);
if (getLaserDistances().getInt(direction.getOpposite()) > 0) {
if (duration >= recipe.value().duration()) {
if (duration >= nextRecipeCache.value().duration()) {
duration = 0;
updateRecipe(0);
setActive(this.nextRecipeCache != null);
} else {
amount = recipe.value().powerAmount() / recipe.value().duration();
amount = nextRecipeCache.value().powerAmount() / nextRecipeCache.value().duration();
transmitPower(amount);
setPurity(recipe.value().purity());
setPurity(nextRecipeCache.value().purity());
duration++;
}
}
} else {
this.recipe = null;
}
}

private @Nullable RecipeHolder<AquaticCatalystChannelingRecipe> getRecipeForCache(ItemStack stack) {
return level.getRecipeManager()
.getRecipeFor(AquaticCatalystChannelingRecipe.Type.INSTANCE, new SingleRecipeInput(stack), level)
.orElse(null);
}

public int getPower() {
return amount;
}
Expand All @@ -132,23 +159,21 @@ public <T> Map<Direction, Pair<IOActions, int[]>> getSidedInteractions(BlockCapa
return Map.of();
}

public boolean isActive() {
return getBlockState().getValue(ACTIVE);
}

@Override
protected void loadData(CompoundTag tag, HolderLookup.Provider provider) {
super.loadData(tag, provider);
this.duration = tag.getInt("duration");
this.active = tag.getBoolean("active");

Optional<RecipeHolder<?>> recipe1 = level.getRecipeManager().byKey(ResourceLocation.parse(tag.getString("recipe")));
recipe1.ifPresent(recipeHolder -> this.recipe = (RecipeHolder<AquaticCatalystChannelingRecipe>) recipeHolder);
recipe1.ifPresent(recipeHolder -> this.nextRecipeCache = (RecipeHolder<AquaticCatalystChannelingRecipe>) recipeHolder);
}

@Override
protected void saveData(CompoundTag tag, HolderLookup.Provider provider) {
super.saveData(tag, provider);
tag.putInt("duration", this.duration);
tag.putString("recipe", this.recipe != null ? this.recipe.toString() : "");
tag.putBoolean("active", this.active);
tag.putString("recipe", this.nextRecipeCache != null ? this.nextRecipeCache.toString() : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Set;

public class BacterialAnalyzerBlockEntity extends LaserBlockEntity implements MenuProvider {
public static final int MAX_PROGRESS = NTConfig.bacteriaAnalyzerCraftingSpeed;
Expand Down Expand Up @@ -78,12 +79,12 @@ public int getProgress() {
}

@Override
public ObjectSet<Direction> getLaserInputs() {
public Set<Direction> getLaserInputs() {
return ObjectSet.of(Direction.DOWN);
}

@Override
public ObjectSet<Direction> getLaserOutputs() {
public Set<Direction> getLaserOutputs() {
return ObjectSet.of();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.List;
import java.util.Map;
import java.util.Set;

public class BreakerBlockEntity extends LaserBlockEntity {
private boolean running;
Expand All @@ -29,12 +30,12 @@ public BreakerBlockEntity(BlockPos blockPos, BlockState blockState) {
}

@Override
public ObjectSet<Direction> getLaserInputs() {
public Set<Direction> getLaserInputs() {
return ObjectSet.of(Direction.UP, Direction.DOWN, Direction.EAST, Direction.NORTH,Direction.WEST, Direction.SOUTH);
}

@Override
public ObjectSet<Direction> getLaserOutputs() {
public Set<Direction> getLaserOutputs() {
return ObjectSet.of();
}

Expand Down
Loading

0 comments on commit c30335e

Please sign in to comment.