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

128 lines
5.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 ("ItemPhantomConnector.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;
2015-05-20 22:39:43 +02:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.tile.IPhantomTile;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
2024-03-02 21:23:08 +01:00
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
2024-03-03 01:20:53 +01:00
import net.minecraft.core.registries.Registries;
2024-03-02 21:23:08 +01:00
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
2015-05-20 22:39:43 +02:00
2021-02-26 22:15:48 +01:00
import javax.annotation.Nullable;
import java.util.List;
2019-05-02 09:10:29 +02:00
public class ItemPhantomConnector extends ItemBase {
2015-05-20 22:39:43 +02:00
public ItemPhantomConnector() {
super(ActuallyItems.defaultNonStacking());
2015-05-20 22:39:43 +02:00
}
2024-03-02 21:23:08 +01:00
public static ResourceKey<Level> getStoredWorld(ItemStack stack) {
CompoundTag tag = stack.getOrCreateTag();
if (!tag.contains("WorldOfTileStored")) {
return null;
2021-02-26 22:15:48 +01:00
}
2024-03-03 01:20:53 +01:00
return ResourceKey.create(Registries.DIMENSION, new ResourceLocation(tag.getString("WorldOfTileStored")));
2016-02-01 20:39:11 +01:00
}
2019-05-02 09:10:29 +02:00
public static BlockPos getStoredPosition(ItemStack stack) {
2024-03-02 21:23:08 +01:00
CompoundTag tag = stack.getOrCreateTag();
int x = tag.getInt("XCoordOfTileStored");
int y = tag.getInt("YCoordOfTileStored");
int z = tag.getInt("ZCoordOfTileStored");
if (!(x == 0 && y == 0 && z == 0)) {
return new BlockPos(x, y, z);
2016-02-01 20:39:11 +01:00
}
2016-02-01 20:39:11 +01:00
return null;
}
2019-05-02 09:10:29 +02:00
public static void clearStorage(ItemStack stack, String... keys) {
2024-03-02 21:23:08 +01:00
CompoundTag compound = stack.getOrCreateTag();
for (String key : keys) {
compound.remove(key);
}
2016-02-01 20:39:11 +01:00
}
2024-03-02 21:23:08 +01:00
public static void storeConnection(ItemStack stack, int x, int y, int z, Level world) {
CompoundTag tag = stack.getOrCreateTag();
2016-02-01 20:39:11 +01:00
tag.putInt("XCoordOfTileStored", x);
tag.putInt("YCoordOfTileStored", y);
tag.putInt("ZCoordOfTileStored", z);
tag.putString("WorldOfTileStored", world.dimension().location().toString());
2016-02-01 20:39:11 +01:00
}
2015-05-20 22:39:43 +02:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult useOn(UseOnContext context) {
ItemStack stack = context.getPlayer().getItemInHand(context.getHand());
if (!context.getLevel().isClientSide) {
2015-05-25 17:00:54 +02:00
//Passing Data to Phantoms
BlockPos pos = context.getClickedPos();
2024-03-02 21:23:08 +01:00
BlockEntity tile = context.getLevel().getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile != null) {
2015-08-22 19:47:20 +02:00
//Passing to Phantom
2019-05-02 09:10:29 +02:00
if (tile instanceof IPhantomTile) {
BlockPos stored = getStoredPosition(stack);
if (stored != null && getStoredWorld(stack) == context.getLevel().dimension()) {
2019-05-02 09:10:29 +02:00
((IPhantomTile) tile).setBoundPosition(stored);
if (tile instanceof TileEntityBase) {
((TileEntityBase) tile).sendUpdate();
}
clearStorage(stack, "XCoordOfTileStored", "YCoordOfTileStored", "ZCoordOfTileStored", "WorldOfTileStored");
2024-03-03 01:20:53 +01:00
context.getPlayer().displayClientMessage(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".phantom.connected.desc"), true);
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2015-05-25 17:00:54 +02:00
}
2024-03-02 21:23:08 +01:00
return InteractionResult.FAIL;
2015-05-20 22:39:43 +02:00
}
}
2015-05-25 17:00:54 +02:00
//Storing Connections
storeConnection(stack, pos.getX(), pos.getY(), pos.getZ(), context.getLevel());
2024-03-03 01:20:53 +01:00
context.getPlayer().displayClientMessage(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".phantom.stored.desc"), true);
2015-05-25 17:00:54 +02:00
}
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2015-05-25 17:00:54 +02:00
}
2015-05-20 22:39:43 +02:00
2024-03-04 20:21:48 +01:00
// @Nullable
// @Override
// public CompoundTag getShareTag(ItemStack stack) {
// return new CompoundTag();
// }
2015-05-20 22:39:43 +02:00
2021-12-30 18:30:01 +01:00
@OnlyIn(Dist.CLIENT)
2015-10-23 16:54:33 +02:00
@Override
2024-03-02 21:23:08 +01:00
public void appendHoverText(ItemStack stack, @Nullable Level playerIn, List<Component> list, TooltipFlag advanced) {
2016-01-08 13:31:58 +01:00
BlockPos coords = getStoredPosition(stack);
2019-05-02 09:10:29 +02:00
if (coords != null) {
2024-03-03 01:20:53 +01:00
list.add(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".boundTo.desc").append(":"));
list.add(Component.literal("X: " + coords.getX()));
list.add(Component.literal("Y: " + coords.getY()));
list.add(Component.literal("Z: " + coords.getZ()));
list.add(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".clearStorage.desc").withStyle(ChatFormatting.ITALIC));
2015-10-23 16:54:33 +02:00
}
}
2015-05-20 22:39:43 +02:00
}