Java Code Examples for net.minecraft.item.ItemStack#decrement()

The following examples show how to use net.minecraft.item.ItemStack#decrement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DispenserBehaviorFireChargeMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 7 votes vote down vote up
@SuppressWarnings("UnresolvedMixinReference")
@Inject(method = "dispenseSilently(Lnet/minecraft/util/math/BlockPointer;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;", at = @At("HEAD"), cancellable = true)
private void convertNetherrack(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir)
{
    if (!CarpetExtraSettings.fireChargeConvertsToNetherrack)
        return;
    World world = pointer.getWorld();
    Direction direction = pointer.getBlockState().get(DispenserBlock.FACING);
    BlockPos front = pointer.getBlockPos().offset(direction);
    BlockState state = world.getBlockState(front);
    if (state.getBlock() == Blocks.COBBLESTONE)
    {
        world.setBlockState(front, Blocks.NETHERRACK.getDefaultState());
        stack.decrement(1);
        cir.setReturnValue(stack);
        cir.cancel();
    }
}
 
Example 2
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 6 votes vote down vote up
private ItemStack defaultBehaviour(BlockPointer source, ItemStack stack)
{
    if (this.minecartType == AbstractMinecartEntity.Type.TNT)
    {
        World world = source.getWorld();
        BlockPos pos = source.getBlockPos().offset((Direction) source.getBlockState().get(DispenserBlock.FACING));
        TntEntity tntEntity = new TntEntity(world, (double)pos.getX() + 0.5D, (double)pos.getY(), (double)pos.getZ() + 0.5D, (LivingEntity)null);
        world.spawnEntity(tntEntity);
        world.playSound((PlayerEntity)null, tntEntity.getX(), tntEntity.getY(), tntEntity.getZ(), SoundEvents.ENTITY_TNT_PRIMED, SoundCategory.BLOCKS, 1.0F, 1.0F);
        stack.decrement(1);
        return stack;
    }
    else
    {
        return super.dispenseSilently(source, stack);
    }
}
 
Example 3
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack)
{
    if (!CarpetExtraSettings.dragonsBreathConvertsCobbleToEndstone)
        return super.dispenseSilently(source, stack);
    
    World world = source.getWorld();
    Direction direction = source.getBlockState().get(DispenserBlock.FACING);
    BlockPos front = source.getBlockPos().offset(direction);
    BlockState state = world.getBlockState(front);
    
    if (state.getBlock() == Blocks.COBBLESTONE)
    {
        world.setBlockState(front, Blocks.END_STONE.getDefaultState());
        stack.decrement(1);
        return stack;
    }
    return super.dispenseSilently(source, stack);
}
 
Example 4
Source File: SyncedGuiDescription.java    From LibGui with MIT License 6 votes vote down vote up
/** WILL MODIFY toInsert! Returns true if anything was inserted. */
private boolean insertIntoExisting(ItemStack toInsert, Slot slot, PlayerEntity player) {
	ItemStack curSlotStack = slot.getStack();
	if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack) && slot.canTakeItems(player)) {
		int combinedAmount = curSlotStack.getCount() + toInsert.getCount();
		if (combinedAmount <= toInsert.getMaxCount()) {
			toInsert.setCount(0);
			curSlotStack.setCount(combinedAmount);
			slot.markDirty();
			return true;
		} else if (curSlotStack.getCount() < toInsert.getMaxCount()) {
			toInsert.decrement(toInsert.getMaxCount() - curSlotStack.getCount());
			curSlotStack.setCount(toInsert.getMaxCount());
			slot.markDirty();
			return true;
		}
	}
	return false;
}
 
Example 5
Source File: CannedFoodItem.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) {
    if (entity instanceof PlayerEntity) {
        PlayerEntity player = (PlayerEntity) entity;
        player.eatFood(world, stack);
        player.dropStack(new ItemStack(GalacticraftItems.TIN_CANISTER));
    }
    stack.decrement(1);
    return stack;
}
 
Example 6
Source File: TinyPumpkinBlockEntity.java    From the-hallow with MIT License 5 votes vote down vote up
public ActionResult use(PlayerEntity player, Hand hand, BlockHitResult hit) {
	Direction facing = getCachedState().get(HorizontalFacingBlock.FACING);
	Direction hitSide = hit.getSide();
	if (hitSide != facing.rotateYClockwise() && hitSide != facing.rotateYCounterclockwise()) {
		return ActionResult.PASS;
	}
	
	if (!world.isClient) {
		ItemStack handStack = player.getStackInHand(hand);
		boolean isLeft = hitSide == facing.rotateYCounterclockwise();
		ItemStack heldItem = isLeft ? leftItem : rightItem;
		if (!heldItem.isEmpty()) {
			ItemScatterer.spawn(world, pos, DefaultedList.copyOf(ItemStack.EMPTY, heldItem));
			if (isLeft) {
				leftItem = ItemStack.EMPTY;
			} else {
				rightItem = ItemStack.EMPTY;
			}
			sync();
			markDirty();
		} else if (!handStack.isEmpty()) {
			if (isLeft) {
				leftItem = handStack.copy();
				leftItem.setCount(1);
			} else {
				rightItem = handStack.copy();
				rightItem.setCount(1);
			}
			handStack.decrement(1);
			sync();
			markDirty();
		}
	}
	
	return ActionResult.SUCCESS;
}
 
