Search in sources :

Example 1 with PatternHelper

use of net.minecraft.block.state.pattern.BlockPattern.PatternHelper in project Cavern2 by kegare.

the class BlockPortalCavern method createPatternHelper.

@Override
public PatternHelper createPatternHelper(World world, BlockPos pos) {
    EnumFacing.Axis axis = EnumFacing.Axis.Z;
    Size size = new Size(world, pos, EnumFacing.Axis.X);
    LoadingCache<BlockPos, BlockWorldState> cache = BlockPattern.createLoadingCache(world, true);
    if (!size.isValid()) {
        axis = EnumFacing.Axis.X;
        size = new Size(world, pos, EnumFacing.Axis.Z);
    }
    if (!size.isValid()) {
        return new PatternHelper(pos, EnumFacing.NORTH, EnumFacing.UP, cache, 1, 1, 1);
    } else {
        int[] values = new int[EnumFacing.AxisDirection.values().length];
        EnumFacing facing = size.rightDir.rotateYCCW();
        BlockPos blockpos = size.bottomLeft.up(size.getHeight() - 1);
        for (EnumFacing.AxisDirection direction : EnumFacing.AxisDirection.values()) {
            PatternHelper pattern = new PatternHelper(facing.getAxisDirection() == direction ? blockpos : blockpos.offset(size.rightDir, size.getWidth() - 1), EnumFacing.getFacingFromAxis(direction, axis), EnumFacing.UP, cache, size.getWidth(), size.getHeight(), 1);
            for (int i = 0; i < size.getWidth(); ++i) {
                for (int j = 0; j < size.getHeight(); ++j) {
                    BlockWorldState state = pattern.translateOffset(i, j, 1);
                    if (state.getBlockState() != null && state.getBlockState().getMaterial() != Material.AIR) {
                        ++values[direction.ordinal()];
                    }
                }
            }
        }
        EnumFacing.AxisDirection axis1 = EnumFacing.AxisDirection.POSITIVE;
        for (EnumFacing.AxisDirection direction : EnumFacing.AxisDirection.values()) {
            if (values[direction.ordinal()] < values[axis1.ordinal()]) {
                axis1 = direction;
            }
        }
        return new PatternHelper(facing.getAxisDirection() == axis1 ? blockpos : blockpos.offset(size.rightDir, size.getWidth() - 1), EnumFacing.getFacingFromAxis(axis1, axis), EnumFacing.UP, cache, size.getWidth(), size.getHeight(), 1);
    }
}
Also used : PatternHelper(net.minecraft.block.state.pattern.BlockPattern.PatternHelper) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) BlockWorldState(net.minecraft.block.state.BlockWorldState)

Example 2 with PatternHelper

use of net.minecraft.block.state.pattern.BlockPattern.PatternHelper in project Cavern2 by kegare.

the class BlockPortalCavern method onEntityCollidedWithBlock.

