Skip to content

Commit

Permalink
Merge pull request #420 from ferriarnus/enchanterchanges
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus authored Aug 20, 2023
2 parents 5ec6805 + c2b3981 commit 12370ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.Tags;
import net.minecraftforge.items.wrapper.RecipeWrapper;

import java.util.Optional;
import org.jetbrains.annotations.Nullable;

public class EnchanterBlockEntity extends MachineBlockEntity {

private final RecipeWrapper container;

@Nullable
private EnchanterRecipe currentRecipe;
public static final SingleSlotAccess BOOK = new SingleSlotAccess();
public static final SingleSlotAccess CATALYST = new SingleSlotAccess();
public static final SingleSlotAccess LAPIS = new SingleSlotAccess();
Expand Down Expand Up @@ -79,15 +79,15 @@ public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory,
protected MachineInventory createMachineInventory(MachineInventoryLayout layout) {
// Custom behaviour as this works more like a crafting table than a machine.
return new MachineInventory(getIOConfig(), layout) {

protected void onContentsChanged(int slot) {
if (level == null) {
return;
}

currentRecipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level).orElse(null);
if (!OUTPUT.isSlot(slot)) {
Optional<EnchanterRecipe> recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level);
if (recipe.isPresent()) {
OUTPUT.setStackInSlot(this, recipe.get().assemble(container, level.registryAccess()));
if (currentRecipe != null) {
OUTPUT.setStackInSlot(this, currentRecipe.assemble(container, level.registryAccess()));
} else {
OUTPUT.setStackInSlot(this, ItemStack.EMPTY);
}
Expand All @@ -109,5 +109,9 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) {
}
};
}


@Nullable
public EnchanterRecipe getCurrentRecipe() {
return currentRecipe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,26 @@

import com.enderio.machines.common.blockentity.EnchanterBlockEntity;
import com.enderio.machines.common.init.MachineMenus;
import com.enderio.machines.common.init.MachineRecipes;
import com.enderio.machines.common.io.item.SingleSlotAccess;
import com.enderio.machines.common.recipe.EnchanterRecipe;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.items.wrapper.RecipeWrapper;
import org.apache.logging.log4j.LogManager;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public class EnchanterMenu extends MachineMenu<EnchanterBlockEntity> {
public static int INPUTS_INDEX = 0;
public static int INPUT_COUNT = 3;
public static int LAST_INDEX = 3;

private Level level;

public EnchanterMenu(@Nullable EnchanterBlockEntity blockEntity, Inventory inventory, int pContainerId) {
super(blockEntity, inventory, MachineMenus.ENCHANTER.get(), pContainerId);
if (blockEntity != null) {
this.level = blockEntity.getLevel();
addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.BOOK, 16, 35));
addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.CATALYST, 65, 35));
addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.LAPIS, 85, 35));
Expand All @@ -46,11 +39,9 @@ public static EnchanterMenu factory(@Nullable MenuType<EnchanterMenu> pMenuType,
}

public int getCurrentCost() {
if (level != null) {
Optional<EnchanterRecipe> recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), getBlockEntity().getContainer(), level);
if (recipe.isPresent()) {
return recipe.get().getXPCost(new RecipeWrapper(this.getBlockEntity().getInventory()));
}
EnchanterRecipe recipe = this.getBlockEntity().getCurrentRecipe();
if (recipe != null) {
return recipe.getXPCost(new RecipeWrapper(this.getBlockEntity().getInventory()));
}
return -1;
}
Expand All @@ -66,11 +57,11 @@ public EnchanterOutputMachineSlot(@Nullable EnchanterBlockEntity blockEntity, Si

@Override
public void onTake(Player pPlayer, ItemStack pStack) {
Optional<EnchanterRecipe> recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), blockEntity.getContainer(), level);
if (recipe.isPresent() && (pPlayer.experienceLevel >= recipe.get().getXPCost(blockEntity.getContainer()) || pPlayer.isCreative())) {
int amount = recipe.get().getInputAmountConsumed(blockEntity.getContainer());
int lapizForLevel = recipe.get().getLapisForLevel(recipe.get().getEnchantmentLevel(EnchanterBlockEntity.CATALYST.getItemStack(blockEntity).getCount()));
pPlayer.giveExperienceLevels(-recipe.get().getXPCost(blockEntity.getContainer()));
EnchanterRecipe recipe = blockEntity.getCurrentRecipe();
if (recipe != null && (pPlayer.experienceLevel >= recipe.getXPCost(blockEntity.getContainer()) || pPlayer.isCreative())) {
int amount = recipe.getInputAmountConsumed(blockEntity.getContainer());
int lapizForLevel = recipe.getLapisForLevel(recipe.getEnchantmentLevel(EnchanterBlockEntity.CATALYST.getItemStack(blockEntity).getCount()));
pPlayer.giveExperienceLevels(-recipe.getXPCost(blockEntity.getContainer()));
EnchanterBlockEntity.BOOK.getItemStack(blockEntity).shrink(1);
EnchanterBlockEntity.CATALYST.getItemStack(blockEntity).shrink(amount);
EnchanterBlockEntity.LAPIS.getItemStack(blockEntity).shrink(lapizForLevel);
Expand All @@ -80,8 +71,8 @@ public void onTake(Player pPlayer, ItemStack pStack) {

@Override
public boolean mayPickup(Player playerIn) {
Optional<EnchanterRecipe> recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), blockEntity.getContainer(), level);
if (recipe.isPresent() && (playerIn.experienceLevel >= recipe.get().getXPCost(blockEntity.getContainer()) || playerIn.isCreative()) && blockEntity.canAct()) {
EnchanterRecipe recipe = blockEntity.getCurrentRecipe();
if (recipe != null && (playerIn.experienceLevel >= recipe.getXPCost(blockEntity.getContainer()) || playerIn.isCreative()) && blockEntity.canAct()) {
return super.mayPickup(playerIn);
}
return false;
Expand Down

0 comments on commit 12370ce

Please sign in to comment.