|
| 1 | +package net.just_s.sds.mixin; |
| 2 | + |
| 3 | +import net.minecraft.block.Block; |
| 4 | +import net.minecraft.block.BlockState; |
| 5 | +import net.minecraft.entity.player.PlayerEntity; |
| 6 | +import net.minecraft.item.DebugStickItem; |
| 7 | +import net.minecraft.item.ItemStack; |
| 8 | +import net.minecraft.nbt.NbtCompound; |
| 9 | +import net.minecraft.state.StateManager; |
| 10 | +import net.minecraft.state.property.Property; |
| 11 | +import net.minecraft.text.Text; |
| 12 | +import net.minecraft.util.math.BlockPos; |
| 13 | +import net.minecraft.util.registry.Registry; |
| 14 | +import net.minecraft.world.WorldAccess; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | +import org.spongepowered.asm.mixin.Mixin; |
| 17 | +import org.spongepowered.asm.mixin.Shadow; |
| 18 | +import org.spongepowered.asm.mixin.injection.At; |
| 19 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 20 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 21 | + |
| 22 | +import java.util.Collection; |
| 23 | + |
| 24 | +@Mixin(DebugStickItem.class) |
| 25 | +public class DebugStickMixin { |
| 26 | + |
| 27 | + @Shadow |
| 28 | + private static void sendMessage(PlayerEntity player, Text message) { |
| 29 | + // shadowing for ease access |
| 30 | + } |
| 31 | + |
| 32 | + @Shadow |
| 33 | + private static <T> T cycle(Iterable<T> elements, @Nullable T current, boolean inverse) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + @Shadow |
| 38 | + private static <T extends Comparable<T>> BlockState cycle(BlockState state, Property<T> property, boolean inverse) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + @Shadow |
| 43 | + private static <T extends Comparable<T>> String getValueString(BlockState state, Property<T> property) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + @Inject(at = @At("HEAD"), method = "use", cancellable = true) |
| 48 | + private void injectUse(PlayerEntity player, BlockState state, WorldAccess world, BlockPos pos, boolean update, ItemStack stack, CallbackInfoReturnable<Boolean> cir) { |
| 49 | + if (!player.isCreativeLevelTwoOp()) { |
| 50 | + Block block = state.getBlock(); |
| 51 | + if ( |
| 52 | + block.getTranslationKey().contains("stairs") | |
| 53 | + block.getTranslationKey().contains("wall") | |
| 54 | + block.getTranslationKey().contains("fence") | |
| 55 | + block.getTranslationKey().contains("glass_pane") | |
| 56 | + block.getTranslationKey().contains("iron_bars")) |
| 57 | + { |
| 58 | + StateManager<Block, BlockState> stateManager = block.getStateManager(); |
| 59 | + Collection<Property<?>> collection = stateManager.getProperties(); |
| 60 | + String string = Registry.BLOCK.getId(block).toString(); |
| 61 | + if (collection.isEmpty()) { |
| 62 | + sendMessage(player, Text.of("Невозможно преобразовать данный блок")); |
| 63 | + cir.setReturnValue(false); |
| 64 | + } else { |
| 65 | + NbtCompound nbtCompound = stack.getOrCreateSubNbt("DebugProperty"); |
| 66 | + String string2 = nbtCompound.getString(string); |
| 67 | + Property<?> property = stateManager.getProperty(string2); |
| 68 | + if (!player.shouldCancelInteraction()) { |
| 69 | + if (property == null) { |
| 70 | + property = (Property)collection.iterator().next(); |
| 71 | + } |
| 72 | + |
| 73 | + if (property.getName().equals("waterlogged")) { |
| 74 | + property = (Property)cycle((Iterable)collection, (Object)property, false); |
| 75 | + } |
| 76 | + |
| 77 | + BlockState blockState = cycle(state, property, false); |
| 78 | + world.setBlockState(pos, blockState, 18); |
| 79 | + sendMessage(player, Text.of("Параметр «" + property.getName() +"» изменён ("+ getValueString(blockState, property) + ")")); |
| 80 | + } else { |
| 81 | + property = (Property)cycle((Iterable)collection, (Object)property, false); |
| 82 | + if (property.getName().equals("waterlogged")) { |
| 83 | + property = (Property)cycle((Iterable)collection, (Object)property, false); |
| 84 | + } |
| 85 | + String string3 = property.getName(); |
| 86 | + nbtCompound.putString(string, string3); |
| 87 | + sendMessage(player, Text.of("Параметр «" + string3 +"» выбран ("+ getValueString(state, property) + ")")); |
| 88 | + } |
| 89 | + |
| 90 | + cir.setReturnValue(true); |
| 91 | + } |
| 92 | + } else { |
| 93 | + sendMessage(player, Text.of("Невозможно преобразовать данный блок")); |
| 94 | + cir.setReturnValue(false); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments