-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Movecraft/src/main/java/net/countercraft/movecraft/craft/movement/MovementController.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,76 @@ | ||
package net.countercraft.movecraft.craft.movement; | ||
|
||
import net.countercraft.movecraft.MovecraftLocation; | ||
import net.countercraft.movecraft.MovecraftRotation; | ||
import net.countercraft.movecraft.craft.Craft; | ||
import net.countercraft.movecraft.craft.datatag.CraftDataTagKey; | ||
import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry; | ||
import net.countercraft.movecraft.processing.MovecraftWorld; | ||
import org.bukkit.Effect; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.plugin.Plugin; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class MovementController { | ||
private static final @NotNull MovecraftLocation ZERO = new MovecraftLocation(0,0,0); | ||
private static final @NotNull MovementData DEFAULT = new MovementData(ZERO, MovecraftRotation.NONE, null); | ||
private final @NotNull CraftDataTagKey<MovementData> _dataTagKey; | ||
|
||
public MovementController(Plugin plugin) { | ||
_dataTagKey = CraftDataTagRegistry.INSTANCE.registerTagKey( | ||
new NamespacedKey(plugin, "movement"), | ||
craft -> DEFAULT); | ||
} | ||
|
||
public void move(@NotNull Craft craft, @Nullable MovecraftLocation translation, @Nullable MovecraftRotation rotation, @Nullable MovecraftWorld changeWorld){ | ||
craft.computeDataTag(_dataTagKey, (data) -> data.merge(new MovementData( | ||
translation == null ? ZERO : translation, | ||
rotation == null ? MovecraftRotation.NONE : rotation, | ||
changeWorld))); | ||
} | ||
|
||
private MovementData reset(@NotNull Craft craft){ | ||
return craft.setDataTag(_dataTagKey, DEFAULT); | ||
} | ||
|
||
private class MovementTask implements Supplier<Effect> { | ||
|
||
private final @NotNull Craft craft; | ||
|
||
public MovementTask(@NotNull Craft craft){ | ||
this.craft = craft; | ||
} | ||
|
||
@Override | ||
public @Nullable Effect get() { | ||
// Reset the current movement tracker | ||
var movement = reset(craft); | ||
|
||
return null; | ||
} | ||
} | ||
|
||
private record MovementData(@NotNull MovecraftLocation translation, @NotNull MovecraftRotation rotation, @Nullable MovecraftWorld world){ | ||
|
||
private static MovecraftRotation add(MovecraftRotation a, MovecraftRotation b){ | ||
if(a == MovecraftRotation.NONE || a==b){ | ||
return b; | ||
} | ||
if(b == MovecraftRotation.NONE){ | ||
return a; | ||
} | ||
|
||
return MovecraftRotation.NONE; | ||
} | ||
|
||
@Contract("_ -> new") | ||
public @NotNull MovementData merge(@NotNull MovementData other){ | ||
// TODO: Correct this | ||
return new MovementData(translation.add(other.translation), add(rotation, other.rotation), this.world == null ? other.world : this.world); | ||
} | ||
} | ||
} |