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

137 lines
5.8 KiB
Java
Raw Normal View History

2019-02-17 22:51:05 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.NaturesAura;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityEnderCrate;
2021-12-19 15:32:45 +01:00
import de.ellpeck.naturesaura.blocks.tiles.ModBlockEntities;
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;
2021-12-06 14:38:12 +01:00
import net.minecraft.ChatFormatting;
2021-12-15 16:24:53 +01:00
import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
2021-12-06 14:38:12 +01:00
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
2022-06-27 15:24:04 +02:00
import net.minecraft.util.RandomSource;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
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;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
2021-12-06 14:38:12 +01:00
import net.minecraftforge.network.NetworkHooks;
2019-02-17 22:51:05 +01:00
2019-02-18 19:30:35 +01:00
import javax.annotation.Nullable;
import java.util.List;
2019-02-18 12:15:18 +01:00
2021-12-04 15:40:09 +01:00
public class BlockEnderCrate extends BlockContainerImpl implements ITESRProvider<BlockEntityEnderCrate>, ICustomBlockState {
2019-02-18 19:30:35 +01:00
2019-02-17 22:51:05 +01:00
public BlockEnderCrate() {
2021-12-19 15:32:45 +01:00
super("ender_crate", BlockEntityEnderCrate.class, Properties.of(Material.STONE).strength(5F).lightLevel(s -> 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)
2021-12-06 14:38:12 +01:00
public static void addEnderNameInfo(ItemStack stack, List<Component> tooltip) {
2022-06-27 15:24:04 +02:00
var name = BlockEnderCrate.getEnderName(stack);
2021-12-06 14:38:12 +01:00
if (name != null && !name.isEmpty()) {
2022-06-27 15:24:04 +02:00
tooltip.add(Component.literal(ChatFormatting.DARK_PURPLE + I18n.get("info." + NaturesAura.MOD_ID + ".ender_name", ChatFormatting.ITALIC + name + ChatFormatting.RESET)));
2021-12-06 14:38:12 +01:00
} else {
2022-06-27 15:24:04 +02:00
tooltip.add(Component.literal(ChatFormatting.DARK_PURPLE + I18n.get("info." + NaturesAura.MOD_ID + ".ender_name.missing")));
2019-02-18 19:30:35 +01:00
}
}
@SubscribeEvent
public void onAnvilUpdate(AnvilUpdateEvent event) {
2021-12-06 14:38:12 +01:00
var player = event.getPlayer();
if (player == null)
2019-02-18 19:30:35 +01:00
return;
2021-12-15 16:30:22 +01:00
var stack = event.getLeft();
2021-12-06 14:38:12 +01:00
if (stack.getItem() != this.asItem() && stack.getItem() != ModItems.ENDER_ACCESS)
2019-02-18 19:30:35 +01:00
return;
2021-12-15 16:30:22 +01:00
var second = event.getRight();
2019-02-18 19:30:35 +01:00
if (second.getItem() != Items.ENDER_EYE || second.getCount() < stack.getCount())
return;
2021-12-15 16:30:22 +01:00
var name = event.getName();
2019-02-18 19:30:35 +01:00
if (name == null || name.isEmpty())
return;
2021-12-06 14:38:12 +01:00
if (ILevelData.getOverworldData(player.level).isEnderStorageLocked(name))
2019-02-18 19:30:35 +01:00
return;
2021-12-15 16:30:22 +01:00
var 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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
2021-12-06 14:38:12 +01:00
public InteractionResult use(BlockState state, Level levelIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
2021-12-04 15:40:09 +01:00
if (!levelIn.isClientSide) {
2021-12-15 16:30:22 +01:00
var tile = levelIn.getBlockEntity(pos);
2023-02-16 19:03:26 +01:00
if (tile instanceof BlockEntityEnderCrate crate && crate.canOpen() && crate.canUseRightNow(2500)) {
2021-12-06 14:38:12 +01:00
crate.drainAura(2500);
NetworkHooks.openScreen((ServerPlayer) player, crate, pos);
2019-02-17 22:51:05 +01:00
}
}
2021-12-04 15:40:09 +01:00
return InteractionResult.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)
2021-12-06 14:38:12 +01:00
public void appendHoverText(ItemStack stack, @Nullable BlockGetter levelIn, List<Component> tooltip, TooltipFlag flagIn) {
2022-06-27 15:24:04 +02:00
BlockEnderCrate.addEnderNameInfo(stack, tooltip);
2019-02-18 19:30:35 +01:00
}
2019-02-18 12:15:18 +01:00
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2022-06-27 15:24:04 +02:00
public void animateTick(BlockState stateIn, Level levelIn, BlockPos pos, RandomSource rand) {
2021-12-15 16:30:22 +01:00
for (var i = 0; i < 3; ++i) {
var j = rand.nextInt(2) * 2 - 1;
var k = rand.nextInt(2) * 2 - 1;
var d0 = (double) pos.getX() + 0.5D + 0.25D * (double) j;
double d1 = (float) pos.getY() + rand.nextFloat();
2021-12-15 16:30:22 +01:00
var d2 = (double) pos.getZ() + 0.5D + 0.25D * (double) k;
double d3 = rand.nextFloat() * (float) j;
2021-12-15 16:30:22 +01:00
var d4 = ((double) rand.nextFloat() - 0.5D) * 0.125D;
double d5 = rand.nextFloat() * (float) k;
2021-12-04 15:40:09 +01:00
levelIn.addParticle(ParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
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")));
}
2021-12-15 16:24:53 +01:00
@Override
public void registerTESR() {
2021-12-19 15:32:45 +01:00
BlockEntityRenderers.register(ModBlockEntities.ENDER_CRATE, RenderEnderCrate::new);
2021-12-15 16:24:53 +01:00
}
2019-02-17 22:51:05 +01:00
}