From f5024a75c36150d420425e04387429888fa6aeb7 Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Fri, 19 Apr 2019 23:52:27 +0200 Subject: [PATCH] update entity tracking registration code for new snapshot changes (#145) --- .../api/entity/EntityTrackingRegistry.java | 14 +++-- .../api/entity/FabricEntityTypeBuilder.java | 17 +++-- .../fabric/impl/entity/FabricEntityType.java | 62 +++++++++++++++++++ .../texture/MixinSpriteAtlasTexture.java | 1 + 4 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/main/java/net/fabricmc/fabric/impl/entity/FabricEntityType.java diff --git a/src/main/java/net/fabricmc/fabric/api/entity/EntityTrackingRegistry.java b/src/main/java/net/fabricmc/fabric/api/entity/EntityTrackingRegistry.java index 10fcb705b..43aadadd3 100644 --- a/src/main/java/net/fabricmc/fabric/api/entity/EntityTrackingRegistry.java +++ b/src/main/java/net/fabricmc/fabric/api/entity/EntityTrackingRegistry.java @@ -19,6 +19,7 @@ package net.fabricmc.fabric.api.entity; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.network.Packet; +import net.minecraft.util.registry.Registry; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -28,14 +29,14 @@ import java.util.function.Function; /** * Registry for server->client entity tracking values. + * + * @deprecated Use FabricEntityTypeBuilder methods */ @SuppressWarnings("deprecation") +@Deprecated public class EntityTrackingRegistry { private static final Logger LOGGER = LogManager.getLogger(); - /** - * @deprecated Should be hidden; will be removed in 0.3.0. - */ @Deprecated public static class Entry { private final int trackingDistance; @@ -61,6 +62,7 @@ public class EntityTrackingRegistry { } } + @Deprecated public static final EntityTrackingRegistry INSTANCE = new EntityTrackingRegistry(); private final Map entries = new HashMap<>(); @@ -68,19 +70,19 @@ public class EntityTrackingRegistry { } - /** - * @deprecated Should be hidden; will be removed in 0.3.0. - */ @Deprecated public Entry get(EntityType type) { return entries.get(type); } + @Deprecated public void register(EntityType type, int trackingDistance, int updateIntervalTicks) { register(type, trackingDistance, updateIntervalTicks, true); } + @Deprecated public void register(EntityType type, int trackingDistance, int updateIntervalTicks, boolean alwaysUpdateVelocity) { + LOGGER.warn("Deprecation warning: As of February 2019, registering tracking via EntityTrackingRegistry is no longer effective. Use FabricEntityTypeBuilder. (Entity: " + Registry.ENTITY_TYPE.getId(type) + ")"); entries.put(type, new Entry(trackingDistance, updateIntervalTicks, alwaysUpdateVelocity)); } } diff --git a/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java b/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java index 063516efa..a7adb0b5b 100644 --- a/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java +++ b/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java @@ -16,6 +16,7 @@ package net.fabricmc.fabric.api.entity; +import net.fabricmc.fabric.impl.entity.FabricEntityType; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityCategory; import net.minecraft.entity.EntitySize; @@ -41,7 +42,7 @@ public class FabricEntityTypeBuilder { private boolean summonable = true; private int trackingDistance = -1; private int updateIntervalTicks = -1; - private boolean alwaysUpdateVelocity = true; + private Boolean alwaysUpdateVelocity; private boolean immuneToFire = false; private EntitySize size = EntitySize.resizeable(-1.0f, -1.0f); @@ -95,12 +96,12 @@ public class FabricEntityTypeBuilder { return this; } - public FabricEntityTypeBuilder trackable(int trackingDistance, int updateIntervalTicks) { - return trackable(trackingDistance, updateIntervalTicks, true); + public FabricEntityTypeBuilder trackable(int trackingDistanceBlocks, int updateIntervalTicks) { + return trackable(trackingDistanceBlocks, updateIntervalTicks, true); } - public FabricEntityTypeBuilder trackable(int trackingDistance, int updateIntervalTicks, boolean alwaysUpdateVelocity) { - this.trackingDistance = trackingDistance; + public FabricEntityTypeBuilder trackable(int trackingDistanceBlocks, int updateIntervalTicks, boolean alwaysUpdateVelocity) { + this.trackingDistance = trackingDistanceBlocks; this.updateIntervalTicks = updateIntervalTicks; this.alwaysUpdateVelocity = alwaysUpdateVelocity; return this; @@ -112,10 +113,8 @@ public class FabricEntityTypeBuilder { // TODO: Flesh out once modded datafixers exist. } - EntityType type = new EntityType(this.function, this.category, this.saveable, this.summonable, this.immuneToFire, null, size); - if (trackingDistance != -1) { - EntityTrackingRegistry.INSTANCE.register(type, trackingDistance, updateIntervalTicks, alwaysUpdateVelocity); - } + EntityType type = new FabricEntityType(this.function, this.category, this.saveable, this.summonable, this.immuneToFire, null, size, trackingDistance, updateIntervalTicks, alwaysUpdateVelocity); + return type; } } diff --git a/src/main/java/net/fabricmc/fabric/impl/entity/FabricEntityType.java b/src/main/java/net/fabricmc/fabric/impl/entity/FabricEntityType.java new file mode 100644 index 000000000..07ba1e79e --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/impl/entity/FabricEntityType.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl.entity; + +import com.mojang.datafixers.types.Type; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityCategory; +import net.minecraft.entity.EntitySize; +import net.minecraft.entity.EntityType; + +public class FabricEntityType extends EntityType { + private final int maxTrackDistance, trackTickInterval; + private final Boolean alwaysUpdateVelocity; + + public FabricEntityType(EntityFactory entityType$EntityFactory_1, EntityCategory entityCategory_1, boolean boolean_1, boolean boolean_2, boolean boolean_3, Type type_1, EntitySize entitySize_1, int maxTrackDistance, int trackTickInterval, boolean alwaysUpdateVelocity) { + super(entityType$EntityFactory_1, entityCategory_1, boolean_1, boolean_2, boolean_3, type_1, entitySize_1); + this.maxTrackDistance = maxTrackDistance; + this.trackTickInterval = trackTickInterval; + this.alwaysUpdateVelocity = alwaysUpdateVelocity; + } + + @Override + public int getMaxTrackDistance() { + if (maxTrackDistance != -1) { + return (maxTrackDistance + 15) / 16; + } + + return super.getMaxTrackDistance(); + } + + @Override + public int getTrackTickInterval() { + if (trackTickInterval != -1) { + return trackTickInterval; + } + + return super.getTrackTickInterval(); + } + + @Override + public boolean alwaysUpdateVelocity() { + if (alwaysUpdateVelocity != null) { + return alwaysUpdateVelocity; + } + + return super.alwaysUpdateVelocity(); + } +} diff --git a/src/main/java/net/fabricmc/fabric/mixin/client/texture/MixinSpriteAtlasTexture.java b/src/main/java/net/fabricmc/fabric/mixin/client/texture/MixinSpriteAtlasTexture.java index f9364b6cb..8766353d1 100644 --- a/src/main/java/net/fabricmc/fabric/mixin/client/texture/MixinSpriteAtlasTexture.java +++ b/src/main/java/net/fabricmc/fabric/mixin/client/texture/MixinSpriteAtlasTexture.java @@ -59,6 +59,7 @@ public abstract class MixinSpriteAtlasTexture { * method_18160 is a lambda used in runAsync. */ @SuppressWarnings("JavaDoc") + @Redirect(method = "method_18160", at = @At(value = "NEW", target = "net/minecraft/client/texture/Sprite")) public Sprite newSprite(Identifier id, PngFile pngFile, AnimationResourceMetadata animationMetadata) { if (fabric_injectedSprites.containsKey(id)) {