use of net.minecraft.entity.player.PlayerEntity in project Bookshelf by Darkhax-Minecraft.
the class InventoryUtils method keepDamageableItems.
/**
* An extension of the IRecipe getRemainingItems method which attempts to keep items that
* have durability. Instead of being consumed these items will attempt to have their
* durability decreased. Things like unbreaking and unbreakable nbt settings are considered
* depending on the input arguments.
*
* @param inv The inventory doing the crafting.
* @param keptItems The list of items being kept.
* @param ignoreUnbreaking Whether or not unbreaking enchantments should be ignored.
* @param damageAmount The amount of damage to set on the item.
* @return The list of items being kept.
*/
public static NonNullList<ItemStack> keepDamageableItems(CraftingInventory inv, NonNullList<ItemStack> keptItems, boolean ignoreUnbreaking, int damageAmount) {
for (int i = 0; i < keptItems.size(); i++) {
final ItemStack stack = inv.getItem(i);
// Checks if the item has durability or has the unbreaking tag.
if (stack.getItem().canBeDepleted() || stack.hasTag() && stack.getTag().getBoolean("Unbreakable")) {
@Nullable final PlayerEntity player = InventoryUtils.getCraftingPlayer(inv);
final Random random = player != null ? player.getRandom() : Bookshelf.RANDOM;
final ItemStack retainedStack = stack.copy();
// Sometimes you may want to ignore/bypass the unbreaking enchantment.
if (ignoreUnbreaking) {
// other item damaging mechanics.
if (retainedStack.isDamageableItem()) {
retainedStack.setDamageValue(retainedStack.getDamageValue() + damageAmount);
}
} else {
// Attempts to damage the item, taking things like the unbreaking
// enchantment into consideration.
retainedStack.hurt(damageAmount, random, player instanceof ServerPlayerEntity ? (ServerPlayerEntity) player : null);
}
keptItems.set(i, retainedStack);
}
}
return keptItems;
}
use of net.minecraft.entity.player.PlayerEntity in project Bookshelf by Darkhax-Minecraft.
the class CommandLootChest method translate.
private int translate(CommandContext<CommandSource> context) throws CommandSyntaxException {
final ItemStack item = new ItemStack(Items.CHEST);
final CompoundNBT tag = item.getOrCreateTagElement("BlockEntityTag");
tag.putString("LootTable", ArgumentTypeLootTable.getTableId(context, "table"));
if (CommandUtils.hasArgument(context, "seed")) {
tag.putLong("LootTableSeed", StringArgumentType.getString(context, "seed").hashCode());
}
for (final PlayerEntity player : EntityArgument.getPlayers(context, "targets")) {
ItemHandlerHelper.giveItemToPlayer(player, item.copy());
}
return 0;
}
use of net.minecraft.entity.player.PlayerEntity in project BluePower by Qmunity.
the class BPEventHandler method onEntityAttack.
@SubscribeEvent
public void onEntityAttack(LivingAttackEvent event) {
if (!isAttacking && event.getSource() instanceof EntityDamageSource) {
// this event will be trigger recursively by EntityLiving#hurt,
// so we need to stop the loop.
EntityDamageSource entitySource = (EntityDamageSource) event.getSource();
if (entitySource.getEntity() instanceof PlayerEntity) {
PlayerEntity killer = (PlayerEntity) entitySource.getEntity();
if (!killer.inventory.getSelected().isEmpty()) {
if (EnchantmentHelper.getEnchantments(killer.inventory.getSelected()).containsKey(BPEnchantments.disjunction)) {
if (event.getEntityLiving() instanceof EndermanEntity || event.getEntityLiving() instanceof EnderDragonEntity) {
int level = EnchantmentHelper.getItemEnchantmentLevel(BPEnchantments.disjunction, killer.inventory.getSelected());
isAttacking = true;
event.getEntityLiving().hurt(event.getSource(), event.getAmount() * (level * 0.5F + 1));
isAttacking = false;
}
}
}
}
}
}
use of net.minecraft.entity.player.PlayerEntity in project BluePower by Qmunity.
the class BPEventHandler method itemPickUp.
@SubscribeEvent
public void itemPickUp(EntityItemPickupEvent event) {
PlayerEntity player = event.getPlayer();
ItemStack pickUp = event.getItem().getItem();
if (!(player.containerMenu instanceof ContainerSeedBag)) {
for (ItemStack is : player.inventory.items) {
if (!is.isEmpty() && is.getItem() instanceof ItemSeedBag) {
ItemStack seedType = ItemSeedBag.getSeedType(is);
if (!seedType.isEmpty() && seedType.sameItem(pickUp)) {
ItemStackHandler seedBagInvHandler = new ItemStackHandler(9);
// Get Items from the NBT Handler
if (is.hasTag())
seedBagInvHandler.deserializeNBT(is.getTag().getCompound("inv"));
// Attempt to insert items
for (int j = 0; j < 9 && !pickUp.isEmpty(); ++j) {
pickUp = seedBagInvHandler.insertItem(j, pickUp, false);
}
// Update items in the NBT
if (!is.hasTag())
is.setTag(new CompoundNBT());
if (is.getTag() != null) {
is.getTag().put("inv", seedBagInvHandler.serializeNBT());
}
// Pickup Leftovers
if (pickUp.isEmpty()) {
event.setResult(Event.Result.ALLOW);
event.getItem().remove();
return;
} else {
event.getItem().setItem(pickUp);
}
}
}
}
}
}
use of net.minecraft.entity.player.PlayerEntity in project BluePower by Qmunity.
the class BlockBPMicroblock method getStateForPlacement.
@Nullable
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
PlayerEntity player = context.getPlayer();
FluidState fluidstate = context.getLevel().getFluidState(context.getClickedPos());
if (player != null && !player.isCrouching()) {
return this.defaultBlockState().setValue(FACING, context.getClickedFace()).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
}
Vector3d vec = context.getPlayer().getLookAngle();
return this.defaultBlockState().setValue(FACING, Direction.getNearest(vec.x, vec.y, vec.z)).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
}
Aggregations