Skip to content

Commit

Permalink
make field nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Aug 15, 2023
1 parent 0f67242 commit 2e5265a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 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;
private Optional<EnchanterRecipe> currentRecipe;
@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 @@ -84,10 +84,10 @@ protected void onContentsChanged(int slot) {
if (level == null) {
return;
}
currentRecipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level);
currentRecipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level).orElse(null);
if (!OUTPUT.isSlot(slot)) {
if (currentRecipe.isPresent()) {
OUTPUT.setStackInSlot(this, currentRecipe.get().assemble(container, level.registryAccess()));
if (currentRecipe != null) {
OUTPUT.setStackInSlot(this, currentRecipe.assemble(container, level.registryAccess()));
} else {
OUTPUT.setStackInSlot(this, ItemStack.EMPTY);
}
Expand All @@ -110,7 +110,8 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) {
};
}

public Optional<EnchanterRecipe> getCurrentRecipe() {
@Nullable
public EnchanterRecipe getCurrentRecipe() {
return currentRecipe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,30 @@
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));
addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.OUTPUT, 144, 35) {
@Override
public void onTake(Player pPlayer, ItemStack pStack) {
Optional<EnchanterRecipe> recipe = blockEntity.getCurrentRecipe();
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 @@ -47,8 +41,8 @@ public void onTake(Player pPlayer, ItemStack pStack) {

@Override
public boolean mayPickup(Player playerIn) {
Optional<EnchanterRecipe> recipe = blockEntity.getCurrentRecipe();
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 All @@ -67,11 +61,9 @@ public static EnchanterMenu factory(@Nullable MenuType<EnchanterMenu> pMenuType,
}

public int getCurrentCost() {
if (level != null) {
Optional<EnchanterRecipe> recipe = this.getBlockEntity().getCurrentRecipe();
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 Down

0 comments on commit 2e5265a

Please sign in to comment.