Search in sources :

Example 26 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class FireArmorItem method clientTick.

// @Override
// public void damageArmor(LivingEntity player, ItemStack stack, DamageSource source, int damage, int slot) {
// if (source == DamageSource.IN_FIRE || source == DamageSource.LAVA) {
// //cheap way to slow the damage
// //if (player.worldObj.getWorldTime() % 2 == 0) {
// stack.damageItem(damage, player);
// }
// }
// 
@OnlyIn(Dist.CLIENT)
public void clientTick(Player player) {
    // Don't show fire particles underwater
    if (player.isInWater())
        return;
    float range = 0.2F;
    float speed = 0.08F;
    Random rand = new Random();
    Level worldRef = player.level;
    int extraRand = 0;
    final Vec3 motion = player.getDeltaMovement();
    double plSpeed = Math.sqrt(motion.x * motion.x + motion.z * motion.z);
    if (plSpeed < 0.1F) {
        extraRand = 7;
    }
    /**
     * 0 for all, 1 for minimal, 2 for off
     */
    ParticleStatus particles = Minecraft.getInstance().options.particles;
    if (particles == ParticleStatus.MINIMAL)
        return;
    if (this == TropicraftItems.FIRE_BOOTS.get()) {
        boolean onLava = false;
        boolean inLava = false;
        // for (int x = -1; x < 2; x++) {
        // for (int z = -1; z < 2; z++) {
        int x = 0;
        int z = 0;
        if (motion.y < 0) {
            BlockState state = player.level.getBlockState(new BlockPos(Mth.floor(player.getX() + x), Mth.floor(player.getY() - 2), Mth.floor(player.getZ() + z)));
            if (state.getMaterial() == Material.LAVA) {
                onLava = true;
            }
        }
        BlockState block2 = player.level.getBlockState(new BlockPos(Mth.floor(player.getX() + x), Mth.floor(player.getY() - 1), Mth.floor(player.getZ() + z)));
        if (block2.getMaterial() == Material.LAVA) {
            inLava = true;
        }
        // why do we do this on the client?
        if (onLava && !inLava) {
            player.setDeltaMovement(motion.multiply(1, 0, 1));
            player.setOnGround(true);
        }
        // why do we do this on the client???????
        if (inLava) {
            if (plSpeed < 0.4D) {
                player.setDeltaMovement(motion.multiply(1.5D, 1.5D, 1.5D));
            }
        }
        float look = player.level.getGameTime() * (10 + (onLava ? 10 : 0));
        double dist = 1F;
        double gatherX = player.getX();
        double gatherY = player.getBoundingBox().minY;
        double gatherZ = player.getZ();
        double motionX = ((rand.nextFloat() * speed) - (speed / 2));
        double motionZ = ((rand.nextFloat() * speed) - (speed / 2));
        final int numFeetParticles = particles == ParticleStatus.DECREASED ? 2 : 11;
        for (int i = 0; i < numFeetParticles + (onLava ? 5 : 0); i++) {
            motionX = (-Math.sin((look) / 180.0F * 3.1415927F) * Math.cos(0 / 180.0F * 3.1415927F) * (speed + (0.1 * rand.nextDouble())));
            motionZ = (Math.cos((look) / 180.0F * 3.1415927F) * Math.cos(0 / 180.0F * 3.1415927F) * (speed + (0.1 * rand.nextDouble())));
            SimpleParticleType particle = ParticleTypes.FLAME;
            if (rand.nextInt(22) == 0)
                particle = ParticleTypes.LARGE_SMOKE;
            if (onLava || rand.nextInt(1 + extraRand) == 0) {
                Vec3 motion1 = player.getDeltaMovement();
                player.level.addParticle(particle, gatherX + ((rand.nextFloat() * range) - (range / 2)), gatherY + ((rand.nextFloat() * range) - (range / 2)), gatherZ + ((rand.nextFloat() * range) - (range / 2)), motion1.x + motionX, 0.01F, motion1.z + motionZ);
                player.level.addParticle(particle, (double) gatherX + ((rand.nextFloat() * range) - (range / 2)), (double) gatherY + ((rand.nextFloat() * range) - (range / 2)), (double) gatherZ + ((rand.nextFloat() * range) - (range / 2)), motion1.x - motionX, 0.01F, motion1.z - motionZ);
            }
        }
    } else if (this == TropicraftItems.FIRE_LEGGINGS.get()) {
        SimpleParticleType particle = ParticleTypes.FLAME;
        if (rand.nextInt(2) == 0)
            particle = ParticleTypes.LARGE_SMOKE;
        if (rand.nextInt(3 + extraRand) == 0) {
            player.level.addParticle(particle, player.getX() + ((rand.nextFloat() * range) - (range / 2)), player.getY() - 0.8F + ((rand.nextFloat() * range) - (range / 2)), player.getZ() + ((rand.nextFloat() * range) - (range / 2)), ((rand.nextFloat() * speed) - (speed / 2)), -0.05F, ((rand.nextFloat() * speed) - (speed / 2)));
        }
    } else if (this == TropicraftItems.FIRE_CHESTPLATE.get()) {
        float look = -180F;
        double dist = 0.5F;
        double gatherX = player.getX() + (-Math.sin((player.getYRot() + look) / 180.0F * 3.1415927F) * Math.cos(player.getXRot() / 180.0F * 3.1415927F) * dist);
        double gatherZ = player.getZ() + (Math.cos((player.getYRot() + look) / 180.0F * 3.1415927F) * Math.cos(player.getXRot() / 180.0F * 3.1415927F) * dist);
        SimpleParticleType particle = ParticleTypes.FLAME;
        if (rand.nextInt(2) == 0)
            particle = ParticleTypes.LARGE_SMOKE;
        if (rand.nextInt(1 + extraRand) == 0) {
            player.level.addParticle(particle, gatherX + ((rand.nextFloat() * range) - (range / 2)), player.getY() - 0.4F + ((rand.nextFloat() * range) - (range / 2)), gatherZ + ((rand.nextFloat() * range) - (range / 2)), ((rand.nextFloat() * speed) - (speed / 2)), -0.01F, ((rand.nextFloat() * speed) - (speed / 2)));
        }
    } else if (this == TropicraftItems.FIRE_HELMET.get()) {
        float look = -180F;
        double dist = 0.5F;
        range = 2F;
        double gatherX = player.getX() + (-Math.sin((player.getYRot() + look) / 180.0F * 3.1415927F) * Math.cos(player.getXRot() / 180.0F * 3.1415927F) * dist);
        double gatherZ = player.getZ() + (Math.cos((player.getYRot() + look) / 180.0F * 3.1415927F) * Math.cos(player.getXRot() / 180.0F * 3.1415927F) * dist);
        SimpleParticleType particle = ParticleTypes.FLAME;
        if (rand.nextInt(2) == 0)
            particle = ParticleTypes.LARGE_SMOKE;
        if (rand.nextInt(2) == 0) {
            player.level.addParticle(particle, gatherX + ((rand.nextFloat() * range) - (range / 2)), player.getY() + 0.7F, gatherZ + ((rand.nextFloat() * range) - (range / 2)), ((rand.nextFloat() * speed) - (speed / 2)), -0.01F, ((rand.nextFloat() * speed) - (speed / 2)));
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) ParticleStatus(net.minecraft.client.ParticleStatus) Vec3(net.minecraft.world.phys.Vec3) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) SimpleParticleType(net.minecraft.core.particles.SimpleParticleType) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 27 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class TropicalFertilizerItem method useOn.

@Override
public InteractionResult useOn(UseOnContext context) {
    BlockState state = context.getLevel().getBlockState(context.getClickedPos());
    if (state.getBlock() == Blocks.GRASS_BLOCK) {
        if (!context.getLevel().isClientSide) {
            // Logic from GrassBlock#grow, with probability for grass significantly reduced
            BlockPos blockpos = context.getClickedPos().above();
            BlockState blockstate = Blocks.GRASS.defaultBlockState();
            Level world = context.getLevel();
            Random rand = world.getRandom();
            for (int i = 0; i < 128; ++i) {
                BlockPos blockpos1 = blockpos;
                int j = 0;
                while (true) {
                    if (j >= i / 16) {
                        BlockState blockstate2 = world.getBlockState(blockpos1);
                        if (blockstate2.getBlock() == blockstate.getBlock() && rand.nextInt(10) == 0) {
                            if (world instanceof ServerLevel) {
                                ((BonemealableBlock) blockstate.getBlock()).performBonemeal((ServerLevel) world, rand, blockpos1, blockstate2);
                            }
                        }
                        if (!blockstate2.isAir()) {
                            break;
                        }
                        BlockState blockstate1;
                        if (rand.nextInt(8) > 0) {
                            // Modification here, == changed to > to invert chances
                            List<ConfiguredFeature<?, ?>> list = world.getBiome(blockpos1).getGenerationSettings().getFlowerFeatures();
                            if (list.isEmpty()) {
                                break;
                            }
                            // TODO this is so ugly and hacky, pls
                            ConfiguredFeature<?, ?> pFlowerFeature = list.get(0);
                            AbstractFlowerFeature<FeatureConfiguration> abstractflowerfeature = (AbstractFlowerFeature) pFlowerFeature.feature;
                            blockstate1 = abstractflowerfeature.getRandomFlower(rand, blockpos1, pFlowerFeature.config());
                        } else {
                            blockstate1 = blockstate;
                        }
                        if (blockstate1.canSurvive(world, blockpos1)) {
                            world.setBlockAndUpdate(blockpos1, blockstate1);
                        }
                        break;
                    }
                    blockpos1 = blockpos1.offset(rand.nextInt(3) - 1, (rand.nextInt(3) - 1) * rand.nextInt(3) / 2, rand.nextInt(3) - 1);
                    if (world.getBlockState(blockpos1.below()).getBlock() != Blocks.GRASS_BLOCK || world.getBlockState(blockpos1).isCollisionShapeFullBlock(world, blockpos1)) {
                        break;
                    }
                    ++j;
                }
            }
        }
        return InteractionResult.SUCCESS;
    }
    return super.useOn(context);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) ConfiguredFeature(net.minecraft.world.level.levelgen.feature.ConfiguredFeature) BonemealableBlock(net.minecraft.world.level.block.BonemealableBlock) FeatureConfiguration(net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) BlockPos(net.minecraft.core.BlockPos) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) AbstractFlowerFeature(net.minecraft.world.level.levelgen.feature.AbstractFlowerFeature)

