Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This program 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.
- *
- * This program 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 l2f.gameserver;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import l2f.commons.util.Rnd;
- import l2f.gameserver.model.Player;
- import l2f.gameserver.utils.Location;
- import l2f.loginserver.database.L2DatabaseFactory;
- /**
- * @author AccessDenied
- */
- public class Faction
- {
- public Map<Integer, FactionHolder> factions = new ConcurrentHashMap<>();
- public ArrayList<RewardHolder> rewards = new ArrayList<>();
- public Faction()
- {
- try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- {
- PreparedStatement statement = con.prepareStatement("SELECT * FROM faction");
- ResultSet result = statement.executeQuery();
- while(result.next())
- {
- int id = result.getInt("factionId");
- String name = result.getString("factionName");
- String[] pos = result.getString("factionPos").split(";");
- Location loc = new Location(Integer.parseInt(pos[0]), Integer.parseInt(pos[1]), Integer.parseInt(pos[2]));
- String color = result.getString("factionColor");
- factions.put(id, new FactionHolder(id, name, loc, color));
- }
- result.close();
- statement.close();
- }
- catch (Exception e)
- {
- System.out.println("Failed to load factions from database: " + e);
- return;
- }
- }
- public void teleToBase(Player player)
- {
- player.teleToLocation(factions.get(player.getFactionId()).getFactionLoc(), 0);
- }
- public Location getFactionLocation(Player player)
- {
- return factions.get(player.getFactionId()).getFactionLoc();
- }
- public void onPlayerEnter(Player player)
- {
- player.setTitle(factions.get(player.getFactionId()).getFactionName());
- player.setNameColor("0x" + factions.get(player.getFactionId()).getFactionColor());
- teleToBase(player);
- }
- public void onReward(Player player)
- {
- if (rewards.isEmpty())
- return;
- for (RewardHolder holder : rewards)
- if (Rnd.get(100) <= holder.getChance())
- player.getInventory().addItem(holder.getRewardId(), holder.getRewardCount(), "reward");
- }
- public class RewardHolder
- {
- private int _id;
- private int _count;
- private int _chance;
- public RewardHolder(int id, int count, int chance)
- {
- _id = id;
- _count = count;
- _chance = chance;
- }
- public int getChance()
- {
- return _chance;
- }
- public int getRewardId()
- {
- return _id;
- }
- public int getRewardCount()
- {
- return _count;
- }
- }
- public class FactionHolder
- {
- private int _id;
- private String _name;
- private Location _pos;
- private String _color;
- public FactionHolder(int id, String name, Location pos, String color)
- {
- _id = id;
- _name = name;
- _pos = pos;
- _color = color;
- }
- public String getFactionColor()
- {
- return _color;
- }
- public int getFactionId()
- {
- return _id;
- }
- public String getFactionName()
- {
- return _name;
- }
- public Location getFactionLoc()
- {
- return _pos;
- }
- }
- public static Faction getInstance()
- {
- return SingletonHolder._instance;
- }
- private static class SingletonHolder
- {
- protected static final Faction _instance = new Faction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement