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

157 lines
5.5 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;
import de.ellpeck.naturesaura.reg.ICreativeItem;
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.IModelProvider;
import de.ellpeck.naturesaura.reg.ModRegistry;
import net.minecraft.block.BlockRailBase;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.DimensionType;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
public class BlockDimensionRail extends BlockRailBase implements IModItem, ICreativeItem, IModelProvider {
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;
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
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
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
public void onMinecartPass(World world, EntityMinecart cart, BlockPos pos) {
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
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-03-21 22:27:51 +01:00
WorldServer 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
public int getMetaFromState(IBlockState state) {
2019-03-21 22:27:51 +01:00
return state.getValue(SHAPE).getMetadata();
2019-03-20 20:51:24 +01:00
}
@Override
public IBlockState 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) {
}
}