use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class LevelMixin method bridge$createEntity.
@Override
@SuppressWarnings("unchecked")
public <E extends org.spongepowered.api.entity.Entity> E bridge$createEntity(final EntityType<E> type, final Vector3d position, final boolean naturally) throws IllegalArgumentException, IllegalStateException {
if (type == net.minecraft.world.entity.EntityType.PLAYER) {
// Unable to construct these
throw new IllegalArgumentException("A Player cannot be created by the API!");
}
net.minecraft.world.entity.Entity entity = null;
final double x = position.x();
final double y = position.y();
final double z = position.z();
final net.minecraft.world.level.Level thisWorld = (net.minecraft.world.level.Level) (Object) this;
// Not all entities have a single World parameter as their constructor
if (type == net.minecraft.world.entity.EntityType.LIGHTNING_BOLT) {
entity = net.minecraft.world.entity.EntityType.LIGHTNING_BOLT.create(thisWorld);
entity.moveTo(x, y, z);
((LightningBolt) entity).setVisualOnly(false);
}
// TODO - archetypes should solve the problem of calling the correct constructor
if (type == net.minecraft.world.entity.EntityType.ENDER_PEARL) {
final ArmorStand tempEntity = new ArmorStand(thisWorld, x, y, z);
tempEntity.setPos(tempEntity.getX(), tempEntity.getY() - tempEntity.getEyeHeight(), tempEntity.getZ());
entity = new ThrownEnderpearl(thisWorld, tempEntity);
((EnderPearl) entity).offer(Keys.SHOOTER, UnknownProjectileSource.UNKNOWN);
}
// set them is to use the more specialised constructor).
if (type == net.minecraft.world.entity.EntityType.FALLING_BLOCK) {
entity = new FallingBlockEntity(thisWorld, x, y, z, Blocks.SAND.defaultBlockState());
}
if (type == net.minecraft.world.entity.EntityType.ITEM) {
entity = new ItemEntity(thisWorld, x, y, z, new ItemStack(Blocks.STONE));
}
if (entity == null) {
final ResourceKey key = (ResourceKey) (Object) Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) type);
try {
entity = ((net.minecraft.world.entity.EntityType) type).create(thisWorld);
entity.moveTo(x, y, z);
} catch (final Exception e) {
throw new RuntimeException("There was an issue attempting to construct " + key, e);
}
}
if (entity instanceof HangingEntity) {
if (!((HangingEntity) entity).survives()) {
throw new IllegalArgumentException("Hanging entity does not survive at the given position: " + position);
}
}
if (naturally && entity instanceof Mob) {
// Adding the default equipment
final DifficultyInstance difficulty = this.shadow$getCurrentDifficultyAt(new BlockPos(x, y, z));
((MobAccessor) entity).invoker$populateDefaultEquipmentSlots(difficulty);
}
if (entity instanceof Painting) {
// This is default when art is null when reading from NBT, could
// choose a random art instead?
((Painting) entity).motive = Motive.KEBAB;
}
return (E) entity;
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class ServerPlayerMixin_Tracker method tracker$throwItemDrop.
/**
* @author gabizou - June 4th, 2016
* @author i509VCB - February 17th, 2020 - 1.14.4
* @author gabizou - December 31st, 2021 - 1.16.5
* @reason We inject a construct event for the item drop and conveniently
* can redirect the super call.
*/
@Redirect(method = "drop", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;drop(Lnet/minecraft/world/item/ItemStack;ZZ)Lnet/minecraft/world/entity/item/ItemEntity;"))
@Nullable
private ItemEntity tracker$throwItemDrop(final Player thisPlayer, final ItemStack droppedItem, final boolean dropAround, final boolean traceItem) {
if (droppedItem.isEmpty()) {
return null;
}
if (((PlatformEntityBridge) this).bridge$isFakePlayer()) {
return super.shadow$drop(droppedItem, dropAround, traceItem);
}
if (((LevelBridge) this.level).bridge$isFake()) {
return super.shadow$drop(droppedItem, dropAround, traceItem);
}
final double posX1 = this.shadow$getX();
final double posY1 = this.shadow$getEyeY() - (double) 0.3F;
final double posZ1 = this.shadow$getZ();
// Now the real fun begins.
final ItemStack item;
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(droppedItem);
final List<ItemStackSnapshot> original = new ArrayList<>();
original.add(snapshot);
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
item = SpongeCommonEventFactory.throwDropItemAndConstructEvent((ServerPlayer) (Object) this, posX1, posY1, posZ1, snapshot, original, frame);
if (item == null || item.isEmpty()) {
return null;
}
// Here is where we would potentially perform item pre-merging (merge the item stacks with previously captured item stacks
// and only if those stacks can be stacked (count increased). Otherwise, we'll just continue to throw the entity item.
// For now, due to refactoring a majority of all of this code, pre-merging is disabled entirely.
final ItemEntity itemEntity = new ItemEntity(this.level, posX1, posY1, posZ1, droppedItem);
itemEntity.setPickUpDelay(40);
if (traceItem) {
itemEntity.setThrower(this.shadow$getUUID());
}
final Random random = this.shadow$getRandom();
if (dropAround) {
final float f = random.nextFloat() * 0.5F;
final float f1 = random.nextFloat() * ((float) Math.PI * 2F);
itemEntity.setDeltaMovement(-Mth.sin(f1) * f, 0.2F, Mth.cos(f1) * f);
} else {
final float f8 = Mth.sin(this.xRot * ((float) Math.PI / 180F));
final float f2 = Mth.cos(this.xRot * ((float) Math.PI / 180F));
final float f3 = Mth.sin(this.yRot * ((float) Math.PI / 180F));
final float f4 = Mth.cos(this.yRot * ((float) Math.PI / 180F));
final float f5 = this.random.nextFloat() * ((float) Math.PI * 2F);
final float f6 = 0.02F * this.random.nextFloat();
itemEntity.setDeltaMovement((double) (-f3 * f2 * 0.3F) + Math.cos(f5) * (double) f6, (-f8 * 0.3F + 0.1F + (this.random.nextFloat() - this.random.nextFloat()) * 0.1F), (double) (f4 * f2 * 0.3F) + Math.sin(f5) * (double) f6);
}
return itemEntity;
}
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method throwDropItemAndConstructEvent.
/**
* @author gabizou - April 19th, 2018
* Creates two events here:
* - {@link DropItemEvent}
* - {@link ConstructEntityEvent}
*
* This is to reduce the code size from normal entity drops and player drops.
* While player drops usually require performing position and motion modifications,
* we return the item stack if it is to be thrown (this allows the event to have a
* say in what item is dropped).
*
* @param entity The entity throwing the item
* @param posX The position x for the item stack to spawn
* @param posY The position y for the item stack to spawn
* @param posZ The position z for the item stack to spawn
* @param snapshot The item snapshot of the item to drop
* @param original The original list to be used
* @param frame
* @return The item if it is to be spawned, null if to be ignored
*/
@Nullable
public static ItemStack throwDropItemAndConstructEvent(final net.minecraft.world.entity.Entity entity, final double posX, final double posY, final double posZ, final ItemStackSnapshot snapshot, final List<ItemStackSnapshot> original, final CauseStackManager.StackFrame frame) {
final ItemStack item;
frame.pushCause(entity);
// FIRST we want to throw the DropItemEvent.PRE
final DropItemEvent.Pre dropEvent = SpongeEventFactory.createDropItemEventPre(frame.currentCause(), ImmutableList.of(snapshot), original);
SpongeCommon.post(dropEvent);
if (dropEvent.isCancelled()) {
return null;
}
if (dropEvent.droppedItems().isEmpty()) {
return null;
}
// SECOND throw the ConstructEntityEvent
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
final ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(frame.currentCause(), ServerLocation.of((ServerWorld) entity.level, posX, posY, posZ), new Vector3d(0, 0, 0), EntityTypes.ITEM.get());
frame.removeContext(EventContextKeys.SPAWN_TYPE);
SpongeCommon.post(event);
if (event.isCancelled()) {
return null;
}
item = event.isCancelled() ? null : ItemStackUtil.fromSnapshotToNative(dropEvent.droppedItems().get(0));
if (item == null) {
return null;
}
return item;
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class HumanEntity method performRangedAttack.
@Override
public void performRangedAttack(final LivingEntity target, final float distanceFactor) {
// Borrowed from Skeleton
final Arrow entitytippedarrow = new Arrow(this.level, this);
final double d0 = target.getX() - this.getX();
final double d1 = target.getBoundingBox().minY + target.getBbHeight() / 3.0F - entitytippedarrow.getY();
final double d2 = target.getZ() - this.getZ();
final double d3 = Mth.sqrt(d0 * d0 + d2 * d2);
entitytippedarrow.shoot(d0, d1 + d3 * 0.20000000298023224D, d2, 1.6F, 14 - this.level.getDifficulty().getId() * 4);
// These names are wrong
final int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH_ARROWS, this);
final int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAMING_ARROWS, this);
entitytippedarrow.setBaseDamage(distanceFactor * 2.0F + this.random.nextGaussian() * 0.25D + this.level.getDifficulty().getId() * 0.11F);
if (i > 0) {
entitytippedarrow.setBaseDamage(entitytippedarrow.getBaseDamage() + i * 0.5D + 0.5D);
}
if (j > 0) {
entitytippedarrow.setKnockback(j);
}
final ItemStack itemstack = this.getItemInHand(InteractionHand.OFF_HAND);
if (itemstack.getItem() == Items.TIPPED_ARROW) {
entitytippedarrow.setEffectsFromItem(itemstack);
}
this.playSound(SoundEvents.ARROW_SHOOT, 1.0F, 1.0F / (this.random.nextFloat() * 0.4F + 0.8F));
this.level.addFreshEntity(entitytippedarrow);
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class SpongeUserInventory method readList.
/**
* Reads from the given tag list and fills the slots in the inventory with the correct items.
*/
public void readList(final ListTag nbtTagListIn) {
this.mainInventory.clear();
this.armorInventory.clear();
this.offHandInventory.clear();
for (int i = 0; i < nbtTagListIn.size(); ++i) {
final CompoundTag nbttagcompound = nbtTagListIn.getCompound(i);
final int j = nbttagcompound.getByte("Slot") & 255;
final ItemStack itemstack = ItemStack.of(nbttagcompound);
if (!itemstack.isEmpty()) {
if (j >= 0 && j < this.mainInventory.size()) {
this.mainInventory.set(j, itemstack);
} else if (j >= 100 && j < this.armorInventory.size() + 100) {
this.armorInventory.set(j - 100, itemstack);
} else if (j >= 150 && j < this.offHandInventory.size() + 150) {
this.offHandInventory.set(j - 150, itemstack);
}
}
}
}
Aggregations