mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 16:59:39 -05:00
Began rework to dynamic gifts. Did not fetch from url yet.
This commit is contained in:
@@ -22,19 +22,18 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.gifts;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
|
||||
import io.github.jwdeveloper.tiktok.live.GiftManager;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class TikTokGiftManager implements GiftManager {
|
||||
|
||||
private final Map<Integer, GiftOld> indexById;
|
||||
private final Map<String, GiftOld> indexByName;
|
||||
private final Map<Integer, Gift> indexById;
|
||||
private final Map<String, Gift> indexByName;
|
||||
private final Logger logger;
|
||||
|
||||
public TikTokGiftManager(Logger logger)
|
||||
@@ -46,58 +45,29 @@ public class TikTokGiftManager implements GiftManager {
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
for (var gift : GiftOld.values()) {
|
||||
for (var gift : Gift.getGifts()) {
|
||||
indexById.put(gift.getId(), gift);
|
||||
indexByName.put(gift.getName(), gift);
|
||||
}
|
||||
}
|
||||
|
||||
public GiftOld registerGift(int id, String name, int diamondCost, Picture picture) {
|
||||
try {
|
||||
var constructor = Unsafe.class.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
var unsafe = (Unsafe) constructor.newInstance();
|
||||
GiftOld enumInstance = (GiftOld) unsafe.allocateInstance(GiftOld.class);
|
||||
|
||||
var field = GiftOld.class.getDeclaredField("id");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, id);
|
||||
|
||||
field = GiftOld.class.getDeclaredField("name");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, name);
|
||||
|
||||
|
||||
// EnumSet
|
||||
field = GiftOld.class.getDeclaredField("diamondCost");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, diamondCost);
|
||||
|
||||
field = GiftOld.class.getDeclaredField("picture");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, picture);
|
||||
|
||||
indexById.put(enumInstance.getId(), enumInstance);
|
||||
indexByName.put(enumInstance.getName(), enumInstance);
|
||||
|
||||
return enumInstance;
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unable to register gift: " + name + ": " + id);
|
||||
}
|
||||
public Gift registerGift(int id, String name, int diamondCost, Picture picture, JsonObject properties) {
|
||||
Gift gift = new Gift(id, name, diamondCost, picture, properties);
|
||||
indexById.put(gift.getId(), gift);
|
||||
indexByName.put(gift.getName(), gift);
|
||||
return gift;
|
||||
}
|
||||
|
||||
public GiftOld findById(int giftId) {
|
||||
GiftOld gift = indexById.get(giftId);
|
||||
return gift == null ? GiftOld.UNDEFINED : gift;
|
||||
public Gift findById(int giftId) {
|
||||
return indexById.getOrDefault(giftId, Gift.UNDEFINED);
|
||||
}
|
||||
|
||||
public GiftOld findByName(String giftName) {
|
||||
GiftOld gift = indexByName.get(giftName);
|
||||
return gift == null ? GiftOld.UNDEFINED : gift;
|
||||
public Gift findByName(String giftName) {
|
||||
return indexByName.getOrDefault(giftName, Gift.UNDEFINED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GiftOld> getGifts() {
|
||||
public List<Gift> getGifts() {
|
||||
return indexById.values().stream().toList();
|
||||
}
|
||||
}
|
||||
@@ -24,11 +24,9 @@ package io.github.jwdeveloper.tiktok.mappers.handlers;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.TikTokRoomInfo;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftComboEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.*;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftSendType;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.live.GiftManager;
|
||||
import io.github.jwdeveloper.tiktok.mappers.TikTokMapperHelper;
|
||||
@@ -37,9 +35,7 @@ import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
|
||||
import lombok.SneakyThrows;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class TikTokGiftEventHandler {
|
||||
private final GiftManager giftManager;
|
||||
@@ -112,37 +108,35 @@ public class TikTokGiftEventHandler {
|
||||
return new TikTokGiftComboEvent(gift, tikTokRoomInfo.getHost(), message, state);
|
||||
}
|
||||
|
||||
private GiftOld getGiftObject(WebcastGiftMessage giftMessage) {
|
||||
private Gift getGiftObject(WebcastGiftMessage giftMessage) {
|
||||
var giftId = (int) giftMessage.getGiftId();
|
||||
var gift = giftManager.findById(giftId);
|
||||
if (gift == GiftOld.UNDEFINED) {
|
||||
if (gift == Gift.UNDEFINED)
|
||||
gift = giftManager.findByName(giftMessage.getGift().getName());
|
||||
}
|
||||
if (gift == GiftOld.UNDEFINED) {
|
||||
if (gift == Gift.UNDEFINED) {
|
||||
gift = giftManager.registerGift(
|
||||
giftId,
|
||||
giftMessage.getGift().getName(),
|
||||
giftMessage.getGift().getDiamondCount(),
|
||||
Picture.map(giftMessage.getGift().getImage()));
|
||||
giftId,
|
||||
giftMessage.getGift().getName(),
|
||||
giftMessage.getGift().getDiamondCount(),
|
||||
Picture.map(giftMessage.getGift().getImage()));
|
||||
}
|
||||
|
||||
if (gift.getPicture().getLink().endsWith(".webp")) {
|
||||
if (gift.getPicture().getLink().endsWith(".webp"))
|
||||
updatePicture(gift, giftMessage);
|
||||
}
|
||||
return gift;
|
||||
}
|
||||
|
||||
|
||||
private void updatePicture(GiftOld gift, WebcastGiftMessage webcastGiftMessage) {
|
||||
// TODO-kohlerpop1: I do not think this method is needed for any reason?
|
||||
private void updatePicture(Gift gift, WebcastGiftMessage webcastGiftMessage) {
|
||||
try {
|
||||
var picture = Picture.map(webcastGiftMessage.getGift().getImage());
|
||||
var constructor = Unsafe.class.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
var field = GiftOld.class.getDeclaredField("picture");
|
||||
var field = Gift.class.getDeclaredField("picture");
|
||||
field.setAccessible(true);
|
||||
field.set(gift, picture);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unable to update picture in gift: " + gift.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user