Search in sources :

Example 1 with DimensionType

use of net.minecraft.world.dimension.DimensionType in project NetherEx by LogicTechCorp.

the class PlayerHandler method onPlayerClone.

@SubscribeEvent
public static void onPlayerClone(PlayerEvent.Clone event) {
    PlayerEntity oldPlayer = event.getOriginal();
    PlayerEntity newPlayer = event.getPlayer();
    IInventory oldInventory = oldPlayer.inventory;
    IInventory newInventory = newPlayer.inventory;
    if (event.isWasDeath()) {
        ItemStack mirrorStack = ItemStack.EMPTY;
        for (int i = 0; i < oldInventory.getSizeInventory(); i++) {
            ItemStack stack = oldInventory.getStackInSlot(i);
            if (stack.getItem() == NetherExItems.DULL_MIRROR.get()) {
                NBTHelper.ensureTagExists(stack);
                if (stack.getTag().contains("SpawnPoint") && (stack.getDamage() < (stack.getMaxDamage() - 1))) {
                    stack.damageItem(1, newPlayer, entity -> {
                    });
                }
                newInventory.setInventorySlotContents(i, stack);
                mirrorStack = stack;
                break;
            }
        }
        if (!mirrorStack.isEmpty()) {
            if (mirrorStack.getDamage() < mirrorStack.getMaxDamage() - 1) {
                CompoundNBT compound = NBTHelper.ensureTagExists(mirrorStack);
                if (compound.contains("SpawnDimension") && compound.contains("SpawnPoint")) {
                    DimensionType spawnDimension = DimensionType.byName(new ResourceLocation(compound.getString("SpawnDimension")));
                    BlockPos spawnPoint = NBTUtil.readBlockPos(compound.getCompound("SpawnPoint"));
                    if (spawnDimension != null) {
                        newPlayer.setSpawnDimenion(spawnDimension);
                        newPlayer.setSpawnPoint(spawnPoint, true, false, spawnDimension);
                    }
                }
            }
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) DimensionType(net.minecraft.world.dimension.DimensionType) CompoundNBT(net.minecraft.nbt.CompoundNBT) ResourceLocation(net.minecraft.util.ResourceLocation) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 2 with DimensionType

use of net.minecraft.world.dimension.DimensionType in project NetherEx by LogicTechCorp.

the class DullMirrorItem method onItemRightClick.

@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
    ItemStack stack = player.getHeldItem(hand);
    if (!world.isRemote) {
        if (stack.getDamage() < stack.getMaxDamage() && player.isShiftKeyDown()) {
            DimensionType spawnDimension = world.getDimension().getType();
            BlockPos spawnPoint = player.getPosition();
            CompoundNBT compound = NBTHelper.ensureTagExists(stack);
            if (!compound.contains("SpawnDimension") || !compound.contains("SpawnPoint")) {
                DimensionType bedDimension = player.getSpawnDimension();
                BlockPos bedPos = player.getBedLocation(bedDimension);
                if (bedPos == null) {
                    bedPos = world.getServer().getWorld(bedDimension).getSpawnPoint();
                }
                compound.putString("BedDimension", bedDimension.getRegistryName().toString());
                compound.put("BedPoint", NBTUtil.writeBlockPos(bedPos));
                compound.putString("SpawnDimension", spawnDimension.getRegistryName().toString());
                compound.put("SpawnPoint", NBTUtil.writeBlockPos(spawnPoint));
                player.setSpawnDimenion(spawnDimension);
                player.setSpawnPoint(spawnPoint, true, false, spawnDimension);
                player.sendMessage(new TranslationTextComponent("tooltip.netherex.dull_mirror.spawn_point_set", spawnPoint.getX() + " " + spawnPoint.getY() + " " + spawnPoint.getZ()));
                world.playSound((double) spawnPoint.getX() + 0.5D, (double) spawnPoint.getY() + 0.5D, (double) spawnPoint.getZ() + 0.5D, SoundEvents.BLOCK_PORTAL_AMBIENT, SoundCategory.BLOCKS, 0.5F, world.rand.nextFloat() * 0.4F + 0.8F, false);
            } else {
                DimensionType bedDimension = DimensionType.byName(new ResourceLocation(compound.getString("BedDimension")));
                if (bedDimension != null) {
                    compound.remove("SpawnDimension");
                    compound.remove("SpawnPoint");
                    player.setSpawnDimenion(bedDimension);
                    player.setSpawnPoint(NBTUtil.readBlockPos(compound.getCompound("BedPoint")), true, false, bedDimension);
                    player.sendMessage(new TranslationTextComponent("tooltip.netherex.dull_mirror.spawn_point_removed", spawnPoint.getX() + " " + spawnPoint.getY() + " " + spawnPoint.getZ()));
                    world.playSound((double) spawnPoint.getX() + 0.5D, (double) spawnPoint.getY() + 0.5D, (double) spawnPoint.getZ() + 0.5D, SoundEvents.BLOCK_FIRE_AMBIENT, SoundCategory.BLOCKS, 0.5F, world.rand.nextFloat() * 0.4F + 0.8F, false);
                }
            }
            return new ActionResult<>(ActionResultType.SUCCESS, stack);
        }
    }
    return new ActionResult<>(ActionResultType.FAIL, stack);
}
Also used : DimensionType(net.minecraft.world.dimension.DimensionType) CompoundNBT(net.minecraft.nbt.CompoundNBT) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 3 with DimensionType

use of net.minecraft.world.dimension.DimensionType in project NetherEx by LogicTechCorp.

the class DullMirrorItem method addInformation.

@Override
@OnlyIn(Dist.CLIENT)
public void addInformation(ItemStack stack, World world, List<ITextComponent> tooltip, ITooltipFlag flag) {
    CompoundNBT compound = stack.getTag();
    if (compound != null) {
        if (compound.contains("SpawnDimension")) {
            DimensionType type = DimensionType.byName(new ResourceLocation(compound.getString("SpawnDimension")));
            tooltip.add(new TranslationTextComponent("tooltip.netherex.dull_mirror.spawn_dimension", StringUtils.capitalize(type.getRegistryName().getPath().replace("_", " "))));
        }
        if (compound.contains("SpawnPoint")) {
            BlockPos spawnPos = NBTUtil.readBlockPos(compound.getCompound("SpawnPoint"));
            tooltip.add(new TranslationTextComponent("tooltip.netherex.dull_mirror.spawn_point", spawnPos.getX() + " " + spawnPos.getY() + " " + spawnPos.getZ()));
        }
        if (compound.contains("DeathPoint")) {
            BlockPos deathPos = NBTUtil.readBlockPos(compound.getCompound("DeathPoint"));
            tooltip.add(new TranslationTextComponent("tooltip.netherex.dull_mirror.death_point", deathPos.getX() + " " + deathPos.getY() + " " + deathPos.getZ()));
        }
    }
}
Also used : DimensionType(net.minecraft.world.dimension.DimensionType) CompoundNBT(net.minecraft.nbt.CompoundNBT) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) BlockPos(net.minecraft.util.math.BlockPos) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 4 with DimensionType