Example 7
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack)
{
    if (!CarpetExtraSettings.dispensersFillMinecarts)
    {
        return defaultBehaviour(source, stack);
    }
    else
    {
        BlockPos pos = source.getBlockPos().offset((Direction) source.getBlockState().get(DispenserBlock.FACING));
        List<MinecartEntity> list = source.getWorld().<MinecartEntity>getEntities(MinecartEntity.class, new Box(pos), null);
    
        if (list.isEmpty())
        {
            return defaultBehaviour(source, stack);
        }
        else
        {
            MinecartEntity minecart = list.get(0);
            minecart.remove();
            AbstractMinecartEntity minecartEntity = AbstractMinecartEntity.create(minecart.world, minecart.getX(), minecart.getY(), minecart.getZ(), this.minecartType);
            minecartEntity.setVelocity(minecart.getVelocity());
            minecartEntity.pitch = minecart.pitch;
            minecartEntity.yaw = minecart.yaw;
            
            minecart.world.spawnEntity(minecartEntity);
            stack.decrement(1);
            return stack;
        }
    }
}
 
Example 8
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack) {
    BlockPos pos = source.getBlockPos().offset((Direction) source.getBlockState().get(DispenserBlock.FACING));
    List<AnimalEntity> list = source.getWorld().getEntities(AnimalEntity.class, new Box(pos),null);
    boolean failure = false;

    for(AnimalEntity mob : list) {
        if(!mob.isBreedingItem(stack)) continue;
        if(mob.getBreedingAge() != 0 || mob.isInLove()) {
            failure = true;
            continue;
        }
        stack.decrement(1);
        mob.lovePlayer(null);
        return stack;
    }
    if(failure) return stack;
    // fix here for now - if problem shows up next time, will need to fix it one level above.
    if(
            CarpetExtraSettings.dispenserPlacesBlocks &&
            stack.getItem() instanceof BlockItem &&
            PlaceBlockDispenserBehavior.canPlace(((BlockItem) stack.getItem()).getBlock())
    )
    {
        return PlaceBlockDispenserBehavior.getInstance().dispenseSilently(source, stack);
    }
    else
    {
        return super.dispenseSilently(source, stack);
    }
}
 
Example 9
Source File: DispenserBehaviorBucketCowsMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Inject(
        method = "dispenseSilently(Lnet/minecraft/util/math/BlockPointer;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;",
        at = @At("HEAD"), cancellable = true
)
private void milkCow(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir)
{
    if (!CarpetExtraSettings.dispensersMilkCows)
        return;
    World world = pointer.getWorld();
    
    if (!world.isClient)
    {
        BlockPos pos = pointer.getBlockPos().offset(pointer.getBlockState().get(DispenserBlock.FACING));
        List<CowEntity> cows = world.getEntities(CowEntity.class, new Box(pos), e -> e.isAlive() && !e.isBaby());
        if (!cows.isEmpty())
        {
            stack.decrement(1);
            if (stack.isEmpty())
            {
                cir.setReturnValue(new ItemStack(Items.MILK_BUCKET));
            }
            else
            {
                if (((DispenserBlockEntity)pointer.getBlockEntity()).addToFirstFreeSlot(new ItemStack(Items.MILK_BUCKET)) < 0)
                {
                    this.dispense(pointer, new ItemStack(Items.MILK_BUCKET));
                }
                cir.setReturnValue(stack);
            }
        }
    }
}
 
Example 10
Source File: FarmerVillagerTask_wartFarmMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Redirect(method = "keepRunning", at = @At(
        value = "INVOKE",
        target = "Lnet/minecraft/inventory/BasicInventory;getInvSize()I"
))
private int plantWart(BasicInventory basicInventory, ServerWorld serverWorld, VillagerEntity villagerEntity, long l)
{
    if (isFarmingCleric) // fill cancel that for loop by setting length to 0
    {
        for(int i = 0; i < basicInventory.getInvSize(); ++i)
        {
            ItemStack itemStack = basicInventory.getInvStack(i);
            boolean bl = false;
            if (!itemStack.isEmpty())
            {
                if (itemStack.getItem() == Items.NETHER_WART)
                {
                    serverWorld.setBlockState(currentTarget, Blocks.NETHER_WART.getDefaultState(), 3);
                    bl = true;
                }
            }

            if (bl)
            {
                serverWorld.playSound(null,
                        currentTarget.getX(), currentTarget.getY(), this.currentTarget.getZ(),
                        SoundEvents.ITEM_NETHER_WART_PLANT, SoundCategory.BLOCKS, 1.0F, 1.0F);
                itemStack.decrement(1);
                if (itemStack.isEmpty())
                {
                    basicInventory.setInvStack(i, ItemStack.EMPTY);
                }
                break;
            }
        }
        return 0;

    }
    return basicInventory.getInvSize();
}
 