Example 28 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class ScubaData method onPlayerTick.

@SubscribeEvent
public static void onPlayerTick(PlayerTickEvent event) {
    Level world = event.player.level;
    if (event.phase == Phase.END) {
        // TODO support more than chest slot?
        ItemStack chestStack = event.player.getItemBySlot(EquipmentSlot.CHEST);
        Item chestItem = chestStack.getItem();
        if (chestItem instanceof ScubaArmorItem) {
            LazyOptional<ScubaData> data = event.player.getCapability(CAPABILITY);
            if (!world.isClientSide) {
                underwaterPlayers.add((ServerPlayer) event.player);
            }
            if (isUnderWater(event.player)) {
                data.ifPresent(d -> {
                    d.tick(event.player);
                    if (!world.isClientSide) {
                        d.updateClient((ServerPlayer) event.player, false);
                    }
                });
                ((ScubaArmorItem) chestItem).tickAir(event.player, EquipmentSlot.CHEST, chestStack);
                if (!world.isClientSide && world.getGameTime() % 60 == 0) {
                    // TODO this effect could be better, custom packet?
                    Vec3 eyePos = event.player.getEyePosition(0);
                    Vec3 motion = event.player.getDeltaMovement();
                    Vec3 particlePos = eyePos.add(motion.reverse());
                    ((ServerLevel) world).sendParticles(ParticleTypes.BUBBLE, particlePos.x(), particlePos.y(), particlePos.z(), 4 + world.random.nextInt(3), 0.25, 0.25, 0.25, motion.length());
                }
            } else if (!world.isClientSide && underwaterPlayers.remove(event.player)) {
                // Update client state as they leave the water
                data.ifPresent(d -> d.updateClient((ServerPlayer) event.player, false));
            }
        }
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Direction(net.minecraft.core.Direction) Item(net.minecraft.world.item.Item) CapabilityManager(net.minecraftforge.common.capabilities.CapabilityManager) EventBusSubscriber(net.minecraftforge.fml.common.Mod.EventBusSubscriber) PacketDistributor(net.minecraftforge.fmllegacy.network.PacketDistributor) TropicraftDimension(net.tropicraft.core.common.dimension.TropicraftDimension) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) ServerLevel(net.minecraft.server.level.ServerLevel) LazyOptional(net.minecraftforge.common.util.LazyOptional) ServerPlayer(net.minecraft.server.level.ServerPlayer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) Nonnull(javax.annotation.Nonnull) Phase(net.minecraftforge.event.TickEvent.Phase) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) Nullable(javax.annotation.Nullable) WeakHashMap(java.util.WeakHashMap) PlayerEvent(net.minecraftforge.event.entity.player.PlayerEvent) PlayerTickEvent(net.minecraftforge.event.TickEvent.PlayerTickEvent) PlayerChangedDimensionEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerChangedDimensionEvent) FluidTags(net.minecraft.tags.FluidTags) Set(java.util.Set) AttachCapabilitiesEvent(net.minecraftforge.event.AttachCapabilitiesEvent) Capability(net.minecraftforge.common.capabilities.Capability) Player(net.minecraft.world.entity.player.Player) PlayerRespawnEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerRespawnEvent) TropicraftPackets(net.tropicraft.core.common.network.TropicraftPackets) ICapabilitySerializable(net.minecraftforge.common.capabilities.ICapabilitySerializable) CompoundTag(net.minecraft.nbt.CompoundTag) Entity(net.minecraft.world.entity.Entity) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Constants(net.tropicraft.Constants) PlayerLoggedInEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) CapabilityToken(net.minecraftforge.common.capabilities.CapabilityToken) ParticleTypes(net.minecraft.core.particles.ParticleTypes) Bus(net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus) Collections(java.util.Collections) INBTSerializable(net.minecraftforge.common.util.INBTSerializable) Item(net.minecraft.world.item.Item) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) ServerLevel(net.minecraft.server.level.ServerLevel) Vec3(net.minecraft.world.phys.Vec3) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 29 with Level

