diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java index 39d6ff9ed..142b526f5 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.items.useables.AllInOneTool; import de.ellpeck.actuallyadditions.common.items.useables.ManualItem; +import de.ellpeck.actuallyadditions.common.items.useables.TeleportStaffItem; import de.ellpeck.actuallyadditions.common.materials.ArmorMaterials; import de.ellpeck.actuallyadditions.common.materials.ToolMaterials; import net.minecraft.item.Item; @@ -95,7 +96,7 @@ public final class ActuallyItems { public static final RegistryObject LENS_OF_DISENCHANTING = ITEMS.register("lens_of_disenchanting", basicItem()); public static final RegistryObject LENS_OF_THE_MINER = ITEMS.register("lens_of_the_miner", basicItem()); public static final RegistryObject LASER_WRENCH = ITEMS.register("laser_wrench", basicItem()); - public static final RegistryObject TELEPORT_STAFF = ITEMS.register("teleport_staff", basicItem()); + public static final RegistryObject TELEPORT_STAFF = ITEMS.register("teleport_staff", TeleportStaffItem::new); public static final RegistryObject WINGS_OF_THE_BATS = ITEMS.register("wings_of_the_bats", basicItem()); public static final RegistryObject SINGLE_BATTERY = ITEMS.register("single_battery", basicItem()); public static final RegistryObject DOUBLE_BATTERY = ITEMS.register("double_battery", basicItem()); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/IUseItem.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/IUseItem.java new file mode 100644 index 000000000..3aca58d9c --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/IUseItem.java @@ -0,0 +1,7 @@ +package de.ellpeck.actuallyadditions.common.items; + +import net.minecraft.item.ItemStack; + +public interface IUseItem { + boolean canUse(ItemStack stack); +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/TeleportStaffItem.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/TeleportStaffItem.java new file mode 100644 index 000000000..882f728ed --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/TeleportStaffItem.java @@ -0,0 +1,79 @@ +package de.ellpeck.actuallyadditions.common.items.useables; + +import de.ellpeck.actuallyadditions.common.items.ActuallyItem; +import de.ellpeck.actuallyadditions.common.items.IUseItem; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.*; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.BlockRayTraceResult; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.util.math.vector.Vector3f; +import net.minecraft.world.World; + +public class TeleportStaffItem extends ActuallyItem implements IUseItem { + public TeleportStaffItem() { + super(baseProps()); + } + + @Override + public ActionResult onItemRightClick(World world, PlayerEntity player, Hand hand) { + ItemStack stack = player.getHeldItem(hand); + if (world.isRemote) { + return super.onItemRightClick(world, player, hand); + } + + + RayTraceResult traceResult = player.pick(100, 1.0f, false); + if (traceResult.getType() != RayTraceResult.Type.BLOCK || !this.canUse(stack)) { + return super.onItemRightClick(world, player, hand); + } + + BlockPos pos = ((BlockRayTraceResult) traceResult).getPos(); + BlockPos toPos = pos.offset(((BlockRayTraceResult) traceResult).getFace(), 1); + + Vector3f centerOfHit = new Vector3f(toPos.getX(), toPos.getY(), toPos.getZ()); + centerOfHit.add(.5f, (((BlockRayTraceResult) traceResult).getFace().getAxis() == Direction.Axis.Y ? .5f : 0), .5f); + + // power cost for thing + // int use = baseUse + (int) (baseUse * pos.hitVec.distanceTo(new Vec3d(player.posX, player.posY + (player.getEyeHeight() - player.getDefaultEyeHeight()), player.posZ))); + + if (!player.isCreative()) { + player.getCooldownTracker().setCooldown(this, 50); + } + + player.dismount(); + player.playSound(SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0f, 1.0f); + ((ServerPlayerEntity) player).connection.setPlayerLocation(centerOfHit.getX(), centerOfHit.getY(), centerOfHit.getZ(), player.rotationYaw, player.rotationPitch); +// +// if (pos != null && (pos.typeOfHit == RayTraceResult.Type.BLOCK || player.rotationPitch >= -5)) { +// int side = pos.sideHit.ordinal(); +// if (side != -1) { +// 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; +// 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) { +// ((EntityPlayerMP) 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); +// if (!player.capabilities.isCreativeMode) { +// this.extractEnergyInternal(stack, use, false); +// player.getCooldownTracker().setCooldown(this, 50); +// } +// return ActionResult.newResult(EnumActionResult.SUCCESS, stack); +// } +// } +// } + + player.swingArm(hand); + return ActionResult.resultSuccess(stack); + } + + @Override + public boolean canUse(ItemStack stack) { + return true; + } +}