|
| 1 | +package github.kasuminova.novaeng.common.handler; |
| 2 | + |
| 3 | +import blusunrize.immersiveengineering.api.tool.ExcavatorHandler; |
| 4 | +import blusunrize.immersiveengineering.common.blocks.metal.BlockMetalDevice1; |
| 5 | +import blusunrize.immersiveengineering.common.blocks.metal.BlockTypes_MetalDevice1; |
| 6 | +import blusunrize.immersiveengineering.common.blocks.metal.TileEntitySampleDrill; |
| 7 | +import codechicken.lib.util.ItemUtils; |
| 8 | +import crafttweaker.api.item.IItemStack; |
| 9 | +import crafttweaker.api.minecraft.CraftTweakerMC; |
| 10 | +import flaxbeard.immersivepetroleum.api.crafting.PumpjackHandler; |
| 11 | +import hellfirepvp.modularmachinery.common.tiles.base.TileMultiblockMachineController; |
| 12 | +import lombok.val; |
| 13 | +import net.minecraft.entity.player.EntityPlayer; |
| 14 | +import net.minecraft.item.ItemStack; |
| 15 | +import net.minecraft.util.EnumHand; |
| 16 | +import net.minecraft.util.ResourceLocation; |
| 17 | +import net.minecraft.util.math.BlockPos; |
| 18 | +import net.minecraft.util.text.TextComponentTranslation; |
| 19 | +import net.minecraft.world.World; |
| 20 | +import net.minecraftforge.common.util.FakePlayer; |
| 21 | +import net.minecraftforge.event.entity.player.PlayerInteractEvent; |
| 22 | +import net.minecraftforge.event.world.BlockEvent; |
| 23 | +import net.minecraftforge.fml.common.Optional; |
| 24 | +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
| 25 | + |
| 26 | +import static crafttweaker.CraftTweakerAPI.itemUtils; |
| 27 | + |
| 28 | +public class IEHandler { |
| 29 | + public static final IEHandler INSTANCE = new IEHandler(); |
| 30 | + |
| 31 | + private static final ResourceLocation tky = new ResourceLocation("contenttweaker","tky"); |
| 32 | + private static final ResourceLocation scanner = new ResourceLocation("orevisualdetector,scanner"); |
| 33 | + |
| 34 | + private static final byte[] NEXT_MODES = {1, 2, 3, 0}; |
| 35 | + private static final String[] MESSAGE_KEYS = { |
| 36 | + "new.orevisualdetector.advanced.tooltips1", |
| 37 | + "new.orevisualdetector.advanced.tooltips2", |
| 38 | + "new.orevisualdetector.advanced.tooltips3", |
| 39 | + "new.orevisualdetector.advanced.tooltips0" |
| 40 | + }; |
| 41 | + |
| 42 | + @SubscribeEvent |
| 43 | + @Optional.Method(modid = "immersivepetroleum") |
| 44 | + public void onPlayerRightClickItem(PlayerInteractEvent.RightClickItem event) { |
| 45 | + val world = event.getWorld(); |
| 46 | + val item = event.getEntityPlayer().getHeldItem(EnumHand.MAIN_HAND); |
| 47 | + EntityPlayer player; |
| 48 | + if (event.getHand() != EnumHand.MAIN_HAND |
| 49 | + ||(player = event.getEntityPlayer()) instanceof FakePlayer |
| 50 | + ||event.isCanceled() |
| 51 | + ||world.isRemote |
| 52 | + ) return; |
| 53 | + val eventpos = new BlockPos(event.getPos().getX(),250,event.getPos().getZ()); |
| 54 | + if (tky.equals(item.getItem().getRegistryName())){ |
| 55 | + giveCoresample(event, world, eventpos, player, item); |
| 56 | + } else if (scanner.equals(item.getItem().getRegistryName())){ |
| 57 | + var nbt = item.getTagCompound(); |
| 58 | + var mode = nbt.getByte("mode"); |
| 59 | + if (player.isSneaking()) { |
| 60 | + nbt.setByte("mode", NEXT_MODES[mode]); |
| 61 | + player.sendMessage( |
| 62 | + new TextComponentTranslation( |
| 63 | + "new.orevisualdetector.scan", |
| 64 | + new TextComponentTranslation(MESSAGE_KEYS[mode]) |
| 65 | + ) |
| 66 | + ); |
| 67 | + event.setCanceled(true); |
| 68 | + } else if (mode == 3) |
| 69 | + giveCoresample(event, world, eventpos, player, item); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static void giveCoresample(PlayerInteractEvent.RightClickItem event, World world, BlockPos eventpos, EntityPlayer player, ItemStack item) { |
| 74 | + world.setBlockState(eventpos, BlockMetalDevice1.getStateById(BlockTypes_MetalDevice1.SAMPLE_DRILL.getMeta())); |
| 75 | + val drill = (TileEntitySampleDrill) world.getTileEntity(eventpos); |
| 76 | + val worldInfo = ExcavatorHandler.getMineralWorldInfo( |
| 77 | + world, |
| 78 | + player.chunkCoordX, |
| 79 | + player.chunkCoordZ |
| 80 | + ); |
| 81 | + var coresample = drill.createCoreSample( |
| 82 | + world, |
| 83 | + player.chunkCoordX, |
| 84 | + player.chunkCoordZ, |
| 85 | + worldInfo |
| 86 | + ); |
| 87 | + val oilInfo = PumpjackHandler.getOilWorldInfo( |
| 88 | + world, |
| 89 | + player.chunkCoordX, |
| 90 | + player.chunkCoordZ |
| 91 | + ); |
| 92 | + if (oilInfo != null && oilInfo.getType() != null) { |
| 93 | + var nbt = coresample.getTagCompound(); |
| 94 | + nbt.setString("resType", oilInfo.getType().name); |
| 95 | + nbt.setInteger("oil", oilInfo.current); |
| 96 | + } |
| 97 | + player.inventory.placeItemBackInInventory(player.getEntityWorld(), coresample); |
| 98 | + player.getCooldownTracker().setCooldown(item.getItem(), 20); |
| 99 | + world.setBlockToAir(eventpos); |
| 100 | + event.setCanceled(true); |
| 101 | + } |
| 102 | + |
| 103 | + @SubscribeEvent |
| 104 | + public void onBlockBreak(BlockEvent.BreakEvent event) { |
| 105 | + var world = event.getWorld(); |
| 106 | + if (world.isRemote) { |
| 107 | + return; |
| 108 | + } |
| 109 | + var tile = world.getTileEntity(event.getPos()); |
| 110 | + if (!(tile instanceof TileMultiblockMachineController ctrl)) return; |
| 111 | + var pos = event.getPos(); |
| 112 | + var data = ctrl.getCustomDataTag(); |
| 113 | + for (int i = 0; i < 4; i++) { |
| 114 | + var component = data.getByte("additional_component_" + i); |
| 115 | + if (component == 1 && !event.getPlayer().isCreative()) { |
| 116 | + dropItem(world, pos, itemUtils.getItem("contenttweaker:additional_component_" + i, 0)); |
| 117 | + } |
| 118 | + } |
| 119 | + var component = data.getBoolean("additional_component_raw_ore"); |
| 120 | + if (component && !event.getPlayer().isCreative()) { |
| 121 | + dropItem(world, pos, itemUtils.getItem("contenttweaker:additional_component_raw_ore", 0)); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static void dropItem(World world, BlockPos pos, IItemStack item) { |
| 126 | + ItemUtils.dropItem(world, pos, CraftTweakerMC.getItemStack(item)); |
| 127 | + } |
| 128 | +} |
0 commit comments