Search in sources :

Example 61 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project Tropicraft by Tropicraft.

the class TropicraftSpawnEgg method useOn.

public InteractionResult useOn(UseOnContext context) {
    Level world = context.getLevel();
    if (world.isClientSide) {
        return InteractionResult.SUCCESS;
    } else {
        ItemStack itemStack = context.getItemInHand();
        BlockPos pos = context.getClickedPos();
        Direction dir = context.getClickedFace();
        BlockState state = world.getBlockState(pos);
        Block block = state.getBlock();
        if (block == Blocks.SPAWNER) {
            BlockEntity te = world.getBlockEntity(pos);
            if (te instanceof SpawnerBlockEntity) {
                BaseSpawner spawner = ((SpawnerBlockEntity) te).getSpawner();
                EntityType<?> spawnType = typeIn.get();
                spawner.setEntityId(spawnType);
                te.setChanged();
                world.sendBlockUpdated(pos, state, state, 3);
                itemStack.shrink(1);
                return InteractionResult.SUCCESS;
            }
        }
        BlockPos spawnPos;
        if (state.getCollisionShape(world, pos).isEmpty()) {
            spawnPos = pos;
        } else {
            spawnPos = pos.relative(dir);
        }
        EntityType<?> type3 = typeIn.get();
        if (type3.spawn((ServerLevel) world, itemStack, context.getPlayer(), spawnPos, MobSpawnType.SPAWN_EGG, true, !Objects.equals(pos, spawnPos) && dir == Direction.UP) != null) {
            itemStack.shrink(1);
        }
        return InteractionResult.SUCCESS;
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockState(net.minecraft.world.level.block.state.BlockState) BaseSpawner(net.minecraft.world.level.BaseSpawner) LiquidBlock(net.minecraft.world.level.block.LiquidBlock) Block(net.minecraft.world.level.block.Block) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 62 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.

the class EntityMyPig method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(final net.minecraft.world.entity.player.Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
    if (enumhand == InteractionHand.OFF_HAND) {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.LEAD) {
                ((ServerLevel) this.level).getChunkProvider().broadcastAndSend(this, new ClientboundSetEntityLinkPacket(this, null));
                entityhuman.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        if (entityhuman instanceof ServerPlayer) {
                            // TODO
                            entityhuman.setItemInHand(InteractionHand.OFF_HAND, itemStack);
                            Player p = (Player) entityhuman.getBukkitEntity();
                            if (!p.isOnline()) {
                                p.saveData();
                            }
                        }
                    }
                }.runTaskLater(MyPetApi.getPlugin(), 5);
            }
        }
        return InteractionResult.CONSUME;
    }
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
        if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            getMyPet().setSaddle(CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        } else if (itemStack.getItem() == Items.SHEARS && getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getSaddle()));
            entityitem.pickupDelay = 10;
            entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
            this.level.addFreshEntity(entityitem);
            makeSound("entity.sheep.shear", 1.0F, 1.0F);
            getMyPet().setSaddle(null);
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                try {
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
                } catch (Error e) {
                    // TODO REMOVE
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
                        try {
                            CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
                        } catch (IllegalAccessException | InvocationTargetException ex) {
                            ex.printStackTrace();
                        }
                    });
                }
            }
            return InteractionResult.CONSUME;
        } else if (Configuration.MyPet.Pig.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            getMyPet().setBaby(false);
            return InteractionResult.CONSUME;
        }
    }
    return InteractionResult.PASS;
}
Also used : MyPet(de.Keyle.MyPet.api.entity.MyPet) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetApi(de.Keyle.MyPet.MyPetApi) MyPig(de.Keyle.MyPet.api.entity.types.MyPig) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) Player(org.bukkit.entity.Player) ServerLevel(net.minecraft.server.level.ServerLevel) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) ServerPlayer(net.minecraft.server.level.ServerPlayer) Configuration(de.Keyle.MyPet.api.Configuration) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) CompatManager(de.Keyle.MyPet.compat.v1_17_R1.CompatManager) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) ServerPlayer(net.minecraft.server.level.ServerPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 63 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.

the class EntityMyEnderDragon method onLivingUpdate.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onLivingUpdate() {
    super.onLivingUpdate();
    if (Configuration.MyPet.EnderDragon.CAN_GLIDE) {
        if (!this.onGround && this.getDeltaMovement().y() < 0.0D) {
            this.setDeltaMovement(getDeltaMovement().multiply(1, 0.6D, 1));
        }
    }
    if (!registered && this.valid) {
        if (this.getCommandSenderWorld() instanceof ServerLevel) {
            ServerLevel world = (ServerLevel) this.getCommandSenderWorld();
            // The next part used to be prettier but... whilst it is listed everywhere I looked, world.dragonParts is just not... available?
            // Mojang Field: dragonParts
            Field dragonPartsField = ReflectionUtil.getField(ServerLevel.class, "ac");
            Int2ObjectMap dragonParts = (Int2ObjectMap) ReflectionUtil.getFieldValue(dragonPartsField, world);
            Arrays.stream(this.children).forEach(entityMyPetPart -> dragonParts.put(entityMyPetPart.getId(), entityMyPetPart));
            ReflectionUtil.setFieldValue(dragonPartsField, world, dragonParts);
        }
        this.registered = true;
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) Field(java.lang.reflect.Field) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap)

Example 64 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.

the class PlatformHelper method strikeLightning.

@Override
public void strikeLightning(Location loc, float distance) {
    ServerLevel world = ((CraftWorld) loc.getWorld()).getHandle();
    LightningBolt lightning = new LightningBolt(EntityType.LIGHTNING_BOLT, world);
    lightning.setVisualOnly(true);
    lightning.moveTo(loc.getX(), loc.getY(), loc.getZ(), 0.0F, 0.0F);
    world.getCraftServer().getServer().getPlayerList().broadcast(null, loc.getX(), loc.getY(), loc.getZ(), distance, world.dimension(), new ClientboundAddEntityPacket(lightning));
    world.getCraftServer().getServer().getPlayerList().broadcast(null, loc.getX(), loc.getY(), loc.getZ(), distance, world.dimension(), new ClientboundSoundPacket(SoundEvents.LIGHTNING_BOLT_THUNDER, SoundSource.WEATHER, loc.getX(), loc.getY(), loc.getZ(), distance, 1F));
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LightningBolt(net.minecraft.world.entity.LightningBolt) ClientboundAddEntityPacket(net.minecraft.network.protocol.game.ClientboundAddEntityPacket) ClientboundSoundPacket(net.minecraft.network.protocol.game.ClientboundSoundPacket) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 65 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.

the class EntityMyPig method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(final net.minecraft.world.entity.player.Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
    if (enumhand == InteractionHand.OFF_HAND) {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.LEAD) {
                ((ServerLevel) this.level).getChunkSource().broadcastAndSend(this, new ClientboundSetEntityLinkPacket(this, null));
                entityhuman.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        if (entityhuman instanceof ServerPlayer) {
                            // TODO
                            entityhuman.setItemInHand(InteractionHand.OFF_HAND, itemStack);
                            Player p = (Player) entityhuman.getBukkitEntity();
                            if (!p.isOnline()) {
                                p.saveData();
                            }
                        }
                    }
                }.runTaskLater(MyPetApi.getPlugin(), 5);
            }
        }
        return InteractionResult.CONSUME;
    }
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
        if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            getMyPet().setSaddle(CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        } else if (itemStack.getItem() == Items.SHEARS && getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getSaddle()));
            entityitem.pickupDelay = 10;
            entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
            this.level.addFreshEntity(entityitem);
            makeSound("entity.sheep.shear", 1.0F, 1.0F);
            getMyPet().setSaddle(null);
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                try {
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
                } catch (Error e) {
                    // TODO REMOVE
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
                        try {
                            CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
                        } catch (IllegalAccessException | InvocationTargetException ex) {
                            ex.printStackTrace();
                        }
                    });
                }
            }
            return InteractionResult.CONSUME;
        } else if (Configuration.MyPet.Pig.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            getMyPet().setBaby(false);
            return InteractionResult.CONSUME;
        }
    }
    return InteractionResult.PASS;
}
Also used : MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetApi(de.Keyle.MyPet.MyPetApi) MyPig(de.Keyle.MyPet.api.entity.types.MyPig) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) CompatManager(de.Keyle.MyPet.compat.v1_18_R1.CompatManager) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) Player(org.bukkit.entity.Player) ServerLevel(net.minecraft.server.level.ServerLevel) InvocationTargetException(java.lang.reflect.InvocationTargetException) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) ServerPlayer(net.minecraft.server.level.ServerPlayer) Configuration(de.Keyle.MyPet.api.Configuration) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) ServerPlayer(net.minecraft.server.level.ServerPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)115 BlockPos (net.minecraft.core.BlockPos)30 Nullable (org.checkerframework.checker.nullness.qual.Nullable)21 Level (net.minecraft.world.level.Level)20 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)16 Entity (net.minecraft.world.entity.Entity)14 ServerPlayer (net.minecraft.server.level.ServerPlayer)11 ItemStack (net.minecraft.world.item.ItemStack)10 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)10 IOException (java.io.IOException)9 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)9 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)9 ArrayList (java.util.ArrayList)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 List (java.util.List)7 Optional (java.util.Optional)7 UUID (java.util.UUID)7 Vec3 (net.minecraft.world.phys.Vec3)7 CraftWorld (org.bukkit.craftbukkit.v1_17_R1.CraftWorld)7 Cause (org.spongepowered.api.event.Cause)7