Skip to content

Commit 92dc52c

Browse files
committed
KillRecorderItem - more stuff
1 parent 83a6e88 commit 92dc52c

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

src/main/java/net/roboxgamer/modernutils/data/KillData.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@
99
import java.util.List;
1010
import java.util.function.IntFunction;
1111

12-
public record KillData(List<String> kills, boolean isRecording, int totalXp) {
12+
public record KillData(List<String> kills, boolean isRecording, int totalXp, long recordingStart, long recordingEnd) {
1313
private static final IntFunction<List<String>> LIST_FACTORY = ArrayList::new;
1414

1515
public static final Codec<KillData> CODEC = RecordCodecBuilder.create(instance ->
1616
instance.group(
1717
Codec.list(Codec.STRING).fieldOf("kills").forGetter(KillData::kills),
1818
Codec.BOOL.fieldOf("recording").forGetter(KillData::isRecording),
19-
Codec.INT.fieldOf("totalXp").forGetter(KillData::totalXp)
19+
Codec.INT.fieldOf("totalXp").forGetter(KillData::totalXp),
20+
Codec.LONG.fieldOf("recordingStart").forGetter(KillData::recordingStart),
21+
Codec.LONG.fieldOf("recordingEnd").forGetter(KillData::recordingEnd)
2022
).apply(instance, KillData::new)
2123
);
2224

2325
public static final StreamCodec<RegistryFriendlyByteBuf, KillData> STREAM_CODEC = StreamCodec.composite(
2426
ByteBufCodecs.collection(LIST_FACTORY, ByteBufCodecs.STRING_UTF8), KillData::kills,
2527
ByteBufCodecs.BOOL, KillData::isRecording,
2628
ByteBufCodecs.INT, KillData::totalXp,
29+
ByteBufCodecs.VAR_LONG, KillData::recordingStart,
30+
ByteBufCodecs.VAR_LONG, KillData::recordingEnd,
2731
KillData::new);
2832

2933
public KillData addKill(String entityName, int xp) {
3034
List<String> newKills = kills();
3135
newKills.add(entityName);
32-
return new KillData(newKills, isRecording(), totalXp() + xp);
36+
return new KillData(newKills, isRecording(), totalXp() + xp, recordingStart(), recordingEnd());
37+
}
38+
39+
public static KillData createEmpty() {
40+
return new KillData(new ArrayList<>(), false, 0, 0L, 0L);
3341
}
3442
}

src/main/java/net/roboxgamer/modernutils/event/ModEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void onEntityKilled(LivingDeathEvent event) {
2828
// Update kill data with entity name and XP
2929
stack.update(
3030
ModCustomDataComponents.KILL_DATA.get(),
31-
new KillData(killData.kills(), killData.isRecording(), killData.totalXp()),
31+
killData,
3232
data -> data.addKill(killedEntity.getName().getString(), xpReward)
3333
);
3434
}

src/main/java/net/roboxgamer/modernutils/item/custom/KillRecorderItem.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import net.minecraft.world.item.ItemStack;
1010
import net.minecraft.world.level.Level;
1111
import net.minecraft.core.component.DataComponentType;
12+
import net.minecraft.world.item.TooltipFlag;
1213
import net.roboxgamer.modernutils.data.KillData;
1314
import net.roboxgamer.modernutils.item.ModCustomDataComponents;
1415

1516
import java.util.ArrayList;
17+
import java.util.List;
1618

1719
public class KillRecorderItem extends Item {
1820
public KillRecorderItem(Properties properties) {
@@ -26,16 +28,67 @@ public InteractionResultHolder<ItemStack> use(Level level, Player player, Intera
2628

2729
if (player.isShiftKeyDown()) {
2830
DataComponentType<KillData> killDataType = ModCustomDataComponents.KILL_DATA.get();
29-
KillData currentData = stack.getOrDefault(killDataType, new KillData(new ArrayList<>(), false, 0));
31+
KillData currentData = stack.getOrDefault(killDataType, KillData.createEmpty());
3032

31-
// Toggle recording state
32-
KillData newData = new KillData(currentData.kills(), !currentData.isRecording(), currentData.totalXp());
33-
stack.set(killDataType, newData);
34-
35-
player.displayClientMessage(Component.literal("Kill Recording: " +
36-
(newData.isRecording() ? "Enabled" : "Disabled")), true);
33+
if (!currentData.isRecording()) {
34+
// Start recording - clear previous data and start fresh
35+
long currentTime = System.currentTimeMillis();
36+
stack.update(
37+
killDataType,
38+
currentData,
39+
data -> new KillData(new ArrayList<>(), true, 0, currentTime, 0L)
40+
);
41+
player.displayClientMessage(Component.literal("Kill Recording: Enabled (Data Cleared)"), true);
42+
} else {
43+
// Stop recording
44+
long currentTime = System.currentTimeMillis();
45+
stack.update(
46+
killDataType,
47+
currentData,
48+
data -> new KillData(data.kills(), false, data.totalXp(), data.recordingStart(), currentTime)
49+
);
50+
player.displayClientMessage(Component.literal("Kill Recording: Disabled"), true);
51+
}
3752
}
3853
}
3954
return super.use(level, player, hand);
4055
}
56+
@Override
57+
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltipComponents,
58+
TooltipFlag tooltipFlag) {
59+
super.appendHoverText(stack, context, tooltipComponents, tooltipFlag);
60+
61+
DataComponentType<KillData> killDataType = ModCustomDataComponents.KILL_DATA.get();
62+
KillData data = stack.getOrDefault(killDataType, KillData.createEmpty());
63+
64+
// Recording Status
65+
tooltipComponents.add(Component.literal("§6Status: " + (data.isRecording() ? "§aRecording" : "§cNot Recording")));
66+
67+
// Kills Count
68+
tooltipComponents.add(Component.literal("§6Total Kills: §f" + data.kills().size()));
69+
70+
// Total XP
71+
tooltipComponents.add(Component.literal("§6Total XP: §f" + data.totalXp()));
72+
73+
// Recording Duration
74+
if (data.recordingStart() > 0) {
75+
long duration;
76+
if (data.isRecording()) {
77+
duration = System.currentTimeMillis() - data.recordingStart();
78+
} else if (data.recordingEnd() > 0) {
79+
duration = data.recordingEnd() - data.recordingStart();
80+
} else {
81+
duration = 0;
82+
}
83+
84+
long seconds = duration / 1000;
85+
long minutes = seconds / 60;
86+
seconds = seconds % 60;
87+
tooltipComponents.add(Component.literal(String.format("§6Duration: §f%02d:%02d", minutes, seconds)));
88+
}
89+
90+
// Instructions
91+
tooltipComponents.add(Component.literal(""));
92+
tooltipComponents.add(Component.literal("§7Shift + Right Click to toggle recording"));
93+
}
4194
}

0 commit comments

Comments
 (0)