2019-02-17 22:51:05 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2019-02-18 19:30:35 +01:00
|
|
|
import de.ellpeck.naturesaura.api.misc.IWorldData;
|
2019-02-17 22:51:05 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityEnderCrate;
|
2019-02-18 12:15:18 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.render.RenderEnderCrate;
|
2019-02-18 19:30:35 +01:00
|
|
|
import de.ellpeck.naturesaura.items.ModItems;
|
2019-02-18 12:15:18 +01:00
|
|
|
import de.ellpeck.naturesaura.reg.ITESRProvider;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.AnvilBlock;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2019-02-17 22:51:05 +01:00
|
|
|
import net.minecraft.block.SoundType;
|
|
|
|
import net.minecraft.block.material.Material;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
|
2019-02-18 19:30:35 +01:00
|
|
|
import net.minecraft.client.resources.I18n;
|
|
|
|
import net.minecraft.client.util.ITooltipFlag;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2019-02-18 19:30:35 +01:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.item.Items;
|
|
|
|
import net.minecraft.particles.ParticleTypes;
|
2019-02-17 22:51:05 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Hand;
|
2019-02-18 12:15:18 +01:00
|
|
|
import net.minecraft.util.Tuple;
|
2019-02-17 22:51:05 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.util.math.BlockRayTraceResult;
|
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
2019-02-18 19:30:35 +01:00
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.world.IBlockReader;
|
2019-02-17 22:51:05 +01:00
|
|
|
import net.minecraft.world.World;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2019-02-18 19:30:35 +01:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.AnvilUpdateEvent;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
2019-02-17 22:51:05 +01:00
|
|
|
|
2019-02-18 19:30:35 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.List;
|
2019-02-18 12:15:18 +01:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class BlockEnderCrate extends BlockContainerImpl implements ITESRProvider {
|
2019-02-18 19:30:35 +01:00
|
|
|
|
|
|
|
// This is terrible but I can't see a better solution right now so oh well
|
|
|
|
private static final ThreadLocal<WeakReference<World>> CACHED_WORLD = new ThreadLocal<>();
|
|
|
|
|
2019-02-17 22:51:05 +01:00
|
|
|
public BlockEnderCrate() {
|
2020-01-22 01:32:26 +01:00
|
|
|
super("ender_crate", TileEntityEnderCrate::new, ModBlocks.prop(Material.ROCK).hardnessAndResistance(5F).lightValue(7).sound(SoundType.STONE));
|
2019-02-18 19:30:35 +01:00
|
|
|
|
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getEnderName(ItemStack stack) {
|
2019-11-04 19:08:49 +01:00
|
|
|
if (!stack.hasTag())
|
2019-02-18 19:30:35 +01:00
|
|
|
return "";
|
2019-11-04 19:08:49 +01:00
|
|
|
return stack.getTag().getString(NaturesAura.MOD_ID + ":ender_name");
|
2019-02-18 19:30:35 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-11-04 19:08:49 +01:00
|
|
|
public static void addEnderNameInfo(ItemStack stack, List<ITextComponent> tooltip) {
|
2019-02-18 19:30:35 +01:00
|
|
|
String name = getEnderName(stack);
|
|
|
|
if (name != null && !name.isEmpty())
|
2019-11-04 19:08:49 +01:00
|
|
|
tooltip.add(new StringTextComponent(TextFormatting.DARK_PURPLE + I18n.format("info." + NaturesAura.MOD_ID + ".ender_name",
|
|
|
|
TextFormatting.ITALIC + name + TextFormatting.RESET)));
|
2019-02-18 19:30:35 +01:00
|
|
|
else
|
2019-11-04 19:08:49 +01:00
|
|
|
tooltip.add(new StringTextComponent(TextFormatting.DARK_PURPLE + I18n.format("info." + NaturesAura.MOD_ID + ".ender_name.missing")));
|
2019-02-18 19:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onRightClick(PlayerInteractEvent.RightClickBlock event) {
|
|
|
|
World world = event.getWorld();
|
|
|
|
BlockPos pos = event.getPos();
|
2019-10-20 22:30:49 +02:00
|
|
|
BlockState state = world.getBlockState(pos);
|
|
|
|
if (state.getBlock() instanceof AnvilBlock) {
|
2019-02-18 19:30:35 +01:00
|
|
|
CACHED_WORLD.set(new WeakReference<>(world));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onAnvilUpdate(AnvilUpdateEvent event) {
|
|
|
|
WeakReference<World> world = CACHED_WORLD.get();
|
|
|
|
if (world == null || world.get() == null)
|
|
|
|
return;
|
|
|
|
ItemStack stack = event.getLeft();
|
|
|
|
if (stack.getItem() != Item.getItemFromBlock(this) && stack.getItem() != ModItems.ENDER_ACCESS)
|
|
|
|
return;
|
|
|
|
ItemStack second = event.getRight();
|
|
|
|
if (second.getItem() != Items.ENDER_EYE || second.getCount() < stack.getCount())
|
|
|
|
return;
|
|
|
|
String name = event.getName();
|
|
|
|
if (name == null || name.isEmpty())
|
|
|
|
return;
|
|
|
|
if (IWorldData.getOverworldData(world.get()).isEnderStorageLocked(name))
|
|
|
|
return;
|
|
|
|
ItemStack output = stack.copy();
|
2019-11-04 19:08:49 +01:00
|
|
|
output.getOrCreateTag().putString(NaturesAura.MOD_ID + ":ender_name", name);
|
2019-02-18 19:30:35 +01:00
|
|
|
event.setOutput(output);
|
|
|
|
event.setMaterialCost(stack.getCount());
|
|
|
|
event.setCost(1);
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
|
2019-02-17 22:51:05 +01:00
|
|
|
if (!worldIn.isRemote) {
|
|
|
|
TileEntity tile = worldIn.getTileEntity(pos);
|
|
|
|
if (tile instanceof TileEntityEnderCrate) {
|
|
|
|
TileEntityEnderCrate crate = (TileEntityEnderCrate) tile;
|
2019-02-18 13:00:54 +01:00
|
|
|
if (crate.canOpen()) {
|
2019-02-18 19:30:35 +01:00
|
|
|
crate.drainAura(10000);
|
2019-11-04 19:08:49 +01:00
|
|
|
// TODO playerIn.openGui(NaturesAura.MOD_ID, 0, worldIn, pos.getX(), pos.getY(), pos.getZ());
|
2019-02-18 13:00:54 +01:00
|
|
|
}
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-18 12:15:18 +01:00
|
|
|
|
2019-02-18 19:30:35 +01:00
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-11-04 19:08:49 +01:00
|
|
|
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
|
2019-02-18 19:30:35 +01:00
|
|
|
addEnderNameInfo(stack, tooltip);
|
|
|
|
}
|
|
|
|
|
2019-02-18 12:15:18 +01:00
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-11-04 19:08:49 +01:00
|
|
|
public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
|
2019-02-18 12:15:18 +01:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
int j = rand.nextInt(2) * 2 - 1;
|
|
|
|
int k = rand.nextInt(2) * 2 - 1;
|
|
|
|
double d0 = (double) pos.getX() + 0.5D + 0.25D * (double) j;
|
|
|
|
double d1 = (double) ((float) pos.getY() + rand.nextFloat());
|
|
|
|
double d2 = (double) pos.getZ() + 0.5D + 0.25D * (double) k;
|
|
|
|
double d3 = (double) (rand.nextFloat() * (float) j);
|
|
|
|
double d4 = ((double) rand.nextFloat() - 0.5D) * 0.125D;
|
|
|
|
double d5 = (double) (rand.nextFloat() * (float) k);
|
2019-11-04 19:08:49 +01:00
|
|
|
worldIn.addParticle(ParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
|
2019-02-18 12:15:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
|
|
|
public Tuple<Class, TileEntityRenderer> getTESR() {
|
2019-02-18 12:15:18 +01:00
|
|
|
return new Tuple<>(TileEntityEnderCrate.class, new RenderEnderCrate());
|
|
|
|
}
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|