aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/ski/kramkow/mcmod/eyeofnether/EyeOfNetherItem.java
blob: 233205a7826154b7381e3268ba5b2d99fe57c662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2024  Tomasz Kramkowski <tomasz@kramkow.ski>
// SPDX-License-Identifier: LGPL-3.0-or-later

package ski.kramkow.mcmod.eyeofnether;

import com.mojang.datafixers.util.Pair;

import net.minecraft.entity.EyeOfEnderEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntry.Reference;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.registry.entry.RegistryEntryList.Direct;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.Stats;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldEvents;
import net.minecraft.world.event.GameEvent;
import net.minecraft.world.gen.structure.Structure;
import net.minecraft.world.gen.structure.StructureKeys;

public class EyeOfNetherItem extends Item {
    public EyeOfNetherItem(final Settings settings) {
        super(settings);
    }

    @Override
    public TypedActionResult<ItemStack> use(final World world, final PlayerEntity user, final Hand hand) {
        user.setCurrentHand(hand);
        final ItemStack stack = user.getStackInHand(hand);
        if (world instanceof final ServerWorld serverWorld) {
            if (world.getRegistryKey() != World.NETHER) {
                if (!user.getAbilities().creativeMode) {
                    // This explodes at the feet, sending you up. Accidentally very funny.
                    world.createExplosion(null, world.getDamageSources().explosion(user, user), null, user.getPos(),
                            1.0f, true, World.ExplosionSourceType.NONE);
                    stack.decrement(1);
                }
                return TypedActionResult.consume(stack);
            }
            Pair<BlockPos, RegistryEntry<Structure>> pos_pair = null;
            final Registry<Structure> registry = serverWorld.getRegistryManager().get(RegistryKeys.STRUCTURE);
            Reference<Structure> fortress = registry.getEntry(StructureKeys.FORTRESS).orElse(null);
            if (fortress != null) {
                Direct<Structure> registryEntryList = RegistryEntryList.of(fortress);
                pos_pair = serverWorld.getChunkManager().getChunkGenerator().locateStructure(serverWorld,
                        registryEntryList,
                        user.getBlockPos(), 100, false);
            }
            if (pos_pair != null) {
                user.swingHand(hand, true);
                if (!user.getAbilities().creativeMode) {
                    stack.decrement(1);
                }

                // Worth changing this and the sound/event if/when that gets replaced with a
                // custom entity.
                final EyeOfEnderEntity entity = new EyeOfEnderEntity(
                        world,
                        user.getX(), user.getBodyY(0.5), user.getZ());
                entity.setItem(stack);
                entity.initTargetPos(pos_pair.getFirst());
                world.spawnEntity(entity);

                float pitch = 0.35f + world.getRandom().nextFloat() * 0.15f;
                world.playSound(
                        null,
                        user.getX(), user.getY(), user.getZ(),
                        SoundEvents.ENTITY_ENDER_EYE_LAUNCH,
                        SoundCategory.NEUTRAL,
                        0.5f, pitch);

                world.syncWorldEvent(
                        null,
                        WorldEvents.EYE_OF_ENDER_LAUNCHES,
                        user.getBlockPos(),
                        0);
                world.emitGameEvent(
                        GameEvent.PROJECTILE_SHOOT,
                        entity.getPos(),
                        GameEvent.Emitter.of(user));
                user.incrementStat(Stats.USED.getOrCreateStat(this));

                return TypedActionResult.success(stack);
            }
        }
        return TypedActionResult.consume(stack);
    }
}