use of net.minecraft.world.level.Level 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 30 with Level

use of net.minecraft.world.level.Level in project MinecraftForge by MinecraftForge.

the class DispenseFluidContainer method fillContainer.

/**
 * Picks up fluid in front of a Dispenser and fills a container with it.
 */
@Nonnull
private ItemStack fillContainer(@Nonnull BlockSource source, @Nonnull ItemStack stack) {
    Level world = source.getLevel();
    Direction dispenserFacing = source.getBlockState().getValue(DispenserBlock.FACING);
    BlockPos blockpos = source.getPos().relative(dispenserFacing);
    FluidActionResult actionResult = FluidUtil.tryPickUpFluid(stack, null, world, blockpos, dispenserFacing.getOpposite());
    ItemStack resultStack = actionResult.getResult();
    if (!actionResult.isSuccess() || resultStack.isEmpty()) {
        return super.execute(source, stack);
    }
    if (stack.getCount() == 1) {
        return resultStack;
    } else if (((DispenserBlockEntity) source.getEntity()).addItem(resultStack) < 0) {
        this.dispenseBehavior.dispense(source, resultStack);
    }
    ItemStack stackCopy = stack.copy();
    stackCopy.shrink(1);
    return stackCopy;
}
Also used : DispenserBlockEntity(net.minecraft.world.level.block.entity.DispenserBlockEntity) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) Nonnull(javax.annotation.Nonnull)

Aggregations

Level (net.minecraft.world.level.Level)53 BlockPos (net.minecraft.core.BlockPos)26 ServerLevel (net.minecraft.server.level.ServerLevel)19 ItemStack (net.minecraft.world.item.ItemStack)14 BlockState (net.minecraft.world.level.block.state.BlockState)13 IOException (java.io.IOException)10 Direction (net.minecraft.core.Direction)8 ChunkPos (net.minecraft.world.level.ChunkPos)6 Path (java.nio.file.Path)5 ExecutionException (java.util.concurrent.ExecutionException)5 Nullable (javax.annotation.Nullable)5 Entity (net.minecraft.world.entity.Entity)5 Player (net.minecraft.world.entity.player.Player)5 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)5 LevelStem (net.minecraft.world.level.dimension.LevelStem)5 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)5 Map (java.util.Map)4 Set (java.util.Set)4 CompoundTag (net.minecraft.nbt.CompoundTag)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4