-
Notifications
You must be signed in to change notification settings - Fork 150
Entity nulling rework #1299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Robotgiggle
wants to merge
13
commits into
1.21
Choose a base branch
from
entity-nulling-rework
base: 1.21
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Entity nulling rework #1299
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2929f2c
Check for offline playerness without needing a boolean
Robotgiggle d7435b8
Don't null missing entities; throw mishap when used instead
Robotgiggle 08f00a8
Document new mishap in the book
Robotgiggle 566c8d1
Update changelog
Robotgiggle 9630d25
Check for null in the other entity getter methods
Robotgiggle d515c78
Tweak book description
Robotgiggle c3084f7
Only read from disk on server startup
Robotgiggle 6e2a518
Allow binding cleric impeti with offline truenames
Robotgiggle a5ff896
Handle invalid playerdata filenames
Robotgiggle ea2904e
Make nonexistent entities falsy
Robotgiggle 5650adf
Update changelog
Robotgiggle 688e917
Un-special-case the entity falsiness
Robotgiggle 8e86469
Allow truthiness checking without an env just in case
Robotgiggle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package at.petrak.hexcasting.api.casting.iota; | ||
|
|
||
| import at.petrak.hexcasting.api.casting.eval.CastingEnvironment; | ||
| import at.petrak.hexcasting.common.lib.hex.HexIotaTypes; | ||
| import com.mojang.serialization.Codec; | ||
| import com.mojang.serialization.MapCodec; | ||
| import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
| import com.samsthenerd.inline.api.InlineAPI; | ||
|
|
@@ -15,31 +15,34 @@ | |
| import net.minecraft.network.chat.MutableComponent; | ||
| import net.minecraft.network.codec.ByteBufCodecs; | ||
| import net.minecraft.network.codec.StreamCodec; | ||
| import net.minecraft.server.MinecraftServer; | ||
| import net.minecraft.server.level.ServerLevel; | ||
| import net.minecraft.world.entity.Entity; | ||
| import net.minecraft.world.entity.player.Player; | ||
| import net.minecraft.world.item.component.ResolvableProfile; | ||
| import net.minecraft.world.level.storage.LevelResource; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public class EntityIota extends Iota { | ||
| private static final HashSet<UUID> playerUUIDs = new HashSet<UUID>(); | ||
| private final UUID entityId; | ||
| @Nullable | ||
| private final Component entityName; | ||
| private boolean isPlayer; | ||
|
|
||
| public EntityIota(@NotNull Entity e) { | ||
| this(e.getUUID(), getEntityNameWithInline(e), e instanceof Player); | ||
| this(e.getUUID(), getEntityNameWithInline(e)); | ||
| } | ||
|
|
||
| public EntityIota(UUID entityId, @Nullable Component entityName, boolean isPlayer) { | ||
| public EntityIota(UUID entityId, @Nullable Component entityName) { | ||
| super(() -> HexIotaTypes.ENTITY.get()); | ||
| this.entityId = entityId; | ||
| this.entityName = entityName; | ||
| this.isPlayer = isPlayer; | ||
| } | ||
|
|
||
| public UUID getEntityId() { | ||
|
|
@@ -54,8 +57,13 @@ public Entity getEntity(ServerLevel level) { | |
| return entityName; | ||
| } | ||
|
|
||
| public boolean isPlayer() { | ||
| return isPlayer; | ||
| public @Nullable String getRawName() { | ||
| if (entityName == null) return null; | ||
| return entityName.getString().split(":")[0]; | ||
| } | ||
|
|
||
| public boolean uuidIsPlayer() { | ||
| return playerUUIDs.contains(entityId); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -66,8 +74,9 @@ public boolean toleratesOther(Iota that) { | |
| } | ||
|
|
||
| @Override | ||
| public boolean isTruthy() { | ||
| return true; | ||
| public boolean isTruthy(CastingEnvironment env) { | ||
| if (env == null) return true; | ||
| return getEntity(env.getWorld()) != null; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -93,31 +102,37 @@ private static Component getEntityNameWithInline(Entity entity) { | |
| return baseName.append(Component.literal(": ")).append(inlineEnt); | ||
| } | ||
|
|
||
| // This finds anyone who has ever joined the server by reading the UUIDs from the playerdata folder | ||
| // We can't just use GameProfileCache because it only stores 1000 UUIDs and only keeps them for 30 days | ||
| public static void initPlayerUUIDs(MinecraftServer server) { | ||
| var playerDataDir = server.getWorldPath(LevelResource.PLAYER_DATA_DIR).toFile(); | ||
| String[] uuidStrings = playerDataDir.list(); | ||
| if (uuidStrings == null) return; | ||
| for (String string : uuidStrings) { | ||
| if (string.endsWith(".dat")) { | ||
| try { playerUUIDs.add(UUID.fromString(StringUtils.removeEnd(string, ".dat"))); } | ||
| catch (IllegalArgumentException ignored) {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void addPlayerUUID(UUID newUUID) { | ||
| playerUUIDs.add(newUUID); | ||
| } | ||
|
Comment on lines
+105
to
+121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like these being public... |
||
|
|
||
| public static IotaType<EntityIota> TYPE = new IotaType<>() { | ||
| public static final MapCodec<EntityIota> CODEC = RecordCodecBuilder.mapCodec(inst -> | ||
| inst.group( | ||
| UUIDUtil.CODEC.fieldOf("entityId").forGetter(EntityIota::getEntityId), | ||
| ComponentSerialization.CODEC.optionalFieldOf("entityName").forGetter(iota -> Optional.ofNullable(iota.getEntityName())), | ||
| Codec.BOOL.fieldOf("isPlayer").orElse(true).forGetter(EntityIota::isPlayer) | ||
| ).apply(inst, (a, b, c) -> new EntityIota(a, b.orElse(null), c))); | ||
| ComponentSerialization.CODEC.optionalFieldOf("entityName").forGetter(iota -> Optional.ofNullable(iota.getEntityName())) | ||
| ).apply(inst, (a, b) -> new EntityIota(a, b.orElse(null)))); | ||
| public static final StreamCodec<RegistryFriendlyByteBuf, EntityIota> STREAM_CODEC = | ||
| StreamCodec.composite( | ||
| UUIDUtil.STREAM_CODEC, EntityIota::getEntityId, | ||
| ByteBufCodecs.optional(ComponentSerialization.STREAM_CODEC), iota -> Optional.ofNullable(iota.getEntityName()), | ||
| ByteBufCodecs.BOOL, EntityIota::isPlayer, | ||
| (a, b, c) -> new EntityIota(a, b.orElse(null), c) | ||
| (a, b) -> new EntityIota(a, b.orElse(null)) | ||
| ); | ||
|
|
||
| @Override | ||
| public boolean validate(EntityIota iota, ServerLevel level) { | ||
| var entity = iota.getEntity(level); | ||
| // update isPlayer so older non-player entity iotas are not protected | ||
| iota.isPlayer = (entity instanceof Player); | ||
| return entity != null; | ||
| } | ||
|
|
||
| @Override | ||
| public MapCodec<EntityIota> codec() { | ||
| return CODEC; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.