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;
|
2020-01-22 23:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketClient;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2019-03-20 20:51:24 +01:00
|
|
|
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;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.block.Block;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.BlockState;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.block.Blocks;
|
2020-01-24 22:45:11 +01:00
|
|
|
import net.minecraft.client.util.ITooltipFlag;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.entity.item.minecart.AbstractMinecartEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2019-03-20 20:51:24 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.state.EnumProperty;
|
|
|
|
import net.minecraft.state.IProperty;
|
|
|
|
import net.minecraft.state.StateContainer;
|
|
|
|
import net.minecraft.state.properties.BlockStateProperties;
|
|
|
|
import net.minecraft.state.properties.RailShape;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Hand;
|
2019-03-31 14:17:29 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.util.SoundEvents;
|
2019-03-31 14:17:29 +02:00
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
2019-03-20 20:51:24 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.util.math.BlockRayTraceResult;
|
2020-01-24 22:45:11 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
|
|
|
import net.minecraft.util.text.Style;
|
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.world.IBlockReader;
|
2019-03-20 20:51:24 +01:00
|
|
|
import net.minecraft.world.World;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.world.dimension.DimensionType;
|
|
|
|
import net.minecraft.world.gen.Heightmap;
|
|
|
|
import net.minecraft.world.server.ServerWorld;
|
2019-03-20 20:51:24 +01:00
|
|
|
|
2020-01-24 22:45:11 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
public class BlockDimensionRail extends AbstractRailBlock implements IModItem, IModelProvider {
|
2019-03-20 20:51:24 +01:00
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
public static final EnumProperty<RailShape> SHAPE = BlockStateProperties.RAIL_SHAPE;
|
2019-03-20 20:51:24 +01:00
|
|
|
|
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-11-04 19:08:49 +01:00
|
|
|
super(false, ModBlocks.prop(Blocks.RAIL));
|
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
|
2019-11-04 19:08:49 +01:00
|
|
|
public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
|
|
|
|
ItemStack stack = player.getHeldItem(hand);
|
2019-03-20 20:51:24 +01:00
|
|
|
if (stack.getItem() == ModItems.RANGE_VISUALIZER) {
|
|
|
|
if (!worldIn.isRemote) {
|
2019-03-21 22:27:51 +01:00
|
|
|
BlockPos goalPos = this.getGoalCoords(worldIn, pos);
|
2020-01-22 23:21:52 +01:00
|
|
|
PacketHandler.sendTo(player, 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-11-04 19:08:49 +01:00
|
|
|
public void onMinecartPass(BlockState state, World world, BlockPos pos, AbstractMinecartEntity cart) {
|
2019-03-20 20:51:24 +01:00
|
|
|
if (world.isRemote)
|
|
|
|
return;
|
|
|
|
if (cart.isBeingRidden())
|
|
|
|
return;
|
2019-11-04 19:08:49 +01:00
|
|
|
if (!this.canUseHere(world.getDimension().getType()))
|
2019-03-20 20:51:24 +01:00
|
|
|
return;
|
2019-03-21 22:27:51 +01:00
|
|
|
|
2020-01-23 19:20:47 +01:00
|
|
|
AxisAlignedBB box = cart.getBoundingBox();
|
2020-01-26 00:16:06 +01:00
|
|
|
PacketHandler.sendToAllAround(world, pos, 32, new PacketParticles((float) box.minX, (float) box.minY, (float) box.minZ, PacketParticles.Type.DIMENSION_RAIL, (int) ((box.maxX - box.minX) * 100F), (int) ((box.maxY - box.minY) * 100F), (int) ((box.maxZ - box.minZ) * 100F)));
|
2019-11-04 19:08:49 +01:00
|
|
|
world.playSound(null, pos, SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.BLOCKS, 1F, 1F);
|
2019-03-31 14:17:29 +02:00
|
|
|
|
2019-03-21 22:27:51 +01:00
|
|
|
BlockPos goalCoords = this.getGoalCoords(world, pos);
|
2019-11-04 19:08:49 +01:00
|
|
|
|
2020-01-24 22:45:11 +01:00
|
|
|
// TODO wait for Forge to have re-implemented ITeleporter
|
|
|
|
if (true)
|
|
|
|
return;
|
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
cart.changeDimension(DimensionType.getById(this.goalDim));
|
2020-01-23 19:20:47 +01:00
|
|
|
cart.moveToBlockPosAndAngles(goalCoords, cart.rotationYaw, cart.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
|
|
|
}
|
|
|
|
|
2020-01-24 22:45:11 +01:00
|
|
|
@Override
|
|
|
|
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
|
|
|
|
super.addInformation(stack, worldIn, tooltip, flagIn);
|
|
|
|
tooltip.add(new StringTextComponent("CURRENTLY UNIMPLEMENTED - Waiting for a Forge change").setStyle(new Style().setColor(TextFormatting.RED)));
|
|
|
|
}
|
|
|
|
|
2019-03-21 22:27:51 +01:00
|
|
|
private BlockPos getGoalCoords(World world, BlockPos pos) {
|
2019-11-04 19:08:49 +01:00
|
|
|
MinecraftServer server = world.getServer();
|
|
|
|
DimensionType goalDimType = DimensionType.getById(this.goalDim);
|
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-11-04 19:08:49 +01:00
|
|
|
ServerWorld end = server.getWorld(goalDimType);
|
2019-03-20 20:51:24 +01:00
|
|
|
return end.getSpawnCoordinate().up(8);
|
|
|
|
} else {
|
2019-11-04 19:08:49 +01:00
|
|
|
if (world.getDimension().getType() == DimensionType.THE_NETHER) {
|
2019-03-20 20:51:24 +01:00
|
|
|
// 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-11-04 19:08:49 +01:00
|
|
|
World overworld = server.getWorld(goalDimType);
|
|
|
|
BlockPos spawn = overworld.getSpawnPoint();
|
|
|
|
BlockPos ret = new BlockPos(spawn.getX(), 0, spawn.getZ());
|
2020-01-30 13:45:19 +01:00
|
|
|
return ret.up(overworld.getHeight(Heightmap.Type.WORLD_SURFACE, spawn.getX(), spawn.getZ()));
|
2019-03-20 20:51:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public IProperty<RailShape> getShapeProperty() {
|
2019-03-20 20:51:24 +01:00
|
|
|
return SHAPE;
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
public boolean isFlexibleRail(BlockState state, IBlockReader world, BlockPos pos) {
|
2019-03-20 20:51:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public boolean canMakeSlopes(BlockState state, IBlockReader world, BlockPos pos) {
|
|
|
|
return false;
|
2019-03-20 20:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
|
|
|
|
builder.add(SHAPE);
|
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
|
|
|
}
|
|
|
|
}
|