Changed Aeternium crafts

This commit is contained in:
Aleksey 2021-01-06 15:34:01 +03:00
parent 2a2641eea3
commit 22b10fe134
15 changed files with 576 additions and 360 deletions

View file

@ -25,7 +25,6 @@ import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import ru.betterend.interfaces.AnvilScreenHandlerExtended;
import ru.betterend.recipe.builders.AnvilSmithingRecipe;
@Mixin(AnvilScreen.class)
public class AnvilScreenMixin extends ForgingScreen<AnvilScreenHandler> {
@ -34,6 +33,7 @@ public class AnvilScreenMixin extends ForgingScreen<AnvilScreenHandler> {
private TextFieldWidget nameField;
private List<AbstractButtonWidget> be_buttons = Lists.newArrayList();
private AnvilScreenHandlerExtended anvilHandler;
public AnvilScreenMixin(AnvilScreenHandler handler, PlayerInventory playerInventory, Text title,
Identifier texture) {
@ -43,23 +43,22 @@ public class AnvilScreenMixin extends ForgingScreen<AnvilScreenHandler> {
@Inject(method = "setup", at = @At("TAIL"))
protected void setup(CallbackInfo info) {
this.be_buttons.clear();
int x = (this.width - this.backgroundWidth) / 2;
int y = (this.height - this.backgroundHeight) / 2;
this.be_buttons.add(new ButtonWidget(x + 8, y + 45, 15, 20, new LiteralText("<"), (b) -> be_previousRecipe()));
int x = (width - backgroundWidth) / 2;
int y = (height - backgroundHeight) / 2;
this.anvilHandler = AnvilScreenHandlerExtended.class.cast(this.handler);
this.be_buttons.add(new ButtonWidget(x + 8, y + 45, 15, 20, new LiteralText("<"), (b) -> be_previousRecipe()));
this.be_buttons.add(new ButtonWidget(x + 154, y + 45, 15, 20, new LiteralText(">"), (b) -> be_nextRecipe()));
}
@Inject(method = "renderForeground", at = @At("TAIL"))
protected void renderForeground(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo info) {
AnvilScreenHandlerExtended handler = AnvilScreenHandlerExtended.class.cast(this.handler);
if (handler.be_getRecipes().size() > 1) {
if (anvilHandler.be_getRecipes().size() > 1) {
this.be_buttons.forEach(button -> button.render(matrices, mouseX, mouseY, delta));
}
}
@Inject(method = "onSlotUpdate", at = @At("HEAD"), cancellable = true)
public void onSlotUpdate(ScreenHandler handler, int slotId, ItemStack stack, CallbackInfo info) {
AnvilScreenHandlerExtended anvilHandler = AnvilScreenHandlerExtended.class.cast(this.handler);
if (anvilHandler.be_getCurrentRecipe() != null) {
this.nameField.setText("");
this.nameField.setEditable(false);
@ -69,31 +68,19 @@ public class AnvilScreenMixin extends ForgingScreen<AnvilScreenHandler> {
}
private void be_nextRecipe() {
AnvilScreenHandlerExtended handler = AnvilScreenHandlerExtended.class.cast(this.handler);
List<AnvilSmithingRecipe> recipes = handler.be_getRecipes();
AnvilSmithingRecipe current = handler.be_getCurrentRecipe();
int i = recipes.indexOf(current) + 1;
if (i == recipes.size()) {
i = 0;
}
handler.be_updateCurrentRecipe(recipes.get(i));
this.anvilHandler.be_nextRecipe();
}
private void be_previousRecipe() {
AnvilScreenHandlerExtended handler = AnvilScreenHandlerExtended.class.cast(this.handler);
List<AnvilSmithingRecipe> recipes = handler.be_getRecipes();
AnvilSmithingRecipe current = handler.be_getCurrentRecipe();
int i = recipes.indexOf(current) - 1;
if (i == 0) {
i = recipes.size() - 1;
}
handler.be_updateCurrentRecipe(recipes.get(i));
this.anvilHandler.be_previousRecipe();
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
for (AbstractButtonWidget elem : be_buttons) {
if (elem.mouseClicked(mouseX, mouseY, button)) {
int i = be_buttons.indexOf(elem);
this.client.interactionManager.clickButton(handler.syncId, i);
return true;
}
}

View file

@ -22,13 +22,13 @@ import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.tag.BlockTags;
import ru.betterend.interfaces.AnvilScreenHandlerExtended;
import ru.betterend.recipe.builders.AnvilSmithingRecipe;
import ru.betterend.recipe.builders.AnvilRecipe;
@Mixin(AnvilScreenHandler.class)
public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler implements AnvilScreenHandlerExtended {
private List<AnvilSmithingRecipe> be_recipes = Collections.emptyList();
private AnvilSmithingRecipe be_currentRecipe;
private List<AnvilRecipe> be_recipes = Collections.emptyList();
private AnvilRecipe be_currentRecipe;
public AnvilScreenHandlerMixin(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory,
ScreenHandlerContext context) {
@ -44,7 +44,6 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple
ItemStack output = this.be_currentRecipe.craft(input, player);
if (!output.isEmpty()) {
info.setReturnValue(true);
info.cancel();
}
}
}
@ -52,8 +51,8 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple
@Inject(method = "onTakeOutput", at = @At("HEAD"), cancellable = true)
protected void onTakeOutput(PlayerEntity player, ItemStack stack, CallbackInfoReturnable<ItemStack> info) {
if (be_currentRecipe != null) {
this.input.getStack(1).decrement(1);
this.updateResult();
this.input.getStack(0).decrement(1);
this.onContentChanged(input);
this.context.run((world, blockPos) -> {
BlockState anvilState = world.getBlockState(blockPos);
if (!player.abilities.creativeMode && anvilState.isIn(BlockTags.ANVIL) && player.getRandom().nextFloat() < 0.12F) {
@ -70,16 +69,15 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple
}
});
info.setReturnValue(stack);
info.cancel();
}
}
@Inject(method = "updateResult", at = @At("HEAD"), cancellable = true)
public void updateOutput(CallbackInfo info) {
RecipeManager recipeManager = this.player.world.getRecipeManager();
this.be_recipes = recipeManager.getAllMatches(AnvilSmithingRecipe.TYPE, input, player.world);
this.be_recipes = recipeManager.getAllMatches(AnvilRecipe.TYPE, input, player.world);
if (be_recipes.size() > 0) {
this.be_currentRecipe = recipeManager.getFirstMatch(AnvilSmithingRecipe.TYPE, input, player.world).get();
this.be_currentRecipe = recipeManager.getFirstMatch(AnvilRecipe.TYPE, input, player.world).get();
this.be_updateResult();
info.cancel();
}
@ -92,6 +90,18 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple
}
}
@Override
public boolean onButtonClick(PlayerEntity player, int id) {
if (id == 0) {
this.be_previousRecipe();
return true;
} else if (id == 1) {
this.be_nextRecipe();
return true;
}
return super.onButtonClick(player, id);
}
private void be_updateResult() {
if (be_currentRecipe == null) return;
this.output.setStack(0, be_currentRecipe.craft(input));
@ -99,18 +109,18 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple
}
@Override
public void be_updateCurrentRecipe(AnvilSmithingRecipe recipe) {
public void be_updateCurrentRecipe(AnvilRecipe recipe) {
this.be_currentRecipe = recipe;
this.be_updateResult();
}
@Override
public AnvilSmithingRecipe be_getCurrentRecipe() {
public AnvilRecipe be_getCurrentRecipe() {
return this.be_currentRecipe;
}
@Override
public List<AnvilSmithingRecipe> be_getRecipes() {
public List<AnvilRecipe> be_getRecipes() {
return this.be_recipes;
}
}