Question Trying to interfere with Water bottle filling

Kamisenin

New member
Jan 16, 2024
6
0
1
Hi ! Its my first time on the platform so if i did something wrong don't hesistate to tell me. And also sorry if i'm not clear, english is not my mother tongue (first language)

So i'm working on a realistic thirst plugin and as of right know i'm trying to do so that when a player tries to fill a glass bottle, if the block he is aiming at (or the player) is in a Ocean Type biome, it will fill the water bottle as an Awkward potion instead. I've got some clues on how to do it but all of them dont seem to work and i'm really struggling to do any thing more. (Note that the 3 ideas dont work don't work and that i'm using paper 1.20.4) i was wondering then if someone could help me with that and lead me on the best way to do from these 3 ideas or other

public class PlayerEvent implements Listener {


First Idea
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Inventory clickedInventory = event.getClickedInventory();

if (clickedInventory != null && event.getSlotType() == InventoryType.SlotType.RESULT) {
Player player = (Player) event.getWhoClicked();
ItemStack cursor = event.getCursor();
ItemStack currentItem = event.getCurrentItem();

// Verify if GLASS_BOTTLE is missing and replacing it with POTION

if (currentItem != null && currentItem.getType() == Material.GLASS_BOTTLE &&
cursor != null && cursor.getType() == Material.POTION) {

player.sendMessage("Vous avez rempli une bouteille d'eau !");
}
}
}
Second idea
@EventHandler
public void PlayerPickWater(PlayerInventorySlotChangeEvent event){
Player player = event.getPlayer();

ItemStack OldSlot = event.getOldItemStack();

ItemStack NewSlot = event.getNewItemStack();

if (OldSlot.getType()==Material.GLASS_BOTTLE && NewSlot.subtract()==OldSlot ){

}
}

Third and most complete idea
@EventHandler
public void PlayerPickWater(PlayerInteractEvent event) {

//verify if the player rightclicked on a block with a empty GLASS_BOTTLE

if (event.getAction().toString().contains("RIGHT") && event.getItem() != null && event.getItem().getType() == Material.GLASS_BOTTLE) {
Player player = event.getPlayer();

// verify if the bloc the player clicked on is water

if (event.getClickedBlock().getType()==Material.WATER) { //make an error when i'm iteracting with Water as if it is null, error : nullpointerexception

// Get the biome of the block
Biome biome = event.getClickedBlock().getBiome();

// Create waterbottle
ItemStack waterBottle = new ItemStack(Material.POTION);
PotionMeta potionMeta = (PotionMeta) waterBottle.getItemMeta();

// verify if this is a biome ocean
if (biome == Biome.OCEAN || biome == Biome.DEEP_OCEAN || biome == Biome.DEEP_LUKEWARM_OCEAN || biome == Biome.LUKEWARM_OCEAN ||
biome == Biome.COLD_OCEAN || biome == Biome.DEEP_COLD_OCEAN || biome == Biome.DEEP_FROZEN_OCEAN || biome == Biome.WARM_OCEAN || biome == Biome.FROZEN_OCEAN ||
biome == Biome.BEACH || biome == Biome.SNOWY_BEACH || biome == Biome.STONY_SHORE || biome == Biome.SWAMP || biome == Biome.MANGROVE_SWAMP){
// if biome ocean, set potion type as Awkward
potionMeta.setBasePotionData(new PotionData(PotionType.AWKWARD));
potionMeta.setDisplayName("Bouteille d'eau Salée");
} else {
// Else defines the bottle a simple water bottle
potionMeta.setBasePotionData(new PotionData(PotionType.WATER));
}

// Apply metadatas of the potion to the bottle
waterBottle.setItemMeta(potionMeta);

// Removes an empty glass bottle from the inventory of the player

ItemStack emptyBottle = new ItemStack(Material.GLASS_BOTTLE);
player.getInventory().removeItem(emptyBottle);


// Add the new bottle to the player
player.getInventory().addItem(waterBottle);

// Cancel the event to prevent it from filling the bottle with normal water
event.setCancelled(true);
}
}
}
 
Version Output
1.20.4
Last edited: