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

175 lines
7.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;
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;
2023-02-15 23:45:50 +01:00
import de.ellpeck.naturesaura.reg.ICustomBlockState;
import de.ellpeck.naturesaura.reg.ICustomItemModel;
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.ModRegistry;
2021-12-05 23:32:31 +01:00
import net.minecraft.core.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-05 23:32:31 +01:00
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseRailBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.properties.RailShape;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.phys.BlockHitResult;
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;
public class BlockDimensionRail extends BaseRailBlock implements IModItem, 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;
2021-12-05 23:32:31 +01:00
private final ResourceKey<Level> goalDim;
private final ResourceKey<Level>[] canUseDims;
2019-03-21 22:27:51 +01:00
2021-12-05 23:32:31 +01:00
@SafeVarargs
public BlockDimensionRail(String name, ResourceKey<Level> goalDim, ResourceKey<Level>... canUseDims) {
super(false, Properties.copy(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;
2022-06-27 15:24:04 +02:00
ModRegistry.ALL_ITEMS.add(this);
2019-03-20 20:51:24 +01:00
}
2021-12-05 23:32:31 +01:00
private boolean canUseHere(ResourceKey<Level> dimension) {
for (var 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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
2021-12-05 23:32:31 +01:00
public InteractionResult use(BlockState state, Level levelIn, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
var stack = player.getItemInHand(hand);
2019-03-20 20:51:24 +01:00
if (stack.getItem() == ModItems.RANGE_VISUALIZER) {
2021-12-04 15:40:09 +01:00
if (!levelIn.isClientSide) {
2021-12-05 23:32:31 +01:00
var goalPos = this.getGoalCoords(levelIn, pos);
var data = new CompoundTag();
data.putString("dim", this.goalDim.location().toString());
data.putLong("pos", goalPos.asLong());
2020-09-22 15:38:58 +02:00
PacketHandler.sendTo(player, new PacketClient(0, data));
2019-03-20 20:51:24 +01:00
}
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2019-03-20 20:51:24 +01:00
}
2021-12-04 15:40:09 +01:00
return InteractionResult.FAIL;
2019-03-20 20:51:24 +01:00
}
@Override
2021-12-05 23:32:31 +01:00
public void onMinecartPass(BlockState state, Level level, BlockPos pos, AbstractMinecart cart) {
2021-12-04 15:40:09 +01:00
if (level.isClientSide)
2019-03-20 20:51:24 +01:00
return;
2021-12-05 23:32:31 +01:00
if (!cart.getPassengers().isEmpty())
2019-03-20 20:51:24 +01:00
return;
2021-12-05 23:32:31 +01:00
if (!this.canUseHere(level.dimension()))
2019-03-20 20:51:24 +01:00
return;
2019-03-21 22:27:51 +01:00
2021-12-05 23:32:31 +01:00
var box = cart.getBoundingBox();
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(level, 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)));
2021-12-05 23:32:31 +01:00
level.playSound(null, pos, SoundEvents.ENDERMAN_TELEPORT, SoundSource.BLOCKS, 1F, 1F);
2019-03-31 14:17:29 +02:00
2021-12-05 23:32:31 +01:00
var goalCoords = this.getGoalCoords(level, pos);
2021-12-04 15:40:09 +01:00
cart.changeDimension(level.getServer().getLevel(this.goalDim), new ITeleporter() {
2020-01-29 21:36:21 +01:00
@Override
2021-12-04 15:40:09 +01:00
public Entity placeEntity(Entity entity, ServerLevel currentLevel, ServerLevel destLevel, 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
2023-07-08 12:32:27 +02:00
entity.level().getProfiler().popPush("reloading");
2021-12-05 23:32:31 +01:00
var result = entity.getType().create(destLevel);
2020-09-22 15:38:58 +02:00
if (result != null) {
2021-12-05 23:32:31 +01:00
result.restoreFrom(entity);
destLevel.addDuringTeleport(result);
result.moveTo(goalCoords, yaw, result.getXRot());
2020-09-22 15:38:58 +02:00
}
2020-01-29 21:36:21 +01:00
return result;
}
});
2019-03-21 22:27:51 +01:00
2021-12-05 23:32:31 +01:00
var spot = IAuraChunk.getHighestSpot(level, pos, 35, pos);
2021-12-04 15:40:09 +01:00
IAuraChunk.getAuraChunk(level, spot).drainAura(spot, 50000);
2019-03-20 20:51:24 +01:00
}
2021-12-04 15:40:09 +01:00
private BlockPos getGoalCoords(Level level, BlockPos pos) {
2021-12-05 23:32:31 +01:00
var server = level.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
2021-12-05 23:32:31 +01:00
return ServerLevel.END_SPAWN_POINT.above(8);
2019-03-20 20:51:24 +01:00
} else {
2021-12-05 23:32:31 +01:00
if (level.dimension() == Level.OVERWORLD) {
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
2021-12-05 23:32:31 +01:00
var overworld = server.getLevel(this.goalDim);
var spawn = overworld.getSharedSpawnPos();
var ret = new BlockPos(spawn.getX(), 0, spawn.getZ());
return ret.above(overworld.getHeight(Heightmap.Types.WORLD_SURFACE, spawn.getX(), spawn.getZ()));
2019-03-20 20:51:24 +01:00
}
}
}
@Override
2023-02-15 23:45:50 +01:00
@SuppressWarnings({"deprecation", "RedundantSuppression"})
2020-09-22 03:17:02 +02:00
public Property<RailShape> getShapeProperty() {
2022-06-27 15:24:04 +02:00
return BlockDimensionRail.SHAPE;
2019-03-20 20:51:24 +01:00
}
2020-01-29 01:34:58 +01:00
@Override
2021-12-05 23:32:31 +01:00
public boolean isFlexibleRail(BlockState state, BlockGetter level, BlockPos pos) {
2019-03-20 20:51:24 +01:00
return false;
}
@Override
2021-12-05 23:32:31 +01:00
public boolean canMakeSlopes(BlockState state, BlockGetter level, BlockPos pos) {
2019-11-04 19:08:49 +01:00
return false;
2019-03-20 20:51:24 +01:00
}
@Override
2021-12-05 23:32:31 +01:00
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
2022-06-27 15:24:04 +02:00
builder.add(BlockDimensionRail.SHAPE, BaseRailBlock.WATERLOGGED);
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 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
}