Skip to content

Commit

Permalink
Bamboo Mat Temp
Browse files Browse the repository at this point in the history
Bamboo Mat Temp
  • Loading branch information
Nyancatpig committed May 3, 2022
1 parent 86eafe3 commit d112da8
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ncpbails.culturaldelights.block.ModBlocks;
import com.ncpbails.culturaldelights.block.entity.ModBlockEntities;
import com.ncpbails.culturaldelights.item.ModItems;
import com.ncpbails.culturaldelights.recipe.ModRecipes;
import com.ncpbails.culturaldelights.screen.BambooMatScreen;
import com.ncpbails.culturaldelights.screen.ModMenuTypes;
import net.minecraft.client.gui.screens.MenuScreens;
Expand Down Expand Up @@ -39,6 +40,7 @@ public CulturalDelights()
ModBlocks.register(eventBus);
ModBlockEntities.register(eventBus);
ModMenuTypes.register(eventBus);
ModRecipes.register(eventBus);

eventBus.addListener(this::setup);
eventBus.addListener(this::clientSetup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ncpbails.culturaldelights.block.entity.ModBlockEntities;
import com.ncpbails.culturaldelights.item.ModItems;
import com.ncpbails.culturaldelights.recipe.BambooMatRecipe;
import com.ncpbails.culturaldelights.screen.BambooMatMenu;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand All @@ -14,6 +15,7 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.PotionUtils;
Expand All @@ -31,6 +33,7 @@
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nonnull;
import java.util.Optional;
import java.util.Random;

public class BambooMatBlockEntity extends BlockEntity implements MenuProvider {
Expand All @@ -44,8 +47,32 @@ protected void onContentsChanged(int slot) {

private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();

protected final ContainerData data;
private int progress = 0;
private int maxProgress = 72;

public BambooMatBlockEntity(BlockPos pWorldPosition, BlockState pBlockState) {
super(ModBlockEntities.BAMBOO_MAT_BLOCK_ENTITY.get(), pWorldPosition, pBlockState);
this.data = new ContainerData() {
public int get(int index) {
switch (index) {
case 0: return BambooMatBlockEntity.this.progress;
case 1: return BambooMatBlockEntity.this.maxProgress;
default: return 0;
}
}

public void set(int index, int value) {
switch(index) {
case 0: BambooMatBlockEntity.this.progress = value; break;
case 1: BambooMatBlockEntity.this.maxProgress = value; break;
}
}

public int getCount() {
return 2;
}
};
}

@Override
Expand All @@ -56,7 +83,7 @@ public Component getDisplayName() {
@Nullable
@Override
public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory, Player pPlayer) {
return new BambooMatMenu(pContainerId, pInventory, this);
return new BambooMatMenu(pContainerId, pInventory, this, this.data);
}

@Nonnull
Expand Down Expand Up @@ -84,13 +111,15 @@ public void invalidateCaps() {
@Override
protected void saveAdditional(@NotNull CompoundTag tag) {
tag.put("inventory", itemHandler.serializeNBT());
tag.putInt("bamboo_mat.progress", progress);
super.saveAdditional(tag);
}

@Override
public void load(CompoundTag nbt) {
super.load(nbt);
itemHandler.deserializeNBT(nbt.getCompound("inventory"));
progress = nbt.getInt("bamboo_mat.progress");
}

public void drops() {
Expand All @@ -104,30 +133,73 @@ public void drops() {


public static void tick(Level pLevel, BlockPos pPos, BlockState pState, BambooMatBlockEntity pBlockEntity) {
if(hasRecipe(pBlockEntity) && hasNotReachedStackLimit(pBlockEntity)) {
craftItem(pBlockEntity);
if(hasRecipe(pBlockEntity)) {
pBlockEntity.progress++;
setChanged(pLevel, pPos, pState);
if(pBlockEntity.progress > pBlockEntity.maxProgress) {
craftItem(pBlockEntity);
}
} else {
pBlockEntity.resetProgress();
setChanged(pLevel, pPos, pState);
}
}

private static boolean hasRecipe(BambooMatBlockEntity entity) {
Level level = entity.level;
SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots());
for (int i = 0; i < entity.itemHandler.getSlots(); i++) {
inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
}

Optional<BambooMatRecipe> match = level.getRecipeManager()
.getRecipeFor(BambooMatRecipe.Type.INSTANCE, inventory, level);

return match.isPresent() && canInsertAmountIntoOutputSlot(inventory)
&& canInsertItemIntoOutputSlot(inventory, match.get().getResultItem())
&& hasWaterInWaterSlot(entity) && hasToolsInToolSlot(entity);
}

private static boolean hasWaterInWaterSlot(BambooMatBlockEntity entity) {
return PotionUtils.getPotion(entity.itemHandler.getStackInSlot(0)) == Potions.WATER;
}

private static boolean hasToolsInToolSlot(BambooMatBlockEntity entity) {
return entity.itemHandler.getStackInSlot(2).getItem() == Items.IRON_AXE;
}

private static void craftItem(BambooMatBlockEntity entity) {
entity.itemHandler.extractItem(0, 1, false);
entity.itemHandler.extractItem(1, 1, false);
entity.itemHandler.getStackInSlot(2).hurt(1, new Random(), null);
Level level = entity.level;
SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots());
for (int i = 0; i < entity.itemHandler.getSlots(); i++) {
inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
}

Optional<BambooMatRecipe> match = level.getRecipeManager()
.getRecipeFor(BambooMatRecipe.Type.INSTANCE, inventory, level);

if(match.isPresent()) {
entity.itemHandler.extractItem(0,1, false);
entity.itemHandler.extractItem(1,1, false);
entity.itemHandler.getStackInSlot(2).hurt(1, new Random(), null);

entity.itemHandler.setStackInSlot(3, new ItemStack(ModItems.AVOCADO_TOAST.get(),
entity.itemHandler.getStackInSlot(3).getCount() + 1));
entity.itemHandler.setStackInSlot(3, new ItemStack(match.get().getResultItem().getItem(),
entity.itemHandler.getStackInSlot(3).getCount() + 1));

entity.resetProgress();
}
}

private static boolean hasRecipe(BambooMatBlockEntity entity) {
boolean hasItemInWaterSlot = PotionUtils.getPotion(entity.itemHandler.getStackInSlot(0)) == Potions.WATER;
boolean hasItemInFirstSlot = entity.itemHandler.getStackInSlot(1).getItem() == ModItems.AVOCADO.get();
boolean hasItemInSecondSlot = entity.itemHandler.getStackInSlot(2).getItem() == Items.IRON_AXE;
private void resetProgress() {
this.progress = 0;
}

return hasItemInWaterSlot && hasItemInFirstSlot && hasItemInSecondSlot;
private static boolean canInsertItemIntoOutputSlot(SimpleContainer inventory, ItemStack output) {
return inventory.getItem(3).getItem() == output.getItem() || inventory.getItem(3).isEmpty();
}

private static boolean hasNotReachedStackLimit(BambooMatBlockEntity entity) {
return entity.itemHandler.getStackInSlot(3).getCount() < entity.itemHandler.getStackInSlot(3).getMaxStackSize();
private static boolean canInsertAmountIntoOutputSlot(SimpleContainer inventory) {
return inventory.getItem(3).getMaxStackSize() > inventory.getItem(3).getCount();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ncpbails.culturaldelights.event;

import com.ncpbails.culturaldelights.CulturalDelights;
import com.ncpbails.culturaldelights.recipe.BambooMatRecipe;
import net.minecraft.core.Registry;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = CulturalDelights.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModEventBusEvents {

@SubscribeEvent
public static void registerRecipeTypes(final RegistryEvent.Register<RecipeSerializer<?>> event) {
Registry.register(Registry.RECIPE_TYPE, BambooMatRecipe.Type.ID, BambooMatRecipe.Type.INSTANCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.ncpbails.culturaldelights.recipe;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ncpbails.culturaldelights.CulturalDelights;
import net.minecraft.core.NonNullList;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;

import javax.annotation.Nullable;

public class BambooMatRecipe implements Recipe<SimpleContainer> {

private final ResourceLocation id;
private final ItemStack output;
private final NonNullList<Ingredient> recipeItems;

public BambooMatRecipe(ResourceLocation id, ItemStack output,
NonNullList<Ingredient> recipeItems) {
this.id = id;
this.output = output;
this.recipeItems = recipeItems;
}

@Override
public boolean matches(SimpleContainer pContainer, Level pLevel) {
return recipeItems.get(0).test(pContainer.getItem(1));
}

@Override
public ItemStack assemble(SimpleContainer p_44001_) {
return output;
}

@Override
public boolean canCraftInDimensions(int p_43999_, int p_44000_) {
return true;
}

@Override
public ItemStack getResultItem() {
return output.copy();
}

@Override
public ResourceLocation getId() {
return id;
}

@Override
public RecipeSerializer<?> getSerializer() {
return Serializer.INSTANCE;
}

@Override
public RecipeType<?> getType() {
return Type.INSTANCE;
}

public static class Type implements RecipeType<BambooMatRecipe> {
private Type() { }
public static final Type INSTANCE = new Type();
public static final String ID = "mat_rolling";
}

public static class Serializer implements RecipeSerializer<BambooMatRecipe> {
public static final Serializer INSTANCE = new Serializer();
public static final ResourceLocation ID =
new ResourceLocation(CulturalDelights.MOD_ID,"mat_rolling");

@Override
public BambooMatRecipe fromJson(ResourceLocation id, JsonObject json) {
ItemStack output = ShapedRecipe.itemStackFromJson(GsonHelper.getAsJsonObject(json, "output"));

JsonArray ingredients = GsonHelper.getAsJsonArray(json, "ingredients");
NonNullList<Ingredient> inputs = NonNullList.withSize(1, Ingredient.EMPTY);

for (int i = 0; i < inputs.size(); i++) {
inputs.set(i, Ingredient.fromJson(ingredients.get(i)));
}

return new BambooMatRecipe(id, output, inputs);
}

@Override
public BambooMatRecipe fromNetwork(ResourceLocation id, FriendlyByteBuf buf) {
NonNullList<Ingredient> inputs = NonNullList.withSize(buf.readInt(), Ingredient.EMPTY);

for (int i = 0; i < inputs.size(); i++) {
inputs.set(i, Ingredient.fromNetwork(buf));
}

ItemStack output = buf.readItem();
return new BambooMatRecipe(id, output, inputs);
}

@Override
public void toNetwork(FriendlyByteBuf buf, BambooMatRecipe recipe) {
buf.writeInt(recipe.getIngredients().size());
for (Ingredient ing : recipe.getIngredients()) {
ing.toNetwork(buf);
}
buf.writeItemStack(recipe.getResultItem(), false);
}

@Override
public RecipeSerializer<?> setRegistryName(ResourceLocation name) {
return INSTANCE;
}

@Nullable
@Override
public ResourceLocation getRegistryName() {
return ID;
}

@Override
public Class<RecipeSerializer<?>> getRegistryType() {
return Serializer.castClass(RecipeSerializer.class);
}

@SuppressWarnings("unchecked") // Need this wrapper, because generics
private static <G> Class<G> castClass(Class<?> cls) {
return (Class<G>)cls;
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/ncpbails/culturaldelights/recipe/ModRecipes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ncpbails.culturaldelights.recipe;

import com.ncpbails.culturaldelights.CulturalDelights;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public class ModRecipes {

public static final DeferredRegister<RecipeSerializer<?>> SERIALIZERS =
DeferredRegister.create(ForgeRegistries.RECIPE_SERIALIZERS, CulturalDelights.MOD_ID);


public static final RegistryObject<RecipeSerializer<BambooMatRecipe>> MAT_ROLLING_SERIALIZER =
SERIALIZERS.register("mat_rolling", () -> BambooMatRecipe.Serializer.INSTANCE);


public static void register(IEventBus eventBus) {
SERIALIZERS.register(eventBus);
}
}
Loading

0 comments on commit d112da8

Please sign in to comment.