Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Eclipse Workspace Patch 1.0
- #P aCis_gameserver
- diff --git java/Base/Data/XMLDocument.java java/Base/Data/XMLDocument.java
- new file mode 100644
- index 0000000..3f06c0f
- --- /dev/null
- +++ java/Base/Data/XMLDocument.java
- @@ -0,0 +1,121 @@
- +package Base.Data;
- +
- +
- +
- +import java.io.File;
- +import java.util.logging.Level;
- +import java.util.logging.Logger;
- +
- +import javax.xml.parsers.DocumentBuilderFactory;
- +import javax.xml.transform.OutputKeys;
- +import javax.xml.transform.Transformer;
- +import javax.xml.transform.TransformerException;
- +import javax.xml.transform.TransformerFactory;
- +import javax.xml.transform.dom.DOMSource;
- +import javax.xml.transform.stream.StreamResult;
- +
- +import net.sf.l2j.commons.data.StatSet;
- +
- +import org.w3c.dom.Document;
- +import org.w3c.dom.NamedNodeMap;
- +import org.w3c.dom.Node;
- +
- +/**
- + * An XML document, relying on a static and single DocumentBuilderFactory.
- + */
- +public abstract class XMLDocument
- +{
- + protected static final Logger LOG = Logger.getLogger(XMLDocument.class.getName());
- +
- + protected Document document;
- +
- + private static final DocumentBuilderFactory BUILDER;
- + static
- + {
- + BUILDER = DocumentBuilderFactory.newInstance();
- + BUILDER.setValidating(false);
- + BUILDER.setIgnoringComments(true);
- + }
- +
- + abstract protected void load();
- +
- + abstract protected void parseDocument(Document doc, File f);
- +
- + public void loadDocument(String filePath)
- + {
- + loadDocument(new File(filePath));
- + }
- +
- + public void writeDocument(Document doc, String fileName)
- + {
- + try
- + {
- + TransformerFactory transformerFactory = TransformerFactory.newInstance();
- + Transformer transformer = transformerFactory.newTransformer();
- + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
- + transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
- +
- + DOMSource source = new DOMSource(doc);
- + StreamResult result = new StreamResult(new File(fileName));
- +
- + transformer.transform(source, result);
- + LOG.info("XML file saved to " + fileName);
- + }
- + catch (TransformerException e)
- + {
- + LOG.warning("Error saving XML file: " + e.getMessage());
- + }
- + }
- +
- + /**
- + * Parse an entire directory or file if found.
- + * @param file
- + */
- + public void loadDocument(File file)
- + {
- + if (!file.exists())
- + {
- + LOG.severe("The following file or directory doesn't exist: " + file.getName());
- + return;
- + }
- +
- + if (file.isDirectory())
- + {
- + for (File f : file.listFiles())
- + {
- + loadDocument(f);
- + }
- + }
- + else if (file.isFile())
- + {
- + try
- + {
- + parseDocument(BUILDER.newDocumentBuilder().parse(file), file);
- + }
- + catch (Exception e)
- + {
- + LOG.log(Level.SEVERE, "Error loading XML file " + file.getName(), e);
- + }
- + }
- + }
- +
- + public Document getDocument()
- + {
- + return document;
- + }
- +
- + /**
- + * This method parses the content of a NamedNodeMap and feed the given StatsSet.
- + * @param attrs : The NamedNodeMap to parse.
- + * @param set : The StatsSet to feed.
- + */
- + public static void parseAndFeed(NamedNodeMap attrs, StatSet set)
- + {
- + for (int i = 0; i < attrs.getLength(); i++)
- + {
- + final Node attr = attrs.item(i);
- + set.set(attr.getNodeName(), attr.getNodeValue());
- + }
- + }
- +}
- diff --git data/xml/RandomCraft.xml data/xml/RandomCraft.xml
- new file mode 100644
- index 0000000..7e7a013
- --- /dev/null
- +++ data/xml/RandomCraft.xml
- @@ -0,0 +1,9 @@
- +<?xml version="1.0" encoding="UTF-8"?>
- +<rewards>
- + <item id="8762" count="1" chance="20" announce="false"/>
- + <item id="8790" count="1" chance="40" announce="true"/>
- + <item id="8793" count="1" chance="40" announce="true"/>
- + <item id="8794" count="1" chance="40" announce="true"/>
- +
- +
- +</rewards>
- \ No newline at end of file
- diff --git config/players.properties config/players.properties
- index 745d341..944d438 100644
- --- config/players.properties
- +++ config/players.properties
- @@ -235,4 +235,12 @@
- # ExpertisePenalty, чтобы отключить штраф за уровень
- # False : Игроки могут использовать экипировку S ранга с 1 уровня.
- # True : Игроки не могут использовать экипировку S ранга с 1 уровня.
- -ExpertisePenalty = True
- \ No newline at end of file
- +ExpertisePenalty = True
- +
- +
- +
- +RandomCraftItemIdRefresh = 3470
- +RandomCraftItemIdCreate = 6392
- +RandomCraftConsumeRefresh = 10
- +RandromCraftConsumeCreate = 50
- +MaxItemGeneratorForRandomCraft = 5
- diff --git sql/randomcraftitem.sql sql/randomcraftitem.sql
- new file mode 100644
- index 0000000..e5be5fd
- --- /dev/null
- +++ sql/randomcraftitem.sql
- @@ -0,0 +1,29 @@
- +/*
- +Navicat MySQL Data Transfer
- +
- +Source Server : localhost
- +Source Server Version : 50740
- +Source Host : localhost:3306
- +Source Database : 1faris
- +
- +Target Server Type : MYSQL
- +Target Server Version : 50740
- +File Encoding : 65001
- +
- +Date: 2024-01-02 21:52:28
- +*/
- +
- +SET FOREIGN_KEY_CHECKS=0;
- +
- +-- ----------------------------
- +-- Table structure for randomcraftitem
- +-- ----------------------------
- +DROP TABLE IF EXISTS `randomcraftitem`;
- +CREATE TABLE `randomcraftitem` (
- + `object_id` int(11) NOT NULL,
- + `item_id` int(11) NOT NULL,
- + `amount` int(11) NOT NULL,
- + `chance` double NOT NULL,
- + `announce` tinyint(1) NOT NULL DEFAULT '0',
- + PRIMARY KEY (`object_id`,`item_id`)
- +) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- diff --git java/Base/RandomCraft/RandomCraftItem.java java/Base/RandomCraft/RandomCraftItem.java
- new file mode 100644
- index 0000000..c69f3c0
- --- /dev/null
- +++ java/Base/RandomCraft/RandomCraftItem.java
- @@ -0,0 +1,52 @@
- +package Base.RandomCraft;
- +
- +
- +import net.sf.l2j.gameserver.data.xml.ItemData;
- +import net.sf.l2j.gameserver.model.item.kind.Item;
- +
- +public class RandomCraftItem {
- + private int id;
- + private int cantidad;
- + private double probabilidad;
- + private boolean announce;
- + private String itemName; // New field for item name
- +
- + public RandomCraftItem(int id, int cantidad, double probabilidad, boolean announce) {
- + this.id = id;
- + this.cantidad = cantidad;
- + this.probabilidad = probabilidad;
- + this.announce = announce;
- + }
- +
- + public int getId() {
- + return id;
- + }
- +
- + public int getCantidad() {
- + return cantidad;
- + }
- +
- + public double getProbabilidad() {
- + return probabilidad;
- + }
- +
- + public boolean getAnnounce() {
- + return announce;
- + }
- +
- + public String getIcon() {
- + return getItem().getIcon();
- + }
- +
- + public Item getItem() {
- + return ItemData.getInstance().getTemplate(id);
- + }
- +
- + public void setItemName(String itemName) { // Setter for item name
- + this.itemName = itemName;
- + }
- +
- + public String getItemName() { // Getter for item name
- + return itemName;
- + }
- +}
- diff --git java/Base/RandomCraft/RandomCraftXML.java java/Base/RandomCraft/RandomCraftXML.java
- new file mode 100644
- index 0000000..4448cb3
- --- /dev/null
- +++ java/Base/RandomCraft/RandomCraftXML.java
- @@ -0,0 +1,77 @@
- +package Base.RandomCraft;
- +
- +import java.io.File;
- +import java.util.HashMap;
- +import java.util.Map;
- +
- +import org.w3c.dom.Document;
- +import org.w3c.dom.NamedNodeMap;
- +import org.w3c.dom.Node;
- +import Base.Data.XMLDocument;
- +
- +public class RandomCraftXML extends XMLDocument {
- + private Map<Integer, RandomCraftItem> items;
- +
- + public RandomCraftXML() {
- + items = new HashMap<>();
- + load();
- +
- + }
- +
- + public void reload() {
- + items.clear();
- + load();
- + }
- +
- + public static RandomCraftXML getInstance() {
- + return SingletonHolder.INSTANCE;
- + }
- +
- + private static class SingletonHolder {
- + protected static final RandomCraftXML INSTANCE = new RandomCraftXML();
- + }
- +
- + @Override
- + protected void load() {
- + loadDocument("./data/xml/RandomCraft.xml");
- + LOG.info("RandomCraftItemData: Loaded " + items.size() + " items.");
- +
- + }
- +
- +
- +
- +
- + @Override
- + protected void parseDocument(Document doc, File file) {
- + try {
- + final Node root = doc.getFirstChild();
- +
- + for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
- + if (!"item".equalsIgnoreCase(node.getNodeName())) {
- + continue;
- + }
- +
- + NamedNodeMap attrs = node.getAttributes();
- + int id = Integer.parseInt(attrs.getNamedItem("id").getNodeValue());
- + int cantidad = Integer.parseInt(attrs.getNamedItem("count").getNodeValue());
- + double probabilidad = Double.parseDouble(attrs.getNamedItem("chance").getNodeValue());
- + boolean announce = Boolean.parseBoolean(attrs.getNamedItem("announce").getNodeValue());
- +
- + RandomCraftItem item = new RandomCraftItem(id, cantidad, probabilidad, announce);
- + items.put(id, item);
- +
- +
- + }
- + } catch (Exception e) {
- + e.printStackTrace();
- + }
- + }
- +
- + public Map<Integer, RandomCraftItem> getItems() {
- + return items;
- + }
- +
- + public RandomCraftItem getItemById(int id) {
- + return items.get(id);
- + }
- +}
- diff --git java/net/sf/l2j/Config.java java/net/sf/l2j/Config.java
- index 2e168ac..e9fef43 100644
- --- java/net/sf/l2j/Config.java
- +++ java/net/sf/l2j/Config.java
- @@ -57,6 +57,18 @@
- public static final String SERVER_FILE = "./config/server.properties";
- public static final String SIEGE_FILE = "./config/siege.properties";
- +
- +
- + public static int RANDOM_CRAFT_ITEM_ID_CONSUME;
- + public static int RANDOM_CRAFT_ITEM_CONSUME_REFRESH;
- + public static int RANDOM_CRAFT_ITEM_ID_CREATE;
- + public static int RANDOM_CRAFT_ITEM_CONSUME_CREATE;
- + public static int RANDOM_CRAFT_ITEM_GENERATOR;
- +
- +
- +
- +
- +
- // --------------------------------------------------
- // Clans settings
- // --------------------------------------------------
- @@ -1957,6 +1969,14 @@
- {
- final ExProperties players = initProperties(PLAYERS_FILE);
- +
- + RANDOM_CRAFT_ITEM_ID_CONSUME = players.getProperty("RandomCraftItemIdRefresh", 57);
- +
- + RANDOM_CRAFT_ITEM_CONSUME_REFRESH = players.getProperty("RandomCraftConsumeRefresh", 50000);
- + RANDOM_CRAFT_ITEM_ID_CREATE = players.getProperty("RandomCraftItemIdCreate", 3470);
- + RANDOM_CRAFT_ITEM_CONSUME_CREATE = players.getProperty("RandromCraftConsumeCreate", 300000);
- + RANDOM_CRAFT_ITEM_GENERATOR = players.getProperty("MaxItemGeneratorForRandomCraft", 5);
- +
- EFFECT_CANCELING = players.getProperty("CancelLesserEffect", true);
- HP_REGEN_MULTIPLIER = players.getProperty("HpRegenMultiplier", 1.);
- MP_REGEN_MULTIPLIER = players.getProperty("MpRegenMultiplier", 1.);
- diff --git java/net/sf/l2j/gameserver/GameServer.java java/net/sf/l2j/gameserver/GameServer.java
- index 2c0ee64..760aa4b 100644
- --- java/net/sf/l2j/gameserver/GameServer.java
- +++ java/net/sf/l2j/gameserver/GameServer.java
- @@ -116,6 +116,7 @@
- import net.sf.l2j.util.IPv4Filter;
- import Base.DropMaster.DropMaster;
- +import Base.RandomCraft.RandomCraftXML;
- import Base.TeleportInterface.TeleportLocationData;
- public class GameServer
- @@ -323,7 +324,8 @@
- StringUtil.printSection("Gk Interface");
- TeleportLocationData.getInstance();
- -
- + StringUtil.printSection("RandomCraft");
- + RandomCraftXML.getInstance();
- StringUtil.printSection("System");
- diff --git java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
- index 5b7cc45..60e0619 100644
- --- java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
- +++ java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
- @@ -28,6 +28,7 @@
- import net.sf.l2j.gameserver.model.World;
- import net.sf.l2j.gameserver.model.actor.Player;
- +import Base.RandomCraft.RandomCraftXML;
- import Base.TeleportInterface.TeleportLocationData;
- public class AdminReload implements IAdminCommandHandler
- @@ -108,7 +109,11 @@
- TeleportLocationData.getInstance().reload();
- player.sendMessage("Teleport Reloads.");
- }
- -
- + else if (type.equals("randomcraft"))
- + {
- + RandomCraftXML.getInstance().reload();
- + player.sendMessage("RandomCraft Reload.");
- + }
- else if (type.equals("npc"))
- {
- NpcData.getInstance().reload();
- @@ -182,7 +187,7 @@
- player.sendMessage("Usage : //reload <admin|announcement|buylist|config>");
- player.sendMessage("Usage : //reload <crest|cw|door|htm|item|multisell|npc>");
- player.sendMessage("Usage : //reload <npcwalker|script|skill|teleport|zone>");
- - player.sendMessage("Usage : //reload <spawnlist|sysstring|teleto|capsule>");
- + player.sendMessage("Usage : //reload <spawnlist|sysstring|randomcraft|teleto|capsule>");
- }
- @Override
- diff --git java/net/sf/l2j/gameserver/model/actor/Player.java java/net/sf/l2j/gameserver/model/actor/Player.java
- index ddfcb84..1c756b4 100644
- --- java/net/sf/l2j/gameserver/model/actor/Player.java
- +++ java/net/sf/l2j/gameserver/model/actor/Player.java
- @@ -243,6 +243,7 @@
- import net.sf.l2j.gameserver.taskmanager.WaterTaskManager;
- import Base.AutoFarm.AutofarmPlayerRoutine;
- +import Base.RandomCraft.RandomCraftItem;
- /**
- * This class represents a player in the world.<br>
- @@ -7970,4 +7971,21 @@
- }
- }
- + private List<RandomCraftItem> generatedCraftItems;
- +
- + public List<RandomCraftItem> getGeneratedCraftItems()
- + {
- + return generatedCraftItems;
- + }
- +
- + public void setGeneratedCraftItems(List<RandomCraftItem> items)
- + {
- + generatedCraftItems = items;
- + }
- +
- + public void clearGeneratedCraftItems()
- + {
- + generatedCraftItems.clear();
- + }
- +
- }
- \ No newline at end of file
- diff --git java/net/sf/l2j/gameserver/model/actor/instance/RandomCraft.java java/net/sf/l2j/gameserver/model/actor/instance/RandomCraft.java
- new file mode 100644
- index 0000000..edd3dc4
- --- /dev/null
- +++ java/net/sf/l2j/gameserver/model/actor/instance/RandomCraft.java
- @@ -0,0 +1,511 @@
- +package net.sf.l2j.gameserver.model.actor.instance;
- +
- +
- +
- +import java.sql.Connection;
- +import java.sql.PreparedStatement;
- +import java.sql.ResultSet;
- +import java.sql.SQLException;
- +import java.util.ArrayList;
- +import java.util.LinkedList;
- +import java.util.List;
- +import java.util.Map;
- +import java.util.Random;
- +
- +import net.sf.l2j.commons.pool.ConnectionPool;
- +
- +import net.sf.l2j.Config;
- +import net.sf.l2j.gameserver.data.xml.ItemData;
- +import net.sf.l2j.gameserver.model.World;
- +import net.sf.l2j.gameserver.model.actor.Npc;
- +import net.sf.l2j.gameserver.model.actor.Player;
- +import net.sf.l2j.gameserver.model.actor.template.NpcTemplate;
- +import net.sf.l2j.gameserver.model.item.instance.ItemInstance;
- +import net.sf.l2j.gameserver.model.item.kind.Item;
- +import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
- +import net.sf.l2j.gameserver.network.serverpackets.ExShowScreenMessage;
- +import net.sf.l2j.gameserver.network.serverpackets.MagicSkillUse;
- +import net.sf.l2j.gameserver.network.serverpackets.MoveToPawn;
- +import net.sf.l2j.gameserver.network.serverpackets.MyTargetSelected;
- +import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
- +import net.sf.l2j.gameserver.network.serverpackets.ValidateLocation;
- +
- +import Base.RandomCraft.RandomCraftItem;
- +import Base.RandomCraft.RandomCraftXML;
- +
- +/**
- + * @author Terius
- + */
- +
- +public class RandomCraft extends Npc
- +{
- + private boolean hasGeneratedItems;
- + List<RandomCraftItem> items = new LinkedList<>();
- +
- +
- + public RandomCraft(int objectId, NpcTemplate template)
- + {
- + super(objectId, template);
- + setHasGeneratedItems(false);
- + }
- +
- + @Override
- + public void onInteract(Player player)
- + {
- + if (this != player.getTarget())
- + {
- + player.setTarget(this);
- + player.sendPacket(new MyTargetSelected(getObjectId(), 0));
- + player.sendPacket(new ValidateLocation(this));
- + }
- + else
- + {
- + // Gira el jugador para enfrentar la instancia
- + player.sendPacket(new MoveToPawn(player, this, Npc.INTERACTION_DISTANCE));
- +
- +
- +
- + loadGeneratedItems(player);
- +
- + showHtmlWindow(player);
- +
- + // Enviar ActionFailed al jugador para evitar quedarse atascado
- + player.sendPacket(ActionFailed.STATIC_PACKET);
- + }
- + }
- +
- +
- +
- + private static String getItemNameById(int id) {
- + Item item = ItemData.getInstance().getTemplate(id);
- + if (item != null) {
- + return item.getName();
- + }
- + return "Unknown"; // Si no se encuentra el elemento, se devuelve "Unknown" como nombre predeterminado
- + }
- +
- +
- + @Override
- + public void onBypassFeedback(Player player, String command)
- + {
- + if (command.startsWith("refresh"))
- + {
- + // Verifique si el jugador tiene el artículo requerido (ID 57) y la cantidad (50000)
- + ItemInstance item57 = player.getInventory().getItemByItemId(Config.RANDOM_CRAFT_ITEM_ID_CONSUME);
- + if (item57 != null && item57.getCount() >= Config.RANDOM_CRAFT_ITEM_CONSUME_REFRESH)
- + {
- + // Cargue al jugador el artículo requerido (ID 57) y la cantidad (50000)
- + player.destroyItemByItemId("Random Craft", Config.RANDOM_CRAFT_ITEM_ID_CONSUME, Config.RANDOM_CRAFT_ITEM_CONSUME_REFRESH, null, true);
- +
- + generateItems(player);
- +
- +
- +
- +
- +
- +
- + showHtmlWindow(player);
- +
- + // Almacene los elementos generados en la base de datos para el jugador
- + saveGeneratedItems(player);
- + }
- + else
- + {
- + player.sendMessage("Necesitas al menos " + Config.RANDOM_CRAFT_ITEM_CONSUME_REFRESH + " De: " + getItemNameById(Config.RANDOM_CRAFT_ITEM_ID_CONSUME) + " para actualizar Random Craft.");
- + showHtmlWindow(player);
- + }
- + }
- + else if (command.startsWith("create"))
- + {
- +
- + // Carga al jugador el artículo con ID 57 y cantidad 300000
- + ItemInstance item57 = player.getInventory().getItemByItemId(Config.RANDOM_CRAFT_ITEM_ID_CREATE);
- + if (item57 != null && item57.getCount() >= Config.RANDOM_CRAFT_ITEM_CONSUME_CREATE) {
- + // Eliminar los elementos generados para el jugador de la tabla RandomCraftItem
- + deleteGeneratedItems(player);
- +
- + createItem(player);
- + }
- + else {
- + player.sendMessage("Necesitas al menos " + Config.RANDOM_CRAFT_ITEM_CONSUME_CREATE + " De: " + getItemNameById(Config.RANDOM_CRAFT_ITEM_ID_CREATE) + " para crear un elemento aleatorio.");
- + showHtmlWindow(player);
- + }
- +
- +
- + }
- +
- + else if (command.startsWith("back"))
- + {
- +
- + showHtmlWindow(player);
- + }
- +
- + }
- +
- + private void generateItems(Player player)
- + {
- + List<RandomCraftItem> items = new LinkedList<>();
- + RandomCraftXML randomCraftXML = RandomCraftXML.getInstance();
- + Map<Integer, RandomCraftItem> craftItems = randomCraftXML.getItems();
- +
- + // Genera 4 elementos únicos para cada jugador en función de la probabilidad
- + List<Integer> selectedItems = new ArrayList<>();
- + while (selectedItems.size() < Config.RANDOM_CRAFT_ITEM_GENERATOR)
- + {
- + int itemId = getRandomItem(craftItems);
- + if (!selectedItems.contains(itemId))
- + {
- + selectedItems.add(itemId);
- + items.add(craftItems.get(itemId));
- + }
- + }
- +
- + // Asignar los elementos generados al jugador
- + player.setGeneratedCraftItems(items);
- + setHasGeneratedItems(true);
- +
- + }
- +
- + private static void deleteGeneratedItems(Player player)
- + {
- + try (Connection con = ConnectionPool.getConnection(); PreparedStatement stmt = con.prepareStatement("DELETE FROM RandomCraftItem WHERE object_id = ?"))
- + {
- + stmt.setInt(1, player.getObjectId());
- + stmt.execute();
- + }
- + catch (SQLException e)
- + {
- + e.printStackTrace();
- + }
- + }
- +
- + private static void saveGeneratedItems(Player player)
- + {
- + try (Connection con = ConnectionPool.getConnection(); PreparedStatement stmt = con.prepareStatement("DELETE FROM RandomCraftItem WHERE object_id = ?"))
- + {
- + stmt.setInt(1, player.getObjectId());
- + stmt.execute();
- +
- + try (PreparedStatement insertStmt = con.prepareStatement("INSERT INTO RandomCraftItem (object_id, item_id, amount, chance, announce) VALUES (?, ?, ?, ?, ?)"))
- + {
- + insertStmt.setInt(1, player.getObjectId());
- + List<RandomCraftItem> items = player.getGeneratedCraftItems();
- + if (items != null)
- + {
- + for (RandomCraftItem item : items)
- + {
- + insertStmt.setInt(2, item.getId());
- + insertStmt.setInt(3, item.getCantidad());
- + insertStmt.setDouble(4, item.getProbabilidad());
- + insertStmt.setBoolean(5, item.getAnnounce());
- + insertStmt.addBatch(); // Agregar la consulta al lote (batch)
- + }
- + insertStmt.executeBatch(); // Ejecutar el lote de consultas
- + }
- + }
- + }
- + catch (SQLException e)
- + {
- + e.printStackTrace();
- + }
- + }
- +
- + private void loadGeneratedItems(Player player)
- + {
- + try (Connection con = ConnectionPool.getConnection(); PreparedStatement stmt = con.prepareStatement("SELECT item_id, amount, chance, announce FROM RandomCraftItem WHERE object_id = ?"))
- + {
- + stmt.setInt(1, player.getObjectId());
- +
- + try (ResultSet rset = stmt.executeQuery())
- + {
- + List<RandomCraftItem> items = new LinkedList<>();
- + while (rset.next())
- + {
- + int itemId = rset.getInt("item_id");
- + int amount = rset.getInt("amount");
- + int chance = rset.getInt("chance");
- + boolean announce = rset.getBoolean("announce");
- + RandomCraftItem item = new RandomCraftItem(itemId, amount, chance, announce);
- + items.add(item);
- + }
- +
- + player.setGeneratedCraftItems(items);
- + setHasGeneratedItems(!items.isEmpty());
- + }
- + }
- + catch (SQLException e)
- + {
- + e.printStackTrace();
- + }
- + }
- +
- + private static int getRandomItem(Map<Integer, RandomCraftItem> craftItems)
- + {
- + // Calcular la suma de probabilidad total
- + double totalProbability = 0;
- + for (RandomCraftItem item : craftItems.values())
- + {
- + totalProbability += item.getProbabilidad();
- + }
- +
- + // Generar un valor aleatorio entre 0 y la probabilidad total
- + Random random = new Random();
- + double randomValue = random.nextDouble() * totalProbability;
- +
- + // Seleccione el elemento en función de la probabilidad
- + double cumulativeProbability = 0;
- + for (RandomCraftItem item : craftItems.values())
- + {
- + cumulativeProbability += item.getProbabilidad();
- + if (randomValue <= cumulativeProbability)
- + {
- + return item.getId();
- + }
- + }
- +
- + // Si no se selecciona ningún artículo, devolver un artículo al azar
- + List<Integer> itemIds = new ArrayList<>(craftItems.keySet());
- + int index = random.nextInt(itemIds.size());
- + return itemIds.get(index);
- + }
- +
- + private void createItem(Player player)
- + {
- + // Comprueba si la lista de elementos está vacía
- + List<RandomCraftItem> items = player.getGeneratedCraftItems();
- + if (items == null || items.isEmpty())
- + {
- + player.sendMessage("Necesita actualizar para poder crear un elemento aleatorio.");
- + return;
- + }
- +
- + // Obtén un elemento aleatorio de la lista de elementos generados
- + Random random = new Random();
- + int index = random.nextInt(items.size());
- + RandomCraftItem craftItem = items.get(index);
- +
- + // Carga al jugador el artículo con ID 57 y cantidad 300000
- + ItemInstance item57 = player.getInventory().getItemByItemId(Config.RANDOM_CRAFT_ITEM_ID_CREATE);
- + if (item57 != null && item57.getCount() >= Config.RANDOM_CRAFT_ITEM_CONSUME_CREATE)
- + {
- +
- + // Da el artículo al jugador
- + ItemInstance itemInstance = player.getInventory().addItem("Random Craft", craftItem.getId(), craftItem.getCantidad(), player, this);
- + if (itemInstance != null)
- + {
- +
- +
- +
- + player.destroyItemByItemId("Random Craft", Config.RANDOM_CRAFT_ITEM_ID_CREATE, Config.RANDOM_CRAFT_ITEM_CONSUME_CREATE, null, true);
- +
- +
- +
- + // Envía un mensaje al jugador con el nombre del artículo y la cantidad.
- + String message = "¡Felicidades! Has recibido " + craftItem.getItem().getName() + " (Cantidad: " + craftItem.getCantidad() + ")";
- + player.sendMessage(message);
- +
- + // Obtén el nombre del jugador que creó el elemento
- + String creatorName = player.getName();
- +
- + // Comprueba si el artículo tiene announce en true en el archivo XML
- + if (craftItem.getAnnounce())
- + {
- +
- + for (Player players : World.getInstance().getPlayers())
- + {
- +
- + String text = creatorName + " Ha Crafteado: " + craftItem.getItem().getName() + " En el RandomCraft System";
- +
- + players.sendPacket(new ExShowScreenMessage(text, 5000));
- +
- + }
- +
- + }
- +
- + // Agrega un efecto al jugador si el artículo lo permite
- + if (craftItem.getAnnounce())
- + {
- + MagicSkillUse MSU = new MagicSkillUse(player, player, 2024, 1, 1, 0);
- + player.broadcastPacket(MSU);
- + }
- +
- +
- + // Borra la lista de elementos para el jugador
- + player.clearGeneratedCraftItems();
- + setHasGeneratedItems(false);
- +
- + // Muestra la ventana de felicitaciones con el item ganador
- + showCongratulationsWindow(player, craftItem);
- +
- + return;
- + }
- + }
- + player.sendMessage("Necesitas al menos " + Config.RANDOM_CRAFT_ITEM_CONSUME_CREATE + " De: " + getItemNameById(Config.RANDOM_CRAFT_ITEM_ID_CREATE) + " para crear un elemento aleatorio.");
- +
- + }
- +
- + private void showHtmlWindow(Player player) {
- + StringBuilder html = new StringBuilder();
- +
- + int requiredItemsCount = player.getInventory().getItemCount(Config.RANDOM_CRAFT_ITEM_ID_CREATE);
- +
- + html.append("<html><body>");
- + // Agregar información sobre los CraftPoints y la cantidad necesaria
- +
- + if(requiredItemsCount > 0) {
- + html.append("<center><font color=\"00FF00\">CraftPoints: ").append(requiredItemsCount).append("</font></center>");
- + }else {
- + html.append("<center><font color=\"00FF00\">CraftPoints: ").append(0).append("</font></center>");
- + }
- +
- + html.append("<center><font color=\"FF0000\">Necesarios: ").append(Config.RANDOM_CRAFT_ITEM_CONSUME_CREATE).append("</font></center>");
- + html.append("<br>");
- + html.append("<center>Bienvenido a Random Craft System!</center>");
- + html.append("<br>");
- + html.append("<center>Podrás Crear 1 item entre los 5 que salgan random!</center>");
- + html.append("<br>");
- + html.append("<center>Les Deseo Mucha Suerte</center>");
- + html.append("<br>");
- + html.append("<br>");
- +
- + List<RandomCraftItem> items = player.getGeneratedCraftItems();
- + if (items == null || items.isEmpty()) {
- + html.append("<center>La Lista Esta Vacia. Dale a Refresh</center>");
- + } else {
- + // Generar los iconos de los artículos en forma horizontal
- + for (RandomCraftItem item : items) {
- + html.append("<img src=\"L2UI.SquareGray\" width=295 height=1>");
- + html.append("<div align=center>");
- + html.append("<table>");
- + html.append("<tr>");
- + html.append("<td>");
- + html.append("<img src=").append(item.getIcon()).append(" width=32 height=32>");
- + html.append("</td>");
- + html.append("<td width=260>");
- +
- + double chance = item.getProbabilidad();
- + String color;
- + String additionalInfo;
- +
- + if (chance <= 25) {
- + color = "FF0000"; // Rojo
- + additionalInfo = " - Legendary";
- + } else if (chance <= 50) {
- + color = "9932CC"; // Violeta
- + additionalInfo = " - Epic";
- + } else {
- + color = "FFFF00"; // Amarillo
- + additionalInfo = " - Normal";
- + }
- +
- + html.append("<font color=").append(color).append(">").append(item.getItem().getName()).append(" x").append(item.getCantidad()).append(additionalInfo).append("</font>");
- +
- + html.append("</td>");
- + html.append("</tr>");
- + html.append("</table>");
- + html.append("</div>");
- + html.append("<img src=\"L2UI.SquareGray\" width=295 height=1>");
- + html.append("<br>");
- + }
- + }
- +
- + html.append("<br>");
- + html.append("<br>");
- + html.append("<center>");
- + html.append("<table>");
- + html.append("<tr>");
- + html.append("<td width=75 height=21>");
- + html.append("<button value=\"Refresh\" action=\"bypass -h npc_").append(getObjectId()).append("_refresh\" width=75 height=21 back=\"L2UI.DefaultButton_click\" fore=\"L2UI.DefaultButton\">");
- + html.append("</td>");
- + html.append("<td width=75 height=21>");
- + html.append("<button value=\"Create\" action=\"bypass -h npc_").append(getObjectId()).append("_create\" width=75 height=21 back=\"L2UI.DefaultButton_click\" fore=\"L2UI.DefaultButton\">");
- + html.append("</td>");
- + html.append("</tr>");
- + html.append("</table>");
- + html.append("</center>");
- +
- + html.append("</body></html>");
- +
- + showHtmlWindow(player, html.toString());
- + }
- +
- +
- +
- + private void showHtmlWindow(Player player, String htmlContent)
- + {
- + NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
- + html.setHtml(htmlContent);
- +
- + player.sendPacket(html);
- + }
- +
- + private void showCongratulationsWindow(Player player, RandomCraftItem craftItem) {
- + StringBuilder html = new StringBuilder();
- +
- + html.append("<html><body>");
- + html.append("<center>Felicidades, has ganado un item</center>");
- + html.append("<br>");
- + html.append("<center>¡Has recibido</center>");
- + html.append("<br>");
- + html.append("<br>");
- + html.append("<img src=\"L2UI.SquareGray\" width=295 height=1>");
- + html.append("<center>");
- + html.append("<table>");
- + html.append("<tr>");
- + html.append("<td>");
- + html.append("<img src=").append(craftItem.getIcon()).append(" width=32 height=32>");
- + html.append("</td>");
- + html.append("<td width=260>");
- +
- + double chance = craftItem.getProbabilidad();
- + String color;
- + String additionalInfo;
- +
- + if (chance <= 25) {
- + color = "FF0000"; // Rojo
- + additionalInfo = " - Legendary";
- + } else if (chance <= 50) {
- + color = "9932CC"; // Violeta
- + additionalInfo = " - Epic";
- + } else {
- + color = "FFFF00"; // Amarillo
- + additionalInfo = " - Normal";
- + }
- +
- + html.append("<font color=").append(color).append(">").append(craftItem.getItem().getName()).append(" x").append(craftItem.getCantidad()).append(additionalInfo).append("</font>");
- +
- +
- + html.append("</td>");
- + html.append("</tr>");
- + html.append("</table>");
- + html.append("</center>");
- + html.append("<img src=\"L2UI.SquareGray\" width=295 height=1>");
- + html.append("<br>");
- + html.append("<br>");
- + html.append("<br>");
- + html.append("<br>");
- + html.append("<center>");
- + html.append("<button value=\"Back\" action=\"bypass -h npc_").append(getObjectId()).append("_back\" width=75 height=21 back=\"L2UI.DefaultButton_click\" fore=\"L2UI.DefaultButton\">");
- + html.append("</center>");
- + html.append("</body></html>");
- +
- + showHtmlWindow(player, html.toString());
- + }
- +
- +
- + /**
- + * @return the hasGeneratedItems
- + */
- + public boolean isHasGeneratedItems()
- + {
- + return hasGeneratedItems;
- + }
- +
- + /**
- + * @param hasGeneratedItems the hasGeneratedItems to set
- + */
- + public void setHasGeneratedItems(boolean hasGeneratedItems)
- + {
- + this.hasGeneratedItems = hasGeneratedItems;
- + }
- +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement