Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 179 lines 7.2 kB view raw
1package net.lerariemann.infinity.item; 2 3//? if <1.21 { 4/*import net.lerariemann.infinity.access.MinecraftServerAccess; 5import net.minecraft.ChatFormatting; 6import net.minecraft.core.BlockPos; 7import net.minecraft.nbt.CompoundTag; 8import net.minecraft.network.chat.Component; 9import net.minecraft.sounds.SoundEvents; 10import net.minecraft.stats.Stats; 11import net.minecraft.world.InteractionResult; 12import net.minecraft.world.entity.player.Player; 13import net.minecraft.world.item.context.UseOnContext; 14import net.minecraft.world.level.Level; 15import net.minecraft.world.level.block.Blocks; 16import net.minecraft.world.level.block.JukeboxBlock; 17import net.minecraft.world.level.block.entity.BlockEntity; 18import net.minecraft.world.level.block.entity.JukeboxBlockEntity; 19import net.minecraft.world.level.block.state.BlockState; 20import net.minecraft.world.level.gameevent.GameEvent; 21import org.jetbrains.annotations.NotNull; 22*///?} else { 23import net.lerariemann.infinity.InfinityMod; 24import net.lerariemann.infinity.util.core.ConfigType; 25import net.minecraft.ChatFormatting; 26import net.minecraft.core.component.DataComponentMap; 27import net.minecraft.core.component.DataComponents; 28import net.minecraft.core.registries.Registries; 29import net.minecraft.network.chat.Component; 30import net.minecraft.network.chat.MutableComponent; 31import net.minecraft.resources.ResourceKey; 32import net.minecraft.resources.ResourceLocation; 33//?} 34import net.lerariemann.infinity.registry.core.ModComponentTypes; 35import net.lerariemann.infinity.registry.core.ModItems; 36import net.lerariemann.infinity.util.InfinityMethods; 37import net.lerariemann.infinity.util.VersionMethods; 38import net.minecraft.server.MinecraftServer; 39import net.minecraft.world.level.Level; 40import net.minecraft.world.InteractionHand; 41import net.minecraft.world.InteractionResult; 42import net.minecraft.world.item.*; 43//? if <1.21.5 { 44/*import net.minecraft.world.InteractionResultHolder; 45import java.util.List; 46*///?} else { 47import net.minecraft.world.item.component.TooltipDisplay; 48//?} 49import net.minecraft.world.entity.player.Player; 50 51import java.util.function.Consumer; 52 53public class DiscItem 54//? if <1.21 { 55/*extends RecordItem 56 *///?} else { 57extends Item 58//?} 59{ 60 public static String emptySubtitle = "subtitles.music.missing"; 61 //? if <1.21 { 62 63 /*public static int defaultDuration = 185; 64 65 public DiscItem(Properties builder) { 66 super(15, SoundEvents.MUSIC_DISC_CAT, builder, 185); 67 } 68 69 public static ItemStack getRandom(MinecraftServer server) { 70 MinecraftServerAccess acc = (MinecraftServerAccess)server; 71 ItemStack res = ModItems.DISC.get().getDefaultInstance(); 72 String id = acc.infinity$getRandomJukeboxId(); 73 CompoundTag t = res.getOrCreateTag(); 74 t.putString("sound_id", id); 75 t.putString("sound_sub", acc.infinity$getJukeboxSubtitle(id)); 76 t.putInt("sound_duration", acc.infinity$getJukeboxDuration(id)); 77 t.putInt(ModComponentTypes.COLOR, (int)InfinityMethods.getDimensionSeed(id)); 78 res.setTag(t); 79 return res; 80 } 81 82 @Override 83 public void appendHoverText(@NotNull ItemStack stack, Level level, List<Component> tooltip, TooltipFlag flag) { 84 tooltip.add(Component.translatable(VersionMethods.getOrDefaultString(stack, "sound_sub", emptySubtitle)).withStyle(ChatFormatting.GRAY)); 85 } 86 87 /^* our discs are stackable which mojang did not foresee ^/ 88 @Override 89 public InteractionResult useOn(UseOnContext context) { 90 Level level = context.getLevel(); 91 BlockPos blockpos = context.getClickedPos(); 92 BlockState blockstate = level.getBlockState(blockpos); 93 if (blockstate.is(Blocks.JUKEBOX) && !(Boolean)blockstate.getValue(JukeboxBlock.HAS_RECORD)) { 94 ItemStack itemstack = context.getItemInHand(); 95 if (isBlank(itemstack)) return InteractionResult.PASS; 96 if (!level.isClientSide) { 97 Player player = context.getPlayer(); 98 BlockEntity blockentity = level.getBlockEntity(blockpos); 99 if (blockentity instanceof JukeboxBlockEntity) { 100 JukeboxBlockEntity jukeboxblockentity = (JukeboxBlockEntity)blockentity; 101 jukeboxblockentity.setFirstItem(itemstack.copyWithCount(1)); 102 level.gameEvent(GameEvent.BLOCK_CHANGE, blockpos, GameEvent.Context.of(player, blockstate)); 103 } 104 105 itemstack.shrink(1); 106 if (player != null) { 107 player.awardStat(Stats.PLAY_RECORD); 108 } 109 } 110 return InteractionResult.sidedSuccess(level.isClientSide); 111 } else { 112 return InteractionResult.PASS; 113 } 114 } 115*///?} else { 116 public DiscItem(Properties properties) { 117 super(properties); 118 } 119 120 @Override 121 public void appendHoverText(ItemStack stack, TooltipContext context, 122 //? if >1.21.2 { 123 TooltipDisplay tooltipDisplay, Consumer<Component> tooltipAdder 124 //?} else { 125 /*List<Component> tooltip 126 *///?} 127 , TooltipFlag type) { 128 //? if >1.21.2 { 129 super.appendHoverText(stack, context, tooltipDisplay, tooltipAdder, type); 130 //?} else { 131 /*super.appendHoverText(stack, context, tooltip, type); 132 Consumer<Component> tooltipAdder = tooltip::add; 133 *///?} 134 if (isBlank(stack)) { 135 MutableComponent mutableText = Component.translatable(emptySubtitle).withStyle(ChatFormatting.GRAY); 136 tooltipAdder.accept(mutableText); 137 } 138 } 139 140 public static ItemStack getRandom(MinecraftServer server) { 141 ItemStack stack = ModItems.DISC.get().getDefaultInstance(); 142 ResourceLocation song = VersionMethods.id(InfinityMod.provider.randomName(InfinityMod.random, ConfigType.JUKEBOXES)); 143 stack.applyComponents(DataComponentMap.builder() 144 .set(DataComponents.JUKEBOX_PLAYABLE, new JukeboxPlayable(new EitherHolder<>( 145 ResourceKey.create(Registries.JUKEBOX_SONG, song)) 146 //? if <1.21.2 147 /*, true*/ 148 )) 149 .set(ModComponentTypes.COLOR.get(), (int) InfinityMethods.getNumericFromId(song)).build()); 150 return stack; 151 } 152//?} 153 public static boolean isBlank(ItemStack st) { 154 //? if <1.21 { 155 /*return !st.getOrCreateTag().contains("sound_id"); 156 *///?} else { 157 return !st.getComponents().has(DataComponents.JUKEBOX_PLAYABLE); 158 //?} 159 } 160 161 @Override 162 public 163 //? if <1.21.5 { 164 /*InteractionResultHolder<ItemStack> 165 *///?} else { 166 InteractionResult 167 //?} 168 use(Level world, Player player, InteractionHand hand) { 169 ItemStack st = player.getItemInHand(hand); 170 if (isBlank(st)) { 171 player.setItemInHand(hand, st.copyWithCount(st.getCount() - 1)); 172 if (!world.isClientSide()) { 173 player.getInventory().add(getRandom(world.getServer())); 174 } 175 return VersionMethods.success(player.getItemInHand(hand)); 176 } 177 return VersionMethods.pass(st); 178 } 179}