Jump to content

Question

Posted

I have in mind to create a new VIP system, like Bronze VIP, Silver VIP and Gold VIP. Any ideas for using the same isVip variable for the 3 functions? Because I don't want to have to create isVipBronze, isVipPrata and isVipOuro and neither 3 tasks because I will save the statuses in the characters' memo. To using java 11.

13 answers to this question

Recommended Posts

  • 0
Posted

Make PremiumLevel enum like:

public enum PremiumLevel
{
	NONE(0),
	BRONZE(1),
	SILVER(2),
	GOLD(3);
	
	public static final PremiumLevel[] VALUES = values();
	
	private final int _id;
	
	private PremiumLevel(int id)
	{
		_id = id;
	}
	
	public int getId()
	{
		return _id;
	}
	
	public boolean isGreater(PremiumLevel premiumLevel)
	{
		return getId() > premiumLevel.getId();
	}
	
	public boolean isLesser(PremiumLevel premiumLevel)
	{
		return getId() < premiumLevel.getId();
	}
}

 

And manage following this states as you want. The more elegant solution don't find.

  • 0
Posted (edited)
14 minutes ago, Rootware said:

Make PremiumLevel enum like:



public enum PremiumLevel
{
	NONE(0),
	BRONZE(1),
	SILVER(2),
	GOLD(3);
	
	public static final PremiumLevel[] VALUES = values();
	
	private final int _id;
	
	private PremiumLevel(int id)
	{
		_id = id;
	}
	
	public int getId()
	{
		return _id;
	}
	
	public boolean isGreater(PremiumLevel premiumLevel)
	{
		return getId() > premiumLevel.getId();
	}
	
	public boolean isLesser(PremiumLevel premiumLevel)
	{
		return getId() < premiumLevel.getId();
	}
}

 

And manage following this states as you want. The more elegant solution don't find.

good idea I did more or less like this, define the variable by name I find it easier.

 

https://imgur.com/a/qrM1yPP

 

template

 

https://imgur.com/a/R9paVUO

 

Enum

 

https://imgur.com/a/JQGd960

Edited by Williams
  • 0
Posted (edited)

Pointless, IMHO.

 

final PremiumLevel premiumLevel = PremiumLevel.VALUES[levelId];

 

or

 

final PremiumLevel premiumLevel = Enum.valueOf(PremiumLevel.class "GOLD");

 

or add into PremiumLevel enum one more method:

 

	public static PremiumLevel getPremiumLevel(int levelId)
	{
		for (PremiumLevel bit : VALUES)
			if (bit.getId() == levelId)
				return bit;
			
		return NONE;
	}

 

and use

 

final PremiumLevel premiumLevel = PremiumLevel.getPremiumLevel(levelId);

 

Edited by Rootware
Typos.
  • 0
Posted

Why not an abstract class?

 

public abstract class VipStatus
{
	private final long _endTime;
	
	public VipStatus(long endTime)
	{
		_endTime = endTime;
	}
	
	public long getEndTime()
	{
		return _endTime;
	}
	
	public abstract double getExpBonusRate();
	public abstract double getSpoilBonusRate();
	public abstract double getAdenaBonusRate();
}

 

 

Then your classes

 

public class VipBronze extends VipStatus
{
	public VipBronze(long endTime)
	{
		super(endTime);
	}
	
	@Override
	public double getExpBonusRate()
	{
		return 1.2;
	}
	
	@Override
	public double getSpoilBonusRate()
	{
		return 1.7;
	}
	
	@Override
	public double getAdenaBonusRate()
	{
		return 2.5;
	}
}

 

 

So in conclusion, your player instance will have only VipStatus variable and you wont mess with enums and checks for what vip status the player have

  • 0
Posted
3 minutes ago, melron said:

Why not an abstract class?

 


public abstract class VipStatus
{
	private final long _endTime;
	
	public VipStatus(long endTime)
	{
		_endTime = endTime;
	}
	
