#Subtract and Addtime pdc instance

1 messages · Page 1 of 1 (latest)

late sinew
#

How to fix

#

@chrome stone

chrome stone
#

nothing wrong with that code

#

you can't be calling it

late sinew
#

wdym

#

public void PlayerDeathEvent(PlayerDeathEvent event) {
Player p = event.getEntity();
subtractTime(p, 1);
p.sendMessage(DIALOG+ ChatColor.DARK_RED +"You Died... this means you have lost one hour of your life!");
Entity n = event.getEntity().getKiller();
if (n instanceof Player){
Player np = (Player) n;
addTime(np,1);
p.sendMessage(DIALOG+ChatColor.GREEN + "You killed someone, you get 1 Hour!");
}
}

#

I see the m,essage here

#

It tells me You Died but it dosent - time

chrome stone
#

looks fine

#

where are you reading/displaying the time?

late sinew
#

actionbar

#

I even have /time command

chrome stone
#

show your getTime

late sinew
#

the command?

chrome stone
#

method

late sinew
chrome stone
#

why is your key showing red?

late sinew
#

its a theme

#

I have no errors

#

red swiggly underline=error

chrome stone
#

thats an error, red = warning

#

hover over it and it shows no warning?

#

I'm expecting a static warning

late sinew
chrome stone
#

ok

late sinew
#

not an error my g

chrome stone
#

I see no reason for it to not be working. It looks fine

late sinew
#

ikr

#

i shuld check server ,ogs

chrome stone
#

I can't check it here at the moment. slightly busy

late sinew
#

alr

#

time stays same

late sinew
#

@chrome stone remind me when your back

chrome stone
#

make sure .minus affects the variable

#

you may have to do expires = expires.minus(hours, ChronoUnit.HOURS);

late sinew
#

I was thinking that I should probs try it tho

chrome stone
#

I just check it returns a copy, so yes you have to reassign it

#

you'll have to update teh add method too

late sinew
#

ohh rlyyy

late sinew
#

wannna test it w/ me or nah

chrome stone
#
    /**
     * Add time to a Players Death Clock.
     * 
     * @param player the Player to add time to.
     * @param hours the hours to remove.
     */
    public static void addTime(Player player, long hours) {

        PersistentDataContainer pdc = player.getPersistentDataContainer();
        Instant expires = Instant.ofEpochMilli(pdc.getOrDefault(key, PersistentDataType.LONG, Instant.now().toEpochMilli()));
        expires = expires.plus(hours, ChronoUnit.HOURS);
        pdc.set(key, PersistentDataType.LONG, expires.toEpochMilli());
    }

    /**
     * Subtract time from a Players Deathclock.
     * 
     * @param player the Player to remove time from.
     * @param hours the number of hours to remove.
     */
    public static void subtractTime(Player player, long hours) {

        PersistentDataContainer pdc = player.getPersistentDataContainer();
        Instant expires = Instant.ofEpochMilli(pdc.getOrDefault(key, PersistentDataType.LONG, Instant.now().toEpochMilli()));
        expires = expires.minus(hours, ChronoUnit.HOURS);
        pdc.set(key, PersistentDataType.LONG, expires.toEpochMilli());
    }```
late sinew
#

works very nicely

#

for dying didnt try it with killing yet or losing time