Jump to content
  • 0

Daily Reward


extasie80

Question

5 answers to this question

Recommended Posts

  • 0
package custom.RewardForTimeOnline;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.events.Containers;
import com.l2jserver.gameserver.model.events.EventType;
import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerLogin;
import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerLogout;
import com.l2jserver.gameserver.model.events.listeners.ConsumerEventListener;
import com.l2jserver.gameserver.model.quest.Quest;

/**
 * @author LifeGame32
 */
public final class RewardForTimeOnline extends Quest
{
	static final Logger LOG = LoggerFactory.getLogger(RewardForTimeOnline.class);
	
	// Enable/Disable
	static final boolean LOAD = true;
	
	final Map<Integer, PlayerHolder> players;
	
	final List<ItemHolder> rewardItem;
	
	// unique var DB(for saving), Item ID, Amount, time in milliseconds, save the last rewarded time.
	private void addItem()
	{
		rewardItem.add(new ItemHolder("rfto_1", 57, 50000, 30 * 1000, false));
		rewardItem.add(new ItemHolder("rfto_2", 57, 150000, 60 * 1000, true));
		rewardItem.add(new ItemHolder(getVar(), 57, 150000, 60 * 1000, true));
	}
	
	private String getVar()
	{
		return getClass().getSimpleName() + "_" + rewardItem.size();
	}
	
	private final class PlayerHolder
	{
		final L2PcInstance player;
		final List<RewardTask> rewardTask = new ArrayList<>();
		
		public PlayerHolder(L2PcInstance player)
		{
			this.player = player;
		}
		
		public final PlayerHolder startRewardTask()
		{
			for (ItemHolder item : rewardItem)
			{
				rewardTask.add(new RewardTask(this, item));
			}
			
			return this;
		}
		
		public final void onPlayerLogout()
		{
			for (RewardTask t : rewardTask)
			{
				t.onPlayerLogout();
			}
		}
	}
	
	private final class RewardTask implements Runnable
	{
		private final PlayerHolder ph;
		private final ItemHolder item;
		private ScheduledFuture<?> task = null;
		
		private long lastTime;
		
		public RewardTask(PlayerHolder playerHolder, ItemHolder item)
		{
			this.ph = playerHolder;
			this.item = item;
			this.lastTime = System.currentTimeMillis();
			
			long initialDelay;
			
			if (item.isSaveTime)
			{
				initialDelay = ph.player.getVariables().getLong(item.var, 0);
				if ((initialDelay == 0) || (initialDelay > item.time))
				{
					initialDelay = item.time;
				}
			}
			else
			{
				initialDelay = item.time;
			}
			
			this.task = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(this, initialDelay, item.time);
		}
		
		@Override
		public void run()
		{
			if ((ph.player == null) || (ph.player.getClient() == null) || ph.player.getClient().isDetached())
			{
				return;
			}
			
			if (ph.player.isOnline())
			{
				if (item.isSaveTime)
				{
					ph.player.getVariables().set(item.var, 0);
				}
				
				lastTime = System.currentTimeMillis();
				
				ph.player.addItem(RewardForTimeOnline.class.getSimpleName(), item.id, item.count, null, true);
			}
		}
		
		public final void onPlayerLogout()
		{
			stopTask();
			
			if (item.isSaveTime)
			{
				ph.player.getVariables().set(item.var, (item.time - (System.currentTimeMillis() - lastTime)));
			}
		}
		
		public final void stopTask()
		{
			if (task != null)
			{
				task.cancel(false);
				task = null;
			}
		}
	}
	
	private final class ItemHolder
	{
		final String var;
		final int id;
		final long count;
		final long time;
		final boolean isSaveTime;
		
		public ItemHolder(String var, int id, long count, long time, boolean isSaveTime)
		{
			this.var = var;
			this.id = id;
			this.count = count;
			this.time = time;
			this.isSaveTime = isSaveTime;
		}
	}
	
