package it.hurts.sskirillss.relics.items;
import it.hurts.sskirillss.relics.init.ItemRegistry;
import it.hurts.sskirillss.relics.utils.EntityUtils;
import it.hurts.sskirillss.relics.utils.NBTUtils;
import it.hurts.sskirillss.relics.utils.RelicsConfig;
import it.hurts.sskirillss.relics.utils.RelicsTab;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import javax.annotation.Nullable;
import java.util.List;
public class SpatialSignItem extends Item {
public static final String TAG_POSITION = "position";
public static final String TAG_TIME = "time";
public SpatialSignItem() {
super(new Item.Properties()
.group(RelicsTab.RELICS_TAB));
}
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
ItemStack itemStack = playerIn.getHeldItem(handIn);
if (NBTUtils.getString(itemStack, TAG_POSITION, "").equals("")) {
if (!playerIn.isSneaking()) {
NBTUtils.setString(itemStack, TAG_POSITION, Math.round(playerIn.getPosX()) + "," + Math.round(playerIn.getPosY()) + "," + Math.round(playerIn.getPosZ()));
NBTUtils.setInt(itemStack, TAG_TIME, RelicsConfig.SpatialSign.timeBeforeActivation * 20);
}
} else {
if (playerIn.isSneaking()) {
activateSign(itemStack, playerIn, false);
itemStack.damageItem(1, playerIn, (player) -> playerIn.sendBreakAnimation(handIn));
}
}
return super.onItemRightClick(worldIn, playerIn, handIn);
}
@Override
public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
PlayerEntity player = (PlayerEntity) entityIn;
if (!NBTUtils.getString(stack, TAG_POSITION, "").equals("") && player.ticksExisted % 20 == 0) {
int time = NBTUtils.getInt(stack, TAG_TIME, -1);
if (time > 0) {
NBTUtils.setInt(stack, TAG_TIME, time - 1);
} else {
stack.damageItem(1, player, (playerIn) -> player.sendBreakAnimation(player.getActiveHand()));
NBTUtils.setInt(stack, TAG_TIME, RelicsConfig.SpatialSign.timeBeforeActivation * 20);
NBTUtils.setString(stack, TAG_POSITION, "");
}
}
super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
}
@Override
public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
if (!NBTUtils.getString(stack, TAG_POSITION, "").equals("")) {
String[] position = NBTUtils.getString(stack, TAG_POSITION, "").split(",");
tooltip.add(new StringTextComponent("Hold shift and right-click to initiate teleporting"));
tooltip.add(new StringTextComponent(""));
tooltip.add(new StringTextComponent("Stored destination:"));
tooltip.add(new StringTextComponent("X: " + position[0] + " Y: " + position[1] + " Z: " + position[2]));
tooltip.add(new StringTextComponent(""));
tooltip.add(new StringTextComponent("Time before returning:"));
tooltip.add(new StringTextComponent(String.valueOf(NBTUtils.getInt(stack, TAG_TIME, 0))));
} else {
tooltip.add(new StringTextComponent("Right-click to save current position"));
}
}
@Override
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
return slotChanged;
}
@Override
public boolean hasEffect(ItemStack stack) {
return !NBTUtils.getString(stack, TAG_POSITION, "").equals("");
}
public static void activateSign(ItemStack itemStack, PlayerEntity player, boolean shouldBreak) {
if (!NBTUtils.getString(itemStack, TAG_POSITION, "").equals("")) {
String[] position = NBTUtils.getString(itemStack, TAG_POSITION, "").split(",");
player.setPositionAndUpdate(Double.parseDouble(position[0]), Double.parseDouble(position[1]), Double.parseDouble(position[2]));
player.setMotion(0, 0, 0);
player.setHealth(1.0F);
player.getFoodStats().setFoodLevel(1);
NBTUtils.setString(itemStack, TAG_POSITION, "");
if (player.isOnePlayerRiding()) {
player.dismount();
player.getRidingEntity().setPositionAndUpdate(Double.parseDouble(position[0]), Double.parseDouble(position[1]), Double.parseDouble(position[2]));
player.startRiding(player.getRidingEntity());
}
if (shouldBreak) itemStack.shrink(1);
}
}
@Mod.EventBusSubscriber
public static class SpatialSignServerEvents {
@SubscribeEvent
public static void onEntityHurt(LivingHurtEvent event) {
if (event.getEntity() instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) event.getEntity();
if (player.getHealth() < event.getAmount() && EntityUtils.getSlotWithItem(player, ItemRegistry.SPATIAL_SIGN.get()) != -1) {
activateSign(player.inventory.getStackInSlot(EntityUtils.getSlotWithItem(player, ItemRegistry.SPATIAL_SIGN.get())), player, true);
event.setCanceled(true);
}
} else if (event.getEntity() instanceof LivingEntity && event.getEntity().isBeingRidden()) {
LivingEntity mount = (LivingEntity) event.getEntity();
if (mount.getRidingEntity() instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) mount.getRidingEntity();
if (EntityUtils.getSlotWithItem(player, ItemRegistry.SPATIAL_SIGN.get()) != -1) {
ItemStack itemStack = player.inventory.getStackInSlot(EntityUtils.getSlotWithItem(player, ItemRegistry.SPATIAL_SIGN.get()));
String[] position = NBTUtils.getString(itemStack, TAG_POSITION, "").split(",");
itemStack.damageItem(1, player, (playerIn) -> player.sendBreakAnimation(player.getActiveHand()));
player.dismount();
mount.setPositionAndUpdate(Double.parseDouble(position[0]), Double.parseDouble(position[1]), Double.parseDouble(position[2]));
mount.setMotion(0, 0, 0);
}
}
}
}
}
}