Skip to content

Commit b601ce9

Browse files
authored
Add tag support for disabling (#159)
1 parent 1fa331a commit b601ce9

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

common/src/main/java/com/xtracr/realcamera/compat/CompatibilityHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class CompatibilityHelper {
1414
private static Method NEA_playerTransformer_setDeltaTick;
1515
private static Field NEA_NEAnimationsLoader_INSTANCE;
1616
private static Field NEA_NEAnimationsLoader_playerTransformer;
17+
private static Class<?> SW_ClientEventHandler;
18+
private static Field SW_ClientEventHandler_zoomTime;
1719

1820
public static void initialize(PlatformHelper platformHelper) {
1921
CompatibilityHelper.platformHelper = platformHelper;
@@ -41,6 +43,23 @@ public static void initialize(PlatformHelper platformHelper) {
4143
} catch (Exception e) {
4244
RealCamera.LOGGER.warn("Compatibility with Not Enough Animations is outdated: [{}] {}", e.getClass().getName(), e.getMessage());
4345
}
46+
if(isModLoaded("superbwarfare")) try{
47+
SW_ClientEventHandler = Class.forName("com.atsuishio.superbwarfare.event.ClientEventHandler");
48+
SW_ClientEventHandler_zoomTime = SW_ClientEventHandler.getDeclaredField("zoomTime");
49+
DisableHelper.MAIN_FEATURE.registerOrInBinding(player -> CompatibilityHelper.SW_gunsIsZooming());
50+
} catch (Exception e) {
51+
RealCamera.LOGGER.warn("SuperbWarfare is not loaded correctly: [{}] {}", e.getClass().getName(), e.getMessage());
52+
}
53+
}
54+
55+
private static boolean SW_gunsIsZooming(){
56+
try {
57+
double zoomTimeValue = SW_ClientEventHandler_zoomTime.getDouble(null);
58+
return zoomTimeValue > 0;
59+
} catch (Exception e) {
60+
RealCamera.LOGGER.error("Failed to access SuperbWarfare's zoomTime field", e);
61+
return false;
62+
}
4463
}
4564

4665
public static void NEA_setDeltaTick(float deltaTick) {

common/src/main/java/com/xtracr/realcamera/compat/DisableHelper.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import com.xtracr.realcamera.RealCameraCore;
44
import com.xtracr.realcamera.config.ConfigFile;
5+
import net.minecraft.core.Holder;
56
import net.minecraft.core.registries.BuiltInRegistries;
7+
import net.minecraft.resources.ResourceKey;
8+
import net.minecraft.resources.ResourceLocation;
9+
import net.minecraft.tags.TagKey;
610
import net.minecraft.world.entity.Entity;
711
import net.minecraft.world.entity.LivingEntity;
812
import net.minecraft.world.entity.player.Player;
13+
import net.minecraft.world.item.Item;
914

1015
import java.util.HashMap;
1116
import java.util.Map;
17+
import java.util.Optional;
1218
import java.util.function.Predicate;
1319

1420
public class DisableHelper {
@@ -28,15 +34,15 @@ public class DisableHelper {
2834
String mainHand = BuiltInRegistries.ITEM.getKey(player.getMainHandItem().getItem()).toString();
2935
String offHand = BuiltInRegistries.ITEM.getKey(player.getOffhandItem().getItem()).toString();
3036
for (String item : ConfigFile.config().getDisableMainFeatureItems())
31-
if (simpleWildcardMatch(mainHand, item) || simpleWildcardMatch(offHand, item))
37+
if (matchesItemPattern(mainHand, item) || matchesItemPattern(offHand, item))
3238
return true;
3339
return false;
3440
});
3541
RENDER_MODEL.registerOrInBinding(player -> {
3642
String mainHand = BuiltInRegistries.ITEM.getKey(player.getMainHandItem().getItem()).toString();
3743
String offHand = BuiltInRegistries.ITEM.getKey(player.getOffhandItem().getItem()).toString();
3844
for (String item : ConfigFile.config().getDisableRenderItems())
39-
if (simpleWildcardMatch(mainHand, item) || simpleWildcardMatch(offHand, item))
45+
if (matchesItemPattern(mainHand, item) || matchesItemPattern(offHand, item))
4046
return true;
4147
return false;
4248
});
@@ -62,7 +68,20 @@ public static void registerOr(String name, Predicate<LivingEntity> predicate) {
6268
entries.get(name).registerOrInBinding(predicate::test);
6369
}
6470

65-
public static boolean simpleWildcardMatch(String text, String pattern) {
71+
public static boolean matchesItemPattern(String text, String pattern) {
72+
if (pattern.startsWith("#")) {
73+
String tagId = pattern.substring(1);
74+
TagKey<Item> itemTag = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.parse(tagId));
75+
return BuiltInRegistries.ITEM.getTag(itemTag)
76+
.<Boolean>map(tag -> {
77+
ResourceLocation itemLocation = ResourceLocation.tryParse(text);
78+
if (itemLocation == null) return false;
79+
ResourceKey<Item> itemKey = ResourceKey.create(BuiltInRegistries.ITEM.key(), itemLocation);
80+
Optional<Holder.Reference<Item>> itemRef = BuiltInRegistries.ITEM.getHolder(itemKey);
81+
return itemRef.map(holderRef -> tag.contains(holderRef)).orElse(false);
82+
})
83+
.orElse(false);
84+
}
6685
if (pattern.isEmpty()) return text.isEmpty();
6786
String[] parts = pattern.split("\\*+");
6887
if (parts.length == 0) return true;

common/src/main/resources/assets/realcamera/lang/en_us.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"config.tooltip.xtracr_realcamera.bindResultRetentionFrames": "For several frames after the binding failure, continue to use the previous successful binding result",
6565
"config.tooltip.xtracr_realcamera.displacementSmoothFactor": "Smooth factor of camera displacement, the higher the value, the smoother the movement",
6666
"config.tooltip.xtracr_realcamera.rotationSmoothFactor": "Smooth factor of camera rotation, the higher the value, the smoother the movement",
67-
"config.tooltip.xtracr_realcamera.disableRenderItems": "Support using `*` to match any number of characters, for example `minecraft:*sword` matches all swords",
67+
"config.tooltip.xtracr_realcamera.disableRenderItems": "Three matching methods are supported:\n1. Wildcard `*` matching (e.g., `minecraft:*sword`)\n2. Tag matching (starts with `#`, e.g., `#minecraft:tools`)\n3. Direct item ID (e.g., `minecraft:diamond_sword`)",
6868

6969
"message.xtracr_realcamera.bindingFailed": "[%s]: Binding failed, please go to the %s screen and configure manually (Key: %s)",
7070

common/src/main/resources/assets/realcamera/lang/zh_cn.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"config.tooltip.xtracr_realcamera.bindResultRetentionFrames": "在绑定失败后的数帧内,沿用上个成功的绑定结果",
6565
"config.tooltip.xtracr_realcamera.displacementSmoothFactor": "相机位移平滑系数,值越大越平滑",
6666
"config.tooltip.xtracr_realcamera.rotationSmoothFactor": "相机旋转平滑系数,值越大越平滑",
67-
"config.tooltip.xtracr_realcamera.disableRenderItems": "支持用`*`匹配任意数量字符,比如用`minecraft:*sword`匹配所有剑",
67+
"config.tooltip.xtracr_realcamera.disableRenderItems": "支持三种匹配方式:\n1. 通配符`*`匹配(如`minecraft:*sword`\n2. 标签匹配以`#`开头(如`#minecraft:tools`)\n3. 直接物品ID(如`minecraft:diamond_sword`)",
6868

6969
"message.xtracr_realcamera.bindingFailed": "[%s]: 绑定失败,请前往%s界面手动设置(键位:%s)",
7070

0 commit comments

Comments
 (0)