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

106 lines
5.2 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;
2019-11-04 19:08:49 +01:00
import net.minecraft.block.BlockState;
2018-11-13 00:36:47 +01:00
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
2020-01-29 01:34:58 +01:00
import net.minecraft.client.renderer.RenderType;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.tileentity.BlockEntity;
import net.minecraft.util.InteractionResult;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Hand;
2018-11-13 00:36:47 +01:00
import net.minecraft.util.math.BlockPos;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.math.BlockRayTraceResult;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.text.TranslationTextComponent;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.Level;
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-04 15:40:09 +01:00
super("field_creator", BlockEntityFieldCreator::new, Properties.create(Material.ROCK).hardnessAndResistance(2F).notSolid().sound(SoundType.STONE));
2018-11-13 00:36:47 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public InteractionResult onBlockActivated(BlockState state, Level levelIn, BlockPos pos, Player player, Hand handIn, BlockRayTraceResult p_225533_6_) {
BlockEntity tile = levelIn.getBlockEntity(pos);
if (tile instanceof BlockEntityFieldCreator) {
if (!levelIn.isClientSide) {
2018-11-13 00:36:47 +01:00
String key = NaturesAura.MOD_ID + ":field_creator_pos";
2021-12-04 15:40:09 +01:00
CompoundTag compound = player.getPersistentData();
2020-09-22 03:17:02 +02:00
if (!player.isSneaking() && compound.contains(key)) {
2018-11-13 00:36:47 +01:00
BlockPos stored = BlockPos.fromLong(compound.getLong(key));
2021-12-04 15:40:09 +01:00
BlockEntityFieldCreator creator = (BlockEntityFieldCreator) tile;
2018-11-13 00:36:47 +01:00
if (!pos.equals(stored)) {
if (creator.isCloseEnough(stored)) {
2021-12-04 15:40:09 +01:00
BlockEntity otherTile = levelIn.getBlockEntity(stored);
if (otherTile instanceof BlockEntityFieldCreator) {
2018-11-13 00:36:47 +01:00
creator.connectionOffset = stored.subtract(pos);
creator.isMain = true;
creator.sendToClients();
2021-12-04 15:40:09 +01:00
BlockEntityFieldCreator otherCreator = (BlockEntityFieldCreator) otherTile;
2018-11-13 00:36:47 +01:00
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.sendStatusMessage(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".connected"), true);
2018-11-13 00:36:47 +01:00
} else
2019-11-04 19:08:49 +01:00
player.sendStatusMessage(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".stored_pos_gone"), true);
2018-11-13 00:36:47 +01:00
} else
2019-11-04 19:08:49 +01:00
player.sendStatusMessage(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".too_far"), true);
2018-11-13 00:36:47 +01:00
} else
2019-11-04 19:08:49 +01:00
player.sendStatusMessage(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".same_position"), true);
2018-11-13 00:36:47 +01:00
} else {
2019-11-04 19:08:49 +01:00
compound.putLong(key, pos.toLong());
player.sendStatusMessage(new TranslationTextComponent("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) {
BlockEntity tile = levelIn.getBlockEntity(pos);
if (tile instanceof BlockEntityFieldCreator) {
BlockEntityFieldCreator creator = (BlockEntityFieldCreator) tile;
2018-11-13 00:36:47 +01:00
if (creator.isCharged) {
BlockPos connected = creator.getConnectedPos();
if (connected != null)
NaturesAuraAPI.instance().spawnParticleStream(
2018-11-13 01:27:47 +01:00
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,
2018-11-13 00:36:47 +01:00
0.65F, 0x4245f4, 1F
);
}
}
}
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() {
2020-09-22 03:17:02 +02:00
return RenderType::getCutoutMipped;
2020-01-29 01:34:58 +01:00
}
2018-11-13 00:36:47 +01:00
}