NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockEnderCrate.java

150 lines
6 KiB
Java
Raw Normal View History

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-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.block.BlockState;
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;
import net.minecraft.item.Items;
2019-02-18 19:30:35 +01:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
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.Direction;
import net.minecraft.util.Hand;
2019-02-18 12:15:18 +01:00
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Tuple;
2019-02-17 22:51:05 +01:00
import net.minecraft.util.math.BlockPos;
2019-02-18 19:30:35 +01:00
import net.minecraft.util.text.TextFormatting;
2019-02-17 22:51:05 +01:00
import net.minecraft.world.World;
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;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
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() {
super(Material.ROCK, "ender_crate", TileEntityEnderCrate.class, "ender_crate");
this.setSoundType(SoundType.STONE);
this.setHardness(5F);
2019-02-18 12:15:18 +01:00
this.setLightLevel(0.75F);
2019-02-18 19:30:35 +01:00
MinecraftForge.EVENT_BUS.register(this);
}
public static String getEnderName(ItemStack stack) {
if (!stack.hasTagCompound())
return "";
2019-10-20 22:30:49 +02:00
CompoundNBT compound = stack.getTagCompound();
2019-02-18 19:30:35 +01:00
return compound.getString(NaturesAura.MOD_ID + ":ender_name");
}
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2019-02-18 19:30:35 +01:00
public static void addEnderNameInfo(ItemStack stack, List<String> tooltip) {
String name = getEnderName(stack);
if (name != null && !name.isEmpty())
tooltip.add(TextFormatting.DARK_PURPLE + I18n.format("info." + NaturesAura.MOD_ID + ".ender_name",
TextFormatting.ITALIC + name + TextFormatting.RESET));
else
tooltip.add(TextFormatting.DARK_PURPLE + I18n.format("info." + NaturesAura.MOD_ID + ".ender_name.missing"));
}
@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();
if (!output.hasTagCompound())
2019-10-20 22:30:49 +02:00
output.setTagCompound(new CompoundNBT());
2019-02-18 19:30:35 +01:00
output.getTagCompound().setString(NaturesAura.MOD_ID + ":ender_name", name);
event.setOutput(output);
event.setMaterialCost(stack.getCount());
event.setCost(1);
2019-02-17 22:51:05 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public boolean onBlockActivated(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn, Hand hand, Direction facing, float hitX, float hitY, float hitZ) {
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-02-17 22:51:05 +01:00
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-02-18 19:30:35 +01:00
public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
addEnderNameInfo(stack, tooltip);
}
2019-02-18 12:15:18 +01:00
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
public void randomDisplayTick(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);
worldIn.spawnParticle(EnumParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
}
}
@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
}