-
Notifications
You must be signed in to change notification settings - Fork 9
Architects Wand - select block from backhand, randomize with trowel #121
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
Open
tomasz-brak
wants to merge
10
commits into
GTNewHorizons:master
Choose a base branch
from
tomasz-brak:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cd0f6fe
architects wand builds with off hand blocks
tomasz-brak 4917d1c
Merge branch 'GTNewHorizons:master' into master
tomasz-brak 8a2346c
works, but buggy
tomasz-brak 5c898ac
fix placemnt of block entities
tomasz-brak 76ea28b
spotless
tomasz-brak 8a48b06
remove dep
tomasz-brak a1eec2a
style
tomasz-brak 0db5cb4
added missing is gt loaded check
tomasz-brak dc030b8
added NEI infopages
tomasz-brak 57d2983
fix sound
tomasz-brak 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
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
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
145 changes: 145 additions & 0 deletions
145
src/main/java/com/fouristhenumber/utilitiesinexcess/utils/ArchitectsSelection.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,145 @@ | ||
| package com.fouristhenumber.utilitiesinexcess.utils; | ||
|
|
||
| import java.util.*; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
|
|
||
| import net.minecraft.block.Block; | ||
| import net.minecraft.entity.player.EntityPlayer; | ||
| import net.minecraft.item.ItemBlock; | ||
| import net.minecraft.item.ItemStack; | ||
| import net.minecraft.util.MovingObjectPosition; | ||
| import net.minecraft.world.World; | ||
|
|
||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import com.fouristhenumber.utilitiesinexcess.UtilitiesInExcess; | ||
| import com.fouristhenumber.utilitiesinexcess.compat.Mods; | ||
| import com.gtnewhorizon.gtnhlib.blockpos.BlockPos; | ||
|
|
||
| import gregtech.api.items.MetaGeneratedTool; | ||
| import gregtech.common.tools.ToolTrowel; | ||
| import xonin.backhand.api.core.BackhandUtils; | ||
|
|
||
| public class ArchitectsSelection { | ||
|
|
||
| private final Set<ItemStack> validBlocks; | ||
| private final ItemStack backhand; | ||
| private final ItemStack lookAtBlock; | ||
|
|
||
| public ArchitectsSelection(EntityPlayer player, World world, MovingObjectPosition movingObjectPosition) { | ||
| this.validBlocks = new HashSet<>(); | ||
| backhand = Mods.Backhand.isLoaded() ? BackhandUtils.getOffhandItem(player) : null; | ||
| lookAtBlock = getBlockByLocation(world, movingObjectPosition, player); | ||
|
|
||
| // No logic is executed if we don't look at any block, no need to bother checking other cases | ||
| if (lookAtBlock == null) { | ||
| return; | ||
| } | ||
|
|
||
| this.validBlocks.add(lookAtBlock); // Clicked block is always valid | ||
|
|
||
| if (backhand == null) { | ||
| return; | ||
| } | ||
| if (isValidBlock(backhand)) { | ||
| this.validBlocks.add(backhand.copy()); | ||
| return; | ||
| } | ||
| if (isTrowel(backhand)) { | ||
| this.validBlocks.addAll(hotbarBlocks(player)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @param player | ||
| * @return Always a valid block list or null | ||
| */ | ||
| public List<ItemStack> blockToPlace(EntityPlayer player) { | ||
| boolean isCreative = player.capabilities.isCreativeMode; | ||
|
|
||
| if (backhand == null) { | ||
| return Collections.singletonList((lookAtBlock)); | ||
| } | ||
| if (isValidBlock(backhand)) { | ||
| return Collections.singletonList(backhand); | ||
| } | ||
| if (isTrowel(backhand)) { | ||
| return hotbarBlocks(player); | ||
| } | ||
| UtilitiesInExcess.LOG.warn("Could not determent a block to place."); | ||
| return null; | ||
| } | ||
|
|
||
| public int maxPlaceCount(EntityPlayer player, int wandLimit) { | ||
| if (player.capabilities.isCreativeMode) return wandLimit; | ||
| int count = 0; | ||
| for (ItemStack block : blockToPlace(player)) { | ||
| count += ArchitectsWandUtils.countItemInInventory(player, block); | ||
| } | ||
| return Math.min(count, wandLimit); | ||
| } | ||
|
|
||
| public boolean matches(ItemStack other) { | ||
| if (other == null) return false; | ||
| return this.validBlocks.stream() | ||
| .anyMatch( | ||
| validBlock -> validBlock.getItem() == other.getItem() | ||
| && ItemStack.areItemStackTagsEqual(validBlock, other)); | ||
| } | ||
|
|
||
| private static boolean isTrowel(@Nullable ItemStack stack) { | ||
| if (stack == null) { | ||
| return false; | ||
| } | ||
| if (stack.getItem() instanceof MetaGeneratedTool metaGeneratedTool) { | ||
| return metaGeneratedTool.getToolStats(stack) instanceof ToolTrowel; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static boolean isValidBlock(@Nullable ItemStack stack) { | ||
| return (stack != null && stack.getItem() instanceof ItemBlock); | ||
| } | ||
|
|
||
| private static List<ItemStack> hotbarBlocks(EntityPlayer player) { | ||
| List<ItemStack> candidates = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < 9; i++) { | ||
| if (i == player.inventory.currentItem) { | ||
| continue; | ||
| } | ||
| ItemStack item = player.inventory.mainInventory[i]; | ||
| if (!isValidBlock(item)) { | ||
| continue; | ||
| } | ||
| candidates.add(item); | ||
| } | ||
| return candidates; | ||
| } | ||
|
|
||
| private static ItemStack randomHotbarBlock(EntityPlayer player) { | ||
| List<ItemStack> candidates = hotbarBlocks(player); | ||
| if (candidates.isEmpty()) { | ||
| return null; | ||
| } | ||
| return candidates.get( | ||
| ThreadLocalRandom.current() | ||
| .nextInt(candidates.size())); | ||
| } | ||
|
|
||
| public static ItemStack getBlockByLocation(World world, MovingObjectPosition movingObjectPosition, | ||
| EntityPlayer player) { | ||
|
|
||
| BlockPos blockPos = new BlockPos( | ||
| movingObjectPosition.blockX, | ||
| movingObjectPosition.blockY, | ||
| movingObjectPosition.blockZ); | ||
|
|
||
| Block block = world.getBlock(blockPos.x, blockPos.y, blockPos.z); | ||
| if (block == null) { | ||
| return null; | ||
| } | ||
| return block.getPickBlock(movingObjectPosition, world, blockPos.x, blockPos.y, blockPos.z, player); | ||
| } | ||
| } |
Oops, something went wrong.
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.