|
1 | 1 | package galena.oreganized.content.item;
|
2 | 2 |
|
3 | 3 | import galena.oreganized.content.block.ICrystalGlass;
|
| 4 | +import galena.oreganized.index.OBlocks; |
4 | 5 | import net.minecraft.core.BlockPos;
|
5 | 6 | import net.minecraft.core.particles.BlockParticleOption;
|
6 | 7 | import net.minecraft.core.particles.ParticleTypes;
|
|
15 | 16 | import net.minecraft.world.item.enchantment.Enchantment;
|
16 | 17 | import net.minecraft.world.item.enchantment.EnchantmentCategory;
|
17 | 18 | import net.minecraft.world.level.Level;
|
| 19 | +import net.minecraft.world.level.block.Block; |
| 20 | +import net.minecraft.world.level.block.Blocks; |
18 | 21 | import net.minecraft.world.level.block.state.BlockState;
|
19 | 22 | import net.minecraft.world.level.gameevent.GameEvent;
|
20 | 23 | import net.minecraft.world.phys.Vec3;
|
21 | 24 |
|
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.function.Supplier; |
| 28 | + |
22 | 29 | import static galena.oreganized.index.OTags.Blocks.MINEABLE_WITH_SCRIBE;
|
23 | 30 | import static galena.oreganized.index.OTags.Blocks.SILKTOUCH_WITH_SCRIBE;
|
24 | 31 |
|
25 | 32 | public class ScribeItem extends Item {
|
26 | 33 |
|
| 34 | + private static final Map<Block, Supplier<Block>> GROOVED_BLOCKS = new HashMap<>(); |
| 35 | + |
| 36 | + public static void registerGroovedBlock(Block from, Supplier<Block> to) { |
| 37 | + GROOVED_BLOCKS.put(from, to); |
| 38 | + } |
| 39 | + |
| 40 | + static { |
| 41 | + registerGroovedBlock(Blocks.ICE, OBlocks.GROOVED_ICE); |
| 42 | + registerGroovedBlock(Blocks.PACKED_ICE, OBlocks.GROOVED_PACKED_ICE); |
| 43 | + registerGroovedBlock(Blocks.BLUE_ICE, OBlocks.GROOVED_BLUE_ICE); |
| 44 | + } |
| 45 | + |
27 | 46 | public ScribeItem(Properties properties) {
|
28 | 47 | super(properties);
|
29 | 48 | }
|
@@ -65,31 +84,41 @@ public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantmen
|
65 | 84 | return super.canApplyAtEnchantingTable(stack, enchantment);
|
66 | 85 | }
|
67 | 86 |
|
68 |
| - @Override |
69 |
| - public InteractionResult useOn(UseOnContext context) { |
70 |
| - var pos = context.getClickedPos(); |
| 87 | + private InteractionResult replaceBlock(UseOnContext context, BlockState to) { |
71 | 88 | var level = context.getLevel();
|
72 |
| - var state = level.getBlockState(pos); |
| 89 | + var pos = context.getClickedPos(); |
| 90 | + var from = level.getBlockState(pos); |
73 | 91 |
|
74 |
| - if (state.hasProperty(ICrystalGlass.TYPE)) { |
75 |
| - var type = state.getValue(ICrystalGlass.TYPE); |
| 92 | + level.setBlockAndUpdate(pos, to); |
| 93 | + level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(context.getPlayer(), from)); |
| 94 | + level.addDestroyBlockEffect(pos, from); |
| 95 | + |
| 96 | + var vec = Vec3.atCenterOf(pos); |
| 97 | + level.addParticle(new BlockParticleOption(ParticleTypes.BLOCK, from), vec.x, vec.y + 1, vec.z, 0.0, 0.0, 0.0); |
76 | 98 |
|
77 |
| - level.setBlockAndUpdate(pos, state.setValue(ICrystalGlass.TYPE, (type + 1) % (ICrystalGlass.MAX_TYPE + 1))); |
78 |
| - level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(context.getPlayer(), state)); |
79 |
| - level.addDestroyBlockEffect(pos, state); |
| 99 | + if (context.getPlayer() != null) { |
| 100 | + context.getPlayer().playSound(SoundEvents.GRINDSTONE_USE, 1F, 1.5F); |
80 | 101 |
|
81 |
| - var vec = Vec3.atCenterOf(pos); |
82 |
| - level.addParticle(new BlockParticleOption(ParticleTypes.BLOCK, state), vec.x, vec.y + 1, vec.z, 0.0, 0.0, 0.0); |
| 102 | + context.getItemInHand().hurtAndBreak(1, context.getPlayer(), player -> { |
| 103 | + player.broadcastBreakEvent(context.getHand()); |
| 104 | + }); |
| 105 | + } |
83 | 106 |
|
84 |
| - if (context.getPlayer() != null) { |
85 |
| - context.getPlayer().playSound(SoundEvents.GRINDSTONE_USE, 1F, 1.5F); |
| 107 | + return InteractionResult.sidedSuccess(level.isClientSide); |
| 108 | + } |
86 | 109 |
|
87 |
| - context.getItemInHand().hurtAndBreak(1, context.getPlayer(), player -> { |
88 |
| - player.broadcastBreakEvent(context.getHand()); |
89 |
| - }); |
90 |
| - } |
| 110 | + @Override |
| 111 | + public InteractionResult useOn(UseOnContext context) { |
| 112 | + var state = context.getLevel().getBlockState(context.getClickedPos()); |
| 113 | + |
| 114 | + if (state.hasProperty(ICrystalGlass.TYPE)) { |
| 115 | + var type = state.getValue(ICrystalGlass.TYPE); |
| 116 | + return replaceBlock(context, state.setValue(ICrystalGlass.TYPE, (type + 1) % (ICrystalGlass.MAX_TYPE + 1))); |
| 117 | + } |
91 | 118 |
|
92 |
| - return InteractionResult.sidedSuccess(level.isClientSide); |
| 119 | + var grooved = GROOVED_BLOCKS.get(state.getBlock()); |
| 120 | + if (grooved != null) { |
| 121 | + return replaceBlock(context, grooved.get().defaultBlockState()); |
93 | 122 | }
|
94 | 123 |
|
95 | 124 | return super.useOn(context);
|
|
0 commit comments