Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.proc>full</maven.compiler.proc>
<interactiblefurniture.version>0.2.0</interactiblefurniture.version>
<rpcharacters.version>2.0.1</rpcharacters.version>
<rpcharacters.version>2.16.0</rpcharacters.version>
<simplefactions.version>3.0.1</simplefactions.version>
<tlibs.version>2.1.0</tlibs.version>
<tlibs.version>2.3.0</tlibs.version>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
</properties>

<dependencyManagement>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/tfminecraft/cooking/Cooking.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@
import net.tfminecraft.cooking.oven.OvenHandler;
import net.tfminecraft.cooking.oven.OvenLifecycleHandler;
import net.tfminecraft.tlibs.itemscan.ItemScanService;
import net.tfminecraft.tlibs.character.Characters;

public class Cooking extends JavaPlugin {

public static Cooking plugin;
private boolean incompatibleCharacterApi;

private final CommandManager commands = new CommandManager();

Expand Down Expand Up @@ -133,6 +135,15 @@ public void onLoad() {

@Override
public void onEnable() {
try {
// Registration may arrive later; this checks API compatibility only.
Characters.available();
} catch (LinkageError error) {
incompatibleCharacterApi = true;
getLogger().severe("Cooking requires TLibs 2.3.0 or newer; disabling.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
plugin = this;

createFolders();
Expand All @@ -158,6 +169,7 @@ public void onEnable() {
meatHookHandler.resumeLoadedHooks();
SaturationGuard.start();
NutritionDrainTask.start();
Bukkit.getOnlinePlayers().forEach(NutritionLifecycleListener::initializeCurrent);
HusbandryLifecycleListener.resumeLoadedWorlds();
HusbandryTickTask.start();
HusbandryLocator.scanUnloaded();
Expand All @@ -177,6 +189,7 @@ public void onEnable() {

@Override
public void onDisable() {
if (incompatibleCharacterApi) return;
SaturationGuard.stop();
NutritionDrainTask.stop();
HusbandryTickTask.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import net.tfminecraft.cooking.utils.PlayerActionContext;
import org.bukkit.Location;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -103,17 +103,17 @@ private void armHarvest(Location location, CropDefinition crop, Player player) {
}

private void scheduleInventoryRewrite(Player player, CropDefinition crop, int quality) {
UUID uuid = player.getUniqueId();
PlayerActionContext context = PlayerActionContext.capture(player);
if (context == null) return;
Location dropLocation = player.getLocation().clone();
ItemStack[] before = cloneStorage(player.getInventory());
Cooking.plugin.getServer().getScheduler().runTaskLater(Cooking.plugin, () -> {
Player online = Bukkit.getPlayer(uuid);
if (online == null || !online.isOnline()) {
return;
}
PlayerInventory inventory = online.getInventory();
if (!context.isCurrent()) return;
PlayerInventory inventory = player.getInventory();
ItemStack[] now = inventory.getStorageContents();
int length = Math.min(before.length, now.length);
java.util.List<ItemStack> additions = new java.util.ArrayList<>();
Map<Integer, ItemStack> replacements = new java.util.LinkedHashMap<>();
for (int i = 0; i < length; i++) {
if (sameStack(before[i], now[i])) {
continue;
Expand All @@ -129,13 +129,15 @@ private void scheduleInventoryRewrite(Player player, CropDefinition crop, int qu
ItemStack rewritten = CropHarvestItems.rewriteCustomDrop(candidate, crop, quality);
if (rewritten == null || rewritten == candidate || rewritten.getType().isAir()) continue;
// Take the harvest back out and add it as food, so it joins produce that only aged differently.
inventory.setItem(i, grewExisting ? before[i].clone() : null);
replacements.put(i, grewExisting ? before[i].clone() : null);
additions.add(rewritten);
}
if (!context.isCurrent()) return;
replacements.forEach(inventory::setItem);
for (ItemStack addition : additions) {
ItemStack leftover = InventoryAdder.addItem(online, addition);
ItemStack leftover = context.isCurrent() ? InventoryAdder.addItem(player, addition) : addition;
if (leftover != null) {
online.getWorld().dropItemNaturally(online.getLocation(), leftover);
dropLocation.getWorld().dropItemNaturally(dropLocation, leftover);
}
}
}, 1L);
Expand Down
53 changes: 32 additions & 21 deletions src/main/java/net/tfminecraft/cooking/cup/DrinkConsumeListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.tfminecraft.cooking.cup;

import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

import org.bukkit.Bukkit;
Expand All @@ -19,6 +20,7 @@
import net.tfminecraft.cooking.item.FoodItem;
import net.tfminecraft.cooking.nutrition.NutritionService;
import net.tfminecraft.cooking.utils.InventoryAdder;
import net.tfminecraft.cooking.utils.PlayerActionContext;

public final class DrinkConsumeListener implements Listener {

Expand All @@ -32,16 +34,18 @@ public void onMilkBucket(PlayerItemConsumeEvent event) {
event.setCancelled(true);

Player player = event.getPlayer();
PlayerActionContext context = PlayerActionContext.capture(player);
if (context == null) {
return;
}
FoodItem food = FoodItem.fromItem(consumed);
if (food != null && "milk_bucket".equalsIgnoreCase(food.getId())) {
food.updateAge();
if (food != null) food.updateAge();
if (!context.isCurrent()) return;
// This event is cancelled, so spend the bucket before nutrition callbacks.
if (player.getGameMode() != GameMode.CREATIVE && !emptyDrunkBucket(player, event.getHand())) return;
if (context.isCurrent() && food != null && "milk_bucket".equalsIgnoreCase(food.getId())) {
NutritionService.tryApplyEat(player, food);
}

if (player.getGameMode() == GameMode.CREATIVE) {
return;
}
emptyDrunkBucket(player, event.getHand());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand All @@ -58,19 +62,23 @@ public void onDrink(PlayerItemConsumeEvent event) {
}

Player player = event.getPlayer();

if (milk) {
FoodItem food = FoodItem.fromItem(consumed);
if (food != null) {
food.updateAge();
NutritionService.tryApplyEat(player, food);
}
PlayerActionContext context = PlayerActionContext.capture(player);
if (context == null) {
return;
}

FoodItem food = milk ? FoodItem.fromItem(consumed) : null;
if (food != null) food.updateAge();

int amountBefore = consumed.getAmount();
EquipmentSlot hand = event.getHand() == EquipmentSlot.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND;
int slot = hand == EquipmentSlot.OFF_HAND ? -1 : player.getInventory().getHeldItemSlot();
Bukkit.getScheduler().runTask(Cooking.plugin, () -> replaceWithEmptyCup(player, amountBefore, slot));
Bukkit.getScheduler().runTask(Cooking.plugin, () -> {
if (!event.isCancelled() && context.isCurrent()) {
replaceWithEmptyCup(player, amountBefore, slot, CupItems::emptyCup, context::isCurrent);
if (context.isCurrent() && food != null) NutritionService.tryApplyEat(player, food);
}
});
}

/**
Expand All @@ -79,11 +87,12 @@ public void onDrink(PlayerItemConsumeEvent event) {
* must neither leave the bottle and add a cup, nor overwrite whatever is held instead. Cups
* are potions, so an empty slot means the bottle was moved away, or nothing was used up.
*/
static void replaceWithEmptyCup(Player player, int amountBefore, int slot) {
replaceWithEmptyCup(player, amountBefore, slot, CupItems::emptyCup);
static void replaceWithEmptyCup(Player player, int amountBefore, int slot, Supplier<ItemStack> emptyCup) {
replaceWithEmptyCup(player, amountBefore, slot, emptyCup, () -> true);
}

static void replaceWithEmptyCup(Player player, int amountBefore, int slot, Supplier<ItemStack> emptyCup) {
private static void replaceWithEmptyCup(Player player, int amountBefore, int slot,
Supplier<ItemStack> emptyCup, BooleanSupplier current) {
if (amountBefore > 1) {
return;
}
Expand All @@ -93,14 +102,15 @@ static void replaceWithEmptyCup(Player player, int amountBefore, int slot, Suppl
return;
}
ItemStack empty = emptyCup.get();
if (!current.getAsBoolean()) return;
if (slot < 0) {
inv.setItemInOffHand(empty);
} else {
inv.setItem(slot, empty);
}
}

private static void emptyDrunkBucket(Player player, EquipmentSlot hand) {
private static boolean emptyDrunkBucket(Player player, EquipmentSlot hand) {
PlayerInventory inv = player.getInventory();
EquipmentSlot slot = hand != null ? hand : EquipmentSlot.HAND;
ItemStack used = inv.getItem(slot);
Expand All @@ -114,7 +124,7 @@ private static void emptyDrunkBucket(Player player, EquipmentSlot hand) {
slot = EquipmentSlot.OFF_HAND;
used = off;
} else {
return;
return false;
}
}
int amount = used.getAmount();
Expand All @@ -124,8 +134,9 @@ private static void emptyDrunkBucket(Player player, EquipmentSlot hand) {
if (leftover != null && leftover.getAmount() > 0 && player.getWorld() != null) {
player.getWorld().dropItemNaturally(player.getLocation(), leftover);
}
return;
return true;
}
inv.setItem(slot, BucketItems.empty());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.tfminecraft.cooking.quality.OriginQualityResolver;
import net.tfminecraft.cooking.utils.FoodParser;
import net.tfminecraft.cooking.utils.ItemBuilder;
import net.tfminecraft.cooking.utils.PlayerActionContext;

public final class MilkBucketConverter implements Listener {

Expand Down Expand Up @@ -70,13 +71,20 @@ public void onBucketFill(PlayerBucketFillEvent event) {
}

Player player = event.getPlayer();
PlayerActionContext context = PlayerActionContext.capture(player);
if (context == null) {
return;
}
ItemStack filled = event.getItemStack();
ItemStack converted = convert(player, filled);
if (converted == null || converted.equals(filled)) {
if (converted == null || converted.equals(filled) || !context.isCurrent()) {
return;
}

Bukkit.getScheduler().runTask(Cooking.plugin, () -> {
if (!context.isCurrent()) {
return;
}
ItemStack main = player.getInventory().getItemInMainHand();
if (main != null && main.getType() == Material.MILK_BUCKET && FoodItem.fromItem(main) == null) {
converted.setAmount(main.getAmount());
Expand Down
Loading
Loading