Skip to content

Commit

Permalink
new craft system
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Aug 6, 2024
1 parent d141429 commit 7e02b55
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,6 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt
}



// I refused to use this, everything will be better saved in the block inventory NBT data

// private void dropSlotContents(World world, BlockPos pos, BrewingKegBlockEntity brewingKegEntity, int slot) {
// ItemStack stack = brewingKegEntity.getStack(slot);
// if (!stack.isEmpty()) {
// ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), stack);
// brewingKegEntity.setStack(slot, ItemStack.EMPTY);
// }
// }

public static void spawnParticles(World world, BlockPos pos, BlockState state) {
Random random = world.random;
if (world != null) {
Expand All @@ -239,43 +228,43 @@ public static void spawnParticles(World world, BlockPos pos, BlockState state) {
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ItemStack heldStack = player.getStackInHand(hand);
BrewingKegBlockEntity blockEntity = (BrewingKegBlockEntity) world.getBlockEntity(pos);
if (!world.isClient() && world.getBlockEntity(pos) instanceof BrewingKegBlockEntity brewingKegBlockEntity) {
// Use the instance of BrewingKegBlockEntity to call the method
ItemStack serving = brewingKegBlockEntity.useHeldItemOnDrink(heldStack);

if (serving != ItemStack.EMPTY) {
if (!player.getInventory().insertStack(serving)) {
player.dropItem(serving, false);
}
world.playSound(null, pos, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, SoundCategory.BLOCKS, 1.f, 1.f);
return ActionResult.SUCCESS;
}
// if player held item water bucket add 1000 mb to brewingKegBlockEntity
if (heldStack.isOf(Items.WATER_BUCKET)) {
long waterAmount = blockEntity.getWaterAmount();
long waterCapacity = blockEntity.getWaterCapacity();
if (waterAmount + 1000 <= waterCapacity) {
blockEntity.addWater(1000);
if (!world.isClient) {
world.playSound(null, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
if (!player.isCreative()) {
player.setStackInHand(hand, new ItemStack(Items.BUCKET));
}
return ActionResult.SUCCESS;
}
} else {
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
if (screenHandlerFactory != null) {
player.openHandledScreen(screenHandlerFactory);

if (heldStack.isOf(Items.WATER_BUCKET)) {
long waterAmount = brewingKegBlockEntity.getWaterAmount();
long waterCapacity = brewingKegBlockEntity.getWaterCapacity();
if (waterAmount + 1000 <= waterCapacity) {
brewingKegBlockEntity.addWater(1000);
world.playSound(null, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
if (!player.isCreative()) {
player.setStackInHand(hand, new ItemStack(Items.BUCKET));
}
return ActionResult.SUCCESS;
}
} else {
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
if (screenHandlerFactory != null) {
// Open the screen handler and set the container if needed
player.openHandledScreen(screenHandlerFactory);
}
}
return ActionResult.SUCCESS;
}
return ActionResult.SUCCESS;
}




private boolean giveItemToPlayerOrDrop(World world, PlayerEntity player, ItemStack itemStack) {
if (!player.getInventory().insertStack(itemStack)) {
ItemEntity itemEntity = new ItemEntity(world, player.getX(), player.getY(), player.getZ(), itemStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.megatrex4.ukrainian_dlight.util.FluidStack;



import com.nhoryzon.mc.farmersdelight.entity.block.CookingPotBlockEntity;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
Expand Down Expand Up @@ -82,6 +82,8 @@ public class BrewingKegBlockEntity extends BlockEntity implements ExtendedScreen
private int maxProgress = 200; // Adjusted to match the brewing time in the JSON
private Text customName;
private ItemStack drinkContainer;
private ItemStack lastContainer;



@Override
Expand Down Expand Up @@ -128,9 +130,10 @@ public int get(int index) {

@Override
public void set(int index, int value) {
switch (index) {
case 0 -> BrewingKegBlockEntity.this.progress = value;
case 1 -> BrewingKegBlockEntity.this.maxProgress = value;
if (index == 0) {
BrewingKegBlockEntity.this.progress = value;
} else if (index == 1) {
BrewingKegBlockEntity.this.maxProgress = value;
}
}

Expand All @@ -141,6 +144,7 @@ public int size() {
};
}


public long getMaxWaterLevel() {
return fluidStorage.getCapacity();
}
Expand Down Expand Up @@ -208,8 +212,14 @@ public void writeNbt(NbtCompound tag) {
}
tag.put("DisplaySlot", displaySlotTag);

tag.put(CompoundTagUtils.TAG_KEY_CONTAINER, drinkContainer.writeNbt(new NbtCompound()));
System.out.println("BrewingKegBlockEntity: writeNbt drinkContainer: " + drinkContainer);
if (!drinkContainer.isEmpty()) {
NbtCompound containerTag = new NbtCompound();
drinkContainer.writeNbt(containerTag);
tag.put("DrinkContainer", containerTag);
System.out.println("BrewingKegBlockEntity: writeNbt drinkContainer: " + drinkContainer.getItem() + " " + drinkContainer.getName().getString());
} else {
System.out.println("BrewingKegBlockEntity: writeNbt drinkContainer: EMPTY");
}
writeInventoryNbt(tag);

NbtCompound compoundRecipes = new NbtCompound();
Expand All @@ -233,9 +243,14 @@ public void readNbt(NbtCompound tag) {
Inventories.readNbt(tag, inventory);
progress = tag.getInt("progress");

drinkContainer = ItemStack.fromNbt(tag.getCompound(CompoundTagUtils.TAG_KEY_CONTAINER));
// debug log drink container
System.out.println("BrewingKegBlockEntity: readNbt drinkContainer: " + drinkContainer);
if (tag.contains("DrinkContainer", NbtType.COMPOUND)) {
NbtCompound containerTag = tag.getCompound("DrinkContainer");
drinkContainer = ItemStack.fromNbt(containerTag);
System.out.println("BrewingKegBlockEntity: readNbt drinkContainer: " + drinkContainer.getItem() + " " + drinkContainer.getName().getString());
} else {
drinkContainer = ItemStack.EMPTY;
System.out.println("BrewingKegBlockEntity: readNbt drinkContainer: EMPTY");
}

// Load DRINKS_DISPLAY_SLOT
if (tag.contains("DisplaySlot", NbtType.COMPOUND)) {
Expand Down Expand Up @@ -333,11 +348,10 @@ public void tick(World world, BlockPos pos, BlockState state, BrewingKegBlockEnt

private boolean hasInput() {
for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) {
if (!getStack(i).isEmpty()) {
if (i != WATER_SLOT && i != CONTAINER_SLOT && !getStack(i).isEmpty()) {
return true;
}
}

return false;
}

Expand Down Expand Up @@ -385,7 +399,9 @@ private boolean processBrewing(BrewingRecipe recipe) {
}
trackRecipeExperience(recipe);
this.extractFluid(recipe.getWaterAmount());

for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) {
if (i == WATER_SLOT || i == CONTAINER_SLOT) continue;
ItemStack itemStack = getStack(i);
if (itemStack.getItem().hasRecipeRemainder() && world != null) {
Direction direction = getCachedState().get(BrewingKegBlock.FACING).rotateYCounterclockwise();
Expand Down Expand Up @@ -447,16 +463,26 @@ public ItemStack getDrinkContainer() {
}
}

private void setDrinkContainer(ItemStack container) {
this.drinkContainer = container;
markDirty(); // Ensure that the block entity state is updated
}



public ItemStack useHeldItemOnDrink(ItemStack container) {
if (isContainerValid(container) && !getDrink().isEmpty()) {
container.decrement(1);
return getDrink().split(1);
ItemStack drink = getDrink().split(1);
// Update the drink container to reflect the new state
setDrinkContainer(container);
return drink;
}
return ItemStack.EMPTY;
}




public void handleWaterBucket() {
ItemStack waterBucketStack = this.getStack(WATER_SLOT);

Expand Down Expand Up @@ -592,6 +618,15 @@ private void moveDrinkToOutput() {
}


public ItemStack getLastUsedContainer() {
// Ensure the container is updated correctly
return drinkContainer;
}





public void trackRecipeExperience(@Nullable Recipe<?> recipe) {
if (recipe != null) {
Identifier recipeID = recipe.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,37 +105,11 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
@Override
protected void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY) {
if (this.handler.getCursorStack().isEmpty() && this.focusedSlot != null && this.focusedSlot.hasStack()) {
if (focusedSlot.id == BrewingKegBlockEntity.DRINKS_DISPLAY_SLOT) {
List<Text> tooltip = new ArrayList<>();

ItemStack drink = focusedSlot.getStack();
Text text = drink.getName();
if (text instanceof MutableText mutableName) {
tooltip.add(mutableName.formatted(drink.getRarity().formatting));
} else {
tooltip.add(text);
}
drink.getItem().appendTooltip(drink, handler.blockEntity.getWorld(), tooltip, TooltipContext.Default.BASIC);

// get container name
ItemStack containerItem = this.handler.blockEntity.getDrinkContainer();
String container = !containerItem.isEmpty() ? containerItem.getItem().getName().getString() : "";

tooltip.add(UkrainianDelight.i18n("tooltip.poured", container).formatted(Formatting.GRAY));

context.drawTooltip(textRenderer, tooltip, mouseX, mouseY);
} else {
context.drawItemTooltip(textRenderer, focusedSlot.getStack(), mouseX, mouseY);
}
}
}







protected void drawMouseoverTankTooltip(DrawContext context, int mouseX, int mouseY) {
int x = (width - backgroundWidth) / 2;
int y = (height - backgroundHeight) / 2;
Expand Down

0 comments on commit 7e02b55

Please sign in to comment.