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

163 lines
7 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;
2020-01-28 18:08:56 +01:00
import de.ellpeck.naturesaura.blocks.tiles.ModTileEntities;
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;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
2019-02-18 19:30:35 +01:00
import de.ellpeck.naturesaura.items.ModItems;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.ICustomBlockState;
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;
2020-01-28 18:08:56 +01:00
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
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;
2020-01-24 17:05:41 +01:00
import net.minecraft.entity.player.ServerPlayerEntity;
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;
2020-01-28 18:08:56 +01:00
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType;
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;
2020-01-24 17:05:41 +01:00
import net.minecraftforge.fml.network.NetworkHooks;
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;
2020-01-28 18:08:56 +01:00
import java.util.function.Function;
import java.util.function.Supplier;
2019-02-18 12:15:18 +01:00
2020-01-29 01:34:58 +01:00
public class BlockEnderCrate extends BlockContainerImpl implements ITESRProvider<TileEntityEnderCrate>, ICustomBlockState {
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
2020-01-28 18:08:56 +01:00
public ActionResultType 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);
2020-01-24 17:05:41 +01:00
NetworkHooks.openGui((ServerPlayerEntity) player, crate, pos);
2019-02-18 13:00:54 +01:00
}
2019-02-17 22:51:05 +01:00
}
}
2020-01-28 18:08:56 +01:00
return ActionResultType.SUCCESS;
2019-02-17 22:51:05 +01:00
}
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 = (float) pos.getY() + rand.nextFloat();
2019-02-18 12:15:18 +01:00
double d2 = (double) pos.getZ() + 0.5D + 0.25D * (double) k;
double d3 = rand.nextFloat() * (float) j;
2019-02-18 12:15:18 +01:00
double d4 = ((double) rand.nextFloat() - 0.5D) * 0.125D;
double d5 = 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
public Tuple<TileEntityType<TileEntityEnderCrate>, Supplier<Function<? super TileEntityRendererDispatcher, ? extends TileEntityRenderer<? super TileEntityEnderCrate>>>> getTESR() {
return new Tuple<>(ModTileEntities.ENDER_CRATE, () -> RenderEnderCrate::new);
2019-02-18 12:15:18 +01:00
}
2020-01-29 01:34:58 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().cubeBottomTop(this.getBaseName(),
generator.modLoc("block/" + this.getBaseName()),
generator.modLoc("block/" + this.getBaseName() + "_bottom"),
generator.modLoc("block/" + this.getBaseName() + "_top")));
}
2019-02-17 22:51:05 +01:00
}