Skip to content

Commit

Permalink
PotionTypes now requires a version to work correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrooper committed Oct 11, 2023
1 parent 4e151f8 commit 90edf60
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

package com.github.retrooper.packetevents.protocol.potion;

import com.github.retrooper.packetevents.protocol.mapper.StaticMappedEntity;
import com.github.retrooper.packetevents.protocol.mapper.MappedEntity;

public interface PotionType extends StaticMappedEntity {
public interface PotionType extends MappedEntity {

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.github.retrooper.packetevents.protocol.potion;

import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

Expand All @@ -37,22 +38,26 @@ public ResourceLocation getName() {
}

@Override
public int getId() {
public int getId(ClientVersion version) {
if (version.isOlderThan(ClientVersion.V_1_20_2)) {
return id + 1;
}
return id;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof PotionType) {
return getId() == ((PotionType)obj).getId();
return getName().equals(((PotionType)obj).getName());
}
return false;
}
};

//minecraft:speed would be a string for example
POTION_TYPE_MAP.put(potionType.getName().toString(), potionType);
POTION_TYPE_ID_MAP.put((byte) potionType.getId(), potionType);
//We map the 1.20.2 IDs
POTION_TYPE_ID_MAP.put((byte) potionType.getId(ClientVersion.V_1_20_2), potionType);
return potionType;
}

Expand All @@ -67,7 +72,13 @@ public static PotionType getByName(String name) {
}

public static @Nullable PotionType getById(int id, com.github.retrooper.packetevents.manager.server.ServerVersion version) {
if (version.isOlderThan(com.github.retrooper.packetevents.manager.server.ServerVersion.V_1_20_2)) {
return getById(id, version.toClientVersion());
}

public static @Nullable PotionType getById(int id, ClientVersion version) {
if (version.isOlderThan(ClientVersion.V_1_20_2)) {
//We map the 1.20.2 values (starting from 0)
//If the version is older than that, we must subtract one from the input ID (cause it starts from 1)
id--; // potion effects ids where shifted by -1 in 1.20.2
}
return POTION_TYPE_ID_MAP.get((byte)id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public void read() {
public void write() {
writeVarInt(entityID);
if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_18_2)) {
writeVarInt(potionType.getId());
writeVarInt(potionType.getId(serverVersion.toClientVersion()));
} else {
writeByte(potionType.getId());
writeByte(potionType.getId(serverVersion.toClientVersion()));
}
writeByte(effectAmplifier);
writeVarInt(effectDurationTicks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public void read() {
public void write() {
writeVarInt(entityId);
if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_18_2)) {
writeVarInt(potionType.getId());
writeVarInt(potionType.getId(serverVersion.toClientVersion()));
} else {
writeByte(potionType.getId());
writeByte(potionType.getId(serverVersion.toClientVersion()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static PotionType fromBukkitPotionEffectType(org.bukkit.potion.PotionEffe
}

public static org.bukkit.potion.PotionEffectType toBukkitPotionEffectType(PotionType potionType) {
return org.bukkit.potion.PotionEffectType.getById(potionType.getId());
return org.bukkit.potion.PotionEffectType.getById(potionType.getId(PacketEvents.getAPI().getServerManager().getVersion().toClientVersion()));
}

public static GameMode fromBukkitGameMode(org.bukkit.GameMode gameMode) {
Expand Down

0 comments on commit 90edf60

Please sign in to comment.