ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemTeleStaff.java

75 lines
3.2 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ItemTeleStaff.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
2016-05-01 22:26:26 +02:00
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
2021-02-26 22:15:48 +01:00
import net.minecraft.util.Hand;
2016-05-01 22:26:26 +02:00
import net.minecraft.util.SoundCategory;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
2019-05-02 09:10:29 +02:00
public class ItemTeleStaff extends ItemEnergy {
public ItemTeleStaff() {
2021-04-19 21:20:23 +02:00
super(250000, 1000);
}
@Override
2021-02-26 22:15:48 +01:00
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
2016-11-19 21:11:17 +01:00
ItemStack stack = player.getHeldItem(hand);
2019-05-02 09:10:29 +02:00
if (!world.isRemote) {
RayTraceResult pos = WorldUtil.getNearestPositionWithAir(world, player, 100);
2019-05-02 09:10:29 +02:00
if (pos != null && (pos.typeOfHit == RayTraceResult.Type.BLOCK || player.rotationPitch >= -5)) {
int side = pos.sideHit.ordinal();
2019-05-02 09:10:29 +02:00
if (side != -1) {
2021-02-26 22:15:48 +01:00
double x = pos.hitVec.x - (side == 4
? 0.5
: 0) + (side == 5
? 0.5
: 0);
double y = pos.hitVec.y - (side == 0
? 2.0
: 0) + (side == 1
? 0.5
: 0);
double z = pos.hitVec.z - (side == 2
? 0.5
: 0) + (side == 3
? 0.5
: 0);
int baseUse = 200;
2019-05-02 09:10:29 +02:00
int use = baseUse + (int) (baseUse * pos.hitVec.distanceTo(new Vec3d(player.posX, player.posY + (player.getEyeHeight() - player.getDefaultEyeHeight()), player.posZ)));
if (this.getEnergyStored(stack) >= use) {
2021-02-26 22:15:48 +01:00
((ServerPlayerEntity) player).connection.setPlayerLocation(x, y, z, player.rotationYaw, player.rotationPitch);
player.dismountRidingEntity();
world.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
2021-02-28 15:47:54 +01:00
if (!player.isCreative()) {
this.extractEnergyInternal(stack, use, false);
player.getCooldownTracker().setCooldown(this, 50);
2015-10-03 10:19:40 +02:00
}
return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
2015-10-03 10:19:40 +02:00
}
}
}
}
2016-03-18 23:47:22 +01:00
player.swingArm(hand);
return ActionResult.newResult(EnumActionResult.FAIL, stack);
}
}