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

101 lines
4.9 KiB
Java
Raw Normal View History

2018-11-13 00:36:47 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityFieldCreator;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.reg.ICustomBlockState;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.ICustomRenderType;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
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-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-11-13 00:36:47 +01:00
import java.util.Random;
2020-01-29 01:34:58 +01:00
import java.util.function.Supplier;
2018-11-13 00:36:47 +01:00
2020-01-29 01:34:58 +01:00
public class BlockFieldCreator extends BlockContainerImpl implements ICustomBlockState, ICustomRenderType {
2018-11-13 00:36:47 +01:00
public BlockFieldCreator() {
2021-12-19 15:32:45 +01:00
super("field_creator", BlockEntityFieldCreator.class, Properties.of(Material.STONE).strength(2F).noCollission().sound(SoundType.STONE));
2018-11-13 00:36:47 +01:00
}
@Override
public InteractionResult use(BlockState state, Level levelIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult p_225533_6_) {
2021-12-15 16:30:22 +01:00
var tile = levelIn.getBlockEntity(pos);
2021-12-04 15:40:09 +01:00
if (tile instanceof BlockEntityFieldCreator) {
if (!levelIn.isClientSide) {
2021-12-15 16:30:22 +01:00
var key = NaturesAura.MOD_ID + ":field_creator_pos";
var compound = player.getPersistentData();
if (!player.isCrouching() && compound.contains(key)) {
2021-12-15 16:30:22 +01:00
var stored = BlockPos.of(compound.getLong(key));
var creator = (BlockEntityFieldCreator) tile;
2018-11-13 00:36:47 +01:00
if (!pos.equals(stored)) {
if (creator.isCloseEnough(stored)) {
2021-12-15 16:30:22 +01:00
var otherTile = levelIn.getBlockEntity(stored);
if (otherTile instanceof BlockEntityFieldCreator otherCreator) {
2018-11-13 00:36:47 +01:00
creator.connectionOffset = stored.subtract(pos);
creator.isMain = true;
creator.sendToClients();
otherCreator.connectionOffset = pos.subtract(stored);
2019-01-31 18:58:31 +01:00
otherCreator.isMain = false;
2018-11-13 00:36:47 +01:00
otherCreator.sendToClients();
2019-11-04 19:08:49 +01:00
compound.remove(key);
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".connected"), true);
2018-11-13 00:36:47 +01:00
} else
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".stored_pos_gone"), true);
2018-11-13 00:36:47 +01:00
} else
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".too_far"), true);
2018-11-13 00:36:47 +01:00
} else
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".same_position"), true);
2018-11-13 00:36:47 +01:00
} else {
compound.putLong(key, pos.asLong());
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".stored_pos"), true);
2018-11-13 00:36:47 +01:00
}
}
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2018-11-13 00:36:47 +01:00
} else
2021-12-04 15:40:09 +01:00
return InteractionResult.FAIL;
2018-11-13 00:36:47 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2021-12-04 15:40:09 +01:00
public void animateTick(BlockState stateIn, Level levelIn, BlockPos pos, Random rand) {
2021-12-15 16:30:22 +01:00
var tile = levelIn.getBlockEntity(pos);
if (tile instanceof BlockEntityFieldCreator creator && creator.isCharged) {
2021-12-15 16:30:22 +01:00
var connected = creator.getConnectedPos();
if (connected != null)
NaturesAuraAPI.instance().spawnParticleStream(
pos.getX() + 0.25F + rand.nextFloat() * 0.5F,
pos.getY() + 0.25F + rand.nextFloat() * 0.5F,
pos.getZ() + 0.25F + rand.nextFloat() * 0.5F,
connected.getX() + 0.25F + rand.nextFloat() * 0.5F,
connected.getY() + 0.25F + rand.nextFloat() * 0.5F,
connected.getZ() + 0.25F + rand.nextFloat() * 0.5F,
0.65F, 0x4245f4, 1F
);
2018-11-13 00:36:47 +01:00
}
}
2018-11-13 01:27:47 +01:00
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().getExistingFile(generator.modLoc(this.getBaseName())));
}
2020-01-29 01:34:58 +01:00
@Override
public Supplier<RenderType> getRenderType() {
return RenderType::cutoutMipped;
2020-01-29 01:34:58 +01:00
}
2018-11-13 00:36:47 +01:00
}