# Solved Creating a thirst integer?

Discussion in 'Plugin Development' started by valon750, Jan 19, 2013.

Thread Status:
Not open for further replies.
1. Offline

### valon750

Good evening, everyone on the Bukkits,

I'm trying to get in to my head, a method of setting up a thirst integer in game, not using anything such as levels or experience, but just a standard integer.

I was thinking, I should be able to have the initial integer of thirst at say, 100, then I'd need it to go down slowly over time. Once it reaches zero, the player begins to take damage. Of course, drinking water could bring the number back up to 100...

I'm drawing a blank at the moment, there's the PlayerInteractEvent which should let me check for the water bottle and whatnot, however my brain is not cooperating with me today.

Someone shine a light on this for me? I'd appreciate it

#1
2. Offline

### ZeusAllMighty11

To decrease at the same amount, you can set up a scheduler every X seconds to get the current Map entry for the player's thirst, and set it to what it is minus X.

At the time of doing this, check if the player's thirst is less than 1 , then apply damage and stop decreasing their thirst or it will be negative.

OnInteract, you check if there's a water bottle (373:0 / material.potion)
If there is, and they use it (right click air/block), then increase their current map entry integer by X

3. Offline

### valon750

hmm, I see, okay it's slowly making more sense now.

Would I be able to see some example code? I've never gone in to schedulers...

#3
4. Offline

### ZeusAllMighty11

Well I don't really have much code for ya, aside from the map and a link

Code:
```Map<String, Integer>thirstyPlayers = new HashMap<String, Integer>();
```
to get a player from that:
Code:
```for(String s : thirstPlayers){
Player p = getServer().getPlayer(s);
p.doStuff();
}
```

wiki.bukkit.org/Scheduler_Programming

5. Offline

### valon750

ZeusAllMighty11

So I had a look around at the Schedulers, I've got this code:

Code:
```    BukkitTask task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() {
@Override
public void run() {
System.out.println("The message has been sent.");
}
}, 60L, 200L);
}```
But this brings up an error in the console, asking if the plugin is up to date. Am I unable to use this method with 1.4.5 R1.0 ?

#5
6. Offline

### keto23

Code:
```int task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() {
@Override
public void run() {
System.out.println("The message has been sent.");
}
}, 60L, 200L);```
That should work, correct me if i'm wrong.

#6
7. Offline

### valon750

keto23

This brings up the error:

Code:
`Type mismatch: cannot convert from BukkitTask to int`

#7
8. Offline

### Deckerz

