Added AnvilReceipts from BE

This commit is contained in:
Frank 2021-10-18 12:26:35 +02:00
parent 57e2099650
commit 2674940c91
12 changed files with 746 additions and 23 deletions

View file

@ -0,0 +1,96 @@
package ru.bclib.mixin.client;
import java.util.List;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.inventory.AnvilScreen;
import net.minecraft.client.gui.screens.inventory.ItemCombinerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.AnvilMenu;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.bclib.interfaces.AnvilScreenHandlerExtended;
@Mixin(AnvilScreen.class)
public class AnvilScreenMixin extends ItemCombinerScreen<AnvilMenu> {
@Shadow
private EditBox name;
private final List<AbstractWidget> be_buttons = Lists.newArrayList();
public AnvilScreenMixin(AnvilMenu handler, Inventory playerInventory, Component title, ResourceLocation texture) {
super(handler, playerInventory, title, texture);
}
@Inject(method = "subInit", at = @At("TAIL"))
protected void be_subInit(CallbackInfo info) {
int x = (width - imageWidth) / 2;
int y = (height - imageHeight) / 2;
be_buttons.clear();
be_buttons.add(new Button(x + 8, y + 45, 15, 20, new TextComponent("<"), b -> be_previousRecipe()));
be_buttons.add(new Button(x + 154, y + 45, 15, 20, new TextComponent(">"), b -> be_nextRecipe()));
}
@Inject(method = "renderFg", at = @At("TAIL"))
protected void be_renderForeground(PoseStack matrices, int mouseX, int mouseY, float delta, CallbackInfo info) {
be_buttons.forEach(button -> {
button.render(matrices, mouseX, mouseY, delta);
});
}
@Inject(method = "slotChanged", at = @At("HEAD"), cancellable = true)
public void be_onSlotUpdate(AbstractContainerMenu handler, int slotId, ItemStack stack, CallbackInfo info) {
AnvilScreenHandlerExtended anvilHandler = (AnvilScreenHandlerExtended) handler;
if (anvilHandler.be_getCurrentRecipe() != null) {
if (anvilHandler.be_getRecipes().size() > 1) {
be_buttons.forEach(button -> button.visible = true);
}
else {
be_buttons.forEach(button -> button.visible = false);
}
name.setValue("");
info.cancel();
}
else {
be_buttons.forEach(button -> button.visible = false);
}
}
private void be_nextRecipe() {
((AnvilScreenHandlerExtended) menu).be_nextRecipe();
}
private void be_previousRecipe() {
((AnvilScreenHandlerExtended) menu).be_previousRecipe();
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (minecraft != null) {
for (AbstractWidget elem : be_buttons) {
if (elem.visible && elem.mouseClicked(mouseX, mouseY, button)) {
if (minecraft.gameMode != null) {
int i = be_buttons.indexOf(elem);
minecraft.gameMode.handleInventoryButtonClick(menu.containerId, i);
return true;
}
}
}
}
return super.mouseClicked(mouseX, mouseY, button);
}
}

View file

@ -1,2 +0,0 @@
package ru.bclib.mixin.client;public class ClientLevelMixin {
}

View file

@ -0,0 +1,20 @@
package ru.bclib.mixin.client;
import net.minecraft.client.ClientRecipeBook;
import net.minecraft.client.RecipeBookCategories;
import net.minecraft.world.item.crafting.Recipe;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.bclib.interfaces.UnknownReceipBookCategory;
@Mixin(ClientRecipeBook.class)
public abstract class ClientRecipeBookMixin {
@Inject(method = "getCategory", at = @At("HEAD"), cancellable = true)
private static void be_getGroupForRecipe(Recipe<?> recipe, CallbackInfoReturnable<RecipeBookCategories> info) {
if (recipe instanceof UnknownReceipBookCategory) {
info.setReturnValue(RecipeBookCategories.UNKNOWN);
}
}
}

View file

@ -1,18 +0,0 @@
package ru.bclib.mixin.client;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundLoginPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientboundLoginPacket.class)
public abstract class ClientboundLoginPacketMixin {
@Inject(method = "handle", cancellable = true, at=@At("HEAD"))
public void bclib_handle(ClientGamePacketListener clientGamePacketListener, CallbackInfo ci){
//cLevel.setBCLibDidSendHello();
// DataExchangeAPI.sendOnEnter();
// ci.cancel();
}
}

View file

