Skip to content

Commit

Permalink
[Feature] Typesafe datatags (#669)
Browse files Browse the repository at this point in the history
* add API for typesafe craftdatatags

* add implementations

* forgot set method

* allow datatags on subcrafts

* use Function<Craft, T> instead of simple supplier

* Update CraftDataTagContainer.java

* remove unneded interface

* Update CruiseOnPilotSubCraft.java

* Update SinkingCraftImpl.java

* Update SubCraftImpl.java

* Update SubcraftRotateCraft.java
  • Loading branch information
DerToaster98 authored Jul 25, 2024
1 parent d5627a9 commit a7aa511
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import net.countercraft.movecraft.async.rotation.RotationTask;
import net.countercraft.movecraft.async.translation.TranslationTask;
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.datatag.CraftDataTagContainer;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.exception.EmptyHitBoxException;
import net.countercraft.movecraft.localisation.I18nSupport;
import net.countercraft.movecraft.processing.CachedMovecraftWorld;
import net.countercraft.movecraft.processing.MovecraftWorld;
Expand All @@ -21,17 +21,16 @@
import net.countercraft.movecraft.util.hitboxes.SetHitBox;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;

Expand Down Expand Up @@ -74,6 +73,8 @@ public abstract class BaseCraft implements Craft {
@NotNull
private MovecraftLocation lastTranslation = new MovecraftLocation(0, 0, 0);

private final CraftDataTagContainer dataTagContainer = new CraftDataTagContainer();

public BaseCraft(@NotNull CraftType type, @NotNull World world) {
this.type = type;
this.w = world;
Expand All @@ -88,6 +89,7 @@ public BaseCraft(@NotNull CraftType type, @NotNull World world) {
audience = Audience.empty();
}


public boolean isNotProcessing() {
return !processing.get();
}
Expand Down Expand Up @@ -552,4 +554,9 @@ public void setTotalFuel(double fuel) {
public double getTotalFuel() {
return totalFuel;
}

@Override
public CraftDataTagContainer getDataTagContainer() {
return dataTagContainer;
}
}
22 changes: 22 additions & 0 deletions api/src/main/java/net/countercraft/movecraft/craft/Craft.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import net.countercraft.movecraft.CruiseDirection;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.MovecraftRotation;
import net.countercraft.movecraft.craft.datatag.CraftDataTagContainer;
import net.countercraft.movecraft.craft.datatag.CraftDataTagKey;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.processing.MovecraftWorld;
import net.countercraft.movecraft.util.Counter;
Expand Down Expand Up @@ -254,4 +256,24 @@ default void setLastDZ(int dZ){}
double getTotalFuel ();

void setTotalFuel (double fuel);

public default CraftDataTagContainer getDataTagContainer() {
return null;
}

public default <T> boolean setDataTag(CraftDataTagKey<T> tagKey, T data) {
CraftDataTagContainer container = this.getDataTagContainer();
if (container == null) {
return false;
}
container.set(tagKey, data);
return true;
}
public default <T> T getDataTag(CraftDataTagKey<T> tagKey) {
CraftDataTagContainer container = this.getDataTagContainer();
if (container == null) {
return null;
}
return container.get(this, tagKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.countercraft.movecraft.craft.datatag;

import net.countercraft.movecraft.craft.Craft;
import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class CraftDataTagContainer extends HashMap<CraftDataTagKey<?>, Object> {

public static final Map<NamespacedKey, CraftDataTagKey<?>> REGISTERED_TAGS = new HashMap<>();

public static <T> CraftDataTagKey<T> tryRegisterTagKey(final NamespacedKey key, final Function<Craft, T> supplier) throws IllegalArgumentException {
if (REGISTERED_TAGS.containsKey(key)) {
throw new IllegalArgumentException("Duplicate keys are not allowed!");
} else {
CraftDataTagKey<T> result = new CraftDataTagKey<T>(key, supplier);
REGISTERED_TAGS.put(key, result);
return result;
}
}

public <T> T get(final Craft craft, CraftDataTagKey<T> tagKey) {
if (!REGISTERED_TAGS.containsKey(tagKey.key)) {
// TODO: Log error
return null;
}
T result = null;
if (!this.containsKey(tagKey)) {
result = tagKey.createNew(craft);
this.put(tagKey, result);
} else {
Object stored = this.getOrDefault(tagKey, tagKey.createNew(craft));
try {
T temp = (T) stored;
result = temp;
} catch (ClassCastException cce) {
// TODO: Log error
result = tagKey.createNew(craft);
this.put(tagKey, result);
}
}
return result;
}

public <T> void set(CraftDataTagKey<T> tagKey, @NotNull T value) {
this.put(tagKey, value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.countercraft.movecraft.craft.datatag;

import net.countercraft.movecraft.craft.Craft;
import org.bukkit.NamespacedKey;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;

public class CraftDataTagKey<T> {
protected final Function<Craft, T> dataCreator;
protected final NamespacedKey key;

CraftDataTagKey(@NotNull Plugin plugin, @NotNull String key, @NotNull Function<Craft, T> supplier) {
this.key = new NamespacedKey(plugin, key);
this.dataCreator = supplier;
}

CraftDataTagKey(@NotNull String namespace, @NotNull String key, @NotNull Function<Craft, T> supplier) {
this.key = new NamespacedKey(namespace, key);
this.dataCreator = supplier;
}

CraftDataTagKey(@NotNull NamespacedKey key, @NotNull Function<Craft, T> supplier) {
this.dataCreator = supplier;
this.key = key;
}

@Override
public int hashCode() {
return this.key.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CraftDataTagKey<?> other = (CraftDataTagKey) obj;
return this.key.equals(other.key);
}

@Override
public String toString() {
return this.key.toString();
}

public T createNew(final Craft craft) {
return this.dataCreator.apply(craft);
}
}

0 comments on commit a7aa511

Please sign in to comment.