Code:
```package net.arcadecraft.blood;

import java.util.Random;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class blood extends JavaPlugin
implements Listener
{
public FileConfiguration azc;
public void onEnable()
{
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(this, this);
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run()
{
blood.this.Tick();
}

}
, 0L, 1L);
}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if ((sender instanceof Player))
{
String name = sender.getName().toLowerCase();

if (label.toLowerCase().equals("blood"))
{
if (getBleeding(name))
{
sender.sendMessage(ChatColor.RED + "Bleeding: Yes");
}
else
{
sender.sendMessage(ChatColor.RED + "Bleeding: No");
}

sender.sendMessage(ChatColor.RED + "Blood: " + Integer.toString(getBlood(name)) + "/4000");
}

}

return false;
}

public void Tick()
{
for (Player player : getServer().getOnlinePlayers())
{

String name = player.getName().toLowerCase();

if ((player.getHealth() > 0) && (getBleeding(name)))
{
setBlood(name, getBlood(name) - 1);
}

if (getBlood(name) <= 0)
{
player.setHealth(0);
setBlood(name, -1);
}

}

}

public boolean getBleeding(String name)
{
return getConfig().getBoolean("player." + name + ".bleeding");
}

public void setBleeding(String name, boolean bleeding)
{
getConfig().set("player." + name + ".bleeding", Boolean.valueOf(bleeding));

saveConfig();
}

public int getBlood(String name)
{
return getConfig().getInt("player." + name + ".blood");
}

public void setBlood(String name, int blood)
{
getConfig().set("player." + name + ".blood", Integer.valueOf(blood));

saveConfig();
}

@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent event)
{

if ((event.getEntity() instanceof Player))
{

Player player = (Player)event.getEntity();
String name = player.getName().toLowerCase();

Random randomGenerator = new Random();
int random = randomGenerator.nextInt(100);

if (!getBleeding(name))
{
if (random <= 67)
{
setBleeding(name, true);
setBlood(name, getBlood(name) - 1);

player.sendMessage(ChatColor.RED + "You are bleeding!");
}
}
}
}

@EventHandler
public void onEntityDamageByBlock(EntityDamageByBlockEvent event)
{
if ((event.getEntity() instanceof Player))
{

Player player = (Player)event.getEntity();
String name = player.getName().toLowerCase();

Random randomGenerator = new Random();
int random = randomGenerator.nextInt(100);

if (!getBleeding(name))
{
if (random <= 25)
{
setBleeding(name, true);
setBlood(name, getBlood(name) - 1);

player.sendMessage(ChatColor.RED + "You are bleeding!");
}
}
}
}

@EventHandler
public void onEntityDamageByFalling(EntityDamageEvent event)
{

event.getCause();
if(event.getCause().equals(DamageCause.FALL)){
if ((event.getEntity() instanceof Player))
{

Player player = (Player)event.getEntity();
String name = player.getName().toLowerCase();

Random randomGenerator = new Random();
int random = randomGenerator.nextInt(100);

if (!getBleeding(name))
{
if (random <= 50)
{
setBleeding(name, true);
setBlood(name, getBlood(name) - 1);

player.sendMessage(ChatColor.RED + "You are bleeding!");
}
}
}

}
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event)
{

String name = event.getPlayer().getName().toLowerCase();

if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
{
if (getBleeding(name))
{
if ((event.getPlayer().getItemInHand().getType() == Material.PAPER))
{
if (event.getPlayer().getItemInHand().getAmount() > 1)
{
event.getPlayer().getItemInHand().setAmount(event.getPlayer().getItemInHand().getAmount() - 1);
}
else
{
event.getPlayer().setItemInHand(null);
}

setBleeding(name, false);

event.getPlayer().sendMessage(ChatColor.GREEN + "Your bleeding has stopped!");

}
}
}
}

public boolean isFull(int i){
if(i == 4000){
return true;
}else{
return false;
}
}
public void giveTransfusion(String name)
{
setBleeding(name, false);
setBlood(name, 4000);;

}
@EventHandler
public void touchytouchy(PlayerInteractEntityEvent event)
{

Player rightclick = (Player) event.getRightClicked();
String name = rightclick.getName().toLowerCase();
if(rightclick instanceof Player){
if(event.getPlayer().getItemInHand().getTypeId() == 351){
byte data = event.getPlayer().getItemInHand().getData().getData();
if(data == 1){
if(isFull(data)){
giveTransfusion(name);
if (event.getPlayer().getItemInHand().getAmount() > 1)
{
event.getPlayer().getItemInHand().setAmount(event.getPlayer().getItemInHand().getAmount() - 1);
}
else
{
event.getPlayer().setItemInHand(null);

}
}
}
}
}

}

@EventHandler
public void onPlayerLogin(PlayerLoginEvent event)
{

String name = event.getPlayer().getName().toLowerCase();
if(event.getPlayer().hasPlayedBefore()){
setBleeding(name, false);
setBlood(name, 4000);

}else{
if(getBleeding(name) == true){
setBleeding(name, true);
}
}

}

@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event)
{

String name = event.getPlayer().getName().toLowerCase();
setBleeding(name, false);
setBlood(name, 4000);

}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event)
{

String name = event.getEntity().getName();
if(getBlood(name) == -1){
event.setDeathMessage(name + " lost all of his blood!");
}
}

}```
Fully working blood plugin so really to make it thirst just change a few lines and remove a few checks

NB: cba editing it myself to work for your needs

#8
9. Offline

### valon750

declanmc96

Ah.. eh... wow.. that's a lot of text..

Thank you regardless

Right then, well, if someone could lend a hand in sorting this out, I'd appreciate that XP

