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

104 lines
5.1 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;
import de.ellpeck.naturesaura.blocks.tiles.TileEntityFieldCreator;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.reg.ICustomBlockState;
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;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
2018-11-13 00:36:47 +01:00
import net.minecraft.tileentity.TileEntity;
2020-01-28 18:08:56 +01:00
import net.minecraft.util.ActionResultType;
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;
2019-11-04 19:08:49 +01:00
import net.minecraft.world.IBlockReader;
2018-11-13 00:36:47 +01:00
import net.minecraft.world.World;
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 00:40:28 +01:00
public class BlockFieldCreator extends BlockContainerImpl implements ICustomBlockState {
2018-11-13 00:36:47 +01:00
public BlockFieldCreator() {
2020-01-22 01:32:26 +01:00
super("field_creator", TileEntityFieldCreator::new, ModBlocks.prop(Material.ROCK).hardnessAndResistance(2F).sound(SoundType.STONE));
2018-11-13 00:36:47 +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 p_225533_6_) {
2018-11-13 00:36:47 +01:00
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityFieldCreator) {
if (!worldIn.isRemote) {
String key = NaturesAura.MOD_ID + ":field_creator_pos";
2019-11-04 19:08:49 +01:00
CompoundNBT compound = player.getPersistentData();
2020-01-28 18:08:56 +01:00
if (!player.isShiftKeyDown() && compound.contains(key)) {
2018-11-13 00:36:47 +01:00
BlockPos stored = BlockPos.fromLong(compound.getLong(key));
TileEntityFieldCreator creator = (TileEntityFieldCreator) tile;
if (!pos.equals(stored)) {
if (creator.isCloseEnough(stored)) {
TileEntity otherTile = worldIn.getTileEntity(stored);
if (otherTile instanceof TileEntityFieldCreator) {
creator.connectionOffset = stored.subtract(pos);
creator.isMain = true;
creator.sendToClients();
TileEntityFieldCreator otherCreator = (TileEntityFieldCreator) otherTile;
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
}
}
2020-01-28 18:08:56 +01:00
return ActionResultType.SUCCESS;
2018-11-13 00:36:47 +01:00
} else
2020-01-28 18:08:56 +01:00
return ActionResultType.FAIL;
2018-11-13 00:36:47 +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) {
2018-11-13 00:36:47 +01:00
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityFieldCreator) {
TileEntityFieldCreator creator = (TileEntityFieldCreator) tile;
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
@Override
2019-11-04 19:08:49 +01:00
public boolean isNormalCube(BlockState state, IBlockReader worldIn, BlockPos pos) {
2018-11-13 01:27:47 +01:00
return false;
}
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().getExistingFile(generator.modLoc(this.getBaseName())));
}
2018-11-13 00:36:47 +01:00
}