use of net.minecraft.item.DyeableItem in project carpet-extra by gnembon.
the class CauldronWaterDispenserBehavior method dispenseSilently.
@Override
protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
this.setSuccess(true);
Item item = stack.getItem();
ServerWorld world = pointer.getWorld();
BlockPos frontBlockPos = pointer.getPos().offset(pointer.getBlockState().get(DispenserBlock.FACING));
BlockState frontBlockState = world.getBlockState(frontBlockPos);
Block frontBlock = frontBlockState.getBlock();
if (frontBlock == Blocks.WATER_CAULDRON) {
if (item == Items.POTION && PotionUtil.getPotion(stack) == 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);
// return glass bottle
return this.addOrDispense(pointer, stack, new ItemStack(Items.GLASS_BOTTLE));
}
} else if (item == Items.GLASS_BOTTLE) {
// decrease cauldron level
LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
// return water bottle
return this.addOrDispense(pointer, stack, PotionUtil.setPotion(new ItemStack(Items.POTION), Potions.WATER));
} else if (Block.getBlockFromItem(item) instanceof ShulkerBoxBlock) {
// make sure item isn't plain shulker box
if (item != Items.SHULKER_BOX) {
// decrease cauldron level
LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
// turn dyed shulker box into undyed shulker box
ItemStack undyedShulkerBox = new ItemStack(Items.SHULKER_BOX);
if (stack.hasNbt()) {
undyedShulkerBox.setNbt(stack.getNbt().copy());
}
// return undyed shulker box
return this.addOrDispense(pointer, stack, undyedShulkerBox);
}
}
if (item instanceof DyeableItem) {
DyeableItem dyeableItem = (DyeableItem) item;
// check if dyeable item has color
if (dyeableItem.hasColor(stack)) {
// decrease cauldron level
LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
// remove color
dyeableItem.removeColor(stack);
// return undyed item
return stack;
}
} else if (item instanceof BannerItem) {
// checks if banner has layers
if (BannerBlockEntity.getPatternCount(stack) > 0) {
// decrease cauldron level
LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
// copy banner stack, set to one item
ItemStack cleanedBanner = stack.copy();
cleanedBanner.setCount(1);
// removes layer from banner (yarn name is misleading)
BannerBlockEntity.loadFromItemStack(cleanedBanner);
// return cleaned banner
return this.addOrDispense(pointer, stack, cleanedBanner);
}
}
} else if (frontBlock == Blocks.CAULDRON && item == Items.POTION && PotionUtil.getPotion(stack) == Potions.WATER) {
// 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));
}
// fail to dispense
this.setSuccess(false);
return stack;
}
Aggregations