-
Notifications
You must be signed in to change notification settings - Fork 0
Add dual-tool mining support for GT + TiC cross-hand combinations #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/com/github/gtexpert/gtmt/integration/tic/DualToolHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package com.github.gtexpert.gtmt.integration.tic; | ||
|
|
||
| import net.minecraft.block.Block; | ||
| import net.minecraft.block.state.IBlockState; | ||
| import net.minecraft.enchantment.EnchantmentHelper; | ||
| import net.minecraft.entity.player.EntityPlayer; | ||
| import net.minecraft.init.Enchantments; | ||
| import net.minecraft.item.ItemStack; | ||
| import net.minecraft.util.math.BlockPos; | ||
| import net.minecraftforge.common.ForgeHooks; | ||
| import net.minecraftforge.event.world.BlockEvent; | ||
| import net.minecraftforge.fml.common.eventhandler.EventPriority; | ||
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||
|
|
||
| import gregtech.api.items.toolitem.IGTTool; | ||
|
|
||
| import slimeknights.tconstruct.library.tools.TinkerToolCore; | ||
| import slimeknights.tconstruct.library.utils.ToolHelper; | ||
|
|
||
| /** | ||
| * Extends TiC's off-hand mining to work with GT tools. | ||
| * | ||
| * <ul> | ||
| * <li>GT main-hand + TiC off-hand: uses TiC dig speed and generates drops when GT cannot harvest.</li> | ||
| * <li>TiC main-hand + GT off-hand: uses GT dig speed when TiC cannot harvest.</li> | ||
| * </ul> | ||
| * | ||
| * Runs at {@link EventPriority#LOW} so TiC's own {@code BreakSpeed} handler fires first. | ||
| */ | ||
| public final class DualToolHandler { | ||
|
|
||
| private DualToolHandler() {} | ||
|
|
||
| @SubscribeEvent(priority = EventPriority.LOW) | ||
| public static void onBreakSpeed(net.minecraftforge.event.entity.player.PlayerEvent.BreakSpeed event) { | ||
| EntityPlayer player = event.getEntityPlayer(); | ||
| ItemStack mainhand = player.getHeldItemMainhand(); | ||
| ItemStack offhand = player.getHeldItemOffhand(); | ||
|
|
||
| if (offhand.isEmpty()) return; | ||
|
|
||
| IBlockState state = event.getState(); | ||
|
|
||
| if (mainhand.getItem() instanceof IGTTool && offhand.getItem() instanceof TinkerToolCore) { | ||
| if (!canHarvestForDrops(player, mainhand, state) && ToolHelper.canHarvest(offhand, state)) { | ||
| float speed = ToolHelper.calcDigSpeed(offhand, state); | ||
| BlockPos pos = event.getPos(); | ||
| if (pos == null || !ForgeHooks.canHarvestBlock(state.getBlock(), player, player.world, pos)) { | ||
| speed *= (100.0f / 30.0f); | ||
| } | ||
| event.setNewSpeed(speed); | ||
| } | ||
| } | ||
|
|
||
| if (mainhand.getItem() instanceof TinkerToolCore && offhand.getItem() instanceof IGTTool) { | ||
| if (!ToolHelper.canHarvest(mainhand, state) && canHarvestForDrops(player, offhand, state)) { | ||
| float speed = offhand.getItem().getDestroySpeed(offhand, state); | ||
| if (speed > 1.0f) { | ||
| int efficiency = EnchantmentHelper.getEnchantmentLevel(Enchantments.EFFICIENCY, offhand); | ||
| if (efficiency > 0) speed += efficiency * efficiency + 1; | ||
| } | ||
| event.setNewSpeed(speed); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Damages the off-hand tool after a block is broken via off-hand mining. | ||
| * For Case 1, also spawns drops using the off-hand TiC tool when | ||
| * {@code ForgeHooks.canHarvestBlock} returns {@code false} for the GT main-hand tool. | ||
| * | ||
| * Runs at {@link EventPriority#LOWEST} so cancellations by other mods are resolved first. | ||
| */ | ||
| @SubscribeEvent(priority = EventPriority.LOWEST) | ||
| public static void onBlockBreak(BlockEvent.BreakEvent event) { | ||
| if (event.isCanceled()) return; | ||
|
|
||
| EntityPlayer player = event.getPlayer(); | ||
| if (player == null || player.world.isRemote) return; | ||
|
|
||
| ItemStack mainhand = player.getHeldItemMainhand(); | ||
| ItemStack offhand = player.getHeldItemOffhand(); | ||
|
|
||
| if (offhand.isEmpty()) return; | ||
|
|
||
| IBlockState state = event.getState(); | ||
|
|
||
| if (mainhand.getItem() instanceof IGTTool && offhand.getItem() instanceof TinkerToolCore) { | ||
| if (!canHarvestForDrops(player, mainhand, state) && ToolHelper.canHarvest(offhand, state)) { | ||
| if (!ForgeHooks.canHarvestBlock(state.getBlock(), player, player.world, event.getPos())) { | ||
| state.getBlock().harvestBlock(player.world, player, event.getPos(), state, | ||
| player.world.getTileEntity(event.getPos()), offhand); | ||
| } | ||
| offhand.getItem().onBlockDestroyed(offhand, player.world, state, | ||
| event.getPos(), player); | ||
| } | ||
| } | ||
|
|
||
| if (mainhand.getItem() instanceof TinkerToolCore && offhand.getItem() instanceof IGTTool) { | ||
| if (!ToolHelper.canHarvest(mainhand, state) && canHarvestForDrops(player, offhand, state)) { | ||
| offhand.getItem().onBlockDestroyed(offhand, player.world, state, | ||
| event.getPos(), player); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean canHarvestForDrops(EntityPlayer player, ItemStack stack, IBlockState state) { | ||
| if (state.getMaterial().isToolNotRequired()) return true; | ||
| Block block = state.getBlock(); | ||
| String toolType = block.getHarvestTool(state); | ||
| if (stack.isEmpty() || toolType == null) { | ||
| return player.canHarvestBlock(state); | ||
| } | ||
| int level = stack.getItem().getHarvestLevel(stack, toolType, player, state); | ||
| if (level < 0) return player.canHarvestBlock(state); | ||
| return level >= block.getHarvestLevel(state); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/main/java/com/github/gtexpert/gtmt/integration/tic/api/ElasticMaterials.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package com.github.gtexpert.gtmt.integration.tic.api; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import gregtech.api.unification.material.Material; | ||
| import gregtech.api.unification.material.Materials; | ||
|
|
||
| /** | ||
| * Registry for TiC BowString and Fletching material stats derived from GT polymer materials. | ||
| * | ||
| * <p> | ||
| * GTMT automatically registers every GT material that carries the {@code POLYMER} property | ||
| * as a TiC BowString and Fletching material. Registrations made here take priority over | ||
| * GTMT's auto-assigned default stats (modifier = 1.0 / accuracy = 1.0). | ||
| * | ||
| * <p> | ||
| * All registrations must occur before GTMT's {@code registerBlocks} phase | ||
| * (your mod's {@code preInit} or {@code init}). | ||
| */ | ||
| public final class ElasticMaterials { | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Entry type | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| /** BowString and Fletching stats for a single GT material. */ | ||
| public static final class Entry { | ||
|
|
||
| private final float stringModifier; | ||
| private final float fletchingAccuracy; | ||
| private final float fletchingModifier; | ||
|
|
||
| public Entry(float stringModifier, float fletchingAccuracy, float fletchingModifier) { | ||
| this.stringModifier = stringModifier; | ||
| this.fletchingAccuracy = fletchingAccuracy; | ||
| this.fletchingModifier = fletchingModifier; | ||
| } | ||
|
|
||
| /** BowString draw-speed modifier (1.0 = neutral). */ | ||
| public float stringModifier() { | ||
| return stringModifier; | ||
| } | ||
|
|
||
| /** Fletching accuracy (1.0 = perfect, lower = spread). */ | ||
| public float fletchingAccuracy() { | ||
| return fletchingAccuracy; | ||
| } | ||
|
|
||
| /** Fletching damage modifier (1.0 = neutral). */ | ||
| public float fletchingModifier() { | ||
| return fletchingModifier; | ||
| } | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Registry | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| private static final Map<Material, Entry> ENTRIES = new LinkedHashMap<>(); | ||
|
|
||
| static { | ||
| // Natural rubber — slimeball-tier baseline | ||
| registerIfAbsent(Materials.Rubber, 1.0f, 1.0f, 1.0f); | ||
| // Vulcanised silicone — more durable | ||
| registerIfAbsent(Materials.SiliconeRubber, 1.05f, 1.0f, 1.05f); | ||
| // SBR — best synthetic rubber | ||
| registerIfAbsent(Materials.StyreneButadieneRubber, 1.1f, 1.0f, 1.1f); | ||
| // Polyethylene — stiffer plastic, lower tier | ||
| registerIfAbsent(Materials.Polyethylene, 0.8f, 0.9f, 0.8f); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an unmodifiable view of the elastic-material registry. | ||
| * Used internally by {@code TiCMaterials} when registering BowString/Fletching materials. | ||
| */ | ||
| public static Map<Material, Entry> getEntries() { | ||
| return Collections.unmodifiableMap(ENTRIES); | ||
| } | ||
|
|
||
| /** | ||
| * Registers BowString and Fletching stats for a GT polymer material. | ||
| * | ||
| * <p> | ||
| * Call this before GTMT's {@code registerBlocks} phase to override the stats | ||
| * GTMT would otherwise auto-assign. | ||
| * | ||
| * <p> | ||
| * Example — register a custom high-grade polymer: | ||
| * | ||
| * <pre> | ||
| * {@code | ||
| * ElasticMaterials.register(MyMaterials.KEVLAR, 1.3f, 1.0f, 1.2f); | ||
| * } | ||
| * </pre> | ||
| * | ||
| * @param material GT material (must carry the {@code POLYMER} property, | ||
| * or be a recognised elastic material) | ||
| * @param stringModifier BowString draw-speed modifier (must be > 0) | ||
| * @param fletchingAccuracy Fletching accuracy (must be > 0 and ≤ 1) | ||
| * @param fletchingModifier Fletching damage modifier (must be > 0) | ||
| * @throws IllegalArgumentException if material is null or any stat is out of range | ||
| */ | ||
| public static void register(Material material, | ||
| float stringModifier, | ||
| float fletchingAccuracy, | ||
| float fletchingModifier) { | ||
| validate(material, stringModifier, fletchingAccuracy, fletchingModifier); | ||
| ENTRIES.put(material, new Entry(stringModifier, fletchingAccuracy, fletchingModifier)); | ||
| } | ||
|
|
||
| /** | ||
| * Fills stats only if the material has not already been registered externally. | ||
| * Called internally by the static initializer and by {@code TiCMaterials} for | ||
| * auto-detected POLYMER materials. | ||
| */ | ||
| public static void registerIfAbsent(Material material, | ||
| float stringModifier, | ||
| float fletchingAccuracy, | ||
| float fletchingModifier) { | ||
| validate(material, stringModifier, fletchingAccuracy, fletchingModifier); | ||
| ENTRIES.putIfAbsent(material, new Entry(stringModifier, fletchingAccuracy, fletchingModifier)); | ||
| } | ||
|
|
||
| private static void validate(Material material, | ||
| float stringModifier, | ||
| float fletchingAccuracy, | ||
| float fletchingModifier) { | ||
| if (material == null) throw new IllegalArgumentException("material must not be null"); | ||
| if (stringModifier <= 0) throw new IllegalArgumentException("stringModifier must be > 0"); | ||
| if (fletchingAccuracy <= 0) throw new IllegalArgumentException("fletchingAccuracy must be > 0"); | ||
| if (fletchingModifier <= 0) throw new IllegalArgumentException("fletchingModifier must be > 0"); | ||
| } | ||
|
|
||
| private ElasticMaterials() {} | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.