	public long getEndTime()
	{
		return _endTime;
	}
	
	public abstract double getExpBonusRate();
	public abstract double getSpoilBonusRate();
	public abstract double getAdenaBonusRate();
}

 

 

Then your classes

 


public class VipBronze extends VipStatus
{
	public VipBronze(long endTime)
	{
		super(endTime);
	}
	
	@Override
	public double getExpBonusRate()
	{
		return 1.2;
	}
	
	@Override
	public double getSpoilBonusRate()
	{
		return 1.7;
	}
	
	@Override
	public double getAdenaBonusRate()
	{
		return 2.5;
	}
}

 

 

So in conclusion, your player instance will have only VipStatus variable and you wont mess with enums and checks for what vip status the player have

I found the model using xml easier to edit doing with abstract class would have to configure by config that way it gives me more work to create countless configs for each type

  • Like 1
  • 0
Posted
5 minutes ago, Williams said:

I found the model using xml easier to edit doing with abstract class would have to configure by config that way it gives me more work to create countless configs for each type

There's nothing that can't be done. You can use xml to parse your values just like you did with the other model.

  • 0
Posted (edited)

Let's start with basic Java things.

 

Enum has build in ordinal that act as ID. So all this:

public enum PremiumLevel
{
	NONE(0),
	BRONZE(1),
	SILVER(2),
	GOLD(3);
	
	public static final PremiumLevel[] VALUES = values();
	
	private final int _id;
	
	private PremiumLevel(int id)
	{
		_id = id;
	}
	
	public int getId()
	{
		return _id;
	}
	
	public boolean isGreater(PremiumLevel premiumLevel)
	{
		return getId() > premiumLevel.getId();
	}
	
	public boolean isLesser(PremiumLevel premiumLevel)
	{
		return getId() < premiumLevel.getId();
	}
}

 

is pointless since you can use the PremiumLevel.BRONZE.ordinal();

 

Also this is more useless than the getId()

 

public static final PremiumLevel[] VALUES = values();

 

since enum is initialized once. So you can use Premium.values();

 

Also to the other guy who suggest class implementation. Not wrong but just to get values without any other option is needless. The whole code can be written using enum but in a proper way:

 

/*
 * Copyright (C) 2004-2021 L2J Server
 * This file is part of L2J Server.
 * L2J Server is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * L2J Server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.datatables;

/**
 * @author Kara`
 */
public enum Premium
{
	NONE
	{
		@Override
		public double getExperienceMultiplier()
		{
			return 1;
		}

		@Override
		public double getSkillPointMultiplier()
		{
			return 1;
		}
	},
	BRONZE
	{
		@Override
		public double getExperienceMultiplier()
		{
			return 1.15;
		}

		@Override
		public double getSkillPointMultiplier()
		{
			return 1.15;
		}
	};

	public abstract double getExperienceMultiplier();

	public abstract double getSkillPointMultiplier();
}

 

You can also use configs inside those and avoid static values. Nonetheless it's up to you to decide base on your need. If you're thinking of extending the code and add functionality, avoid enum and go with interface classes. 

 

If you're thinking of just using 3-4 multiplier then enum is good and easily stored. You can create a field in database "premium_level" and store the ordinal of enum.

 

Once player login you can restore by using Premium.values()[rs.getInt("premium_level")]

Edited by Kara
  • Upvote 1
  • 0
Posted

when the real java dev comes to kick ass.:notbadn:

 

On to the topic, why you think people will buy all this levels of VIP in first place?

PS: @Tryskell what is your opinion you have also years of experience in java

  • Sad 1
  • 0
Posted
1 hour ago, Nightw0lf said:

when the real java dev comes to kick ass.:notbadn:

 

On to the topic, why you think people will buy all this levels of VIP in first place?

PS: @Tryskell what is your opinion you have also years of experience in java

Tryskell would just rename Premium to PremiumType and commit.

Disclaimer: This was a joke, do not carpet bomb me.

  • Sad 1
  • Upvote 1
  • 0