Example 11
Source File: ItemEntityMixin.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Inject(
        method = "tryMerge(Lnet/minecraft/entity/ItemEntity;)V",
        at = @At("HEAD"),
        cancellable = true
)
private void tryStackShulkerBoxes(ItemEntity other, CallbackInfo ci)
{
    ItemEntity self = (ItemEntity)(Object)this;
    ItemStack selfStack = self.getStack();
    if (!CarpetSettings.stackableShulkerBoxes || !(selfStack.getItem() instanceof BlockItem) || !(((BlockItem)selfStack.getItem()).getBlock() instanceof ShulkerBoxBlock)) {
        return;
    }

    ItemStack otherStack = other.getStack();
    if (selfStack.getItem() == otherStack.getItem()
            && !InventoryHelper.shulkerBoxHasItems(selfStack)
            && !InventoryHelper.shulkerBoxHasItems(otherStack)
            && selfStack.hasTag() == otherStack.hasTag()
            && selfStack.getCount() + otherStack.getCount() <= SHULKERBOX_MAX_STACK_AMOUNT)
    {
        int amount = Math.min(otherStack.getCount(), SHULKERBOX_MAX_STACK_AMOUNT - selfStack.getCount());

        selfStack.increment(amount);
        self.setStack(selfStack);

        this.pickupDelay = Math.max(((ItemEntityInterface)other).getPickupDelayCM(), this.pickupDelay);
        this.age = Math.min(((ItemEntityInterface)other).getAgeCM(), this.age);

        otherStack.decrement(amount);
        if (otherStack.isEmpty())
        {
            other.remove();
        }
        else
        {
            other.setStack(otherStack);
        }
        ci.cancel();
    }
}
 
Example 12
Source File: ConfigurableElectricMachineBlockEntity.java    From Galacticraft-Rewoven with MIT License 4 votes vote down vote up
protected void decrement(int slot, int amount) {
    ItemStack stack = getInventory().getStack(slot);
    stack.decrement(amount);
    getInventory().setStack(slot, stack);
}
 
Example 13
Source File: CircuitFabricatorBlockEntity.java    From Galacticraft-Rewoven with MIT License 4 votes vote down vote up
@Override
public void tick() {
    if (world.isClient) {
        return;
    }

    if (disabled()) {
        this.status = CircuitFabricatorStatus.OFF;
        idleEnergyDecrement(true);
        return;
    }
    attemptChargeFromStack(0);


    if (status == CircuitFabricatorStatus.IDLE) {
        idleEnergyDecrement(false);
        if (this.progress > 0) {
            progress--;
        }
    }


    if (getCapacitatorComponent().getCurrentEnergy() <= 0) {
        status = CircuitFabricatorStatus.NOT_ENOUGH_ENERGY;
    } else {
        status = CircuitFabricatorStatus.IDLE;
    }


    if (status == CircuitFabricatorStatus.NOT_ENOUGH_ENERGY) {
        if (progress > 0) {
            this.progress--;
        }
        return;
    }


    if (isValidRecipe(this.getInventory().getStack(5))) {
        if (canPutStackInResultSlot(getResultFromRecipeStack())) {
            this.status = CircuitFabricatorStatus.PROCESSING;
        }
    } else {
        if (this.status != CircuitFabricatorStatus.NOT_ENOUGH_ENERGY) {
            this.status = CircuitFabricatorStatus.IDLE;
        }
    }

    if (status == CircuitFabricatorStatus.PROCESSING) {
        ItemStack resultStack = getResultFromRecipeStack();
        if (this.progress < this.maxProgress) {
            ++progress;
            this.getCapacitatorComponent().extractEnergy(GalacticraftEnergy.GALACTICRAFT_JOULES, getEnergyUsagePerTick(), ActionType.PERFORM);
        } else {
            progress = 0;

            ItemStack stack = getInventory().getStack(1);
            stack.decrement(1);
            getInventory().setStack(1, stack);
            stack = getInventory().getStack(2);
            stack.decrement(1);
            getInventory().setStack(2, stack);
            stack = getInventory().getStack(3);
            stack.decrement(1);
            getInventory().setStack(3, stack);
            stack = getInventory().getStack(4);
            stack.decrement(1);
            getInventory().setStack(4, stack);
            stack = getInventory().getStack(5);
            stack.decrement(1);
            getInventory().setStack(5, stack);

            getInventory().insertStack(6, resultStack, ActionType.PERFORM);
        }
    }

    trySpreadEnergy();
}
 
