Skip to content

Commit

Permalink
added to BrewingKegScreen shows a buggy tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Jul 8, 2024
1 parent 1ecca88 commit 78c9f34
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 81 deletions.
File renamed without changes
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DrinkBottleBlock {
public static final Block WINE_BOTTLE = registerBlock("wine_bottle", new BottleBlock(), new FoodComponent.Builder()
.hunger(6).saturationModifier(0.6f)
.statusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 3 * 20, 3), 1.0f)
.alwaysEdible()
.build());

private static Block registerBlock(String name, Block block, FoodComponent foodComponent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class BrewingKegBlock extends BlockWithEntity implements BlockEntityProvi
public static final int DRINKS_DISPLAY_SLOT = 8;
public static final int OUTPUT_SLOT = 9;

public static final int REQUIRE_CONTAINER = 10;


private static final VoxelShape SHAPE = Block.createCuboidShape(0, 0, 0, 16, 12, 16);
public static final DirectionProperty FACING;
Expand Down Expand Up @@ -95,30 +97,31 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt
if (blockEntity instanceof BrewingKegBlockEntity) {
BrewingKegBlockEntity brewingKegEntity = (BrewingKegBlockEntity) blockEntity;

// Create an item stack with the block's item
ItemStack itemStack = new ItemStack(this.asItem());
// Save the ItemStack in REQUIRE_CONTAINER slot to block entity NBT
NbtCompound tag = new NbtCompound();
ItemStack requireContainerStack = brewingKegEntity.getStack(REQUIRE_CONTAINER);
if (!requireContainerStack.isEmpty()) {
NbtCompound requireContainerTag = new NbtCompound();
requireContainerStack.writeNbt(requireContainerTag);
tag.put("requireContainer", requireContainerTag);
}

// Save other necessary data to the tag if needed
// Example: Save fluid variant and amount
tag.put("fluid_variant", brewingKegEntity.fluidStorage.variant.toNbt());
tag.putLong("fluid_amount", brewingKegEntity.fluidStorage.amount);

brewingKegEntity.writeToNbtPublic(tag); // Save updated NBT to block entity

// Save BlockEntity data to NBT
NbtCompound itemNbt = brewingKegEntity.writeToNbtPublic(new NbtCompound()); // Use the public method to get NBT
itemStack.setSubNbt("BlockEntityTag", itemNbt);
// Spawn the item entity with the Block's item and custom NBT
ItemStack itemStack = new ItemStack(this.asItem());
itemStack.setSubNbt("BlockEntityTag", tag);

// Save DRINKS_DISPLAY_SLOT to NBT and remove from block entity
NbtCompound displaySlotTag = new NbtCompound();
brewingKegEntity.getStack(DRINKS_DISPLAY_SLOT).writeNbt(displaySlotTag);
itemStack.setSubNbt("DisplaySlot", displaySlotTag);

// Drop items from INGREDIENT_SLOTS (0-7) and WATER_SLOT (9)
for (int slot : BrewingKegBlockEntity.INGREDIENT_SLOTS) {
if (slot != BrewingKegBlockEntity.WATER_SLOT) { // Skip WATER_SLOT
dropSlotContents(world, pos, brewingKegEntity, slot);
}
}
dropSlotContents(world, pos, brewingKegEntity, BrewingKegBlockEntity.WATER_SLOT);

// Drop the OUTPUT_SLOT
dropSlotContents(world, pos, brewingKegEntity, BrewingKegBlockEntity.OUTPUT_SLOT);

// Spawn the item entity with the BlockEntityTag and DisplaySlot NBT
ItemEntity itemEntity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), itemStack);
world.spawnEntity(itemEntity);
}
Expand All @@ -128,6 +131,7 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt
}
}


