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;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-15 14:26:42 +01:00
|
|
|
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.entity.BlockEntity;
|
|
|
|
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 {
|
2021-12-15 14:26:42 +01:00
|
|
|
|
2018-11-13 00:36:47 +01:00
|
|
|
public BlockFieldCreator() {
|
2021-12-15 14:26:42 +01:00
|
|
|
super("field_creator", BlockEntityFieldCreator::new, Properties.of(Material.STONE).strength(2F).noCollission().sound(SoundType.STONE));
|
2018-11-13 00:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public InteractionResult use(BlockState state, Level levelIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult p_225533_6_) {
|
2021-12-04 15:40:09 +01:00
|
|
|
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();
|
2021-12-15 14:26:42 +01:00
|
|
|
if (!player.isCrouching() && compound.contains(key)) {
|
|
|
|
BlockPos stored = BlockPos.of(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);
|
2021-12-15 14:26:42 +01:00
|
|
|
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);
|
2021-12-15 14:26:42 +01:00
|
|
|
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".connected"), true);
|
2018-11-13 00:36:47 +01:00
|
|
|
} else
|
2021-12-15 14:26:42 +01:00
|
|
|
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".stored_pos_gone"), true);
|
2018-11-13 00:36:47 +01:00
|
|
|
} else
|
2021-12-15 14:26:42 +01:00
|
|
|
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".too_far"), true);
|
2018-11-13 00:36:47 +01:00
|
|
|
} else
|
2021-12-15 14:26:42 +01:00
|
|
|
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".same_position"), true);
|
2018-11-13 00:36:47 +01:00
|
|
|
} else {
|
2021-12-15 14:26:42 +01:00
|
|
|
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) {
|
|
|
|
BlockEntity tile = levelIn.getBlockEntity(pos);
|
2021-12-15 14:26:42 +01:00
|
|
|
if (tile instanceof BlockEntityFieldCreator creator && creator.isCharged) {
|
|
|
|
BlockPos 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() {
|
2021-12-15 14:26:42 +01:00
|
|
|
return RenderType::cutoutMipped;
|
2020-01-29 01:34:58 +01:00
|
|
|
}
|
2018-11-13 00:36:47 +01:00
|
|
|
}
|