use of net.minecraft.world.dimension.DimensionType in project NetherEx by LogicTechCorp.

the class DullMirrorItem method inventoryTick.

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
    if (stack.getDamage() == (this.getMaxDamage(stack) - 1)) {
        CompoundNBT compound = NBTHelper.ensureTagExists(stack);
        if (!compound.getBoolean("RemovedSpawn") && entity instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) entity;
            if (compound.contains("SpawnDimension") && compound.contains("SpawnPoint")) {
                DimensionType bedDimension = DimensionType.byName(new ResourceLocation(compound.getString("BedDimension")));
                if (bedDimension != null) {
                    player.setSpawnDimenion(bedDimension);
                    player.setSpawnPoint(NBTUtil.readBlockPos(compound.getCompound("BedPoint")), true, false, bedDimension);
                    compound.putBoolean("RemovedSpawn", true);
                }
            }
        }
    }
}
Also used : DimensionType(net.minecraft.world.dimension.DimensionType) CompoundNBT(net.minecraft.nbt.CompoundNBT) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 5 with DimensionType

use of net.minecraft.world.dimension.DimensionType in project NetherEx by LogicTechCorp.

the class PlayerHandler method onPlayerRespawn.

@SubscribeEvent
public static void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {
    World world = event.getPlayer().getEntityWorld();
    PlayerEntity player = event.getPlayer();
    PlayerInventory playerInventory = player.inventory;
    if (!world.isRemote) {
        ItemStack mirrorStack = ItemStack.EMPTY;
        for (int i = 0; i < playerInventory.getSizeInventory(); i++) {
            ItemStack stack = playerInventory.getStackInSlot(i);
            if (stack.getItem() == NetherExItems.DULL_MIRROR.get()) {
                mirrorStack = stack;
                break;
            }
        }
        if (!mirrorStack.isEmpty()) {
            if (mirrorStack.getDamage() < mirrorStack.getMaxDamage() - 1) {
                CompoundNBT compound = NBTHelper.ensureTagExists(mirrorStack);
                if (compound.contains("SpawnDimension") && compound.contains("SpawnPoint")) {
                    DimensionType spawnDimension = DimensionType.byName(new ResourceLocation(compound.getString("SpawnDimension")));
                    if (spawnDimension != null && player.dimension != spawnDimension) {
                        MinecraftServer server = world.getServer();
                        if (server != null) {
                            BlockPos spawnPoint = NBTUtil.readBlockPos(compound.getCompound("SpawnPoint"));
                            ((ServerPlayerEntity) player).teleport(server.getWorld(spawnDimension), spawnPoint.getX() + 0.5D, spawnPoint.getY(), spawnPoint.getZ() + 0.5D, player.rotationYaw, player.rotationPitch);
                        }
                    }
                }
            }
        }
    }
}
Also used : DimensionType(net.minecraft.world.dimension.DimensionType) CompoundNBT(net.minecraft.nbt.CompoundNBT) ResourceLocation(net.minecraft.util.ResourceLocation) BlockPos(net.minecraft.util.math.BlockPos) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) PlayerInventory(net.minecraft.entity.player.PlayerInventory) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) MinecraftServer(net.minecraft.server.MinecraftServer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

CompoundNBT (net.minecraft.nbt.CompoundNBT)6 DimensionType (net.minecraft.world.dimension.DimensionType)6 BlockPos (net.minecraft.util.math.BlockPos)5 PlayerEntity (net.minecraft.entity.player.PlayerEntity)4 ItemStack (net.minecraft.item.ItemStack)4 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)3 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)2 PlayerInventory (net.minecraft.entity.player.PlayerInventory)1 IInventory (net.minecraft.inventory.IInventory)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 World (net.minecraft.world.World)1 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)1