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;
|
|
|
|
import net.minecraft.block.BlockRailBase;
|
2019-03-31 12:28:59 +02:00
|
|
|
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;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.item.EntityMinecart;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2019-03-31 14:17:29 +02:00
|
|
|
import net.minecraft.init.SoundEvents;
|
2019-03-20 20:51:24 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumHand;
|
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;
|
|
|
|
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-31 12:28:59 +02:00
|
|
|
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
|
|
|
|
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
|
|
|
|
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-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) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|