Example 14
Source File: CoalGeneratorBlockEntity.java    From Galacticraft-Rewoven with MIT License 4 votes vote down vote up
@Override
public void tick() {
    if (this.world.isClient || disabled()) {
        return;
    }

    if (status == CoalGeneratorStatus.IDLE) {
        if (heat >= 1.0F) {
            heat -= 0.05F;
        } else {
            heat = 0;
        }
    }

    if (FUEL_MAP.containsKey(getInventory().getStack(0).getItem()) && getCapacitatorComponent().getCurrentEnergy() < getCapacitatorComponent().getMaxEnergy() && status == CoalGeneratorStatus.IDLE) {
        this.status = CoalGeneratorStatus.WARMING;

        this.fuelTimeMax = FUEL_MAP.get(getInventory().getStack(0).getItem());
        this.fuelTimeCurrent = 0;
        this.fuelEnergyPerTick = 120;

        ItemStack stack = getInventory().getStack(0).copy();
        stack.decrement(1);
        getInventory().setStack(0, stack);
    }

    if (this.status == CoalGeneratorStatus.WARMING) {
        if (this.heat >= 1.0f) {
            this.status = CoalGeneratorStatus.ACTIVE;
        }
        this.heat += 0.005f; //10 secs of heating - 1/8th of the time is spent heating (in this case) when it comes to coal/charcoal
    }

    if (status == CoalGeneratorStatus.ACTIVE || this.status == CoalGeneratorStatus.WARMING) {
        fuelTimeCurrent++;
        getCapacitatorComponent().insertEnergy(GalacticraftEnergy.GALACTICRAFT_JOULES, (int) (Galacticraft.configManager.get().coalGeneratorEnergyProductionRate() * heat), ActionType.PERFORM);

        if (fuelTimeCurrent >= fuelTimeMax) {
            this.status = CoalGeneratorStatus.IDLE;
            this.fuelTimeCurrent = 0;
        }
    }

    trySpreadEnergy();
    attemptDrainPowerToStack(1);
}
 
Example 15
Source File: PumpcownEntity.java    From the-hallow with MIT License 4 votes vote down vote up
@Override
public boolean interactMob(PlayerEntity player, Hand hand) {
	ItemStack stack = player.getStackInHand(hand);
	if (stack.getItem() == Items.SHEARS && this.getBreedingAge() >= 0) {
		this.world.addParticle(ParticleTypes.EXPLOSION, this.getX(), this.getY() + (double) (this.getHeight() / 2.0F), this.getZ(), 0.0D, 0.0D, 0.0D);
		if (!this.world.isClient) {
			this.remove();
			
			if (this.world.getDimension().getType() == HallowedDimensions.THE_HALLOW) {
				this.world.createExplosion(this, this.getX(), this.getY(), this.getZ(), 3.0F, Explosion.DestructionType.BREAK);
			} else {
				CowEntity cow = EntityType.COW.create(this.world);
				cow.updatePositionAndAngles(this.getX(), this.getY(), this.getZ(), this.yaw, this.pitch);
				cow.setHealth(this.getHealth());
				cow.bodyYaw = this.bodyYaw;
				if (this.hasCustomName()) {
					cow.setCustomName(this.getCustomName());
				}
				this.world.spawnEntity(cow);
			}
			
			for (int i = 0; i < 5; ++i) {
				this.world.spawnEntity(new ItemEntity(this.world, this.getX(), this.getY() + (double) this.getHeight(), this.getZ(), new ItemStack(STEM_FEATURE.getBlock())));
			}
			
			stack.damage(1, player, ((player_1) -> {
				player_1.sendToolBreakStatus(hand);
			}));
			this.playSound(SoundEvents.ENTITY_MOOSHROOM_SHEAR, 1.0F, 1.0F);
		}
		return true;
	} else if (stack.getItem() == Items.BOWL && this.getBreedingAge() >= 0 && !player.abilities.creativeMode) {
		stack.decrement(1);
		ItemStack stew = new ItemStack(HallowedItems.PUMPKIN_STEW);
		
		if (stack.isEmpty()) {
			player.setStackInHand(hand, stew);
		} else if (!player.inventory.insertStack(stew)) {
			player.dropItem(stew, false);
		}
		
		this.playSound(SoundEvents.ENTITY_MOOSHROOM_MILK, 1.0F, 1.0F);
		
		return true;
	} else {
		return super.interactMob(player, hand);
	}
}