Posted (edited)

Thanks for the tips I will use Enum even because it is simpler and I will make my code cleaner. I don't have the same experience as you, but I wrote my enum like this.

 

package net.sf.l2j.gameserver.enums.actors;

import java.util.NoSuchElementException;

/**
 * @author Williams
 *
 */
public enum VipType
{
	NONE("None"),
	BRONZE("Bronze"),
	SILVER("Silver"),
	GOLD("Gold");
	
	public static final VipType[] VALUES = values();
	
	private String _name;
	
	private VipType(String name)
	{
		_name = name;
	}
	
	public String getName()
	{
		return _name;
	}
	
	public static VipType valueOfXml(String name)
	{
		name = name.intern();
		for (VipType type : VALUES)
		{
			if (type.getName().equals(name))
				return type;
		}
		throw new NoSuchElementException("Unknown name '" + name + "' for enum VipType");
	}
}

 

to add the VIP I did it that way 

 


	public void addVip(int duration, VipType type)
	{
		VipTaskManager.getInstance().add(this);
		setVip(true);
		
		long remainingTime = getMemos().getLong(type.name(), 0);
		if (remainingTime > 0)
		{
			getMemos().set(type.name(), remainingTime + TimeUnit.DAYS.toMillis(duration));
			sendMessage("Dear player, your Vip status has been extended by " + duration + " day(s).");
		}
		else
			getMemos().set(type.name(), System.currentTimeMillis() + TimeUnit.DAYS.toMillis(duration));

		setVip(type);
		sendMessage(getName() + " now you are a Vip, your duration is " + duration + " day(s).");	
	}
	

 

Edited by Williams
  • 0
Posted
56 minutes ago, Williams said:

Thanks for the tips I will use Enum even because it is simpler and I will make my code cleaner. I don't have the same experience as you, but I wrote my enum like this.

 


package net.sf.l2j.gameserver.enums.actors;

import java.util.NoSuchElementException;

/**
 * @author Williams
 *
 */
public enum VipType
{
	NONE("None"),
	BRONZE("Bronze"),
	SILVER("Silver"),
	GOLD("Gold");
	
	public static final VipType[] VALUES = values();
	
	private String _name;
	
	private VipType(String name)
	{
		_name = name;
	}
	
	public String getName()
	{
		return _name;
	}
	
	public static VipType valueOfXml(String name)
	{
		name = name.intern();
		for (VipType type : VALUES)
		{
			if (type.getName().equals(name))
				return type;
		}
		throw new NoSuchElementException("Unknown name '" + name + "' for enum VipType");
	}
}

 

to add the VIP I did it that way 

 



	public void addVip(int duration, VipType type)
	{
		VipTaskManager.getInstance().add(this);
		setVip(true);
		
		long remainingTime = getMemos().getLong(type.name(), 0);
		if (remainingTime > 0)
		{
			getMemos().set(type.name(), remainingTime + TimeUnit.DAYS.toMillis(duration));
			sendMessage("Dear player, your Vip status has been extended by " + duration + " day(s).");
		}
		else
			getMemos().set(type.name(), System.currentTimeMillis() + TimeUnit.DAYS.toMillis(duration));

		setVip(type);
		sendMessage(getName() + " now you are a Vip, your duration is " + duration + " day(s).");	
	}
	

 

 

You're rap*ng Java like 99% of the forum.

You simply don't even read a single line from what i wrote in my last comment that intended for you.

 

Experience and laziness are different things. If you don't care of improving your self, go ahead use that s*t code. 

  • 0
Posted
10 minutes ago, Kara said:

 

You're rap*ng Java like 99% of the forum.

You simply don't even read a single line from what i wrote in my last comment that intended for you.

 

Experience and laziness are different things. If you don't care of improving your self, go ahead use that s*t code. 

 

I understood the way you spoke, I just did not understand the way to restore the values of the enum when the player comes into play

Guest
This topic is now closed to further replies.
×
×
  • Create New...