Skip to content

Commit

Permalink
feat: improve filtering of items that can't be eaten by the cradle
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Mar 19, 2024
1 parent 6565ba0 commit c451857
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void addBiomancyTags() {

createTag(ModItemTags.CANNOT_BE_EATEN_BY_CRADLE)
.add(DRAGON_EGG, SPAWNER, HEART_OF_THE_SEA)
.add(NAME_TAG)
.add(NAME_TAG, BUNDLE)
.addTag(ItemTags.MUSIC_DISCS)
.add(ELYTRA)
.addTag(Tags.Items.ARMORS, Tags.Items.TOOLS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.github.elenterius.biomancy.block.cradle;

import com.github.elenterius.biomancy.block.fleshkinchest.FleshkinChestBlock;
import com.github.elenterius.biomancy.block.storagesac.StorageSacBlock;
import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.init.ModBlockEntities;
import com.github.elenterius.biomancy.init.ModItems;
import com.github.elenterius.biomancy.init.ModSoundEvents;
import com.github.elenterius.biomancy.init.ModTriggers;
import com.github.elenterius.biomancy.init.*;
import com.github.elenterius.biomancy.init.tags.ModItemTags;
import com.github.elenterius.biomancy.integration.ModsCompatHandler;
import com.github.elenterius.biomancy.styles.TextStyles;
Expand All @@ -31,7 +26,10 @@
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand All @@ -57,16 +55,21 @@ public class PrimordialCradleBlock extends HorizontalDirectionalBlock implements

Item item = stack.getItem();

if (item instanceof TieredItem || item instanceof Vanishable) return true;

if (item instanceof BlockItem blockItem) {
Block block = blockItem.getBlock();
if (block instanceof ShulkerBoxBlock || block instanceof FleshkinChestBlock || block instanceof StorageSacBlock) {
return true;
}

//prevent all items that have a BlockEntity associated with it from being sacrificed
// e.g. complex modded blocks such as computers, machines, containers, etc.
if (block instanceof EntityBlock) return true;
}

if (ModsCompatHandler.getTetraHelper().isToolOrModularItem(item)) return true;

return item instanceof TieredItem || item instanceof Vanishable || stack.isEnchanted();
if (stack.isEnchanted()) return true;
if (!item.canFitInsideContainerItems()) return true;
return stack.getCapability(ModCapabilities.ITEM_HANDLER).isPresent();
};

protected static final VoxelShape INSIDE_AABB = box(3, 4, 3, 13, 16, 13);
Expand Down Expand Up @@ -95,6 +98,13 @@ public static int getPrimalEnergy(CompoundTag tag) {
return tag.contains(PrimordialCradleBlockEntity.PRIMAL_ENERGY_KEY) ? tag.getInt(PrimordialCradleBlockEntity.PRIMAL_ENERGY_KEY) : 0;
}

private static MutableComponent createValueComponent(DecimalFormat df, int value, String name) {
return ComponentUtil.literal(df.format(value))
.withStyle(TextStyles.PRIMORDIAL_RUNES_LIGHT_GRAY)
.append(ComponentUtil.space())
.append(ComponentUtil.literal(name).withStyle(TextStyles.GRAY));
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING);
Expand Down Expand Up @@ -179,19 +189,20 @@ else if (stack.getItem() instanceof PotionItem) {
}

private boolean increaseFillLevel(@Nullable Entity player, Level level, BlockPos pos, ItemStack stack) {
if (!stack.isEmpty() && !level.isClientSide()) {
if (CANNOT_BE_SACRIFICED.test(stack)) return false;

ItemStack copyOfStack = ItemHandlerHelper.copyStackWithSize(stack, 1); //creator#insertItem modifies the stack which may lead to it being empty
if (level.getBlockEntity(pos) instanceof PrimordialCradleBlockEntity creator && !creator.isFull() && creator.insertItem(stack)) {
if (player instanceof ServerPlayer serverPlayer) {
ModTriggers.SACRIFICED_ITEM_TRIGGER.trigger(serverPlayer, copyOfStack);
}
SoundEvent soundEvent = creator.isFull() ? ModSoundEvents.CRADLE_BECAME_FULL.get() : ModSoundEvents.CRADLE_EAT.get();
SoundUtil.broadcastBlockSound((ServerLevel) level, pos, soundEvent);
return true;
if (level.isClientSide()) return false;
if (stack.isEmpty()) return false;
if (CANNOT_BE_SACRIFICED.test(stack)) return false;

ItemStack copyOfStack = ItemHandlerHelper.copyStackWithSize(stack, 1); //cradle#insertItem modifies the stack which may lead to it being empty
if (level.getBlockEntity(pos) instanceof PrimordialCradleBlockEntity cradle && !cradle.isFull() && cradle.insertItem(stack)) {
if (player instanceof ServerPlayer serverPlayer) {
ModTriggers.SACRIFICED_ITEM_TRIGGER.trigger(serverPlayer, copyOfStack);
}
SoundEvent soundEvent = cradle.isFull() ? ModSoundEvents.CRADLE_BECAME_FULL.get() : ModSoundEvents.CRADLE_EAT.get();
SoundUtil.broadcastBlockSound((ServerLevel) level, pos, soundEvent);
return true;
}

return false;
}

Expand Down Expand Up @@ -258,11 +269,4 @@ public void appendHoverText(ItemStack stack, @Nullable BlockGetter level, List<C
if (anomaly > 0) tooltip.add(createValueComponent(df, anomaly, "Anomaly"));
}
}

private static MutableComponent createValueComponent(DecimalFormat df, int value, String name) {
return ComponentUtil.literal(df.format(value))
.withStyle(TextStyles.PRIMORDIAL_RUNES_LIGHT_GRAY)
.append(ComponentUtil.space())
.append(ComponentUtil.literal(name).withStyle(TextStyles.GRAY));
}
}

0 comments on commit c451857

Please sign in to comment.