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

170 lines
6.2 KiB
Java
Raw Normal View History

2019-03-20 20:51:24 +01:00
package de.ellpeck.naturesaura.blocks;
2019-03-21 22:27:51 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2019-03-20 20:51:24 +01:00
import de.ellpeck.naturesaura.items.ModItems;
import de.ellpeck.naturesaura.packet.PacketClient;
import de.ellpeck.naturesaura.packet.PacketHandler;
2019-03-31 14:17:29 +02:00
import de.ellpeck.naturesaura.packet.PacketParticles;
2019-03-20 20:51:24 +01:00
import de.ellpeck.naturesaura.reg.ICreativeItem;
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.IModelProvider;
import de.ellpeck.naturesaura.reg.ModRegistry;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.AbstractRailBlock;
import net.minecraft.block.SoundType;
2019-03-20 20:51:24 +01:00
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.minecart.AbstractMinecartEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.SoundEvents;
2019-03-20 20:51:24 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
2019-03-31 14:17:29 +02:00
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
2019-03-20 20:51:24 +01:00
import net.minecraft.util.math.BlockPos;
2019-10-20 22:30:49 +02:00
import net.minecraft.world.dimension.DimensionType;
2019-03-20 20:51:24 +01:00
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
2019-10-20 22:30:49 +02:00
import net.minecraft.world.ServerWorld;
2019-03-20 20:51:24 +01:00
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
2019-10-20 22:30:49 +02:00
public class BlockDimensionRail extends AbstractRailBlock implements IModItem, ICreativeItem, IModelProvider {
2019-03-20 20:51:24 +01:00
public static final PropertyEnum<EnumRailDirection> SHAPE = PropertyEnum.create("shape", EnumRailDirection.class, EnumRailDirection.NORTH_SOUTH, EnumRailDirection.EAST_WEST);
2019-03-21 22:27:51 +01:00
private final String name;
private final int goalDim;
private final DimensionType[] canUseDims;
public BlockDimensionRail(String name, DimensionType goalDim, DimensionType... canUseDims) {
2019-03-20 20:51:24 +01:00
super(false);
2019-03-21 22:27:51 +01:00
this.name = name;
this.goalDim = goalDim.getId();
this.canUseDims = canUseDims;
this.setHardness(0.8F);
this.setSoundType(SoundType.METAL);
2019-03-21 22:27:51 +01:00
2019-03-20 20:51:24 +01:00
ModRegistry.add(this);
}
2019-03-21 22:27:51 +01:00
private boolean canUseHere(DimensionType dimension) {
for (DimensionType dim : this.canUseDims)
if (dim == dimension)
return true;
return false;
}
2019-03-20 20:51:24 +01:00
@Override
2019-10-20 22:30:49 +02:00
public boolean onBlockActivated(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn, Hand hand, Direction facing, float hitX, float hitY, float hitZ) {
2019-03-20 20:51:24 +01:00
ItemStack stack = playerIn.getHeldItem(hand);
if (stack.getItem() == ModItems.RANGE_VISUALIZER) {
if (!worldIn.isRemote) {
2019-03-21 22:27:51 +01:00
BlockPos goalPos = this.getGoalCoords(worldIn, pos);
2019-03-20 20:51:24 +01:00
PacketHandler.sendTo(playerIn,
2019-03-21 22:27:51 +01:00
new PacketClient(0, this.goalDim, goalPos.getX(), goalPos.getY(), goalPos.getZ()));
2019-03-20 20:51:24 +01:00
}
return true;
}
return false;
}
@Override
2019-10-20 22:30:49 +02:00
public void onMinecartPass(World world, AbstractMinecartEntity cart, BlockPos pos) {
2019-03-20 20:51:24 +01:00
if (world.isRemote)
return;
if (cart.isBeingRidden())
return;
2019-03-21 22:27:51 +01:00
if (!this.canUseHere(world.provider.getDimensionType()))
2019-03-20 20:51:24 +01:00
return;
2019-03-21 22:27:51 +01:00
2019-03-31 14:17:29 +02:00
AxisAlignedBB box = cart.getEntityBoundingBox();
PacketHandler.sendToAllAround(world, pos, 32,
new PacketParticles((float) box.minX, (float) box.minY, (float) box.minZ, 25,
(int) ((box.maxX - box.minX) * 100F), (int) ((box.maxY - box.minY) * 100F), (int) ((box.maxZ - box.minZ) * 100F)));
world.playSound(null, pos, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.BLOCKS, 1F, 1F);
2019-03-21 22:27:51 +01:00
BlockPos goalCoords = this.getGoalCoords(world, pos);
cart.changeDimension(this.goalDim, (newWorld, entity, yaw) ->
2019-03-20 20:51:24 +01:00
entity.moveToBlockPosAndAngles(goalCoords, yaw, entity.rotationPitch));
2019-03-21 22:27:51 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(world, pos, 35, pos);
IAuraChunk.getAuraChunk(world, spot).drainAura(spot, 50000);
2019-03-20 20:51:24 +01:00
}
2019-03-21 22:27:51 +01:00
private BlockPos getGoalCoords(World world, BlockPos pos) {
2019-03-20 20:51:24 +01:00
MinecraftServer server = world.getMinecraftServer();
2019-03-21 22:27:51 +01:00
if (this == ModBlocks.DIMENSION_RAIL_NETHER) {
2019-03-20 20:51:24 +01:00
// travel to the nether from the overworld
return new BlockPos(pos.getX() / 8, pos.getY() / 2, pos.getZ() / 8);
2019-03-21 22:27:51 +01:00
} else if (this == ModBlocks.DIMENSION_RAIL_END) {
2019-03-20 20:51:24 +01:00
// travel to the end from the overworld
2019-10-20 22:30:49 +02:00
ServerWorld end = server.getWorld(this.goalDim);
2019-03-20 20:51:24 +01:00
return end.getSpawnCoordinate().up(8);
} else {
if (world.provider.getDimensionType() == DimensionType.NETHER) {
// travel to the overworld from the nether
return new BlockPos(pos.getX() * 8, pos.getY() * 2, pos.getZ() * 8);
} else {
// travel to the overworld from the end
2019-03-21 22:27:51 +01:00
World overworld = server.getWorld(this.goalDim);
2019-03-20 20:51:24 +01:00
return overworld.getTopSolidOrLiquidBlock(overworld.getSpawnPoint());
}
}
}
@Override
public IProperty<EnumRailDirection> getShapeProperty() {
return SHAPE;
}
@Override
public boolean isFlexibleRail(IBlockAccess world, BlockPos pos) {
return false;
}
@Override
public boolean canMakeSlopes(IBlockAccess world, BlockPos pos) {
return false;
}
@Override
protected BlockStateContainer createBlockState() {
2019-03-21 22:27:51 +01:00
return new BlockStateContainer(this, SHAPE);
2019-03-20 20:51:24 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public int getMetaFromState(BlockState state) {
2019-03-21 22:27:51 +01:00
return state.getValue(SHAPE).getMetadata();
2019-03-20 20:51:24 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public BlockState getStateFromMeta(int meta) {
2019-03-21 22:27:51 +01:00
return this.getDefaultState().withProperty(SHAPE, EnumRailDirection.byMetadata(meta));
2019-03-20 20:51:24 +01:00
}
@Override
public String getBaseName() {
2019-03-21 22:27:51 +01:00
return "dimension_rail_" + this.name;
2019-03-20 20:51:24 +01:00
}
@Override
public void onPreInit(FMLPreInitializationEvent event) {
}
@Override
public void onInit(FMLInitializationEvent event) {
}
@Override
public void onPostInit(FMLPostInitializationEvent event) {
}
}