Just note everyone that I'll be doing all the player listener stuff, such as all the events, will be in a separate. PlayerListener class

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 30, 2016
#9
10. Offline

### Deckerz

Code:
```package net.arcadecraft.blood;

import java.util.Random;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class blood extends JavaPlugin
implements Listener
{
public FileConfiguration azc;
public void onEnable()
{
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(this, this);
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run()
{
blood.this.Tick();
}

}
, 0L, 1L);
}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if ((sender instanceof Player))
{
String name = sender.getName().toLowerCase();

if (label.toLowerCase().equals("thirst"))
{

sender.sendMessage(ChatColor.RED + "Thirst: " + Integer.toString(getBlood(name)) + "/4000");
}

}

return false;
}

public void Tick()
{
for (Player player : getServer().getOnlinePlayers())
{

String name = player.getName().toLowerCase();

if ((player.getHealth() > 0) )
{
setBlood(name, getBlood(name) - 1);
}

if (getBlood(name) <= 0)
{
player.setHealth(0);
setBlood(name, -1);
}

}

}

public int getBlood(String name)
{
return getConfig().getInt("player." + name + ".blood");
}

public void setBlood(String name, int blood)
{
getConfig().set("player." + name + ".blood", Integer.valueOf(blood));

saveConfig();
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event)
{

String name = event.getPlayer().getName().toLowerCase();

if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
{

if ((event.getPlayer().getItemInHand().getType() == Material.WATER_BUCKET))
{
if (event.getPlayer().getItemInHand().getAmount() > 1)
{
event.getPlayer().getItemInHand().setAmount(event.getPlayer().getItemInHand().getAmount() - 1);
setBlood(name, 4000);
}
else
{
event.getPlayer().setItemInHand(null);
setBlood(name, 4000);
}

event.getPlayer().sendMessage(ChatColor.GREEN + "Your are no longer thirsty!");

}
}

}

public boolean isFull(int i){
if(i == 4000){
return true;
}else{
return false;
}
}

@EventHandler
public void onPlayerLogin(PlayerLoginEvent event)
{

String name = event.getPlayer().getName().toLowerCase();
if(event.getPlayer().hasPlayedBefore()){
setBlood(name, 4000);

}

}

@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event)
{

String name = event.getPlayer().getName().toLowerCase();

setBlood(name, 4000);

}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event)
{

String name = event.getEntity().getName();
if(getBlood(name) == -1){
event.setDeathMessage(name + " was very thirsty!");
}
}
}

```
that should be about right

#10
11. Offline

### valon750

declanmc96

Looking good, I'll need to alter some things, like making it say thirst rather than blood, which reminds me...

Code:
`blood.this.Tick();`
Can't seem to be resolved

#11
12. Offline

### Deckerz

is blood still the name of the class?

if not change blood to the name of the class

#12
13. Offline

### valon750

declanmc96

Ah, my bad, I've been fighting code all day so I'm slowly losing my mind :S

Hmm, about the code... I logged in, instantly I was dead, now everytime I respawn, I die instantly and get this in the console:

Code:
```23:56:13 [INFO] Valon750 died
23:56:13 [INFO] Valon750 died
23:56:13 [INFO] Valon750 died
23:56:13 [INFO] Valon750 died
23:56:13 [INFO] Valon750 died
23:56:13 [INFO] Valon750 died```
declanmc96

Scratch that, make it this long:
Code:
```23:57:59 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died
23:58:00 [INFO] Valon750 died```
declanmc96

Well... that was weird...

I had the shut the server down, then give myself 10000 blood, that allowed me to live :S

Soooo yeah,

At the moment, if you die, you'll keep dying instantly when you respawn, which is rather annoying XD

I need to find out a way of letting players know when their thirst reaches certain points, then when it reaches a certain amount, have it slowly take away the players health, like when a player is drowning, but, obviously without the water... because that would kind of make it pointless

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 30, 2016
#13
14. Offline

### Deckerz

That was because you had played before without the plugin being loaded, also to send messages its a simple for loop with if statements comparing the amount of thirst. I am not spoon feeding you all of it.

#14
Thread Status:
Not open for further replies.