Skip to content

Commit

Permalink
Extract duplicated water bottle checks
Browse files Browse the repository at this point in the history
  • Loading branch information
altrisi committed Jun 24, 2024
1 parent 5c8f94e commit c9a07bb
Showing 1 changed file with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,16 @@ protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
Block frontBlock = frontBlockState.getBlock();

if (frontBlock == Blocks.WATER_CAULDRON) {
if (item == Items.POTION) {
// check if it's a water bottle (https://minecraft.wiki/w/Potion#Base_potions)
PotionContentsComponent potionContentsComponent = stack.get(DataComponentTypes.POTION_CONTENTS);
if (potionContentsComponent != null && potionContentsComponent.matches(Potions.WATER)) {
// check if cauldron is not full
if (!((AbstractCauldronBlock) frontBlock).isFull(frontBlockState)) {
// increase cauldron level
int level = frontBlockState.get(LeveledCauldronBlock.LEVEL);
BlockState cauldronState = frontBlockState.with(LeveledCauldronBlock.LEVEL, level + 1);
setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BOTTLE_EMPTY, GameEvent.FLUID_PLACE);
if (isWaterBottle(stack)) {
// check if cauldron is not full
if (!((AbstractCauldronBlock) frontBlock).isFull(frontBlockState)) {
// increase cauldron level
int level = frontBlockState.get(LeveledCauldronBlock.LEVEL);
BlockState cauldronState = frontBlockState.with(LeveledCauldronBlock.LEVEL, level + 1);
setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BOTTLE_EMPTY, GameEvent.FLUID_PLACE);

// return glass bottle
return this.addOrDispense(pointer, stack, new ItemStack(Items.GLASS_BOTTLE));
}
// return glass bottle
return this.addOrDispense(pointer, stack, new ItemStack(Items.GLASS_BOTTLE));
}
}
else if (item == Items.GLASS_BOTTLE) {
Expand Down Expand Up @@ -98,17 +94,13 @@ else if (item instanceof BannerItem) {
}
}
}
else if(frontBlock == Blocks.CAULDRON && item == Items.POTION) {
// check if it's a water bottle (https://minecraft.wiki/w/Potion#Base_potions)
PotionContentsComponent potionContentsComponent = stack.get(DataComponentTypes.POTION_CONTENTS);
if (potionContentsComponent != null && potionContentsComponent.matches(Potions.WATER)) {
// increase cauldron level
BlockState cauldronState = Blocks.WATER_CAULDRON.getDefaultState();
setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BOTTLE_EMPTY, GameEvent.FLUID_PLACE);
else if (frontBlock == Blocks.CAULDRON && isWaterBottle(stack)) {
// increase cauldron level
BlockState cauldronState = Blocks.WATER_CAULDRON.getDefaultState();
setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BOTTLE_EMPTY, GameEvent.FLUID_PLACE);

// return glass bottle
return this.addOrDispense(pointer, stack, new ItemStack(Items.GLASS_BOTTLE));
}
// return glass bottle
return this.addOrDispense(pointer, stack, new ItemStack(Items.GLASS_BOTTLE));
}

// fail to dispense
Expand All @@ -123,15 +115,20 @@ private static void setCauldron(ServerWorld world, BlockPos pos, BlockState stat
world.emitGameEvent(null, gameEvent, pos);
}

private static boolean isWaterBottle(ItemStack stack) {
if (stack.getItem() != Items.POTION) {
return false;
}

PotionContentsComponent content = stack.get(DataComponentTypes.POTION_CONTENTS);
return content != null && content.matches(Potions.WATER);
}

public static boolean isWaterCauldronItem(ItemStack stack) {
/* accept empty and water bottles, banners and dyeable items */
Item item = stack.getItem();
if (item == Items.GLASS_BOTTLE || item instanceof BannerItem)
if (item == Items.GLASS_BOTTLE || item instanceof BannerItem || isWaterBottle(stack))
return true;
if (item == Items.POTION) {
PotionContentsComponent potionContentsComponent = stack.getComponents().get(DataComponentTypes.POTION_CONTENTS);
return potionContentsComponent != null && potionContentsComponent.matches(Potions.WATER);
}
if (Block.getBlockFromItem(item) instanceof ShulkerBoxBlock) {
return item != Items.SHULKER_BOX; // dyed Shulkers only
}
Expand Down

0 comments on commit c9a07bb

Please sign in to comment.