@Override
public void onEntityCollidedWithBlock(World world, BlockPos pos, IBlockState state, Entity entity) {
    if (world.isRemote || getDimension() == null || teleporting) {
        return;
    }
    if (entity.isDead || entity.isSneaking() || entity.isRiding() || entity.isBeingRidden() || !entity.isNonBoss() || entity instanceof IProjectile) {
        return;
    }
    if (entity.timeUntilPortal <= 0) {
        ResourceLocation key = getRegistryName();
        IPortalCache cache = PortalCache.get(entity);
        MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
        DimensionType dimOld = world.provider.getDimensionType();
        DimensionType dimNew = isEntityInCave(entity) ? cache.getLastDim(key) : getDimension();
        WorldServer worldNew = server.getWorld(dimNew.getId());
        Teleporter teleporter = getTeleporter(worldNew);
        BlockPos prevPos = entity.getPosition();
        entity.timeUntilPortal = Math.max(entity.getPortalCooldown(), 100);
        if (entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) entity;
            if (MinerStats.get(player).getRank() < getMinerRank().getRank()) {
                player.sendStatusMessage(new TextComponentTranslation("cavern.message.portal.rank", new TextComponentTranslation(getMinerRank().getUnlocalizedName())), true);
                return;
            }
        }
        teleporting = true;
        cache.setLastDim(key, dimOld);
        cache.setLastPos(key, dimOld, prevPos);
        PatternHelper pattern = createPatternHelper(world, pos);
        double d0 = pattern.getForwards().getAxis() == EnumFacing.Axis.X ? (double) pattern.getFrontTopLeft().getZ() : (double) pattern.getFrontTopLeft().getX();
        double d1 = pattern.getForwards().getAxis() == EnumFacing.Axis.X ? entity.posZ : entity.posX;
        d1 = Math.abs(MathHelper.pct(d1 - (pattern.getForwards().rotateY().getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE ? 1 : 0), d0, d0 - pattern.getWidth()));
        double d2 = MathHelper.pct(entity.posY - 1.0D, pattern.getFrontTopLeft().getY(), pattern.getFrontTopLeft().getY() - pattern.getHeight());
        cache.setLastPortalVec(new Vec3d(d1, d2, 0.0D));
        cache.setTeleportDirection(pattern.getForwards());
        entity.changeDimension(dimNew.getId(), teleporter);
        teleporting = false;
    } else {
        entity.timeUntilPortal = Math.max(entity.getPortalCooldown(), 100);
    }
}
Also used : DimensionType(net.minecraft.world.DimensionType) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) PatternHelper(net.minecraft.block.state.pattern.BlockPattern.PatternHelper) Teleporter(net.minecraft.world.Teleporter) WorldServer(net.minecraft.world.WorldServer) Vec3d(net.minecraft.util.math.Vec3d) IProjectile(net.minecraft.entity.IProjectile) MinecraftServer(net.minecraft.server.MinecraftServer) IPortalCache(cavern.api.IPortalCache) ResourceLocation(net.minecraft.util.ResourceLocation) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos)

Example 3 with PatternHelper

use of net.minecraft.block.state.pattern.BlockPattern.PatternHelper in project Cavern2 by kegare.

the class CavebornEventHooks method onPlayerLoggedIn.

@SubscribeEvent
public void onPlayerLoggedIn(PlayerLoggedInEvent event) {
    if (!(event.player instanceof EntityPlayerMP)) {
        return;
    }
    EntityPlayerMP player = (EntityPlayerMP) event.player;
    if (!FIRST_PLAYERS.contains(player.getCachedUniqueIdString())) {
        return;
    }
    WorldServer world = player.getServerWorld();
    ConfigCaveborn.Type caveborn = GeneralConfig.caveborn.getType();
    BlockPortalCavern portal = caveborn.getPortalBlock();
    BlockPos pos = player.getPosition();
    IPortalCache cache = PortalCache.get(player);
    PatternHelper pattern = portal.createPatternHelper(world, pos);
    double d0 = pattern.getForwards().getAxis() == EnumFacing.Axis.X ? (double) pattern.getFrontTopLeft().getZ() : (double) pattern.getFrontTopLeft().getX();
    double d1 = pattern.getForwards().getAxis() == EnumFacing.Axis.X ? player.posZ : player.posX;
    d1 = Math.abs(MathHelper.pct(d1 - (pattern.getForwards().rotateY().getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE ? 1 : 0), d0, d0 - pattern.getWidth()));
    double d2 = MathHelper.pct(player.posY - 1.0D, pattern.getFrontTopLeft().getY(), pattern.getFrontTopLeft().getY() - pattern.getHeight());
    cache.setLastPortalVec(new Vec3d(d1, d2, 0.0D));
    cache.setTeleportDirection(pattern.getForwards());
    player.timeUntilPortal = player.getPortalCooldown();
    portal.getTeleporter(world).placeInPortal(player, player.rotationYaw);
    FIRST_PLAYERS.remove(event.player.getCachedUniqueIdString());
    DimensionType type = portal.getDimension();
    if (type == CaveDimensions.CAVERN) {
        PlayerHelper.grantCriterion(player, "root", "entered_cavern");
    } else {
        String name = type.getName();
        PlayerHelper.grantCriterion(player, "enter_the_" + name, "entered_" + name);
    }
    pos = player.getPosition();
    for (BlockPos blockpos : BlockPos.getAllInBoxMutable(pos.add(-1, -1, -1), pos.add(1, 1, 1))) {
        if (world.getBlockState(blockpos).getBlock() == portal) {
            world.setBlockToAir(blockpos);
            break;
        }
    }
    double x = player.posX;
    double y = player.posY + 0.25D;
    double z = player.posZ;
    world.playSound(null, x, y, z, SoundEvents.BLOCK_GLASS_BREAK, SoundCategory.BLOCKS, 1.0F, 0.65F);
    for (ItemMeta itemMeta : GeneralConfig.cavebornBonusItems.getItems()) {
        ItemStack stack = itemMeta.getItemStack();
        if (stack.isStackable()) {
            stack = itemMeta.getItemStack(MathHelper.getInt(CaveEventHooks.RANDOM, 4, 16));
        }
        InventoryHelper.spawnItemStack(world, x, y, z, stack);
    }
}
Also used : DimensionType(net.minecraft.world.DimensionType) PatternHelper(net.minecraft.block.state.pattern.BlockPattern.PatternHelper) ConfigCaveborn(cavern.config.property.ConfigCaveborn) WorldServer(net.minecraft.world.WorldServer) Vec3d(net.minecraft.util.math.Vec3d) IPortalCache(cavern.api.IPortalCache) BlockPortalCavern(cavern.block.BlockPortalCavern) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) ItemMeta(cavern.util.ItemMeta) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 4 with PatternHelper

