Taterzens [Fabric]

Taterzens [Fabric]

86.3k Downloads

Potential crashes when using messages / path nodes

samolego opened this issue ยท 1 comments

commented

int msgPos = pl.getLastMsgPos();
if(this.npcData.messages.get(msgPos).getSecond() < pl.ticksSinceLastMessage()) {
entity.sendSystemMessage(
this.getName().copy().append(" -> you: ").append(this.npcData.messages.get(pl.getLastMsgPos()).getFirst()),
this.uuid
);
// Resetting message counter
pl.resetMessageTicks();
if(++msgPos >= this.npcData.messages.size())
msgPos = 0;
// Setting new message position
pl.setLastMsgPos(msgPos);

This part sets the next message index after message has been sent. If messages are cleared during the coolodown (while this.npcData.messages.get(msgPos).getSecond() < pl.ticksSinceLastMessage() is false), mod will crash with out of bounds exception.

Same can happen with path nodes.

Potential solution

this.world.getEntityCollisions(this, box, entity -> {
    if(entity instanceof ServerPlayerEntity && ((TaterzenEditor) entity).getEditorMode() != TaterzenEditor.Types.MESSAGES) {
        TaterzenPlayer pl = (TaterzenPlayer) entity;
        int msgPos = pl.getLastMsgPos();
+     if(msgPos > this.npcData.messages.size())
+        msgPos = 0;
        if(this.npcData.messages.get(msgPos).getSecond() < pl.ticksSinceLastMessage()) {
            entity.sendSystemMessage(
                    this.getName().copy().append(" -> you: ").append(this.npcData.messages.get(pl.getLastMsgPos()).getFirst()),
                    this.uuid
            );
            // Resetting message counter
            pl.resetMessageTicks();

-           if(++msgPos >= this.npcData.messages.size())
-               msgPos = 0;
            // Setting new message position
+          pl.setLastMsgPos(++msgPos);
-          pl.setLastMsgPos(msgPos);
        }
        return true;
    }
    return false;
});
commented

Probably related to #2 as well