Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 192 lines 8.1 kB view raw
1package net.lerariemann.infinity.item; 2 3import net.lerariemann.infinity.block.custom.AltarBlock; 4import net.lerariemann.infinity.block.entity.ChromaticBlockEntity; 5import net.lerariemann.infinity.block.entity.InfinityPortalBlockEntity; 6import net.lerariemann.infinity.registry.core.ModBlocks; 7import net.lerariemann.infinity.registry.core.ModComponentTypes; 8import net.lerariemann.infinity.registry.var.ModTags; 9import net.lerariemann.infinity.util.InfinityMethods; 10import net.lerariemann.infinity.util.VersionMethods; 11import net.lerariemann.infinity.util.var.ColorLogic; 12import net.minecraft.core.BlockPos; 13//? if >1.21 { 14import net.minecraft.core.component.DataComponentPatch; 15//?} else { 16/*import net.minecraft.nbt.CompoundTag; 17*///?} 18import net.minecraft.sounds.SoundEvents; 19import net.minecraft.world.InteractionHand; 20import net.minecraft.world.InteractionResult; 21import net.minecraft.world.entity.item.ItemEntity; 22import net.minecraft.world.entity.player.Player; 23import net.minecraft.world.item.DyeColor; 24import net.minecraft.world.item.Item; 25import net.minecraft.world.item.ItemStack; 26import net.minecraft.world.item.context.UseOnContext; 27import net.minecraft.world.level.Level; 28import net.minecraft.world.level.block.state.BlockState; 29import net.minecraft.world.phys.Vec3; 30import java.util.Optional; 31import java.util.concurrent.atomic.AtomicBoolean; 32 33public class ChromaticItem extends Item implements PortalDataHolder { 34 public ChromaticItem(Properties settings) { 35 super(settings); 36 } 37 @Override 38 public 39 //? if >1.21 { 40 DataComponentPatch 41 //?} else { 42 /*CompoundTag 43 *///?} 44 getPortalComponents(InfinityPortalBlockEntity ipbe) { 45 return ofColor(ipbe.getPortalColor()); 46 } 47 @Override 48 public Optional< 49 //? if >1.21 { 50 DataComponentPatch 51 //?} else { 52 /*CompoundTag 53 *///?} 54 > getIridComponents(ItemEntity entity) { 55 Vec3 pos = entity.position(); 56 int hue = (int)(360*InfinityMethods.sampler.getValue(pos.x, pos.y, pos.z)); 57 return Optional.of(ofHue(hue)); 58 } 59 60 @Override 61 public ItemStack getStack() { 62 return getDefaultInstance(); 63 } 64 65 public static void playDing(Player player, float pitch) { 66 player.playSound(SoundEvents.EXPERIENCE_ORB_PICKUP, 0.5f, pitch); 67 } 68 69 //? if >1.21 { 70 static DataComponentPatch ofColor(int color) { 71 return DataComponentPatch.builder() 72 .set(ModComponentTypes.COLOR.get(), color) 73 .remove(ModComponentTypes.DYE_COLOR.get()) 74 .remove(ModComponentTypes.HUE.get()) 75 .build(); 76 } 77 static DataComponentPatch ofHue(int hue) { 78 return DataComponentPatch.builder() 79 .set(ModComponentTypes.COLOR.get(), ColorLogic.getPureHue(hue/360f)) 80 .remove(ModComponentTypes.DYE_COLOR.get()) 81 .set(ModComponentTypes.HUE.get(), hue) 82 .build(); 83 } 84 static DataComponentPatch ofDye(DyeColor dyeColor) { 85 return DataComponentPatch.builder() 86 .set(ModComponentTypes.COLOR.get(), ColorLogic.getChromaticColor(dyeColor)) 87 .set(ModComponentTypes.DYE_COLOR.get(), dyeColor.getName()) 88 .remove(ModComponentTypes.HUE.get()) 89 .build(); 90 } 91 //?} else { 92 /*static CompoundTag ofColor(int color) { 93 CompoundTag compoundTag = new CompoundTag(); 94 compoundTag.putInt(ModComponentTypes.COLOR, color); 95 return compoundTag; 96 } 97 static CompoundTag ofHue(int hue) { 98 CompoundTag compoundTag = new CompoundTag(); 99 compoundTag.putInt(ModComponentTypes.COLOR, ColorLogic.getPureHue(hue/360f)); 100 compoundTag.putInt(ModComponentTypes.HUE, hue); 101 return compoundTag; 102 } 103 static CompoundTag ofDye(DyeColor dyeColor) { 104 CompoundTag compoundTag = new CompoundTag(); 105 compoundTag.putInt(ModComponentTypes.COLOR, ColorLogic.getChromaticColor(dyeColor)); 106 compoundTag.putString(ModComponentTypes.DYE_COLOR, dyeColor.getName()); 107 return compoundTag; 108 } 109 *///?} 110 111 @Override 112 public InteractionResult useOn(UseOnContext context) { 113 Player player = context.getPlayer(); 114 if (player == null) return super.useOn(context); 115 return useOnBlock(player, context.getHand(), context.getLevel(), context.getClickedPos(), context.getItemInHand()) 116 ? InteractionResult.SUCCESS : super.useOn(context); 117 } 118 119 public boolean useOnBlock(Player player, InteractionHand hand, Level world, BlockPos pos, ItemStack currStack) { 120 BlockState oldState = world.getBlockState(pos); 121 int currColor = VersionMethods.getOrDefaultInt(currStack, ModComponentTypes.COLOR, 0xFFFFFF); 122 int currHue = VersionMethods.getOrDefaultInt(currStack, ModComponentTypes.HUE, -1); 123 if (player.isShiftKeyDown()) { //copy color 124 ItemStack newStack = currStack.copy(); 125 int i = -1; 126 if (world.getBlockEntity(pos) instanceof ChromaticBlockEntity cbe) i = cbe.getTint(); 127 else if (oldState.is(ModBlocks.ALTAR.get())) { 128 int altarState = oldState.getValue(AltarBlock.COLOR); 129 if (altarState == 0) i = ColorLogic.getChromaticColor(DyeColor.LIGHT_GRAY); 130 else { 131 int hue = (altarState - 1) * (360 / (AltarBlock.numColors - 1)); 132 if (!ColorLogic.matchesPureHue(currColor, hue)) { 133 //? if >1.21 { 134 newStack.applyComponentsAndValidate(ofHue(hue)); 135 //?} else { 136 /*newStack.setTag(ofHue(hue)); 137 *///?} 138 } else return false; 139 } 140 } 141 else { //copy color from vanilla blocks 142 DyeColor dyeColor = ColorLogic.getColorByState(oldState); 143 if (dyeColor != null && 144 !dyeColor.getName().equals(VersionMethods.getOrDefaultString(newStack, ModComponentTypes.DYE_COLOR, "null"))) { 145 //? if >1.21 { 146 newStack.applyComponentsAndValidate(ofDye(dyeColor)); 147 //?} else { 148 /*newStack.setTag(ofDye(dyeColor)); 149 *///?} 150 } 151 else return false; 152 } 153 if (i > 0) { 154 if (currHue > 0) { 155 if (ColorLogic.matchesPureHue(i, currHue)) return false; 156 //? if >1.21 { 157 newStack.applyComponentsAndValidate(ofColor(i)); 158 //?} else { 159 /*newStack.setTag(ofColor(i)); 160 *///?} 161 } 162 else if (i != currColor) { 163 //? if >1.21 { 164 newStack.applyComponentsAndValidate(ofColor(i)); 165 //?} else { 166 /*newStack.setTag(ofColor(i)); 167 *///?} 168 } else return false; 169 } 170 player.setItemInHand(hand, newStack); 171 playDing(player, 0.5f); 172 return true; 173 } 174 else { //paste color to blocks 175 if (oldState.is(ModTags.IRIDESCENT_BLOCKS)) return false; 176 boolean bl = VersionMethods.contains(currStack, ModComponentTypes.DYE_COLOR); 177 BlockState state = ColorLogic.recolor(bl ? VersionMethods.getOrNullString(currStack, ModComponentTypes.DYE_COLOR) : "infinity:chromatic", oldState); 178 if (state == null) return false; 179 world.setBlockAndUpdate(pos, state); 180 AtomicBoolean cancel = new AtomicBoolean(false); 181 if (!bl && world.getBlockEntity(pos) instanceof ChromaticBlockEntity cbe) { 182 if (currHue > 0) cbe.setColor(currHue, 255, 255, cancel); 183 else cbe.setColor(currColor, cancel); 184 } 185 if (!cancel.get()) { 186 playDing(player, 1f); 187 return true; 188 } 189 } 190 return false; 191 } 192}