use of net.minecraft.block.state.pattern.BlockPattern.PatternHelper in project takumicraft by TNTModders.

the class BlockTakumiPortal method createPatternHelper.

public PatternHelper createPatternHelper(World worldIn, BlockPos p_181089_2_) {
    Axis enumfacing$axis = Axis.Z;
    Size blockportal$size = new Size(worldIn, p_181089_2_, Axis.X);
    LoadingCache<BlockPos, BlockWorldState> loadingcache = BlockPattern.createLoadingCache(worldIn, true);
    if (!blockportal$size.isValid()) {
        enumfacing$axis = Axis.X;
        blockportal$size = new Size(worldIn, p_181089_2_, Axis.Z);
    }
    if (!blockportal$size.isValid()) {
        return new PatternHelper(p_181089_2_, EnumFacing.NORTH, EnumFacing.UP, loadingcache, 1, 1, 1);
    }
    int[] aint = new int[AxisDirection.values().length];
    EnumFacing enumfacing = blockportal$size.rightDir.rotateYCCW();
    BlockPos blockpos = blockportal$size.bottomLeft.up(blockportal$size.getHeight() - 1);
    for (AxisDirection enumfacing$axisdirection : AxisDirection.values()) {
        PatternHelper blockpattern$patternhelper = new PatternHelper(enumfacing.getAxisDirection() == enumfacing$axisdirection ? blockpos : blockpos.offset(blockportal$size.rightDir, blockportal$size.getWidth() - 1), EnumFacing.getFacingFromAxis(enumfacing$axisdirection, enumfacing$axis), EnumFacing.UP, loadingcache, blockportal$size.getWidth(), blockportal$size.getHeight(), 1);
        for (int i = 0; i < blockportal$size.getWidth(); ++i) {
            for (int j = 0; j < blockportal$size.getHeight(); ++j) {
                BlockWorldState blockworldstate = blockpattern$patternhelper.translateOffset(i, j, 1);
                if (blockworldstate.getBlockState() != null && blockworldstate.getBlockState().getMaterial() != Material.AIR) {
                    ++aint[enumfacing$axisdirection.ordinal()];
                }
            }
        }
    }
    AxisDirection enumfacing$axisdirection1 = AxisDirection.POSITIVE;
    for (AxisDirection enumfacing$axisdirection2 : AxisDirection.values()) {
        if (aint[enumfacing$axisdirection2.ordinal()] < aint[enumfacing$axisdirection1.ordinal()]) {
            enumfacing$axisdirection1 = enumfacing$axisdirection2;
        }
    }
    return new PatternHelper(enumfacing.getAxisDirection() == enumfacing$axisdirection1 ? blockpos : blockpos.offset(blockportal$size.rightDir, blockportal$size.getWidth() - 1), EnumFacing.getFacingFromAxis(enumfacing$axisdirection1, enumfacing$axis), EnumFacing.UP, loadingcache, blockportal$size.getWidth(), blockportal$size.getHeight(), 1);
}
Also used : PatternHelper(net.minecraft.block.state.pattern.BlockPattern.PatternHelper) EnumFacing(net.minecraft.util.EnumFacing) AxisDirection(net.minecraft.util.EnumFacing.AxisDirection) BlockPos(net.minecraft.util.math.BlockPos) BlockWorldState(net.minecraft.block.state.BlockWorldState) Axis(net.minecraft.util.EnumFacing.Axis)

