2015-12-27 19:38:12 -05:00
|
|
|
package WayofTime.bloodmagic.item.sigil;
|
|
|
|
|
2018-02-05 17:04:38 -08:00
|
|
|
import WayofTime.bloodmagic.apibutnotreally.util.helper.PlayerHelper;
|
2017-08-15 21:30:48 -07:00
|
|
|
import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
|
2015-12-27 19:38:12 -05:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2016-03-18 15:38:26 -04:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2015-12-27 19:38:12 -05:00
|
|
|
import net.minecraft.world.World;
|
2016-10-16 19:33:14 -04:00
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2015-12-27 19:38:12 -05:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class ItemSigilPhantomBridge extends ItemSigilToggleableBase {
|
2016-10-16 19:33:14 -04:00
|
|
|
private Map<EntityPlayer, Pair<Double, Double>> prevPositionMap = new HashMap<EntityPlayer, Pair<Double, Double>>();
|
|
|
|
private double expansionFactor = 2;
|
|
|
|
private int range = 3;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public ItemSigilPhantomBridge() {
|
2017-08-16 16:39:57 -07:00
|
|
|
super("phantom_bridge", 100);
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void onSigilUpdate(ItemStack stack, World world, EntityPlayer player, int itemSlot, boolean isSelected) {
|
2016-11-11 16:57:50 -08:00
|
|
|
if (PlayerHelper.isFakePlayer(player))
|
|
|
|
return;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (!prevPositionMap.containsKey(player)) {
|
2016-10-16 19:33:14 -04:00
|
|
|
prevPositionMap.put(player, Pair.of(player.posX, player.posZ));
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if ((!player.onGround && !player.isRiding()) && !player.isSneaking()) {
|
2016-10-16 19:33:14 -04:00
|
|
|
prevPositionMap.put(player, Pair.of(player.posX, player.posZ));
|
2015-12-27 19:38:12 -05:00
|
|
|
return;
|
2016-10-16 19:33:14 -04:00
|
|
|
}
|
2015-12-27 19:38:12 -05:00
|
|
|
|
|
|
|
int verticalOffset = -1;
|
|
|
|
|
|
|
|
if (player.isSneaking())
|
|
|
|
verticalOffset--;
|
|
|
|
|
2016-10-16 19:33:14 -04:00
|
|
|
Pair<Double, Double> prevPosition = prevPositionMap.get(player);
|
2015-12-27 19:38:12 -05:00
|
|
|
|
2016-10-16 19:33:14 -04:00
|
|
|
double playerVelX = player.posX - prevPosition.getLeft();
|
|
|
|
double playerVelZ = player.posZ - prevPosition.getRight();
|
|
|
|
|
|
|
|
double totalVel = Math.sqrt(playerVelX * playerVelX + playerVelZ * playerVelZ);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (totalVel > 2) {
|
2016-10-16 19:33:14 -04:00
|
|
|
//I am SURE you are teleporting!
|
|
|
|
playerVelX = 0;
|
|
|
|
playerVelZ = 0;
|
2016-12-06 11:53:47 -05:00
|
|
|
totalVel = 0;
|
2016-10-16 19:33:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockPos playerPos = player.getPosition();
|
|
|
|
int posX = playerPos.getX();
|
|
|
|
int posY = playerPos.getY();
|
|
|
|
int posZ = playerPos.getZ();
|
|
|
|
|
|
|
|
double offsetPosX = posX + expansionFactor * playerVelX;
|
|
|
|
double offsetPosZ = posZ + expansionFactor * playerVelZ;
|
|
|
|
double avgX = (posX + offsetPosX) / 2;
|
|
|
|
double avgZ = (posZ + offsetPosZ) / 2;
|
|
|
|
|
|
|
|
double C = 2 * (range + expansionFactor * totalVel) + 1;
|
|
|
|
int truncC = (int) C;
|
|
|
|
|
|
|
|
//TODO: Make this for-loop better.
|
2017-08-15 21:30:48 -07:00
|
|
|
for (int ix = -truncC; ix <= truncC; ix++) {
|
|
|
|
for (int iz = -truncC; iz <= truncC; iz++) {
|
|
|
|
if (computeEllipse(ix + avgX, iz + avgZ, posX, posZ, offsetPosX, offsetPosZ) > C) {
|
2016-10-16 19:33:14 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockPos blockPos = new BlockPos(ix + posX, posY + verticalOffset, iz + posZ);
|
2015-12-27 19:38:12 -05:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (world.isAirBlock(blockPos))
|
2017-08-14 20:53:42 -07:00
|
|
|
world.setBlockState(blockPos, RegistrarBloodMagicBlocks.PHANTOM.getDefaultState());
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-16 19:33:14 -04:00
|
|
|
|
|
|
|
prevPositionMap.put(player, Pair.of(player.posX, player.posZ));
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public static double computeEllipse(double x, double z, double focusX1, double focusZ1, double focusX2, double focusZ2) {
|
2016-10-16 19:33:14 -04:00
|
|
|
return Math.sqrt((x - focusX1) * (x - focusX1) + (z - focusZ1) * (z - focusZ1)) + Math.sqrt((x - focusX2) * (x - focusX2) + (z - focusZ2) * (z - focusZ2));
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
}
|