Skip to content

Commit

Permalink
Fixed water filter state not reverting
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 6, 2024
1 parent 98167d7 commit 1d2e70d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,69 +137,68 @@ public void saveAdditional(CompoundTag nbt)

public static void serverTick(Level level, BlockPos pos, BlockState state, WaterPurifierBlockEntity blockEntity)
{
boolean previouslyFiltering = blockEntity.currentlyFiltering();
boolean previouslyFiltering = blockEntity.isFiltering();
boolean changed = false;

if (blockEntity.currentlyFiltering())
if (blockEntity.isFiltering())
{
--blockEntity.filterTimeRemaining;
}

if (!blockEntity.level.isClientSide)
{
ItemStack filterStack = blockEntity.items.get(1);
boolean hasFilter = !filterStack.isEmpty();
if (blockEntity.currentlyFiltering() || hasFilter && !blockEntity.items.get(0).isEmpty()) {
RecipeHolder<?> recipe = blockEntity.level.getRecipeManager().getRecipeFor((RecipeType<WaterPurifierRecipe>) TANRecipeTypes.WATER_PURIFYING, blockEntity, blockEntity.level).orElse(null);

if (recipe != null)
{
if (!blockEntity.currentlyFiltering() && blockEntity.canFilter(recipe.value())) {
blockEntity.filterTimeRemaining = blockEntity.getFilterDuration(filterStack);
blockEntity.filterDuration = blockEntity.filterTimeRemaining;

// If we are now filtering, consume the filter item
if (blockEntity.currentlyFiltering()) {
changed = true;

if (hasFilter)
ItemStack filterStack = blockEntity.items.get(1);
boolean hasFilter = !filterStack.isEmpty();
if (blockEntity.isFiltering() || hasFilter && !blockEntity.items.get(0).isEmpty()) {
RecipeHolder<?> recipe = blockEntity.level.getRecipeManager().getRecipeFor((RecipeType<WaterPurifierRecipe>) TANRecipeTypes.WATER_PURIFYING, blockEntity, blockEntity.level).orElse(null);

if (recipe != null)
{
if (!blockEntity.isFiltering() && blockEntity.canFilter(recipe.value())) {
blockEntity.filterTimeRemaining = blockEntity.getFilterDuration(filterStack);
blockEntity.filterDuration = blockEntity.filterTimeRemaining;

// If we are now filtering, consume the filter item
if (blockEntity.isFiltering()) {
changed = true;

if (hasFilter)
{
Item filter = filterStack.getItem();
filterStack.shrink(1);
if (filterStack.isEmpty())
{
Item filter = filterStack.getItem();
filterStack.shrink(1);
if (filterStack.isEmpty())
{
Item remainingItem = filter.getCraftingRemainingItem();
blockEntity.items.set(1, remainingItem == null ? ItemStack.EMPTY : new ItemStack(remainingItem));
}
Item remainingItem = filter.getCraftingRemainingItem();
blockEntity.items.set(1, remainingItem == null ? ItemStack.EMPTY : new ItemStack(remainingItem));
}
}
}
}

if (blockEntity.currentlyFiltering() && blockEntity.canFilter(recipe.value())) {
++blockEntity.purifyProgress;
if (blockEntity.isFiltering() && blockEntity.canFilter(recipe.value())) {
++blockEntity.purifyProgress;

if (blockEntity.purifyProgress == blockEntity.purifyTotalTime) {
blockEntity.purifyProgress = 0;
blockEntity.purifyTotalTime = blockEntity.getTotalPurifyTime();
blockEntity.filter(recipe.value());
changed = true;
}
} else {
if (blockEntity.purifyProgress == blockEntity.purifyTotalTime) {
blockEntity.purifyProgress = 0;
blockEntity.purifyTotalTime = blockEntity.getTotalPurifyTime();
blockEntity.filter(recipe.value());
changed = true;
}
} else if (!blockEntity.currentlyFiltering() && blockEntity.purifyProgress > 0) {
blockEntity.purifyProgress = Mth.clamp(blockEntity.purifyProgress - 2, 0, blockEntity.purifyTotalTime);
}

if (previouslyFiltering != blockEntity.currentlyFiltering()) {
changed = true;
blockEntity.level.setBlock(blockEntity.worldPosition, blockEntity.level.getBlockState(blockEntity.worldPosition).setValue(WaterPurifierBlock.PURIFYING, Boolean.valueOf(blockEntity.currentlyFiltering())), 3);
} else {
blockEntity.purifyProgress = 0;
}
} else if (!blockEntity.isFiltering() && blockEntity.purifyProgress > 0) {
blockEntity.purifyProgress = Mth.clamp(blockEntity.purifyProgress - 2, 0, blockEntity.purifyTotalTime);
}
}

if (previouslyFiltering != blockEntity.isFiltering())
{
changed = true;
state = state.setValue(WaterPurifierBlock.PURIFYING, blockEntity.isFiltering());
level.setBlock(pos, state, 3);
}

// Mark as changed
if (changed) blockEntity.setChanged();
if (changed) setChanged(level, pos, state);
}

@Override
Expand Down Expand Up @@ -328,7 +327,7 @@ public void clearContent()
this.items.clear();
}

public boolean currentlyFiltering()
public boolean isFiltering()
{
return this.filterTimeRemaining > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import toughasnails.block.entity.WaterPurifierBlockEntity;

@Mixin(value = WaterPurifierBlockEntity.class, remap = false)
@Mixin(value = WaterPurifierBlockEntity.class)
public abstract class MixinWaterPurifierBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer {
protected MixinWaterPurifierBlockEntity(BlockEntityType<?> p_155076_, BlockPos p_155077_, BlockState p_155078_) {
super(p_155076_, p_155077_, p_155078_);
Expand Down

0 comments on commit 1d2e70d

Please sign in to comment.