Skip to content
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

Added MetaStorage to replace MetadataMap #159

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io.fairyproject.bukkit.command.BukkitCommandExecutor;
import io.fairyproject.bukkit.command.sync.SyncCommandHandler;
import io.fairyproject.command.BaseCommand;
import io.fairyproject.data.MetaKey;
import io.fairyproject.data.MetaStorage;
import io.fairyproject.metadata.MetadataKey;
import io.fairyproject.metadata.MetadataMap;
import lombok.RequiredArgsConstructor;
Expand All @@ -38,7 +40,7 @@
@RequiredArgsConstructor
public class DefaultBukkitCommandMap implements BukkitCommandMap {

public static final MetadataKey<BukkitCommandExecutor> EXECUTOR_KEY = MetadataKey.create("fairy:command-executor", BukkitCommandExecutor.class);
public static final MetaKey<BukkitCommandExecutor> EXECUTOR_KEY = MetaKey.create("fairy:command-executor", BukkitCommandExecutor.class);

private final CommandMap commandMap;
private final Map<String, Command> knownCommands;
Expand All @@ -59,18 +61,18 @@ public void register(BaseCommand command) {
}


command.getMetadata().put(EXECUTOR_KEY, commandExecutor);
command.getMetaStorage().put(EXECUTOR_KEY, commandExecutor);
commandMap.register(fallbackPrefix, commandExecutor);
syncCommandHandler.sync();
}

