Skip to content

Commit

Permalink
Replace fabric metadata impl with data attachment api
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimordialMoros committed Feb 12, 2024
1 parent b0180ea commit 83ed16a
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

Expand All @@ -32,7 +31,6 @@
import me.moros.bending.api.addon.Addon;
import me.moros.bending.api.game.Game;
import me.moros.bending.api.platform.Platform;
import me.moros.bending.api.util.Tasker;
import me.moros.bending.api.util.functional.Suppliers;
import me.moros.bending.common.AbstractBending;
import me.moros.bending.common.command.Commander;
Expand All @@ -49,7 +47,6 @@
import me.moros.bending.fabric.listener.WorldListener;
import me.moros.bending.fabric.platform.CommandSender;
import me.moros.bending.fabric.platform.CommandSender.PlayerCommandSender;
import me.moros.bending.fabric.platform.FabricMetadata;
import me.moros.bending.fabric.platform.FabricPermissionInitializer;
import me.moros.bending.fabric.platform.FabricPlatform;
import me.moros.tasker.fabric.FabricExecutor;
Expand All @@ -72,8 +69,6 @@ final class FabricBending extends AbstractBending<ModContainer> {

injectTasker(new FabricExecutor());

Tasker.async().repeat(FabricMetadata.INSTANCE::removeEmpty, 5, TimeUnit.MINUTES);

listeners = List.of(
new BlockListener(this::game),
new UserListener(this::game),
Expand Down Expand Up @@ -113,7 +108,6 @@ private void onEnable(MinecraftServer server) {

private void onDisable(boolean fullShutdown) {
if (phase == LoadPhase.LOADED) {
FabricMetadata.INSTANCE.cleanup();
if (fullShutdown) {
disable();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import me.moros.bending.fabric.event.ServerInventoryEvents;
import me.moros.bending.fabric.event.ServerItemEvents;
import me.moros.bending.fabric.event.ServerPlayerEvents;
import me.moros.bending.fabric.platform.FabricMetadata;
import me.moros.bending.fabric.platform.PlatformAdapter;
import me.moros.bending.fabric.platform.entity.FabricEntity;
import me.moros.bending.fabric.platform.item.ItemUtil;
Expand Down Expand Up @@ -121,9 +120,8 @@ private void onPlayerRespawn(Entity originalEntity, Entity newEntity, boolean al

private boolean onArrowHit(Projectile projectile, HitResult hitResult) {
if (!disabledWorld(projectile) && projectile instanceof Arrow) {
var data = FabricMetadata.INSTANCE.metadata(projectile).get(MetalCable.CABLE_KEY);
if (data.isPresent()) {
MetalCable cable = data.get();
MetalCable cable = projectile.getAttached(PlatformAdapter.dataType(MetalCable.CABLE_KEY));
if (cable != null) {
if (hitResult instanceof BlockHitResult blockHit) {
var pos = blockHit.getBlockPos();
var world = PlatformAdapter.fromFabricWorld((ServerLevel) projectile.level());
Expand Down Expand Up @@ -286,7 +284,7 @@ private boolean onItemMerge(ItemEntity first, ItemEntity second) {
}

private boolean isNotGlove(Entity entity) {
return !FabricMetadata.INSTANCE.has(entity, EarthGlove.GLOVE_KEY);
return !entity.hasAttached(PlatformAdapter.dataType(EarthGlove.GLOVE_KEY));
}

private boolean onInventoryClick(ServerPlayer player, ItemStack stack) {
Expand Down Expand Up @@ -352,7 +350,7 @@ private double onEntityDamage(LivingEntity entity, DamageSource source, double d
Vector3d origin = null;
var sourceEntity = source.getEntity();
if (sourceEntity != null) {
if (sourceEntity instanceof Arrow && FabricMetadata.INSTANCE.has(sourceEntity, MetalCable.CABLE_KEY)) {
if (sourceEntity instanceof Arrow && sourceEntity.hasAttached(PlatformAdapter.dataType(MetalCable.CABLE_KEY))) {
return 0;
} else if (ActionLimiter.isLimited(sourceEntity.getUUID(), ActionType.DAMAGE)) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import me.moros.bending.api.registry.Registries;
import me.moros.bending.api.user.User;
import me.moros.bending.common.util.Initializer;
import me.moros.bending.fabric.platform.FabricMetadata;
import me.moros.bending.fabric.platform.entity.FabricEntity;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
Expand All @@ -48,7 +47,6 @@ public void init() {
private void onWorldUnload(MinecraftServer server, ServerLevel world) {
var key = world.dimension().location();
game().worldManager().onWorldUnload(key);
FabricMetadata.INSTANCE.cleanup(key);
}

private void onChangeWorld(Entity originalEntity, Entity newEntity, ServerLevel origin, ServerLevel destination) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2020-2024 Moros
*
* This file is part of Bending.
*
* Bending is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bending is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bending. If not, see <https://www.gnu.org/licenses/>.
*/

package me.moros.bending.fabric.platform;

import java.util.Optional;

import me.moros.bending.api.util.data.DataHolder;
import me.moros.bending.api.util.data.DataKey;
import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;

public record FabricDataHolder(AttachmentTarget handle) implements DataHolder {
@Override
public <T> Optional<T> get(DataKey<T> key) {
var type = PlatformAdapter.dataTypeIfExists(key);
if (type == null) {
return Optional.empty();
}
return Optional.ofNullable(handle().getAttached(type));
}

@Override
public <T> void add(DataKey<T> key, T value) {
handle().setAttached(PlatformAdapter.dataType(key), value);
}

@Override
public <T> void remove(DataKey<T> key) {
var type = PlatformAdapter.dataTypeIfExists(key);
if (type != null) {
handle().removeAttached(type);
}
}
}

This file was deleted.

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

import java.util.Optional;

import com.mojang.serialization.Codec;
import me.moros.bending.api.ability.element.ElementHandler;
import me.moros.bending.api.adapter.NativeAdapter;
import me.moros.bending.api.gui.Board;
Expand All @@ -34,13 +35,26 @@
import me.moros.bending.api.platform.item.ItemBuilder;
import me.moros.bending.api.platform.item.ItemSnapshot;
import me.moros.bending.api.user.User;
import me.moros.bending.api.util.metadata.Metadata;
import me.moros.bending.fabric.adapter.NativeAdapterImpl;
import me.moros.bending.fabric.gui.BoardImpl;
import me.moros.bending.fabric.gui.ElementMenu;
import me.moros.bending.fabric.platform.item.FabricItemBuilder;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.minecraft.server.MinecraftServer;

public class FabricPlatform implements Platform, PlatformFactory {
public static final AttachmentType<Boolean> NPC = AttachmentRegistry.createPersistent(
PlatformAdapter.rsl(Metadata.NPC), Codec.BOOL
);
public static final AttachmentType<Boolean> ARMOR = AttachmentRegistry.createPersistent(
PlatformAdapter.rsl(Metadata.ARMOR_KEY), Codec.BOOL
);
public static final AttachmentType<String> METAL_KEY = AttachmentRegistry.createPersistent(
PlatformAdapter.rsl(Metadata.ARMOR_KEY), Codec.STRING
);

private final MinecraftServer server;
private final NativeAdapter adapter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
import me.moros.bending.api.platform.potion.PotionEffect;
import me.moros.bending.api.platform.potion.PotionEffectTag;
import me.moros.bending.api.platform.world.World;
import me.moros.bending.api.util.data.DataKey;
import me.moros.bending.fabric.platform.block.FabricBlockState;
import me.moros.bending.fabric.platform.entity.FabricEntity;
import me.moros.bending.fabric.platform.entity.FabricLivingEntity;
import me.moros.bending.fabric.platform.entity.FabricPlayer;
import me.moros.bending.fabric.platform.item.FabricItem;
import me.moros.bending.fabric.platform.world.FabricWorld;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.impl.attachment.AttachmentRegistryImpl;
import net.kyori.adventure.key.Key;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -53,6 +57,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import org.checkerframework.checker.nullness.qual.Nullable;

public final class PlatformAdapter {
private PlatformAdapter() {
Expand Down Expand Up @@ -108,6 +113,18 @@ public static ResourceLocation rsl(Key key) {
return (ResourceLocation) key.key();
}

@SuppressWarnings("unchecked")
public static <T> AttachmentType<T> dataType(DataKey<T> key) {
var id = rsl(key);
var type = AttachmentRegistryImpl.get(id);
return type == null ? AttachmentRegistry.create(id) : (AttachmentType<T>) type;
}

@SuppressWarnings("unchecked")
public static <T> @Nullable AttachmentType<T> dataTypeIfExists(DataKey<T> key) {
return (AttachmentType<T>) AttachmentRegistryImpl.get(rsl(key));
}

public static World fromFabricWorld(ServerLevel world) {
return new FabricWorld(world);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import me.moros.bending.api.util.data.DataKey;
import me.moros.bending.api.util.functional.Suppliers;
import me.moros.bending.fabric.mixin.accessor.EntityAccess;
import me.moros.bending.fabric.platform.FabricMetadata;
import me.moros.bending.fabric.platform.PlatformAdapter;
import me.moros.bending.fabric.platform.world.FabricWorld;
import me.moros.math.FastMath;
import me.moros.math.Position;
Expand Down Expand Up @@ -239,17 +239,24 @@ public boolean teleport(Position position) {

@Override
public <T> Optional<T> get(DataKey<T> key) {
return FabricMetadata.INSTANCE.metadata(handle()).get(key);
var type = PlatformAdapter.dataTypeIfExists(key);
if (type == null) {
return Optional.empty();
}
return Optional.ofNullable(handle().getAttached(type));
}

@Override
public <T> void add(DataKey<T> key, T value) {
FabricMetadata.INSTANCE.metadata(handle()).add(key, value);
handle().setAttached(PlatformAdapter.dataType(key), value);
}

@Override
public <T> void remove(DataKey<T> key) {
FabricMetadata.INSTANCE.metadata(handle()).remove(key);
var type = PlatformAdapter.dataTypeIfExists(key);
if (type != null) {
handle().removeAttached(type);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import me.moros.bending.api.util.data.DataHolder;
import me.moros.bending.fabric.mixin.accessor.ChunkMapAccess;
import me.moros.bending.fabric.mixin.accessor.FallingBlockEntityAccess;
import me.moros.bending.fabric.platform.FabricMetadata;
import me.moros.bending.fabric.platform.FabricDataHolder;
import me.moros.bending.fabric.platform.PlatformAdapter;
import me.moros.bending.fabric.platform.particle.ParticleMapper;
import me.moros.math.Position;
Expand Down Expand Up @@ -90,7 +90,7 @@ public AABB blockBounds(int x, int y, int z) {

@Override
public DataHolder blockMetadata(int x, int y, int z) {
return FabricMetadata.INSTANCE.metadata(key(), x, y, z);
return new FabricDataHolder(handle());
}

private @Nullable BlockEntity blockEntity(int x, int y, int z) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sponge-api = "11.0.0-SNAPSHOT"
sponge-common = "1.20.1-11.0.0-SNAPSHOT"
minecraft = "1.20.4"
fabric-loom = "1.5.7"
fabric-api = "0.95.4+1.20.4"
fabric-api = "0.96.1+1.20.4"
fabric-loader = "0.15.6"
shadow = "8.1.1"
flyway = "10.7.1"
Expand Down

0 comments on commit 83ed16a

Please sign in to comment.