diff options
Diffstat (limited to 'src/main/java/ski/kramkow/mcmod/eyeofnether/EyeOfNetherItem.java')
-rw-r--r-- | src/main/java/ski/kramkow/mcmod/eyeofnether/EyeOfNetherItem.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/main/java/ski/kramkow/mcmod/eyeofnether/EyeOfNetherItem.java b/src/main/java/ski/kramkow/mcmod/eyeofnether/EyeOfNetherItem.java new file mode 100644 index 0000000..50d85da --- /dev/null +++ b/src/main/java/ski/kramkow/mcmod/eyeofnether/EyeOfNetherItem.java @@ -0,0 +1,95 @@ +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); + } +} |