-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
d5627a9
commit a7aa511
Showing
4 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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
53 changes: 53 additions & 0 deletions
53
api/src/main/java/net/countercraft/movecraft/craft/datatag/CraftDataTagContainer.java
This file contains 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 |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
api/src/main/java/net/countercraft/movecraft/craft/datatag/CraftDataTagKey.java
This file contains 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 |
---|---|---|
@@ -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); | ||
} | ||
} |