Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogItem.java

489 lines
12 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.catalog;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.FurnitureType;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.messages.ISerialize;
import com.eu.habbo.messages.ServerMessage;
import gnu.trove.set.hash.THashSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
public class CatalogItem implements ISerialize, Runnable, Comparable<CatalogItem>
{
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
int id;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int pageId;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private String itemId;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private String name;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int credits;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int points;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private short pointsType;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int amount;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private boolean allowGift = false;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
int limitedStack;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int limitedSells;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private String extradata;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private boolean clubOnly;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private boolean haveOffer;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int offerId;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private boolean needsUpdate;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int orderNumber;
2018-11-17 14:28:00 +01:00
2019-03-18 02:22:00 +01:00
private HashMap<Integer, Integer> bundle;
2018-07-06 15:30:00 +02:00
public CatalogItem(ResultSet set) throws SQLException
{
this.load(set);
this.needsUpdate = false;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public void update(ResultSet set) throws SQLException
{
this.load(set);
}
private void load(ResultSet set) throws SQLException
{
this.id = set.getInt("id");
this.pageId = set.getInt("page_id");
this.itemId = set.getString("item_Ids");
this.name = set.getString("catalog_name");
this.credits = set.getInt("cost_credits");
this.points = set.getInt("cost_points");
this.pointsType = set.getShort("points_type");
this.amount = set.getInt("amount");
this.limitedStack = set.getInt("limited_stack");
this.limitedSells = set.getInt("limited_sells");
this.extradata = set.getString("extradata");
this.clubOnly = set.getBoolean("club_only");
this.haveOffer = set.getBoolean("have_offer");
this.offerId = set.getInt("offer_id");
2018-11-17 14:28:00 +01:00
this.orderNumber = set.getInt("order_number");
2018-07-06 15:30:00 +02:00
this.bundle = new HashMap<>();
this.loadBundle();
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getId()
{
return this.id;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getPageId()
{
return this.pageId;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public void setPageId(int pageId)
{
this.pageId = pageId;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public String getItemId()
{
return this.itemId;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public void setItemId(String itemId)
{
this.itemId = itemId;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public String getName()
{
return this.name;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getCredits()
{
return this.credits;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getPoints()
{
return this.points;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getPointsType()
{
return this.pointsType;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getAmount()
{
return this.amount;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getLimitedStack()
{
return this.limitedStack;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getLimitedSells()
{
CatalogLimitedConfiguration ltdConfig = Emulator.getGameEnvironment().getCatalogManager().getLimitedConfig(this);
if (ltdConfig != null)
{
return this.limitedStack - ltdConfig.available();
}
return this.limitedStack;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public String getExtradata()
{
return this.extradata;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public boolean isClubOnly()
{
return this.clubOnly;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public boolean isHaveOffer()
{
return this.haveOffer;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getOfferId()
{
return this.offerId;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public boolean isLimited()
{
return this.limitedStack > 0;
}
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int getOrderNumber()
2018-11-17 14:28:00 +01:00
{
return this.orderNumber;
}
2018-07-06 15:30:00 +02:00
public synchronized void sellRare()
{
this.limitedSells++;
this.needsUpdate = true;
if(this.limitedSells == this.limitedStack)
{
Emulator.getGameEnvironment().getCatalogManager().moveCatalogItem(this, Emulator.getConfig().getInt("catalog.ltd.page.soldout"));
}
Emulator.getThreading().run(this);
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public THashSet<Item> getBaseItems()
{
THashSet<Item> items = new THashSet<>();
if(!this.itemId.isEmpty())
{
String[] itemIds = this.itemId.split(";");
for (String itemId : itemIds)
{
if (itemId.isEmpty())
continue;
if (itemId.contains(":"))
{
itemId = itemId.split(":")[0];
}
2019-03-18 02:22:00 +01:00
int identifier;
2018-09-28 21:25:00 +02:00
try
{
identifier = Integer.parseInt(itemId);
}
catch (Exception e)
{
Emulator.getLogging().logStart("Invalid value (" + itemId + ") for items_base column for catalog_item id (" + this.id + "). Value must be integer or of the format of integer:amount;integer:amount");
continue;
}
2018-07-06 15:30:00 +02:00
if (identifier > 0)
{
Item item = Emulator.getGameEnvironment().getItemManager().getItem(identifier);
if (item != null)
items.add(item);
}
}
}
return items;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public int getItemAmount(int id)
{
if(this.bundle.containsKey(id))
return this.bundle.get(id);
else
return this.amount;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public HashMap<Integer, Integer> getBundle()
{
return this.bundle;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public void loadBundle()
{
int intItemId;
if(this.itemId.contains(";"))
{
try
{
String[] itemIds = this.itemId.split(";");
for (String itemId : itemIds)
{
if (itemId.contains(":"))
{
String[] data = itemId.split(":");
if (data.length > 1 && Integer.parseInt(data[0]) > 0 && Integer.parseInt(data[1]) > 0)
{
this.bundle.put(Integer.parseInt(data[0]), Integer.parseInt(data[1]));
}
} else
{
if (!itemId.isEmpty())
{
intItemId = (Integer.parseInt(itemId));
this.bundle.put(intItemId, 1);
}
}
}
2019-03-18 02:22:00 +01:00
}
catch (Exception e)
2018-07-06 15:30:00 +02:00
{
2019-03-18 02:22:00 +01:00
Emulator.getLogging().logDebugLine("Failed to load " + this.itemId);
Emulator.getLogging().logErrorLine(e);
2018-07-06 15:30:00 +02:00
}
}
else
{
try
{
Item item = Emulator.getGameEnvironment().getItemManager().getItem(Integer.valueOf(this.itemId));
if (item != null)
{
this.allowGift = item.allowGift();
}
}
catch (Exception e)
{}
}
}
@Override
public void serialize(ServerMessage message)
{
message.appendInt(this.getId());
message.appendString(this.getName());
message.appendBoolean(false);
message.appendInt(this.getCredits());
message.appendInt(this.getPoints());
message.appendInt(this.getPointsType());
message.appendBoolean(this.allowGift); //Can gift
THashSet<Item> items = this.getBaseItems();
message.appendInt(items.size());
for(Item item : items)
{
message.appendString(item.getType().code.toLowerCase());
if(item.getType() == FurnitureType.BADGE)
{
message.appendString(item.getName());
}
else
{
message.appendInt(item.getSpriteId());
if(this.getName().contains("wallpaper_single") || this.getName().contains("floor_single") || this.getName().contains("landscape_single"))
{
message.appendString(this.getName().split("_")[2]);
}
else if(item.getName().contains("bot") && item.getType() == FurnitureType.ROBOT)
{
boolean lookFound = false;
for (String s : this.getExtradata().split(";"))
{
if (s.startsWith("figure:"))
{
lookFound = true;
message.appendString(s.replace("figure:", ""));
break;
}
}
if (!lookFound)
{
message.appendString(this.getExtradata());
}
}
else if(item.getType() == FurnitureType.ROBOT)
{
message.appendString(this.getExtradata());
}
else if(item.getName().equalsIgnoreCase("poster"))
{
message.appendString(this.getExtradata());
}
else if(this.getName().startsWith("SONG "))
{
message.appendString(this.getExtradata());
}
else
{
message.appendString("");
}
message.appendInt(this.getItemAmount(item.getId()));
message.appendBoolean(this.isLimited());
if(this.isLimited())
{
message.appendInt(this.getLimitedStack());
message.appendInt(this.getLimitedStack() - this.getLimitedSells());
}
}
}
2018-10-07 00:28:00 +02:00
message.appendInt(this.clubOnly);
2018-07-06 15:30:00 +02:00
message.appendBoolean(haveOffer(this));
message.appendBoolean(false); //unknown
message.appendString(this.name + ".png");
}
@Override
public void run()
{
if(this.needsUpdate)
{
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE catalog_items SET limited_sells = ?, page_id = ? WHERE id = ?"))
{
statement.setInt(1, this.getLimitedSells());
statement.setInt(2, this.pageId);
statement.setInt(3, this.getId());
statement.execute();
}
catch(SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
this.needsUpdate = false;
}
}
@SuppressWarnings("NullableProblems")
@Override
2018-11-17 14:28:00 +01:00
public int compareTo(CatalogItem catalogItem)
{
if (CatalogManager.SORT_USING_ORDERNUM)
{
return this.getOrderNumber() - catalogItem.getOrderNumber();
}
else
{
return this.getId() - catalogItem.getId();
}
2018-07-06 15:30:00 +02:00
}
2018-07-08 23:32:00 +02:00
2018-09-28 21:25:00 +02:00
public static boolean haveOffer(CatalogItem item)
2018-07-06 15:30:00 +02:00
{
if(!item.haveOffer)
return false;
2018-09-28 21:25:00 +02:00
if(item.getAmount() != 1)
2018-07-06 15:30:00 +02:00
return false;
if(item.isLimited())
return false;
2018-09-28 21:25:00 +02:00
if (item.bundle.size() > 1)
2018-07-06 15:30:00 +02:00
return false;
2018-09-28 21:25:00 +02:00
if(item.getName().toLowerCase().startsWith("cf_") || item.getName().toLowerCase().startsWith("cfc_"))
return false;
for(Item i : item.getBaseItems())
{
if(i.getName().toLowerCase().startsWith("cf_") || i.getName().toLowerCase().startsWith("cfc_") || i.getName().toLowerCase().startsWith("rentable_bot"))
return false;
}
2019-03-18 02:22:00 +01:00
return !item.getName().toLowerCase().startsWith("rentable_bot_");
2018-07-06 15:30:00 +02:00
}
}