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

176 lines
7.1 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;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.data.ItemModelGenerator;
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;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.*;
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-29 01:34:58 +01:00
import net.minecraft.client.renderer.RenderType;
2020-01-29 21:36:21 +01:00
import net.minecraft.entity.Entity;
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;
2020-09-22 15:38:58 +02:00
import net.minecraft.nbt.CompoundNBT;
2019-03-20 20:51:24 +01:00
import net.minecraft.server.MinecraftServer;
2019-11-04 19:08:49 +01:00
import net.minecraft.state.EnumProperty;
2020-09-22 03:17:02 +02:00
import net.minecraft.state.Property;
2019-11-04 19:08:49 +01:00
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.state.properties.RailShape;
2020-09-22 03:17:02 +02:00
import net.minecraft.util.*;
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;
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.gen.Heightmap;
import net.minecraft.world.server.ServerWorld;
2020-01-29 21:36:21 +01:00
import net.minecraftforge.common.util.ITeleporter;
2019-03-20 20:51:24 +01:00
2020-01-29 21:36:21 +01:00
import java.util.function.Function;
2020-01-29 01:34:58 +01:00
import java.util.function.Supplier;
2020-01-29 01:34:58 +01:00
public class BlockDimensionRail extends AbstractRailBlock implements IModItem, ICustomRenderType, ICustomBlockState, ICustomItemModel {
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;
2020-09-22 03:17:02 +02:00
private final RegistryKey<World> goalDim;
private final RegistryKey<World>[] canUseDims;
2019-03-21 22:27:51 +01:00
2020-09-22 03:17:02 +02:00
public BlockDimensionRail(String name, RegistryKey<World> goalDim, RegistryKey<World>... 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;
2020-09-22 03:17:02 +02:00
this.goalDim = goalDim;
2019-03-21 22:27:51 +01:00
this.canUseDims = canUseDims;
2019-03-20 20:51:24 +01:00
ModRegistry.add(this);
}
2020-09-22 03:17:02 +02:00
private boolean canUseHere(RegistryKey<World> dimension) {
for (RegistryKey<World> dim : this.canUseDims)
2019-03-21 22:27:51 +01:00
if (dim == dimension)
return true;
return false;
}
2019-03-20 20:51:24 +01:00
@Override
2020-01-28 18:08:56 +01:00
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
2019-11-04 19:08:49 +01:00
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-09-22 15:38:58 +02:00
CompoundNBT data = new CompoundNBT();
data.putString("dim", this.goalDim.func_240901_a_().toString());
data.putLong("pos", goalPos.toLong());
PacketHandler.sendTo(player, new PacketClient(0, data));
2019-03-20 20:51:24 +01:00
}
2020-01-28 18:08:56 +01:00
return ActionResultType.SUCCESS;
2019-03-20 20:51:24 +01:00
}
2020-01-28 18:08:56 +01:00
return ActionResultType.FAIL;
2019-03-20 20:51:24 +01:00
}
@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;
2020-09-22 03:17:02 +02:00
if (!this.canUseHere(world.func_234923_W_()))
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);
2020-09-22 03:17:02 +02:00
cart.changeDimension(world.getServer().getWorld(this.goalDim), new ITeleporter() {
2020-01-29 21:36:21 +01:00
@Override
public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destWorld, float yaw, Function<Boolean, Entity> repositionEntity) {
2020-09-22 15:38:58 +02:00
// repositionEntity always causes a NPE because why wouldn't it, so this is a fixed copy
entity.world.getProfiler().endStartSection("reloading");
Entity result = entity.getType().create(destWorld);
if (result != null) {
result.copyDataFromOld(entity);
destWorld.addFromAnotherDimension(result);
result.moveToBlockPosAndAngles(goalCoords, yaw, result.rotationPitch);
}
2020-01-29 21:36:21 +01:00
return result;
}
});
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-11-04 19:08:49 +01:00
MinecraftServer server = world.getServer();
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
2020-09-22 03:17:02 +02:00
ServerWorld end = server.getWorld(this.goalDim);
return end.func_241135_u_().up(8);
2019-03-20 20:51:24 +01:00
} else {
2020-09-22 03:17:02 +02:00
if (world.func_234923_W_() == World.field_234919_h_) {
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
2020-09-22 03:17:02 +02:00
ServerWorld overworld = server.getWorld(this.goalDim);
BlockPos spawn = overworld.func_241135_u_();
2019-11-04 19:08:49 +01:00
BlockPos ret = new BlockPos(spawn.getX(), 0, spawn.getZ());
return ret.up(overworld.getHeight(Heightmap.Type.WORLD_SURFACE, spawn.getX(), spawn.getZ()));
2019-03-20 20:51:24 +01:00
}
}
}
@Override
2020-09-22 03:17:02 +02:00
public Property<RailShape> getShapeProperty() {
2019-03-20 20:51:24 +01:00
return SHAPE;
}
2020-01-29 01:34:58 +01:00
@Override
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
}
2020-01-29 01:34:58 +01:00
@Override
public Supplier<RenderType> getRenderType() {
2020-09-22 03:17:02 +02:00
return RenderType::getCutoutMipped;
2020-01-29 01:34:58 +01:00
}
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
// noop
}
@Override
public void generateCustomItemModel(ItemModelGenerator generator) {
generator.withExistingParent(this.getBaseName(), "item/generated").texture("layer0", "block/" + this.getBaseName());
}
2019-03-20 20:51:24 +01:00
}