Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dimensional teleportation #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ loader_version=0.8.8+build.202
#Fabric api
fabric_version=0.13.1+build.370-1.16

mod_version=2.2.0
mod_version=2.3.0
archives_base_name=simpleteleporters
maven_group=party.lemons
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.util.registry.Registry;
import party.lemons.simpleteleporters.init.SimpleTeleportersBlocks;
import party.lemons.simpleteleporters.init.SimpleTeleportersItems;
import party.lemons.simpleteleporters.item.BaseTeleportCrystalItem;

public class SimpleTeleportersClient implements ClientModInitializer {
@Override
Expand All @@ -24,7 +25,7 @@ public void onInitializeClient() {
if (client.world != null && client.player != null) {
for (Hand hand : Hand.values()) {
ItemStack stack = client.player.getStackInHand(hand);
if (!stack.isEmpty() && stack.getItem() == SimpleTeleportersItems.TELE_CRYSTAL) {
if (!stack.isEmpty() && stack.getItem() instanceof BaseTeleportCrystalItem) {
CompoundTag tags = stack.getTag();
if (tags != null) {
String s = tags.getString("dim");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,93 @@
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Style;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemScatterer;
import net.minecraft.util.*;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

import net.minecraft.world.WorldAccess;
import net.minecraft.world.dimension.DimensionType;
import party.lemons.simpleteleporters.block.entity.TeleporterBlockEntity;
import party.lemons.simpleteleporters.init.SimpleTeleportersItems;
import party.lemons.simpleteleporters.item.BaseTeleportCrystalItem;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Random;
import java.util.Set;

public class TeleporterBlock extends BlockWithEntity {
public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;
protected static final VoxelShape TELE_AABB = VoxelShapes.cuboid(0D, 0.0D, 0D, 1D, 0.3D, 1D);
public static BooleanProperty ON = BooleanProperty.of("on");

public TeleporterBlock(Settings settings) {
super(settings);
this.setDefaultState(this.getStateManager().getDefaultState().with(ON, false).with(WATERLOGGED, false));
}

@Override
public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
if (entity.isSneaking() && entity instanceof PlayerEntity) {
TeleporterBlockEntity teleporter = (TeleporterBlockEntity) world.getBlockEntity(pos);
if (teleporter == null) return;
if (!teleporter.isCoolingDown() && teleporter.hasCrystal() && teleporter.isInDimension(entity)) {
if (!teleporter.isCoolingDown() && teleporter.hasCrystal() && teleporter.canTeleportTo(entity)) {
if (entity instanceof ServerPlayerEntity && !world.isClient) {
BlockPos teleporterPos = teleporter.getTeleportPosition();
ServerPlayerEntity splayer = (ServerPlayerEntity) entity;

String targetWorld = teleporter.getTeleportWorld();
ServerWorld wrld = (ServerWorld) world;

// If we need to change dimensions, do so
if (!targetWorld.equals(world.getDimensionRegistryKey().toString())) {
wrld = world.getServer().getWorld(RegistryKey.of(Registry.DIMENSION, Identifier.tryParse(targetWorld)));

if (wrld == null) {
splayer.sendMessage(new TranslatableText("text.teleporters.error.missing_dimen", targetWorld).setStyle(Style.EMPTY.withColor(Formatting.RED)), true);
return;
}
}

if (teleporterPos == null) {
splayer.sendMessage(new TranslatableText("text.teleporters.error.unlinked").setStyle(Style.EMPTY.withColor(Formatting.RED)), true);
return;
} else if (world.getBlockState(teleporterPos).shouldSuffocate(world, teleporterPos)) {
} else if (wrld.getBlockState(teleporterPos).shouldSuffocate(wrld, teleporterPos)) {
splayer.sendMessage(new TranslatableText("text.teleporters.error.invalid_position").setStyle(Style.EMPTY.withColor(Formatting.RED)), true);
return;
}

splayer.velocityModified = true;


// If we need to change dimensions, do so now
if (wrld != world)
splayer.changeDimension(wrld);

Vec3d playerPos = new Vec3d(teleporterPos.getX() + 0.5, teleporterPos.getY(), teleporterPos.getZ() + 0.5);
splayer.networkHandler.teleportRequest(playerPos.getX(), playerPos.getY(), playerPos.getZ(), entity.yaw, entity.pitch, Collections.EMPTY_SET);

splayer.setVelocity(0, 0, 0);
splayer.velocityDirty = true;
world.playSoundFromEntity(null, splayer, SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.BLOCKS, 1.0F, 1.0F);

wrld.playSoundFromEntity(null, splayer, SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.BLOCKS, 1.0F, 1.0F);
teleporter.setCooldown(10);
BlockEntity down = world.getBlockEntity(teleporterPos.down());

BlockEntity down = wrld.getBlockEntity(teleporterPos.down());
if (down != null && down instanceof TeleporterBlockEntity) {
((TeleporterBlockEntity) down).setCooldown(10);
}
Expand All @@ -97,23 +116,23 @@ public void onEntityCollision(BlockState state, World world, BlockPos pos, Entit
}
}
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hitResult) {
TeleporterBlockEntity tele = (TeleporterBlockEntity) world.getBlockEntity(pos);
if (tele.hasCrystal()) {
ItemStack crystalStack = tele.getCrystal();
player.giveItemStack(crystalStack);
player.playSound(SoundEvents.ENTITY_ARROW_SHOOT, 0.5F, 0.4F / (world.random.nextFloat() * 0.4F + 0.8F));

world.setBlockState(pos, state.with(ON, false));
tele.setCrystal(ItemStack.EMPTY);

return ActionResult.SUCCESS;
} else {
ItemStack stack = player.getStackInHand(hand);
if (!stack.isEmpty()) {
if (stack.getItem() == SimpleTeleportersItems.TELE_CRYSTAL && stack.getTag() != null) {
if (stack.getItem() instanceof BaseTeleportCrystalItem && stack.getTag() != null) {
player.playSound(SoundEvents.ENTITY_ARROW_SHOOT, 0.5F, 0.4F / (world.random.nextFloat() * 0.4F + 0.8F));
world.setBlockState(pos, state.with(ON, true));
ItemStack setstack = stack.copy();
Expand All @@ -126,29 +145,29 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
}
return ActionResult.PASS;
}

@Override
public void onBreak(World world, BlockPos blockPos, BlockState blockState, PlayerEntity playerEntity) {
TeleporterBlockEntity tele = (TeleporterBlockEntity) world.getBlockEntity(blockPos);
ItemScatterer.spawn(world, blockPos.getX(), blockPos.getY(), blockPos.getZ(), tele.getCrystal());
super.onBreak(world, blockPos, blockState, playerEntity);
}


@Override
public FluidState getFluidState(BlockState var1) {
return var1.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(var1);
}

@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction facing, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (state.get(WATERLOGGED)) {
world.getFluidTickScheduler().schedule(pos, Fluids.WATER, Fluids.WATER.getTickRate(world)); // getTickRate == method_15789?
}

return super.getStateForNeighborUpdate(state, facing, neighborState, world, pos, neighborPos);
}

@Override
public VoxelShape getRayTraceShape(BlockState state, BlockView world, BlockPos pos) {
return TELE_AABB;
Expand All @@ -170,17 +189,17 @@ public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos po
protected void appendProperties(StateManager.Builder<Block, BlockState> st) {
st.add(ON).add(WATERLOGGED);
}

@Override
public BlockEntity createBlockEntity(BlockView blockView) {
return new TeleporterBlockEntity();
}

@Override
public BlockRenderType getRenderType(BlockState var1) {
return BlockRenderType.MODEL;
}

@Override
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {
if (state.get(ON)) {
Expand All @@ -189,7 +208,7 @@ public void randomDisplayTick(BlockState state, World world, BlockPos pos, Rando
}
}
}

@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
FluidState fs = ctx.getWorld().getFluidState(ctx.getBlockPos());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,63 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.BlockPos;

import net.minecraft.world.World;
import party.lemons.simpleteleporters.init.SimpleTeleportersBlockEntities;
import party.lemons.simpleteleporters.item.BaseTeleportCrystalItem;
import party.lemons.simpleteleporters.item.DimensionalTeleportCrystalItem;
import party.lemons.simpleteleporters.item.TeleportCrystalItem;

public class TeleporterBlockEntity extends BlockEntity implements Tickable {
private ItemStack stack = ItemStack.EMPTY;
private int cooldown = 0;

public TeleporterBlockEntity() {
super(SimpleTeleportersBlockEntities.TELE_BE);
}

@Override
public void tick() {
if (isCoolingDown()) {
this.setCooldown(this.getCooldown() - 1);
}
}

public boolean hasCrystal() {
return !getCrystal().isEmpty();
}

public boolean isInDimension(Entity entityIn) {
if (getCrystal().isEmpty())
return false;

CompoundTag tags = getCrystal().getTag();
if (tags == null)
return false;

return tags.getString("dim").equals(entityIn.world.getDimensionRegistryKey().getValue().toString());
}


public boolean canTeleportTo(Entity entityIn) {
if (getCrystal().isEmpty())
return false;

Item crystal = getCrystal().getItem();
if (crystal instanceof TeleportCrystalItem)
return isInDimension(entityIn);

return crystal instanceof DimensionalTeleportCrystalItem;
}

public ItemStack getCrystal() {
return stack;
}

public void setCrystal(ItemStack stack) {
this.stack = stack;
markDirty();
Expand All @@ -52,22 +68,33 @@ public void setCrystal(ItemStack stack) {
getWorld().updateListeners(getPos(), state, state, 3);
}
}

public BlockPos getTeleportPosition() {
if (!hasCrystal())
return null;

CompoundTag tags = getCrystal().getTag();
if (tags == null)
return null;

int xx = tags.getInt("x");
int yy = tags.getInt("y");
int zz = tags.getInt("z");

return new BlockPos(xx, yy, zz);
}

public String getTeleportWorld() {
if (!hasCrystal())
return null;

CompoundTag tags = getCrystal().getTag();
if (tags == null)
return null;

return tags.getString("dim");
}

@Override
public void fromTag(BlockState state, CompoundTag tag)
{
Expand All @@ -85,27 +112,27 @@ public void fromTag(BlockState state, CompoundTag tag)
}
}


@Override
public CompoundTag toTag(CompoundTag compound) {
compound = super.toTag(compound);

if (!stack.isEmpty()) {
CompoundTag tagCompound = stack.toTag(new CompoundTag());
compound.put("item", tagCompound);
}
compound.putInt("cooldown", cooldown);
return compound;
}

public boolean isCoolingDown() {
return getCooldown() > 0;
}

public int getCooldown() {
return cooldown;
}

public void setCooldown(int cooldown) {
this.cooldown = cooldown;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import net.minecraft.item.ItemGroup;
import net.minecraft.util.registry.Registry;

import party.lemons.simpleteleporters.item.DimensionalTeleportCrystalItem;
import party.lemons.simpleteleporters.item.TeleportCrystalItem;

import static party.lemons.simpleteleporters.SimpleTeleporters.MODID;

public class SimpleTeleportersItems {
public static Item TELE_CRYSTAL;
public static Item DIMEN_TELE_CRYSTAL;

public static void init() {
TELE_CRYSTAL = registerItem(new TeleportCrystalItem(new Item.Settings().group(ItemGroup.TRANSPORTATION)), "tele_crystal");
DIMEN_TELE_CRYSTAL = registerItem(new DimensionalTeleportCrystalItem(new Item.Settings().group(ItemGroup.TRANSPORTATION)), "dimen_tele_crystal");
}

public static Item registerItem(Item item, String name) {
Expand Down
Loading