-
Notifications
You must be signed in to change notification settings - Fork 655
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
Remove hard-coded disassembly item from crafting blocks, use recipe IDs instead #7396
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,50 +18,75 @@ | |
|
||
package appeng.block.crafting; | ||
|
||
import java.util.function.Supplier; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.InteractionHand; | ||
import net.minecraft.world.InteractionResultHolder; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.ItemLike; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.Block; | ||
|
||
import appeng.block.AEBaseBlockItem; | ||
import appeng.core.AEConfig; | ||
import appeng.core.definitions.AEBlocks; | ||
import appeng.core.AELog; | ||
import appeng.core.AppEng; | ||
import appeng.util.InteractionUtil; | ||
|
||
/** | ||
* Item that allows uncrafting CPU parts by disassembling them back into the crafting unit and the extra item. | ||
*/ | ||
public class CraftingBlockItem extends AEBaseBlockItem { | ||
/** | ||
* This can be retrieved when disassembling the crafting unit. | ||
*/ | ||
protected final Supplier<ItemLike> disassemblyExtra; | ||
|
||
public CraftingBlockItem(Block id, Item.Properties props, Supplier<ItemLike> disassemblyExtra) { | ||
public CraftingBlockItem(Block id, Item.Properties props) { | ||
super(id, props); | ||
this.disassemblyExtra = disassemblyExtra; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { | ||
if (AEConfig.instance().isDisassemblyCraftingEnabled() && InteractionUtil.isInAlternateUseMode(player)) { | ||
int itemCount = player.getItemInHand(hand).getCount(); | ||
player.setItemInHand(hand, ItemStack.EMPTY); | ||
public InteractionResultHolder<ItemStack> use(@NotNull Level level, @NotNull Player player, | ||
@NotNull InteractionHand hand) { | ||
return InteractionUtil.isInAlternateUseMode(player) && disassemble(player.getItemInHand(hand), level, player) | ||
? InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()) | ||
: super.use(level, player, hand); | ||
} | ||
|
||
private boolean disassemble(ItemStack stack, Level level, Player player) { | ||
if (!AEConfig.instance().isDisassemblyCraftingEnabled()) { | ||
return false; | ||
} | ||
|
||
player.getInventory().placeItemBackInInventory(AEBlocks.CRAFTING_UNIT.stack(itemCount)); | ||
player.getInventory().placeItemBackInInventory(new ItemStack(disassemblyExtra.get(), itemCount)); | ||
var recipe = level.getRecipeManager().byKey(getRecipeId()); | ||
|
||
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); | ||
if (recipe.isEmpty()) { | ||
AELog.debug("Cannot disassemble crafting block because its crafting recipe doesn't exist: %s", | ||
getRecipeId()); | ||
return false; | ||
} | ||
return super.use(level, player, hand); | ||
|
||
if (level.isClientSide()) { | ||
return true; | ||
} | ||
|
||
var inventory = player.getInventory(); | ||
|
||
if (inventory.getSelected() != stack) { | ||
return false; | ||
} | ||
|
||
inventory.setItem(inventory.selected, ItemStack.EMPTY); | ||
|
||
for (var ingredient : recipe.get().getIngredients()) { | ||
var ingredientStack = new ItemStack(ingredient.getItems()[0].getItem(), inventory.getSelected().getCount()); | ||
inventory.placeItemBackInInventory(ingredientStack); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private void disassemble(ItemStack stack, Player player) { | ||
protected ResourceLocation getRecipeId() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename -> getDisassembleRecipeId() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I am not sure this reverse lookup is a good idea. It might instead just be a property passed in at item creation time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed this was okay to do from the fact that this is already done for portable cells. |
||
return AppEng.makeId("network/crafting/" + BuiltInRegistries.ITEM.getKey(this).getPath()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have to test for empty ingredient.
Also -> If the ingredient is a tag-ingredient, this may have VERY unintended consequences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might it be a good idea then to outright prevent disassembly if a tag is used for the ingredient? Or, in general, suppressing disassembly if the ingredient can be of more than one item?