Jump to content

Question

5 answers to this question

Recommended Posts

  • 0
Posted
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.

  • 0
Posted

@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.

Guest
This topic is now closed to further replies.


  • Posts

    • Inventory restock: Premium business accounts are now available.   ✔ Wallester Business EU 🇪🇺|💳 Unlimited virtual cards, physical cards, 🏦 multi-currency IBAN, ₿ crypto & stablecoin deposits. ✔ Stripe Business UK 🇬🇧|💳 Instant virtual cards (Visa/Mastercard), high-conversion checkout, multi-currency payouts, ₿ crypto payments, no-code payment links. ✔ Mercury Business US 🇺🇸|🏦 US checking & savings, 💳 unlimited virtual cards, domestic & International wires, native stablecoin settlement. ✔ Payset Business EU 🇪🇺|🏦 Multiple IBANs, UK sort code, SEPA Instant, 💳 unlimited virtual cards, multi-currency accounts. ✔ Novo Business US 🇺🇸|🏦 Business checking account, ACH payments & invoicing, 💳 virtual & physical cards, novo boost.
    • Let me see if I understand correctly, older gentlemen, when a newcomer shows up to create modern things with the help of AI, doing what you charge them to do, you point the finger and laugh. I believe that's why everything is stagnant. The product isn't for programming experts, it's for newcomers. Don't buy from you if they can do it themselves using this base. You're going to deliver a similar product, maybe even worse than this one, so why are you complaining? PowerShell, as you well know, started with it, then came new platforms and new apps, new creation models, all with different languages; I chose the simplest one for my taste. This is about being organized and knowing how to choose the right words for each situation. It's not 100%, but it already gives a good impression. Nothing is 100%, so a topic written by AI, and all the code that you charge an absurd amount for to prohibit and sell hacks, could be open source so that everyone can create new practices, new models, new information for passing packets, prohibiting the use of cheats that cause server owners to break so much. Let's remember that the Admin doesn't always shut down the server; it's the players who find problems and take advantage by buying and reselling items, and they say that the GM shuts down the server every week, but that's a lie. What they do is duplicate items with packages and sell them, but perhaps this could give some future developers a starting point to create their own protection following the model in the initial documentation. Because none of you answer a question from a newbie, you think you're superior because you have knowledge, but with AI, people like that can have the same knowledge as you, but with less practice. And if they practice a lot, 10,000 hours, they can be as good as all of you older developers in the L2J field.
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..