This is what we are adding:
279 rewards
first off go to com asteria game world and add a package called 'event.trivia'
add all of the following classes
Code:
package com.asteria.game.world.event.trivia;
import java.util.List;
import java.util.Random;
import com.asteria.game.World;
import com.asteria.game.character.player.Player;
import com.asteria.game.item.Item;
/**
* Handles the logics of the trivia and stores
* @author Zack/Optimum
*
*/
public class TriviaBot
{
/**
* The trivia question
*/
private TriviaQuestion triviaQuestion;
/**
* Object for a trivia typing question
*/
private TriviaTyping triviaTyping;
/**
* Checks if trivia is running or not
*/
private boolean triviaRunning;
/**
* Checks if the trivia question is a ::####### question
*/
private boolean isTypingQuestion;
/**
* Gets the trivia question object
* @return - the trivia question object
*/
public TriviaQuestion getTriviaQuestion()
{
return triviaQuestion;
}
/**
* Gets the trivia typing object
* @return - the trivia typing object
*/
public TriviaTyping getTriviaTyping()
{
return triviaTyping;
}
/**
* Checks if the trivia is running
* @return - true if the trivia is running
*/
public boolean isRunning()
{
return triviaRunning;
}
/**
* Checks if the trivia is a typing question
* @return - true if the trivia is a typing question
*/
public boolean isTypingQuestion()
{
return isTypingQuestion;
}
/**
* Generates a new trivia question
*/
public void generateNewTrivia()
{
triviaRunning = true;
Random rnd = new Random();
if(rnd.nextInt(2) == 0)
{
isTypingQuestion = false;
triviaQuestion = TriviaQuestion.getRandomQuestion();
World.message("[@red@TRIVIA BOT@bla@] "+triviaQuestion.getQuestion() + "...");
World.message("[@red@TRIVIA BOT@bla@] Answer like ::answer [answer here] for a reward");
}
else
{
isTypingQuestion = true;
triviaTyping = new TriviaTyping();
triviaTyping.generateFullKey();
World.message("[@red@TRIVIA BOT@bla@] First person to type @red@::" + triviaTyping.getKey() + " @bla@for a reward!");
}
}
private Random rnd = new Random();
/**
* Gives the player a reward
* @param target - the targeted player
*/
public void giveReward(Player target)
{
resetTrivia();
int amount = (1 + rnd.nextInt(70));
List<TriviaReward> rewards = TriviaReward.generateRewards(amount);
for(TriviaReward reward : rewards)
{
int randomAmount = 0;
if(reward.getRandomAmount() != 0)
{
randomAmount = rnd.nextInt(reward.getRandomAmount());
}
target.getInventory().add(new Item(reward.getItemId(), reward.getLowestAmount() + randomAmount));
}
World.message("[@red@TRIVIA BOT@bla@] " + target.getFormatUsername() + " has won "+ amount+ " rewards from the trivia!");
}
/**
* Resets the trivia
*/
private void resetTrivia()
{
triviaRunning = false;
triviaQuestion = null;
triviaTyping = null;
}
}
Code:
package com.asteria.game.world.event.trivia;
import com.asteria.task.Task;
/**
* Handles how often the trivia runs
* 60 = every hour
* @author Zack/Optimum
*
*/
public class TriviaEvent extends Task
{
/**
* Object for a new trivia bot
*/
public static TriviaBot trivia = new TriviaBot();
/**
* constructor for TriviaEvent
* Sets the timer for the task
*/
public TriviaEvent()
{
super(30, true);
}
/**
* Executes a new trivia
*/
@Override
public void execute()
{
trivia.generateNewTrivia();
}
}
Code:
package com.asteria.game.world.event.trivia;
import java.util.Random;
/**
* Stores all of the trivia questions and possible
* answers
* @author Zack/Optimum
*
*/
public enum TriviaQuestion
{
Q1("What does rsps stand for?", "runescape private server"),
Q2("Question 2", "pos answer 1", "pos answer 2");
/**
* The question string
*/
private String question;
/**
* All the possible answers
*/
private String[] answers;
/**
* Constructor for TriviaQuestions
* @param question - the question
* @param answers - the answers
*/
private TriviaQuestion(String question, String... answers)
{
this.question = question;
this.answers = answers;
}
/**
* Gets the question
* @return - the question
*/
public String getQuestion()
{
return question;
}
/**
* gets the answers
* @return - the answer
*/
public String[] getAnswers()
{
return answers;
}
/**
* Gets a random question
* @return - the random question
*/
public static TriviaQuestion getRandomQuestion()
{
Random rnd = new Random();
return values()[rnd.nextInt(values().length)];
}
/**
* Checks if the player attempt is correct
* @param attempt - the players attempt
* @return - true
* players attempt is correct
* false
* players attempt is wrong
*/
public boolean isCorrect(String attempt)
{
for(String answer : TriviaEvent.trivia.getTriviaQuestion().getAnswers())
{
if(attempt.equalsIgnoreCase(answer))
{
return true;
}
}
return false;
}
}
Code:
package com.asteria.game.world.event.trivia;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Class contains all of the rewards
* @author Zack/Optimum
*
*/
public enum TriviaReward
{
/**
* Common
*/
C1(554, 10, 50, TriviaRewardRarity.COMMON),
C2(555, 10, 50, TriviaRewardRarity.COMMON),
C3(556, 10, 50, TriviaRewardRarity.COMMON),
C4(557, 10, 50, TriviaRewardRarity.COMMON),
C5(43, 10, 25, TriviaRewardRarity.COMMON),
/**
* Uncommon
*/
U1(1333, 1, 0, TriviaRewardRarity.UNCOMMON),
U2(1079, 1, 0, TriviaRewardRarity.UNCOMMON),
U3(1127, 1, 0, TriviaRewardRarity.UNCOMMON),
U4(1163, 1, 0, TriviaRewardRarity.UNCOMMON),
U5(1201, 1, 0, TriviaRewardRarity.UNCOMMON),
/**
* Rare
*/
R1(1187, 1, 0, TriviaRewardRarity.RARE),
R2(4587, 1, 0, TriviaRewardRarity.RARE),
R3(4087, 1, 0, TriviaRewardRarity.RARE),
R4(6967, 1, 0, TriviaRewardRarity.RARE),
/**
* Very rare
*/
V1(1038, 1, 0, TriviaRewardRarity.VERY_RARE),
V2(1040, 1, 0, TriviaRewardRarity.VERY_RARE),
V3(1042, 1, 0, TriviaRewardRarity.VERY_RARE),
V4(1044, 1, 0, TriviaRewardRarity.VERY_RARE),
V5(1046, 1, 0, TriviaRewardRarity.VERY_RARE),
V6(1048, 1, 0, TriviaRewardRarity.VERY_RARE);
/**
* Variables
*/
private int itemId, lowestAmount, randomAmount;
private TriviaRewardRarity rarity;
/**
* Constructor for TriviaReward
* @param itemId - The item id
* @param lowestAmount - Lowest amount to give the player
* @param highestAmount - Highest amount to give to a player
* @param rarity - the rarity level
*/
private TriviaReward(int itemId, int lowestAmount, int randomAmount, TriviaRewardRarity rarity)
{
this.itemId = itemId;
this.lowestAmount = lowestAmount;
this.randomAmount = randomAmount;
this.rarity = rarity;
}
/**
* Generates a random number
*/
private static Random rnd = new Random();
/**
* Gets the item id
* @return - the item id
*/
public int getItemId()
{
return itemId;
}
/**
* Gets the lowest possible amount
* @return - the possible amount
*/
public int getLowestAmount()
{
return lowestAmount;
}
/**
* Gets the random amount to give
* @return
*/
public int getRandomAmount()
{
return randomAmount;
}
/**
* Generates a random reward list.
* The list size will be determined by the amount param
* @param amount - the amount of rewards
* @return - the reward list
*/
public static List<TriviaReward> generateRewards(int amount)
{
List<TriviaReward> temp = new ArrayList<>();
temp.clear();
while(temp.size() != amount)
{
int index = rnd.nextInt(values().length);
int random = (1 + rnd.nextInt(300));
TriviaReward currentReward = values()[index];
if(random % currentReward.rarity.getRarity() == 0)
{
temp.add(currentReward);
}
}
return temp;
}
}
Code:
package com.asteria.game.world.event.trivia;
/**
* The different types of rarity for rewards
* @author Zack/Optimum
*
*/
public enum TriviaRewardRarity
{
COMMON(1),
UNCOMMON(15),
RARE(50),
VERY_RARE(250);
/**
* The rarity
*/
private int rarity;
/**
* Constructor for TriviaRewardRarity
* @param rarity - the rarity
*/
private TriviaRewardRarity(int rarity)
{
this.rarity = rarity;
}
/**
* Gets the rarity
* @return - the rarity
*/
public int getRarity()
{
return rarity;
}
}
Code:
package com.asteria.game.world.event.trivia;
import java.util.Random;
/**
* Handles the creation of a new random key used for
* the player command
* @author Zack/Optimum
*
*/
public class TriviaTyping
{
/**
* Will store the final key
*/
private StringBuilder key = new StringBuilder();
/**
* A temperary key to store all the characters and numbers
*/
private StringBuilder tempKey = new StringBuilder();
/**
* The length of the key
*/
private static final int KEY_LEN = 8;
/**
* Used for selecting random indexes of the {@link tempKey}
*/
private Random rnd = new Random();
/**
* Generated the temperary key and the whole key
*/
public void generateFullKey()
{
generateTempKey();
generateTypingKey();
}
/**
* Generates the temperary key
* used for getting indexes
*/
private void generateTempKey()
{
for(char i = 'a'; i < 'z'; i++ )
{
tempKey.append(i);
}
for(char i = '0'; i < '9'; i++ )
{
tempKey.append(i);
}
}
/**
* Generates the final key that will be used
* for the command
*/
private void generateTypingKey()
{
key.delete(0, key.length());
for(int i = 0; i < KEY_LEN; i++)
{
int index = rnd.nextInt(tempKey.length());
key.append(tempKey.charAt(index));
}
}
/**
* Gets the current key
* @return - the key
*/
public String getKey()
{
return key.toString();
}
}
now in server.java find
Code:
TaskHandler.submit(new MinigameHandler());
and under that add this:
Code:
TaskHandler.submit(new TriviaEvent());
now go to commands.groovy
and add under
Code:
String[] cmd = context.text
add this:
Code:
if(TriviaEvent.trivia.isTypingQuestion() == true && TriviaEvent.trivia.isRunning()) {
if(cmd[0].equalsIgnoreCase(TriviaEvent.trivia.getTriviaTyping().getKey())) {
TriviaEvent.trivia.giveReward(player)
return;
}
}
now below this
add this
Code:
case "trivia":
if(TriviaEvent.trivia.getTriviaQuestion() != null) {
player.encoder.sendMessage(TriviaEvent.trivia.triviaQuestion.getQuestion())
return
} else if(TriviaEvent.trivia.isTypingQuestion() != null) {
player.encoder.sendMessage("first one to type: @red@::" + TriviaEvent.trivia.getTriviaTyping().getKey())
return
}
player.encoder.sendMessage("There is no trivia at the moment!")
break
case "answer":
String attempt = ""
for(int i = 1; i < cmd.length; i++) {
if(i == cmd.length - 1)
attempt += (cmd[i])
else
attempt += (cmd[i] + " ")
}
if (TriviaEvent.trivia.getTriviaQuestion().isCorrect(attempt)) {
TriviaEvent.trivia.giveReward(player)
} else {
player.encoder.sendMessage("Wrong! Please try again!")
}
break
then import these:
Code:
import com.asteria.game.world.event.trivia.TriviaBot
import com.asteria.game.world.event.trivia.TriviaQuestion
import com.asteria.game.world.event.trivia.TriviaEvent;
remember to thanks the thread if you used