private void dropSlotContents(World world, BlockPos pos, BrewingKegBlockEntity brewingKegEntity, int slot) {
ItemStack stack = brewingKegEntity.getStack(slot);
if (!stack.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,15 @@
import java.util.*;

public class BrewingKegBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory, ImplementedInventory {
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(10, ItemStack.EMPTY);
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(INVENTORY_SIZE, ItemStack.EMPTY);


//adds 6 input ingredients slot
public static final int[] INGREDIENT_SLOTS = {0, 1, 2, 3, 4, 5};
//adds container(like bottle of vial) input
public static final int CONTAINER_SLOT = 6;
//adds 1 input slot for water
public static final int WATER_SLOT = 7;
//adds 1 output slot and display slot
public static final int DRINKS_DISPLAY_SLOT = 8;
public static final int OUTPUT_SLOT = 9;
public static final int REQUIRE_CONTAINER = 7;
public static final int WATER_SLOT = 8;
public static final int DRINKS_DISPLAY_SLOT = 9;
public static final int OUTPUT_SLOT = 10;

public static final int INVENTORY_SIZE = OUTPUT_SLOT + 1;

Expand Down Expand Up @@ -187,13 +184,19 @@ public NbtCompound writeToNbtPublic(NbtCompound tag) {
this.getStack(DRINKS_DISPLAY_SLOT).writeNbt(displaySlotTag);
tag.put("DisplaySlot", displaySlotTag);

// Save REQUIRE_CONTAINER to NBT
NbtCompound requireContainerTag = new NbtCompound();
this.getStack(REQUIRE_CONTAINER).writeNbt(requireContainerTag);
tag.put("requireContainer", requireContainerTag);

// Save water amount
tag.putLong("fluid_amount", fluidStorage.amount);
tag.put("fluid_variant", fluidStorage.variant.toNbt());

// Save container
tag.put(CompoundTagUtils.TAG_KEY_CONTAINER, drinkContainer.writeNbt(new NbtCompound()));


// Save experience
tag.putFloat("TotalExperience", totalExperience);

Expand All @@ -202,12 +205,12 @@ public NbtCompound writeToNbtPublic(NbtCompound tag) {
}


public ItemStack getMeal() {
public ItemStack getDrink() {
return getStack(DRINKS_DISPLAY_SLOT);
}

public NbtCompound writeMeal(NbtCompound tag) {
if (getMeal().isEmpty()) {
public NbtCompound writeDrink(NbtCompound tag) {
if (getDrink().isEmpty()) {
return tag;
}

Expand Down Expand Up @@ -265,6 +268,14 @@ protected void writeNbt(NbtCompound tag) {
}
tag.put("DisplaySlot", displaySlotTag);

// Save requireContainer
NbtCompound requireContainerTag = new NbtCompound();
ItemStack requireContainer = getStack(REQUIRE_CONTAINER);
if (!requireContainer.isEmpty()) {
requireContainer.writeNbt(requireContainerTag);
}
tag.put("requireContainer", requireContainerTag);

// Save totalExperience
tag.putFloat("TotalExperience", totalExperience);
}
Expand Down Expand Up @@ -299,6 +310,14 @@ public void readNbt(NbtCompound tag) {
} else {
setStack(DRINKS_DISPLAY_SLOT, ItemStack.EMPTY); // Ensure it's clear if no DisplaySlot is found
}
// Load requireContainer
if (tag.contains("requireContainer", NbtType.COMPOUND)) {
NbtCompound requireContainerTag = tag.getCompound("requireContainer");
setStack(REQUIRE_CONTAINER, ItemStack.fromNbt(requireContainerTag));
} else {
setStack(REQUIRE_CONTAINER, ItemStack.EMPTY);
}

}


Expand Down Expand Up @@ -570,10 +589,14 @@ private boolean craftItem(BrewingRecipe recipe, int craftedAmount) {
if (this.progress < this.maxProgress) {
return false;
} else {
this.progress = 0;
drinkContainer = recipe.getContainer().copy(); // Ensure a copy is made to avoid side effects
this.progress = 0;; // Ensure a copy is made to avoid side effects
ItemStack recipeOutput = recipe.craft(this, this.world.getRegistryManager());
ItemStack currentOutput = this.getStack(DRINKS_DISPLAY_SLOT);
drinkContainer = recipe.getContainer().copy();
// Replace REQUIRE_CONTAINER with item from drinkContainer
ItemStack requireContainerItem = this.getStack(REQUIRE_CONTAINER);
requireContainerItem = recipe.getContainer().copy();
this.setStack(REQUIRE_CONTAINER, requireContainerItem);

if (currentOutput.isEmpty()) {
this.setStack(DRINKS_DISPLAY_SLOT, recipeOutput.copy());
Expand Down Expand Up @@ -603,6 +626,8 @@ private boolean craftItem(BrewingRecipe recipe, int craftedAmount) {
}
}



private void saveLastCraftedRecipe(BrewingRecipe recipe) {
// Assuming recipe.getId() gives the unique ID of the recipe
String recipeId = recipe.getId().toString(); // Convert ID to String if necessary
Expand Down Expand Up @@ -634,8 +659,8 @@ private void handleRecipeRemainder(BrewingRecipe recipe) {



private boolean doesDrinkHaveContainer(ItemStack meal) {
return !drinkContainer.isEmpty() || meal.getItem().hasRecipeRemainder();
private boolean doesDrinkHaveContainer(ItemStack drink) {
return !drinkContainer.isEmpty() || drink.getItem().hasRecipeRemainder();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import com.megatrex4.ukrainian_dlight.block.entity.BrewingKegBlockEntity;
import com.mojang.blaze3d.systems.RenderSystem;
import com.nhoryzon.mc.farmersdelight.FarmersDelightMod;
import com.nhoryzon.mc.farmersdelight.entity.block.CookingPotBlockEntity;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.item.TooltipContext;
Expand All @@ -25,6 +23,15 @@ public class BrewingKegScreen extends HandledScreen<BrewingKegScreenHandler> {
private static final Identifier TEXTURE = new Identifier(UkrainianDelight.MOD_ID, "textures/gui/brewing_keg_gui.png");


public static final int[] INGREDIENT_SLOTS = {0, 1, 2, 3, 4, 5};
public static final int CONTAINER_SLOT = 6;
public static final int REQUIRE_CONTAINER = 7;
public static final int WATER_SLOT = 8;
public static final int DRINKS_DISPLAY_SLOT = 9;
public static final int OUTPUT_SLOT = 10;

public static final int INVENTORY_SIZE = OUTPUT_SLOT + 1;


public BrewingKegScreen(BrewingKegScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
Expand Down Expand Up @@ -70,54 +77,42 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);
super.render(context, mouseX, mouseY, delta);
drawMouseoverTankTooltip(context, mouseX, mouseY);
// drawMouseoverSlotTooltip(context, mouseX, mouseY);
drawMouseoverTooltip(context, mouseX, mouseY);
}



@Override
protected void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY) {
if (this.handler.getCursorStack().isEmpty() && this.focusedSlot != null) {
if (this.focusedSlot.id == BrewingKegBlockEntity.DRINKS_DISPLAY_SLOT && this.focusedSlot.hasStack()) {
// Check if the focused slot is DRINKS_DISPLAY_SLOT (slot ID 8)
if (this.focusedSlot.id == DRINKS_DISPLAY_SLOT && this.focusedSlot.hasStack()) {
List<Text> tooltip = new ArrayList<>();

ItemStack drink = focusedSlot.getStack();
Text text = drink.getName();
if (text instanceof MutableText mutableName) {
// Get the item in DRINKS_DISPLAY_SLOT (slot ID 8)
ItemStack drink = this.focusedSlot.getStack();
Text drinkText = drink.getName();
if (drinkText instanceof MutableText mutableName) {
tooltip.add(mutableName.formatted(drink.getRarity().formatting));
} else {
tooltip.add(text);
tooltip.add(drinkText);
}
drink.getItem().appendTooltip(drink, handler.blockEntity.getWorld(), tooltip, TooltipContext.Default.BASIC);

String container = ""; // Declare container outside of the if block

// Retrieve path for last crafted recipe
String lastCraftedRecipe = handler.blockEntity.getLastCraftedRecipeId();

// Retrieve container item based on last crafted recipe
ItemStack containerItem = handler.blockEntity.getItemFromLastCraft(lastCraftedRecipe);
// Get the item in REQUIRE_CONTAINER (slot ID 10)
ItemStack containerItem = handler.blockEntity.getStack(REQUIRE_CONTAINER);
String containerName = "";
if (!containerItem.isEmpty()) {
// Get the item from the ItemStack
Item item = containerItem.getItem();

// Get the translation key for the item
String translationKey = item.getTranslationKey();

// Get the localized name using Text.translatable
container = Text.translatable(translationKey).getString();
Item container = containerItem.getItem();
containerName = Text.translatable(container.getTranslationKey()).getString();
}

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

// Add debugging output
System.out.println("Tooltip container: " + container);
// Add the localized string with the container name
tooltip.add(UkrainianDelight.i18n("tooltip.slot_item", containerName).formatted(Formatting.GRAY));

// Draw the tooltip
context.drawTooltip(textRenderer, tooltip, mouseX, mouseY);
} else {
// Draw the typical tooltip for all other slots
context.drawItemTooltip(textRenderer, focusedSlot.getStack(), mouseX, mouseY);
context.drawItemTooltip(textRenderer, this.focusedSlot.getStack(), mouseX, mouseY);
}
}
}
Expand All @@ -126,11 +121,6 @@ protected void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY)








protected void drawMouseoverTankTooltip(DrawContext context, int mouseX, int mouseY) {
int x = (width - backgroundWidth) / 2;
int y = (height - backgroundHeight) / 2;
Expand Down Expand Up @@ -164,5 +154,4 @@ private List<Text> getTankTooltip() {
return List.of(UkrainianDelight.i18n("tooltip.tank_empty"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public class BrewingKegScreenHandler extends ScreenHandler {

public static final int[] INGREDIENT_SLOTS = {0, 1, 2, 3, 4, 5};
public static final int CONTAINER_SLOT = 6;
public static final int WATER_SLOT = 7;
public static final int DRINKS_DISPLAY_SLOT = 8;
public static final int OUTPUT_SLOT = 9;
public static final int REQUIRE_CONTAINER = 7;
public static final int WATER_SLOT = 8;
public static final int DRINKS_DISPLAY_SLOT = 9;

public static final int OUTPUT_SLOT = 10;

public static final int INVENTORY_SIZE = OUTPUT_SLOT + 1;

public FluidStack fluidStack;
private final Inventory tileEntity;
Expand All @@ -37,12 +41,12 @@ public long getCapacity() {
}

public BrewingKegScreenHandler(int syncId, PlayerInventory tileEntity, PacketByteBuf buf) {
this(syncId, tileEntity, tileEntity.player.getWorld().getBlockEntity(buf.readBlockPos()), new ArrayPropertyDelegate(10));
this(syncId, tileEntity, tileEntity.player.getWorld().getBlockEntity(buf.readBlockPos()), new ArrayPropertyDelegate(INVENTORY_SIZE));
}

public BrewingKegScreenHandler(int syncId, PlayerInventory playerInventory, BlockEntity blockEntity, PropertyDelegate arrayPropertyDelegate) {
super(ModScreenHandlers.BREWING_KEG_SCREEN_HANDLER, syncId);
checkSize(((Inventory) blockEntity), 10);
checkSize(((Inventory) blockEntity), INVENTORY_SIZE);

this.tileEntity = ((Inventory) blockEntity);
tileEntity.onOpen(playerInventory.player);
Expand All @@ -64,7 +68,20 @@ public BrewingKegScreenHandler(int syncId, PlayerInventory playerInventory, Bloc
this.addSlot(new Slot(tileEntity, CONTAINER_SLOT, 97, 59));
// Add water slot
this.addSlot(new Slot(tileEntity, WATER_SLOT, 30, 59));
// Add display slot
// Add require container slot
this.addSlot(new Slot(tileEntity, REQUIRE_CONTAINER, 10, 10) {
@Override
public boolean canInsert(ItemStack stack) {
return false;
}

@Override
public boolean canTakeItems(PlayerEntity playerEntity) {
return false;
}
});

// Add drinks display slot
this.addSlot(new Slot(tileEntity, DRINKS_DISPLAY_SLOT, 131, 28) {
@Override
public boolean canInsert(ItemStack stack) {
Expand Down Expand Up @@ -130,10 +147,10 @@ public ItemStack quickMove(PlayerEntity playerIn, int index) {
return ItemStack.EMPTY;
}
} else if (index > OUTPUT_SLOT) {
if (slotItemStack.getItem() == Items.BOWL &&
!this.insertItem(slotItemStack, DRINKS_DISPLAY_SLOT, OUTPUT_SLOT, false) ||
if (slotItemStack.getItem() == Items.GLASS_BOTTLE &&
!this.insertItem(slotItemStack, DRINKS_DISPLAY_SLOT, REQUIRE_CONTAINER, false) ||
!this.insertItem(slotItemStack, 0, 6, false) ||
!this.insertItem(slotItemStack, DRINKS_DISPLAY_SLOT, OUTPUT_SLOT, false)) {
!this.insertItem(slotItemStack, DRINKS_DISPLAY_SLOT, REQUIRE_CONTAINER, false)) {
return ItemStack.EMPTY;
}
} else if (index == WATER_SLOT && slotItemStack.getItem() == Items.WATER_BUCKET) {
Expand Down
5 changes: 1 addition & 4 deletions src/main/resources/assets/ukrainian_delight/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@
"ukrainian_delight.tooltip.water_amount": "%d / %d mB",
"ukrainian_delight.tooltip.tank_empty": "Empty",

"ukrainian_delight.tooltip.slot_empty": "Slot is empty",
"ukrainian_delight.tooltip.slot_item": "Poured into %s",

"ukrainian_delight.container.brewing_keg.served_on": "Served on %s"
"ukrainian_delight.tooltip.slot_item": "Poured into %s"

}

0 comments on commit 78c9f34

Please sign in to comment.