Example 5 with PatternHelper

use of net.minecraft.block.state.pattern.BlockPattern.PatternHelper in project takumicraft by TNTModders.

the class TakumiTeleporter method setTakumiPortal.

public void setTakumiPortal(Entity entity, BlockPos pos) {
    if (entity.timeUntilPortal > 0) {
        entity.timeUntilPortal = entity.getPortalCooldown();
    } else {
        try {
            Field posField = TakumiASMNameMap.getField(Entity.class, "lastPortalPos");
            posField.setAccessible(true);
            if (!entity.world.isRemote && !Objects.equals(pos, posField.get(entity))) {
                posField.set(entity, new BlockPos(pos));
                PatternHelper blockpattern$patternhelper = TakumiBlockCore.TAKUMI_PORTAL.createPatternHelper(entity.world, (BlockPos) posField.get(entity));
                double d0 = blockpattern$patternhelper.getForwards().getAxis() == Axis.X ? (double) blockpattern$patternhelper.getFrontTopLeft().getZ() : (double) blockpattern$patternhelper.getFrontTopLeft().getX();
                double d1 = blockpattern$patternhelper.getForwards().getAxis() == Axis.X ? entity.posZ : entity.posX;
                d1 = Math.abs(MathHelper.pct(d1 - (double) (blockpattern$patternhelper.getForwards().rotateY().getAxisDirection() == AxisDirection.NEGATIVE ? 1 : 0), d0, d0 - (double) blockpattern$patternhelper.getWidth()));
                double d2 = MathHelper.pct(entity.posY - 1.0D, (double) blockpattern$patternhelper.getFrontTopLeft().getY(), (double) (blockpattern$patternhelper.getFrontTopLeft().getY() - blockpattern$patternhelper.getHeight()));
                Field vecField = TakumiASMNameMap.getField(Entity.class, "lastPortalVec");
                vecField.setAccessible(true);
                vecField.set(entity, new Vec3d(d1, d2, 0.0D));
                Field directionField = TakumiASMNameMap.getField(Entity.class, "teleportDirection");
                directionField.setAccessible(true);
                directionField.set(entity, blockpattern$patternhelper.getForwards());
            }
            Field inPortalField = TakumiASMNameMap.getField(Entity.class, "inPortal");
            inPortalField.setAccessible(true);
            inPortalField.set(entity, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : Field(java.lang.reflect.Field) PatternHelper(net.minecraft.block.state.pattern.BlockPattern.PatternHelper) BlockPos(net.minecraft.util.math.BlockPos) Vec3d(net.minecraft.util.math.Vec3d)

Aggregations

PatternHelper (net.minecraft.block.state.pattern.BlockPattern.PatternHelper)6 BlockPos (net.minecraft.util.math.BlockPos)6 Vec3d (net.minecraft.util.math.Vec3d)3 IPortalCache (cavern.api.IPortalCache)2 BlockWorldState (net.minecraft.block.state.BlockWorldState)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 EnumFacing (net.minecraft.util.EnumFacing)2 DimensionType (net.minecraft.world.DimensionType)2 WorldServer (net.minecraft.world.WorldServer)2 BlockPortalCavern (cavern.block.BlockPortalCavern)1 ConfigCaveborn (cavern.config.property.ConfigCaveborn)1 ItemMeta (cavern.util.ItemMeta)1 Field (java.lang.reflect.Field)1 IProjectile (net.minecraft.entity.IProjectile)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ItemStack (net.minecraft.item.ItemStack)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 Axis (net.minecraft.util.EnumFacing.Axis)1 AxisDirection (net.minecraft.util.EnumFacing.AxisDirection)1 ResourceLocation (net.minecraft.util.ResourceLocation)1