use of cavern.api.IPortalCache in project Cavern2 by kegare.
the class TeleporterCavern method placeInExistingPortal.
@Override
public boolean placeInExistingPortal(Entity entity, float rotationYaw) {
double portalDist = -1.0D;
int x = MathHelper.floor(entity.posX);
int z = MathHelper.floor(entity.posZ);
boolean flag = true;
BlockPos pos = BlockPos.ORIGIN;
long coord = ChunkPos.asLong(x, z);
if (destinationCoordinateCache.containsKey(coord)) {
PortalPosition portalPos = destinationCoordinateCache.get(coord);
portalDist = 0.0D;
pos = portalPos;
portalPos.lastUpdateTime = world.getTotalWorldTime();
flag = false;
} else {
BlockPos origin = new BlockPos(entity);
for (int px = -128; px <= 128; ++px) {
BlockPos current;
for (int pz = -128; pz <= 128; ++pz) {
for (BlockPos blockpos = origin.add(px, world.getActualHeight() - 1 - origin.getY(), pz); blockpos.getY() >= 0; blockpos = current) {
current = blockpos.down();
if (world.getBlockState(blockpos).getBlock() == portal) {
while (world.getBlockState(current = blockpos.down()).getBlock() == portal) {
blockpos = current;
}
double dist = blockpos.distanceSq(origin);
if (portalDist < 0.0D || dist < portalDist) {
portalDist = dist;
pos = blockpos;
}
}
}
}
}
}
if (portalDist >= 0.0D) {
if (flag) {
destinationCoordinateCache.put(coord, new PortalPosition(pos, world.getTotalWorldTime()));
}
IPortalCache cache = PortalCache.get(entity);
Vec3d portalVec = cache.getLastPortalVec();
EnumFacing teleportDirection = cache.getTeleportDirection();
double posX = pos.getX() + 0.5D;
double posZ = pos.getZ() + 0.5D;
BlockPattern.PatternHelper pattern = portal.createPatternHelper(world, pos);
boolean flag1 = pattern.getForwards().rotateY().getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE;
double d1 = pattern.getForwards().getAxis() == EnumFacing.Axis.X ? (double) pattern.getFrontTopLeft().getZ() : (double) pattern.getFrontTopLeft().getX();
double posY = pattern.getFrontTopLeft().getY() + 1 - portalVec.y * pattern.getHeight();
if (flag1) {
++d1;
}
if (pattern.getForwards().getAxis() == EnumFacing.Axis.X) {
posZ = d1 + (1.0D - portalVec.x) * pattern.getWidth() * pattern.getForwards().rotateY().getAxisDirection().getOffset();
} else {
posX = d1 + (1.0D - portalVec.x) * pattern.getWidth() * pattern.getForwards().rotateY().getAxisDirection().getOffset();
}
float f1 = 0.0F;
float f2 = 0.0F;
float f3 = 0.0F;
float f4 = 0.0F;
if (pattern.getForwards().getOpposite() == teleportDirection) {
f1 = 1.0F;
f2 = 1.0F;
} else if (pattern.getForwards().getOpposite() == teleportDirection.getOpposite()) {
f1 = -1.0F;
f2 = -1.0F;
} else if (pattern.getForwards().getOpposite() == teleportDirection.rotateY()) {
f3 = 1.0F;
f4 = -1.0F;
} else {
f3 = -1.0F;
f4 = 1.0F;
}
double d2 = entity.motionX;
double d3 = entity.motionZ;
entity.motionX = d2 * f1 + d3 * f4;
entity.motionZ = d2 * f3 + d3 * f2;
entity.rotationYaw = rotationYaw - teleportDirection.getOpposite().getHorizontalIndex() * 90 + pattern.getForwards().getHorizontalIndex() * 90;
if (entity instanceof EntityPlayerMP) {
((EntityPlayerMP) entity).connection.setPlayerLocation(posX, posY, posZ, entity.rotationYaw, entity.rotationPitch);
} else {
entity.setLocationAndAngles(posX, posY, posZ, entity.rotationYaw, entity.rotationPitch);
}
return true;
}
return false;
}
use of cavern.api.IPortalCache in project Cavern2 by kegare.
the class ItemMirageBook method attemptToLastPos.
protected boolean attemptToLastPos(World world, Entity entity) {
IPortalCache cache = PortalCache.get(entity);
ResourceLocation key = CaveUtils.getKey("mirage_worlds");
DimensionType type = world.provider.getDimensionType();
if (cache.hasLastPos(key, type)) {
BlockPos pos = cache.getLastPos(key, type);
if (world.getBlockState(pos.down()).getMaterial().isSolid() && world.getBlockState(pos).getBlock().canSpawnInBlock() && world.getBlockState(pos.up()).getBlock().canSpawnInBlock()) {
entity.moveToBlockPosAndAngles(pos, entity.rotationYaw, entity.rotationPitch);
return true;
}
cache.setLastPos(key, type, null);
}
return false;
}
use of cavern.api.IPortalCache in project Cavern2 by kegare.
the class CaveCapabilities method onPlayerClone.
@SubscribeEvent
public void onPlayerClone(PlayerEvent.Clone event) {
EntityPlayer player = event.getEntityPlayer();
if (player.world.isRemote) {
return;
}
EntityPlayer original = event.getOriginal();
IPortalCache originalPortalCache = getCapability(original, PORTAL_CACHE);
IPortalCache portalCache = getCapability(player, PORTAL_CACHE);
if (originalPortalCache != null && portalCache != null) {
NBTTagCompound nbt = new NBTTagCompound();
originalPortalCache.writeToNBT(nbt);
portalCache.readFromNBT(nbt);
}
IPlayerData originalPlayerData = getCapability(original, PLAYER_DATA);
IPlayerData playerData = getCapability(player, PLAYER_DATA);
if (originalPlayerData != null && playerData != null) {
NBTTagCompound nbt = new NBTTagCompound();
originalPlayerData.writeToNBT(nbt);
playerData.readFromNBT(nbt);
}
IMinerStats originalMinerStats = getCapability(original, MINER_STATS);
IMinerStats minerStats = getCapability(player, MINER_STATS);
if (originalMinerStats != null && minerStats != null) {
NBTTagCompound nbt = new NBTTagCompound();
originalMinerStats.writeToNBT(nbt);
minerStats.readFromNBT(nbt);
}
}
use of cavern.api.IPortalCache 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);
}
}
use of cavern.api.IPortalCache 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);
}
}
Aggregations