Anvil crafting

This commit is contained in:
Aleksey 2020-10-06 17:59:23 +03:00
parent 1ceb433a61
commit 115ce76b5a
12 changed files with 270 additions and 61 deletions

View file

@ -1,8 +1,5 @@
package ru.betterend.mixin.common;
import java.util.List;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@ -10,16 +7,19 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.block.AnvilBlock;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.screen.ForgingScreenHandler;
import net.minecraft.screen.Property;
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.tag.BlockTags;
import net.minecraft.world.World;
import ru.betterend.recipe.AnvilSmithingRecipe;
@Mixin(AnvilScreenHandler.class)
@ -29,48 +29,65 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler {
private final RecipeManager recipeManager = this.world.getRecipeManager();
private AnvilSmithingRecipe currentRecipe;
@Final
@Shadow
private Property levelCost;
public AnvilScreenHandlerMixin(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory,
ScreenHandlerContext context) {
super(type, syncId, playerInventory, context);
}
@Inject(method = "canTakeOutput", at = @At("RETURN"))
@Shadow
public abstract void updateResult();
@Inject(method = "canTakeOutput", at = @At("HEAD"), cancellable = true)
protected void canTakeOutput(PlayerEntity player, boolean present, CallbackInfoReturnable<Boolean> info) {
if (!info.getReturnValue()) {
info.setReturnValue(currentRecipe != null && !currentRecipe.craft(input, player).isEmpty());
if (currentRecipe != null) {
ItemStack output = this.currentRecipe.craft(input, player);
if (!output.isEmpty()) {
info.setReturnValue(true);
info.cancel();
}
}
}
@Inject(method = "onTakeOutput", at = @At("HEAD"), cancellable = true)
protected void onTakeOutput(PlayerEntity player, ItemStack stack, CallbackInfoReturnable<ItemStack> info) {
if (currentRecipe != null) {
this.input.getStack(0).decrement(1);
if (!currentRecipe.matches(input)) {
this.currentRecipe = null;
}
this.input.getStack(1).decrement(1);
this.updateResult();
this.context.run((world, blockPos) -> {
BlockState anvilState = world.getBlockState(blockPos);
if (!player.abilities.creativeMode && anvilState.isIn(BlockTags.ANVIL) && player.getRandom().nextFloat() < 0.12F) {
BlockState landingState = AnvilBlock.getLandingState(anvilState);
if (landingState == null) {
world.removeBlock(blockPos, false);
world.syncWorldEvent(1029, blockPos, 0);
} else {
world.setBlockState(blockPos, landingState, 2);
world.syncWorldEvent(1030, blockPos, 0);
}
} else {
world.syncWorldEvent(1030, blockPos, 0);
}
});
info.setReturnValue(stack);
info.cancel();
}
}
@Inject(method = "updateResult", at = @At("HEAD"), cancellable = true)
public void updateResult(CallbackInfo info) {
this.currentRecipe = null;
List<AnvilSmithingRecipe> recipes = this.recipeManager.listAllOfType(AnvilSmithingRecipe.TYPE);
for (AnvilSmithingRecipe entry : recipes) {
if (entry.matches(input)) {
this.currentRecipe = entry;
break;
}
}
public void updateOutput(CallbackInfo info) {
this.currentRecipe = this.recipeManager.getFirstMatch(AnvilSmithingRecipe.TYPE, input, world).orElse(null);
if (currentRecipe != null) {
this.levelCost.set(0);
this.output.setStack(0, currentRecipe.getOutput());
this.output.setStack(0, currentRecipe.craft(input));
this.sendContentUpdates();
info.cancel();
}
}
@Inject(method = "setNewItemName", at = @At("HEAD"), cancellable = true)
public void setNewItemName(String string, CallbackInfo info) {
if (currentRecipe != null) {
info.cancel();
}
}
}