use of net.minecraft.block.entity.DispenserBlockEntity in project carpet-extra by gnembon.
the class CarpetExtraDispenserBehaviors method getCustomDispenserBehavior.
// get custom dispenser behavior
// this checks conditions such as the item and certain block or entity being in front of the dispenser to decide which rule to return
// if the conditions for the rule match, it returns the instance of the dispenser behavior
// returns null to fallback to vanilla (or another mod's) behavior for the given item
public static DispenserBehavior getCustomDispenserBehavior(ServerWorld world, BlockPos pos, BlockPointer pointer, DispenserBlockEntity dispenserBlockEntity, ItemStack stack, Map<Item, DispenserBehavior> VANILLA_BEHAVIORS) {
Item item = stack.getItem();
Direction dispenserFacing = pointer.getBlockState().get(DispenserBlock.FACING);
BlockPos frontBlockPos = pos.offset(dispenserFacing);
BlockState frontBlockState = world.getBlockState(frontBlockPos);
Block frontBlock = frontBlockState.getBlock();
Box frontBlockBox = new Box(frontBlockPos);
// blazeMeal
if (CarpetExtraSettings.blazeMeal && item == Items.BLAZE_POWDER && frontBlock == Blocks.NETHER_WART) {
return BLAZE_MEAL;
}
// chickenShearing
if (CarpetExtraSettings.chickenShearing && item == Items.SHEARS) {
boolean hasShearableChickens = !world.getEntitiesByType(EntityType.CHICKEN, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((chickenEntity) -> {
return !((AnimalEntity) chickenEntity).isBaby();
})).isEmpty();
if (hasShearableChickens) {
return SHEAR_CHICKEN;
}
}
// dispensersCarvePumpkins
if (CarpetExtraSettings.dispensersCarvePumpkins && item instanceof ShearsItem && frontBlock == Blocks.PUMPKIN) {
return CARVE_PUMPKIN;
}
// dispensersFeedAnimals
if (CarpetExtraSettings.dispensersFeedAnimals) {
// check for animals that can be bred with the current item being dispensed in front of dispenser
boolean hasFeedableAnimals = !world.getEntitiesByClass(AnimalEntity.class, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((animalEntity) -> {
return ((AnimalEntity) animalEntity).isBreedingItem(stack);
})).isEmpty();
if (hasFeedableAnimals) {
return FEED_ANIMAL;
}
// get brown mooshrooms in front of dispenser
boolean hasFeedableMooshrooms = !world.getEntitiesByType(EntityType.MOOSHROOM, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((mooshroomEntity) -> {
return ((MooshroomEntity) mooshroomEntity).getMooshroomType() == MooshroomEntity.Type.BROWN;
})).isEmpty();
// check if item is a small flower
if (hasFeedableMooshrooms && item.getRegistryEntry().isIn(ItemTags.SMALL_FLOWERS)) {
return FEED_MOOSHROOM;
}
}
// dispensersFillMinecarts
if (CarpetExtraSettings.dispensersFillMinecarts) {
// check for minecarts with no riders in front of dispenser
boolean hasMinecarts = !world.getEntitiesByType(EntityType.MINECART, frontBlockBox, EntityPredicates.NOT_MOUNTED).isEmpty();
// if a minecart exist, return dispenser behavior according to item type
if (hasMinecarts) {
if (item == Items.CHEST) {
return FILL_MINECART_CHEST;
} else if (item == Items.FURNACE) {
return FILL_MINECART_FURNACE;
} else if (item == Items.TNT) {
return FILL_MINECART_TNT;
} else if (item == Items.HOPPER) {
return FILL_MINECART_HOPPER;
}
}
}
// dispensersMilkAnimals
if (CarpetExtraSettings.dispensersMilkAnimals) {
// bucket to milk
if (item == Items.BUCKET) {
// check for cows, mooshrooms, or goats in front of dispenser
boolean hasMilkable = !world.getEntitiesByClass(AnimalEntity.class, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((animalEntity) -> {
return animalEntity instanceof CowEntity || animalEntity instanceof GoatEntity;
})).isEmpty();
if (hasMilkable) {
return MILK_ANIMAL;
}
} else // bowl to stew
if (item == Items.BOWL) {
// check for mooshrooms in front of dispenser
boolean hasMooshroom = !world.getEntitiesByType(EntityType.MOOSHROOM, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY).isEmpty();
if (hasMooshroom) {
return MILK_MOOSHROOM;
}
}
}
// dispensersPlayRecords
if (CarpetExtraSettings.dispensersPlayRecords && item instanceof MusicDiscItem && frontBlock == Blocks.JUKEBOX) {
return PLAY_DISC;
}
// dispensersPotPlants
if (CarpetExtraSettings.dispensersPotPlants && frontBlock instanceof FlowerPotBlock && FlowerPotHelper.isPottable(item)) {
return FILL_FLOWER_POT;
}
// dispensersStripBlocks
if (CarpetExtraSettings.dispensersStripBlocks && item instanceof AxeItem && (StripBlocksDispenserBehavior.canStrip(frontBlock) || StripBlocksDispenserBehavior.isStripResult(frontBlock))) {
return STRIP_BLOCK;
}
// dispensersTillSoil
if (CarpetExtraSettings.dispensersTillSoil && item instanceof HoeItem) {
// check block in front of dispenser and one block down
for (int i = 0; i < 2; i++) {
BlockPos hoeBlockPos = frontBlockPos.down(i);
Block hoeBlock = world.getBlockState(hoeBlockPos).getBlock();
// check if block is in tilled blocks, or is farmland (to prevent hoe being dispensed when you don't want it to)
if (TillSoilDispenserBehavior.TILLED_BLOCKS.contains(hoeBlock) || hoeBlock == Blocks.FARMLAND) {
return TILL_SOIL;
}
}
}
// dispensersToggleThings
if (CarpetExtraSettings.dispensersToggleThings && item == Items.STICK && ToggleBlockDispenserBehavior.TOGGLEABLE_BLOCKS.contains(frontBlock)) {
return TOGGLE_BLOCK;
}
// dispensersUseCauldrons
if (CarpetExtraSettings.dispensersUseCauldrons && frontBlock instanceof AbstractCauldronBlock) {
// empty cauldron
if (item == Items.BUCKET) {
return CAULDRON_EMPTYING_BUCKET;
} else // fill cauldron
if (item == Items.LAVA_BUCKET || item == Items.WATER_BUCKET || item == Items.POWDER_SNOW_BUCKET) {
return CAULDRON_FILLING_BUCKET;
} else // water cauldron behaviors (leather armor, shulker boxes, banners)
if (CauldronWaterDispenserBehavior.isWaterCauldronItem(stack)) {
return CAULDRON_WATER;
}
}
// renewableEndstone
if (CarpetExtraSettings.renewableEndstone && item == Items.DRAGON_BREATH && frontBlock == Blocks.COBBLESTONE) {
return DRAGON_BREATH_ENDSTONE;
}
// renewableNetherrack
if (CarpetExtraSettings.renewableNetherrack && item == Items.FIRE_CHARGE && frontBlock == Blocks.COBBLESTONE) {
return FIRE_CHARGE_NETHERRACK;
}
// no custom behavior, return null
return null;
}
use of net.minecraft.block.entity.DispenserBlockEntity in project carpet-extra by gnembon.
the class DropperBlock_craftingMixin method tryCraft.
@Inject(method = "dispense", at = @At("HEAD"), cancellable = true)
private void tryCraft(ServerWorld world_1, BlockPos blockPos_1, CallbackInfo ci) {
if (!CarpetExtraSettings.autoCraftingDropper)
return;
BlockPos front = blockPos_1.offset(world_1.getBlockState(blockPos_1).get(DispenserBlock.FACING));
if (world_1.getBlockState(front).getBlock() != Blocks.CRAFTING_TABLE)
return;
DispenserBlockEntity dispenserBlockEntity_1 = (DispenserBlockEntity) world_1.getBlockEntity(blockPos_1);
if (dispenserBlockEntity_1 == null)
return;
CraftingInventory craftingInventory = new CraftingInventory(new VoidContainer(), 3, 3);
for (int i = 0; i < 9; i++) craftingInventory.setStack(i, dispenserBlockEntity_1.getStack(i));
CraftingRecipe recipe = world_1.getRecipeManager().getFirstMatch(RecipeType.CRAFTING, craftingInventory, world_1).orElse(null);
if (recipe == null)
return;
// crafting it
Vec3d target = Vec3d.ofBottomCenter(front).add(0.0, 0.2, 0.0);
ItemStack result = recipe.craft(craftingInventory);
spawn(world_1, target.x, target.y, target.z, result);
// copied from CraftingResultSlot.onTakeItem()
DefaultedList<ItemStack> defaultedList_1 = world_1.getRecipeManager().getRemainingStacks(RecipeType.CRAFTING, craftingInventory, world_1);
for (int int_1 = 0; int_1 < defaultedList_1.size(); ++int_1) {
ItemStack itemStack_2 = dispenserBlockEntity_1.getStack(int_1);
ItemStack itemStack_3 = defaultedList_1.get(int_1);
if (!itemStack_2.isEmpty()) {
dispenserBlockEntity_1.removeStack(int_1, 1);
itemStack_2 = dispenserBlockEntity_1.getStack(int_1);
}
if (!itemStack_3.isEmpty()) {
if (itemStack_2.isEmpty()) {
dispenserBlockEntity_1.setStack(int_1, itemStack_3);
} else if (ItemStack.areItemsEqualIgnoreDamage(itemStack_2, itemStack_3) && ItemStack.areNbtEqual(itemStack_2, itemStack_3)) {
itemStack_3.increment(itemStack_2.getCount());
dispenserBlockEntity_1.setStack(int_1, itemStack_3);
} else {
spawn(world_1, target.x, target.y, target.z, itemStack_3);
}
}
}
// +0.5v
Vec3d vec = Vec3d.ofCenter(blockPos_1);
ServerWorld world = (ServerWorld) world_1;
world.playSound(null, blockPos_1, SoundEvents.ENTITY_VILLAGER_WORK_MASON, SoundCategory.BLOCKS, 0.2f, 2.0f);
ci.cancel();
}
Aggregations