9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-30 04:19:30 +00:00

1.21.4 custommodeldata

This commit is contained in:
XiaoMoMi
2024-12-10 18:09:05 +08:00
parent dfebe52f46
commit 0ac8fc44a1
3 changed files with 33 additions and 7 deletions

View File

@@ -87,6 +87,7 @@ public class GetItemCommand extends BukkitCommandFeature<CommandSender> {
handleFeedback(context, MessageConstants.COMMAND_ITEM_GET_SUCCESS, Component.text(amount), Component.text(id));
} catch (Exception e) {
handleFeedback(context, MessageConstants.COMMAND_ITEM_FAILURE_NOT_EXIST, Component.text(id));
BukkitCustomFishingPlugin.getInstance().getPluginLogger().warn("Failed to get item:" + id, e);
}
});
}

View File

@@ -20,6 +20,7 @@ package net.momirealms.customfishing.bukkit.item.impl;
import com.saicone.rtag.RtagItem;
import com.saicone.rtag.data.ComponentType;
import net.momirealms.customfishing.bukkit.item.BukkitItemFactory;
import net.momirealms.customfishing.common.helper.VersionHelper;
import net.momirealms.customfishing.common.item.ComponentKeys;
import net.momirealms.customfishing.common.plugin.CustomFishingPlugin;
import net.momirealms.customfishing.common.util.Key;
@@ -29,12 +30,37 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
@SuppressWarnings("UnstableApiUsage")
public class ComponentItemFactory extends BukkitItemFactory {
private final BiConsumer<RtagItem, Integer> customModelDataSetter;
private final Function<RtagItem, Optional<Integer>> customModelDataGetter;
public ComponentItemFactory(CustomFishingPlugin plugin) {
super(plugin);
this.customModelDataSetter = VersionHelper.isVersionNewerThan1_21_4() ?
((item, data) -> item.setComponent(ComponentKeys.CUSTOM_MODEL_DATA,
Map.of("floats", List.of(data.floatValue())))) : ((item, data) -> item.setComponent(ComponentKeys.CUSTOM_MODEL_DATA, data));
this.customModelDataGetter = VersionHelper.isVersionNewerThan1_21_4() ?
(item) -> {
Optional<Object> optional = ComponentType.encodeJava(ComponentKeys.CUSTOM_MODEL_DATA, item.getComponent(ComponentKeys.CUSTOM_MODEL_DATA));
if (optional.isEmpty()) return Optional.empty();
@SuppressWarnings("unchecked")
Map<String, Object> data = (Map<String, Object>) optional.get();
@SuppressWarnings("unchecked")
List<Float> floats = (List<Float>) data.get("floats");
if (floats == null || floats.isEmpty()) return Optional.empty();
return Optional.of((int) Math.floor(floats.get(0)));
} : (item) -> Optional.ofNullable(
(Integer) ComponentType.encodeJava(
ComponentKeys.CUSTOM_MODEL_DATA,
item.getComponent(ComponentKeys.CUSTOM_MODEL_DATA)
).orElse(null)
);
}
@Override
@@ -42,19 +68,14 @@ public class ComponentItemFactory extends BukkitItemFactory {
if (data == null) {
item.removeComponent(ComponentKeys.CUSTOM_MODEL_DATA);
} else {
item.setComponent(ComponentKeys.CUSTOM_MODEL_DATA, data);
this.customModelDataSetter.accept(item, data);
}
}
@Override
protected Optional<Integer> customModelData(RtagItem item) {
if (!item.hasComponent(ComponentKeys.CUSTOM_MODEL_DATA)) return Optional.empty();
return Optional.ofNullable(
(Integer) ComponentType.encodeJava(
ComponentKeys.CUSTOM_MODEL_DATA,
item.getComponent(ComponentKeys.CUSTOM_MODEL_DATA)
).orElse(null)
);
return this.customModelDataGetter.apply(item);
}
@Override