@Override
public void unregister(BaseCommand command) {
MetadataMap metadata = command.getMetadata();
MetaStorage metaStorage = command.getMetaStorage();
if (!this.isRegistered(command))
throw new IllegalArgumentException("Command not registered");

metadata.ifPresent(EXECUTOR_KEY, commandExecutor -> {
metaStorage.ifPresent(EXECUTOR_KEY, commandExecutor -> {
String fallbackPrefix = commandExecutor.getFallbackPrefix();

unregisterKnownCommand(fallbackPrefix, commandExecutor.getName());
Expand All @@ -79,7 +81,7 @@ public void unregister(BaseCommand command) {
}

commandExecutor.unregister(commandMap);
metadata.remove(EXECUTOR_KEY);
metaStorage.remove(EXECUTOR_KEY);
});

syncCommandHandler.sync();
Expand All @@ -91,7 +93,7 @@ private boolean isCommandNameRegistered(String name, String fallbackPrefix) {

@Override
public boolean isRegistered(BaseCommand command) {
return command.getMetadata().has(EXECUTOR_KEY);
return command.getMetaStorage().contains(EXECUTOR_KEY);
}

private void unregisterKnownCommand(String fallbackPrefix, String alias) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.fairyproject.bukkit.plugin.impl.RootJavaPluginIdentifier;
import io.fairyproject.bukkit.plugin.impl.SpecifyJavaPluginIdentifier;
import io.fairyproject.command.BaseCommand;
import io.fairyproject.data.MetaStorage;
import io.fairyproject.metadata.MetadataMap;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
Expand All @@ -53,15 +54,15 @@ class DefaultBukkitCommandMapTest {
private CommandMap commandMap;
private Map<String, Command> knownCommands;
private BaseCommand command;
private MetadataMap metadata;
private MetaStorage metadata;

@BeforeEach
void setUp() {
commandMap = Mockito.mock(CommandMap.class);
command = Mockito.mock(BaseCommand.class);
metadata = MetadataMap.create();
metadata = MetaStorage.create();
Mockito.when(command.getCommandNames()).thenReturn(new String[]{"test", "test2"});
Mockito.when(command.getMetadata()).thenReturn(metadata);
Mockito.when(command.getMetaStorage()).thenReturn(metadata);

knownCommands = new HashMap<>();
Mockito.when(commandMap.register(Mockito.anyString(), Mockito.any()))
Expand Down Expand Up @@ -118,7 +119,7 @@ void registerShouldAvoidDuplicateInstance() {
void registerShouldAvoidDuplicateNames() {
BaseCommand secondCommand = Mockito.mock(BaseCommand.class);
Mockito.when(secondCommand.getCommandNames()).thenReturn(new String[]{"test3", "test2"});
Mockito.when(secondCommand.getMetadata()).thenReturn(MetadataMap.create());
Mockito.when(secondCommand.getMetaStorage()).thenReturn(MetaStorage.create());

defaultBukkitCommandMap.register(command);

Expand All @@ -132,7 +133,7 @@ void unregister() {
defaultBukkitCommandMap.unregister(command);

assertTrue(knownCommands.isEmpty());
assertFalse(metadata.has(DefaultBukkitCommandMap.EXECUTOR_KEY));
assertFalse(metadata.contains(DefaultBukkitCommandMap.EXECUTOR_KEY));
assertFalse(executor.isRegistered());
Mockito.verify(syncCommandHandler, Mockito.times(2)).sync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import io.fairyproject.bukkit.gui.slot.GuiSlot;
import io.fairyproject.bukkit.events.BukkitEventFilter;
import io.fairyproject.bukkit.events.BukkitEventNode;
import io.fairyproject.data.MetaStorage;
import io.fairyproject.event.EventNode;
import io.fairyproject.mc.MCAdventure;
import io.fairyproject.mc.MCPlayer;
import io.fairyproject.mc.scheduler.MCSchedulers;
import io.fairyproject.metadata.MetadataMap;
import io.fairyproject.util.ConditionUtils;
import lombok.Getter;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -39,7 +39,7 @@ public class Gui {
@Getter
private final int id;
@Getter
private final MetadataMap metadataMap;
private final MetaStorage metaStorage;
private final BukkitEventNode bukkitEventNode;
private final Map<Integer, EventNode<InventoryEvent>> slotEventNodes;
private final List<Consumer<Player>> openCallbacks;
Expand All @@ -63,7 +63,7 @@ public Gui(BukkitEventNode bukkitEventNode, Component title) {
this.openCallbacks = new ArrayList<>();
this.drawCallbacks = new ArrayList<>();
this.closeCallbacks = new ArrayList<>();
this.metadataMap = MetadataMap.create();
this.metaStorage = MetaStorage.create();
this.slotEventNodes = new HashMap<>();
this.title = title;
this.panes = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import io.fairyproject.bukkit.util.items.behaviour.ItemBehaviour;
import io.fairyproject.bukkit.util.items.impl.FairyItemImpl;
import io.fairyproject.container.Autowired;
import io.fairyproject.data.MetaKey;
import io.fairyproject.data.MetaStorage;
import io.fairyproject.mc.MCPlayer;
import io.fairyproject.metadata.MetadataKey;
import io.fairyproject.metadata.MetadataMap;
import io.fairyproject.metadata.TransientValue;
import io.fairyproject.util.terminable.Terminable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand All @@ -27,7 +26,7 @@ static Builder builder(@NotNull String name) {

@NotNull String getName();

@NotNull MetadataMap getMetadataMap();
@NotNull MetaStorage getMetaStorage();

@NotNull Iterable<ItemBehaviour> getBehaviours();

Expand Down Expand Up @@ -74,7 +73,7 @@ class Builder {

private final String name;
private Function<MCPlayer, ItemBuilder> itemProvider;
private final MetadataMap metadataMap = MetadataMap.create();
private final MetaStorage metaStorage = MetaStorage.create();
private final List<ItemBehaviour> behaviours = new ArrayList<>();

private Builder(@NotNull String name) {
Expand All @@ -99,36 +98,31 @@ public Builder item(@NotNull Function<MCPlayer, ItemBuilder> itemProvider) {
return this;
}

public Builder transformMeta(@NotNull Consumer<MetadataMap> consumer) {
consumer.accept(this.metadataMap);
public Builder transformMeta(@NotNull Consumer<MetaStorage> consumer) {
consumer.accept(this.metaStorage);
return this;
}

public <T> Builder put(@NotNull MetadataKey<T> metadataKey, @NotNull T value) {
this.metadataMap.put(metadataKey, value);
return this;
}

public <T> Builder put(@NotNull MetadataKey<T> metadataKey, @NotNull TransientValue<T> value) {
this.metadataMap.put(metadataKey, value);
public <T> Builder put(@NotNull MetaKey<T> metadataKey, @NotNull T value) {
this.metaStorage.put(metadataKey, value);
return this;
}

@Deprecated
public FairyItem build() {
FairyItem fairyItem = new FairyItemImpl(REGISTRY, this.name, this.metadataMap, this.behaviours, this.itemProvider);
FairyItem fairyItem = new FairyItemImpl(REGISTRY, this.name, this.metaStorage, this.behaviours, this.itemProvider);
REGISTRY.register(fairyItem);

return fairyItem;
}

@Deprecated
public FairyItem create() {
return new FairyItemImpl(REGISTRY, this.name, this.metadataMap, this.behaviours, this.itemProvider);
return new FairyItemImpl(REGISTRY, this.name, this.metaStorage, this.behaviours, this.itemProvider);
}

public FairyItem create(FairyItemRegistry itemRegistry) {
FairyItem fairyItem = new FairyItemImpl(itemRegistry, this.name, this.metadataMap, this.behaviours, this.itemProvider);
FairyItem fairyItem = new FairyItemImpl(itemRegistry, this.name, this.metaStorage, this.behaviours, this.itemProvider);
itemRegistry.register(fairyItem);

return fairyItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@

import com.cryptomorin.xseries.XSound;
import io.fairyproject.bukkit.listener.ListenerRegistry;
import io.fairyproject.bukkit.metadata.Metadata;
import io.fairyproject.bukkit.util.items.FairyItem;
import io.fairyproject.bukkit.util.items.FairyItemRef;
import io.fairyproject.bukkit.util.items.FairyItemRegistry;
import io.fairyproject.container.Autowired;
import io.fairyproject.mc.MCPlayer;
import io.fairyproject.data.MetaKey;
import io.fairyproject.data.MetaStorage;
import io.fairyproject.mc.data.MCMetadata;
import io.fairyproject.mc.registry.player.MCPlayerRegistry;
import io.fairyproject.metadata.MetadataKey;
import io.fairyproject.metadata.MetadataMap;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
Expand All @@ -46,7 +43,7 @@

public class ItemBehaviourBlockMarker extends ItemBehaviourListener {

private static final MetadataKey<String> METADATA = MetadataKey.createStringKey("fairy:block-marker");
private static final MetaKey<String> METADATA = MetaKey.create("fairy:block-marker", String.class);

private final FairyItemRegistry fairyItemRegistry;
private final MCPlayerRegistry mcPlayerRegistry;
Expand Down Expand Up @@ -74,21 +71,21 @@ public void onBlockPlace(BlockPlaceEvent event) {
if (item != this.item)
return;

Metadata.provideForBlock(block).put(METADATA, this.item.getName());
MCMetadata.provide(block).put(METADATA, this.item.getName());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
final Block block = event.getBlock();

final MetadataMap metadataMap = Metadata.provideForBlock(block);
final String itemKey = metadataMap.getOrNull(METADATA);
final MetaStorage metaStorage = MCMetadata.provide(block);
final String itemKey = metaStorage.getOrNull(METADATA);
if (itemKey == null || !itemKey.equals(this.item.getName())) {
return;
}

metadataMap.remove(METADATA);
metaStorage.remove(METADATA);
final FairyItem item = this.fairyItemRegistry.get(itemKey);
if (item == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import io.fairyproject.bukkit.util.items.FairyItemRegistry;
import io.fairyproject.bukkit.util.items.ItemBuilder;
import io.fairyproject.bukkit.util.items.behaviour.ItemBehaviour;
import io.fairyproject.data.MetaStorage;
import io.fairyproject.mc.MCPlayer;
import io.fairyproject.metadata.MetadataMap;
import lombok.Getter;
import lombok.NonNull;
import org.bukkit.inventory.ItemStack;
Expand All @@ -20,7 +20,7 @@ public class FairyItemImpl implements FairyItem {

private final FairyItemRegistry itemRegistry;
private final String name;
private final MetadataMap metadataMap;
private final MetaStorage metaStorage;
private final List<ItemBehaviour> behaviours;
private final Function<MCPlayer, ItemBuilder> itemProvider;

Expand All @@ -29,12 +29,12 @@ public class FairyItemImpl implements FairyItem {
public FairyItemImpl(
@NonNull FairyItemRegistry itemRegistry,
@NonNull String name,
@NonNull MetadataMap metadataMap,
@NonNull MetaStorage metaStorage,
@NonNull List<ItemBehaviour> behaviours,
@NonNull Function<MCPlayer, ItemBuilder> itemProvider) {
this.itemRegistry = itemRegistry;
this.name = name;
this.metadataMap = metadataMap;
this.metaStorage = metaStorage;
this.behaviours = behaviours;
this.itemProvider = itemProvider;
this.closed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
import io.fairyproject.bukkit.events.BukkitEventNode;
import io.fairyproject.bukkit.menu.event.ButtonClickEvent;
import io.fairyproject.bukkit.menu.event.MenuCloseEvent;
import io.fairyproject.bukkit.metadata.Metadata;
import io.fairyproject.container.Autowired;
import io.fairyproject.data.MetaKey;
import io.fairyproject.data.MetaStorage;
import io.fairyproject.event.EventNode;
import io.fairyproject.mc.data.MCMetadata;
import io.fairyproject.mc.scheduler.MCSchedulers;
import io.fairyproject.metadata.MetadataKey;
import io.fairyproject.metadata.MetadataMap;
import io.fairyproject.util.CC;
import io.fairyproject.util.terminable.Terminable;
import io.fairyproject.util.terminable.TerminableConsumer;
Expand Down Expand Up @@ -66,7 +66,7 @@
@Setter
public abstract class Menu implements TerminableConsumer {

private static final MetadataKey<Menu> METADATA = MetadataKey.create("fairy:menu", Menu.class);
private static final MetaKey<Menu> METADATA = MetaKey.create("fairy:menu", Menu.class);
private static final Map<Class<? extends Menu>, List<Menu>> MENU_BY_TYPE = new ConcurrentHashMap<>();

@Deprecated
Expand Down Expand Up @@ -323,7 +323,7 @@ public final void open(Player player) {
this.openMillis = System.currentTimeMillis();

this.player = player;
Metadata.provide(player).put(METADATA, this);
MCMetadata.provide(player).put(METADATA, this);
Menu.addMenu(this);

this.render(true);
Expand All @@ -345,7 +345,7 @@ public void remove() {
}
this.opening = false;

MetadataMap metadataMap = Metadata.provideForPlayer(this.player);
MetaStorage metadataMap = MCMetadata.provide(this.player);
Menu existing = metadataMap.getOrNull(METADATA);
if (existing == this) {
metadataMap.remove(METADATA);
Expand Down Expand Up @@ -535,7 +535,7 @@ private static void removeMenu(Menu menu) {
}

public static Menu getMenuByUuid(UUID uuid) {
return Metadata.provideForPlayer(uuid).getOrNull(METADATA);
return MCMetadata.provide(uuid).getOrNull(METADATA);
}

public static <T extends Menu> List<T> getMenusByType(Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerMultiBlockChange;
import com.google.common.collect.HashMultimap;
import io.fairyproject.Fairy;
import io.fairyproject.bukkit.metadata.Metadata;
import io.fairyproject.bukkit.nms.BukkitNMSManager;
import io.fairyproject.bukkit.visual.sender.impl.BukkitVisualData;
import io.fairyproject.bukkit.visual.sender.impl.NewVisualData;
import io.fairyproject.bukkit.visual.sender.impl.OldVisualData;
import io.fairyproject.bukkit.visual.util.BlockPositionData;
import io.fairyproject.data.MetaKey;
import io.fairyproject.mc.MCPlayer;
import io.fairyproject.mc.data.MCMetadata;
import io.fairyproject.mc.protocol.MCProtocol;
import io.fairyproject.mc.util.BlockPosition;
import io.fairyproject.metadata.MetadataKey;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand All @@ -47,7 +47,7 @@

public class VisualBlockSender {

public final MetadataKey<VisualContainer> fakeBlocksMetadataKey = MetadataKey.create(Fairy.METADATA_PREFIX + "FakeBlockMap", VisualContainer.class);
public final MetaKey<VisualContainer> fakeBlocksMetadataKey = MetaKey.create(Fairy.METADATA_PREFIX + "FakeBlockMap", VisualContainer.class);
private final List<VisualData> visualDataList;

public VisualBlockSender(BukkitNMSManager nmsManager) {
Expand All @@ -59,7 +59,7 @@ public VisualBlockSender(BukkitNMSManager nmsManager) {
}

public void send(Player player, Map<BlockPosition, XMaterial> blockMap, List<BlockPosition> replace, boolean send) {
VisualContainer visualContainer = Metadata.provideForPlayer(player).getOrPut(fakeBlocksMetadataKey, VisualContainer::new);
VisualContainer visualContainer = MCMetadata.provide(player).computeIfAbsent(fakeBlocksMetadataKey, VisualContainer::new);
HashMultimap<BlockPosition, BlockPositionData> map = HashMultimap.create();

for (final Map.Entry<BlockPosition, XMaterial> entry : blockMap.entrySet()) {
Expand Down Expand Up @@ -121,8 +121,9 @@ public void send(Player player, Map<BlockPosition, XMaterial> blockMap, List<Blo
}

public void clearFakeBlocks(Player player, boolean send) {
VisualContainer visualContainer = Metadata.provideForPlayer(player).getOrNull(fakeBlocksMetadataKey);
if (visualContainer == null) return;
VisualContainer visualContainer = MCMetadata.provide(player).getOrNull(fakeBlocksMetadataKey);
if (visualContainer == null)
return;

if (send) {
send(player, Collections.emptyMap(), new ArrayList<>(visualContainer.keySet()), true);
Expand Down
Loading
Loading