	public RewardForTimeOnline()
	{
		super(-1, RewardForTimeOnline.class.getSimpleName(), "custom/RewardForTimeOnline");
		Containers.Global().addListener(new ConsumerEventListener(Containers.Global(), EventType.ON_PLAYER_LOGIN, (OnPlayerLogin event) -> onPlayerLogin(event), this));
		Containers.Global().addListener(new ConsumerEventListener(Containers.Global(), EventType.ON_PLAYER_LOGOUT, (OnPlayerLogout event) -> onPlayerLogout(event), this));
		players = new ConcurrentHashMap<>();
		rewardItem = new ArrayList<>();
		addItem();
	}
	
	private final void onPlayerLogin(OnPlayerLogin event)
	{
		PlayerHolder task = players.get(event.getActiveChar().getObjectId());
		if (task == null)
		{
			players.put(event.getActiveChar().getObjectId(), new PlayerHolder(event.getActiveChar()).startRewardTask());
		}
	}
	
	private final void onPlayerLogout(OnPlayerLogout event)
	{
		PlayerHolder task = players.remove(event.getActiveChar().getObjectId());
		if (task != null)
		{
			task.onPlayerLogout();
		}
	}
	
	public static void main(String[] args)
	{
		if (LOAD)
		{
			new RewardForTimeOnline();
		}
		
		LOG.info("{}: load {}.", RewardForTimeOnline.class.getSimpleName(), LOAD);
	}
}

 

Original share here.

Link to comment
Share on other sites

  • 0

@Rootware

Da heck.. So much code for such a little addon.

 

Hint, take a look on gametime task, you can see the 'take a break' message after an hour. You can follow the logic of the task. Add player on login, store time and then check if hour passed.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Posts

    • need to change it from database. Find the item with ID, and check the square stackable. After that need to find the item from txt file (maybe etcitem.grp) and make this <set name="consume_type" val="stackable" />.
    • Hello, please tell me how I can do this so that when I buy 10 D weapons, they will be in one stack. that's how there are 100 pieces in one nipple I already tried putting 10 pieces in one weapon in the store, but when I clicked on the TAB. they are purchased separately, 1 piece each, etc. Giran  Shop Galladucci  <npc>30097</npc> <!-- Galladucci (Weapon Merchant) --> <item>         <!-- Adena -->         <ingredient count="6680500" id="57" />         <!-- Heavy War Axe -->         <production count="10" id="91" />     </item> When I press TAB-Inventar weapons <!-- Heavy War Ax --> one each, and I need them to be in place x10
    • Is there a way to make it work on L2j Sunrise?  Is it possible to use other items to increse clan level like only donation coin as an alternate to the existing one.  Like this Clan lvl 1: 100coin, lvl2 200, lvl3 300 .... lvl 10 10000coin.   My base idea was, the clan members can buy a clan support coin from donation coin and auto deposited to the caln wh, and the leader leader can use this coint to level up the clan. Should i just forget it, maybe its possible? Im not great in programing, so I looking for help and, I hope some one like this idea.
    • Imbadon Essence server with the new High Elves update, don't miss the opening on April 19.   Grand Opening: April 19 Website: https://imbadon.com Discord Community: https://discord.gg/imbadon General Information and Features Rates: EXP x1, Adena x1 Update: High Elves Gearscore System Guaranteed Enchant System Custom Farm Zones: Imperial Tomb and Fafurion Temple Reworked Class balance Custom Solo, Clan and Party Instances Balanced stages with dynamic experience   Many players and clans are waiting for you on imbadon.com
    • Are you yearning for a connection that's both stimulating and intimate? Look no further than our premium live sex chat platform, where you'll encounter a diverse array of captivating women eager to engage in meaningful conversations and explore your deepest desires. Join our community today and discover the excitement of forging genuine connections with stunning individuals from around the globe.
  • Topics

×
×
  • Create New...