@ -1,5 +1,11 @@
package ru.bclib.mixin.common;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
@ -9,18 +15,27 @@ import net.minecraft.world.inventory.DataSlot;
import net.minecraft.world.inventory.ItemCombinerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
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;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.bclib.blocks.BaseAnvilBlock;
import ru.bclib.blocks.LeveledAnvilBlock;
import ru.bclib.interfaces.AnvilScreenHandlerExtended;
import ru.bclib.recipes.AnvilRecipe;
@Mixin(AnvilMenu.class)
public abstract class AnvilMenuMixin extends ItemCombinerMenu {
public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilScreenHandlerExtended {
private List<AnvilRecipe> be_recipes = Collections.emptyList();
private AnvilRecipe be_currentRecipe;
private DataSlot anvilLevel;
@Shadow
private int repairItemCountCost;
@ -31,9 +46,63 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu {
public AnvilMenuMixin(@Nullable MenuType<?> menuType, int i, Inventory inventory, ContainerLevelAccess containerLevelAccess) {
super(menuType, i, inventory, containerLevelAccess);
}
@Inject(method = "<init>(ILnet/minecraft/world/entity/player/Inventory;Lnet/minecraft/world/inventory/ContainerLevelAccess;)V", at = @At("TAIL"))
public void be_initAnvilLevel(int syncId, Inventory inventory, ContainerLevelAccess context, CallbackInfo info) {
this.anvilLevel = addDataSlot(DataSlot.standalone());
if (context != ContainerLevelAccess.NULL) {
int level = context.evaluate((world, blockPos) -> {
Block anvilBlock = world.getBlockState(blockPos).getBlock();
if (anvilBlock instanceof LeveledAnvilBlock) {
return ((LeveledAnvilBlock) anvilBlock).getCraftingLevel();
}
return 1;
}, 1);
anvilLevel.set(level);
}
else {
anvilLevel.set(1);
}
}
@Shadow
public abstract void createResult();
@Inject(method = "mayPickup", at = @At("HEAD"), cancellable = true)
protected void be_canTakeOutput(Player player, boolean present, CallbackInfoReturnable<Boolean> info) {
if (be_currentRecipe != null) {
info.setReturnValue(be_currentRecipe.checkHammerDurability(inputSlots, player));
}
}
@Inject(method = "onTake", at = @At("HEAD"), cancellable = true)
protected void bclib_onTakeAnvilOutput(Player player, ItemStack stack, CallbackInfo info) {
if (be_currentRecipe != null) {
inputSlots.getItem(0).shrink(be_currentRecipe.getInputCount());
stack = be_currentRecipe.craft(inputSlots, player);
slotsChanged(inputSlots);
access.execute((world, blockPos) -> {
BlockState anvilState = world.getBlockState(blockPos);
BaseAnvilBlock anvil = (BaseAnvilBlock) anvilState.getBlock();
if (!player.getAbilities().instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom().nextDouble() < 0.1) {
BlockState damagedState = anvil.damageAnvilUse(anvilState, player.getRandom());
if (damagedState == null) {
world.removeBlock(blockPos, false);
world.levelEvent(1029, blockPos, 0);
}
else {
world.setBlock(blockPos, damagedState, 2);
world.levelEvent(1030, blockPos, 0);
}
}
else {
world.levelEvent(1030, blockPos, 0);
}
});
info.cancel();
return;
}
this.access.execute((level, blockPos) -> {
BlockState blockState = level.getBlockState(blockPos);
if (blockState.getBlock() instanceof BaseAnvilBlock) {
@ -77,4 +146,68 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu {
}
});
}
@Inject(method = "createResult", at = @At("HEAD"), cancellable = true)
public void be_updateOutput(CallbackInfo info) {
RecipeManager recipeManager = this.player.level.getRecipeManager();
be_recipes = recipeManager.getRecipesFor(AnvilRecipe.TYPE, inputSlots, player.level);
if (be_recipes.size() > 0) {
int anvilLevel = this.anvilLevel.get();
be_recipes = be_recipes.stream()
.filter(recipe -> anvilLevel >= recipe.getAnvilLevel())
.collect(Collectors.toList());
if (be_recipes.size() > 0) {
if (be_currentRecipe == null || !be_recipes.contains(be_currentRecipe)) {
be_currentRecipe = be_recipes.get(0);
}
be_updateResult();
info.cancel();
}
else {
be_currentRecipe = null;
}
}
}
@Inject(method = "setItemName", at = @At("HEAD"), cancellable = true)
public void be_setNewItemName(String string, CallbackInfo info) {
if (be_currentRecipe != null) {
info.cancel();
}
}
@Override
public boolean clickMenuButton(Player player, int id) {
if (id == 0) {
be_previousRecipe();
return true;
}
else if (id == 1) {
be_nextRecipe();
return true;
}
return super.clickMenuButton(player, id);
}
private void be_updateResult() {
if (be_currentRecipe == null) return;
resultSlots.setItem(0, be_currentRecipe.assemble(inputSlots));
broadcastChanges();
}
@Override
public void be_updateCurrentRecipe(AnvilRecipe recipe) {
this.be_currentRecipe = recipe;
be_updateResult();
}
@Override
public AnvilRecipe be_getCurrentRecipe() {
return be_currentRecipe;
}
@Override
public List<AnvilRecipe> be_getRecipes() {
return be_recipes;
}
}