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

Craft DataTags and trackedlocations #611

Closed
1 change: 1 addition & 0 deletions modules/MakeItBuild.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make it Build!
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import net.countercraft.movecraft.processing.WorldManager;
import net.countercraft.movecraft.util.Counter;
import net.countercraft.movecraft.util.Tags;

import net.countercraft.movecraft.util.CollectionUtils;
import net.countercraft.movecraft.util.MathUtils;

import net.countercraft.movecraft.util.TimingData;
import net.countercraft.movecraft.util.hitboxes.HitBox;
import net.countercraft.movecraft.util.hitboxes.MutableHitBox;
Expand All @@ -30,6 +34,7 @@
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.EnumSet;
Expand Down Expand Up @@ -115,6 +120,147 @@ public CraftType getType() {
return type;
}

@Override
public void setDataTag(String key, Object data) {
this.craftTags.put(key, data);
}
@Override
@Nullable
public Object removeDataTag(String key) {
Object tag = this.craftTags.getOrDefault(key, null);
this.craftTags.remove(key);
return tag;
}
@Override
@Nullable
public Object getDataTag(String key) {
return this.craftTags.getOrDefault(key, null);
}
@Override
public Collection<Object> getAllTagValues() {
return this.craftTags.values();
}
@Override
public boolean hasDataKey(String key) {
return this.craftTags.containsKey(key);
}
@Override
public boolean hasDataValue(Object value) {
return this.craftTags.containsValue(value);
}

@Override
public HitBox translateBox(HitBox box, MovecraftLocation vec) {
SetHitBox newBox = new SetHitBox();
for(MovecraftLocation oldLoc : box) {
MovecraftLocation newLoc = oldLoc.translate(vec.getX(),vec.getY(),vec.getZ());
newBox.add(newLoc);
}
return newBox;
}
@Override
public HitBox rotateBox(HitBox box, MovecraftLocation axis, MovecraftRotation rotation) {
SetHitBox newBox = new SetHitBox();
for(MovecraftLocation oldLoc : box){
MovecraftLocation newLoc = MathUtils.rotateVec(rotation,oldLoc.subtract(axis)).add(axis);
newBox.add(newLoc);
}
return newBox;
}

@Override
public boolean isTracking(@NotNull Object tracked) {
boolean found = false;
if (tracked instanceof MovecraftLocation || tracked instanceof HitBox) {
for (Object key : this.trackedLocations.keySet()) {
if (this.trackedLocations.get(key).contains(tracked)) {
found = true;
return found;
}
}
}
return found;
}
@Override
public void setTrackedLocs(@NotNull Collection<Object> tracked, @NotNull Object key) {
if (this.trackedLocations.containsKey(key)) {
Set<Object> mlocs = new HashSet<>();
mlocs.addAll(this.trackedLocations.get(key));
for (Object o : tracked) {
if (o instanceof MovecraftLocation || o instanceof HitBox) {
continue;
} else {
return;
}
}
this.trackedLocations.put(key, tracked);
} else {
for (Object o : tracked) {
if (o instanceof MovecraftLocation || o instanceof HitBox) {
continue;
} else {
return;
}
}
this.trackedLocations.put(key, tracked);
}
}
@Override
public void addTrackedLoc(@NotNull Object tracked, @NotNull Object key) {
if (tracked instanceof MovecraftLocation || tracked instanceof HitBox) {
if (this.trackedLocations.containsKey(key)) {
Set<Object> mlocs = new HashSet<>(this.trackedLocations.get(key));
mlocs.add(tracked);
this.trackedLocations.put(key, mlocs);
} else {
Set<Object> mlocs = new HashSet<>();
mlocs.add(tracked);
this.trackedLocations.put(key, mlocs);
}
}
}
@Override
public void removeTrackedLoc(@NotNull Object tracked, @NotNull Object key) {
if (tracked instanceof MovecraftLocation || tracked instanceof HitBox) {
if (this.trackedLocations.containsKey(key)) {
Set<Object> mlocs = new HashSet<>(this.trackedLocations.get(key));
mlocs.remove(tracked);
this.trackedLocations.put(key, mlocs);
}
}
}
@Override
public Set<Object> getAllTrackedLocation() {
Set<Object> mlocs = new HashSet<>();
for (Object key : this.trackedLocations.keySet()) {
for (Object o : this.trackedLocations.get(key)) {
if (o instanceof MovecraftLocation) {
mlocs.add(o);
}
if (o instanceof SetHitBox) {
mlocs.add(((SetHitBox)o).asSet());
}
}
}
return mlocs;
}
@Override
@Nullable
public Set<MovecraftLocation> getTrackedLocs(@NotNull Object key) {
if (this.trackedLocations.get(key) == null)
return null;
Set<MovecraftLocation> mlocs = new HashSet<>(getHitBox().size()*8);
for (Object o : this.trackedLocations.get(key)) {
if (o instanceof MovecraftLocation || o instanceof HitBox) {
if (o instanceof MovecraftLocation)
mlocs.add((MovecraftLocation)o);
if (o instanceof HitBox)
mlocs.addAll(((HitBox)o).asSet());
}
}
return mlocs;
}

@NotNull
public MovecraftWorld getMovecraftWorld() {
return CachedMovecraftWorld.of(w);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ public void doUpdate() {
CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.EMPTY, false);
return;
}
if (craft.trackedLocations != null) {
ArrayList<Object> clone = new ArrayList<>();
for (Object key : craft.trackedLocations.keySet()) {
ArrayList<Object> mlocs = new ArrayList<>(craft.getTrackedLocs(key));
for (Object tracked : mlocs) {
if (tracked != null) {
if (tracked instanceof MovecraftLocation) {
MovecraftLocation newTracked = MathUtils.rotateVec(rotation,((MovecraftLocation)tracked).subtract(originLocation)).add(originLocation);
clone.add(newTracked);
}
if (tracked instanceof HitBox) {
HitBox newBox = (SetHitBox)(craft).rotateBox(((HitBox)tracked), originLocation, rotation);
clone.add(newBox);
}
}
}
if (!clone.isEmpty() || clone != null) {
(craft).setTrackedLocs(clone,key);
}
}
}
long time = System.nanoTime();
final Set<Material> passthroughBlocks = new HashSet<>(craft.getType().getMaterialSetProperty(CraftType.PASSTHROUGH_BLOCKS));
if (craft instanceof SinkingCraft) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ public void doUpdate() {
CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.EMPTY, false);
return;
}

if (craft.trackedLocations != null) {
for (Object key : craft.trackedLocations.keySet()) {
ArrayList<Object> mlocs = new ArrayList<>(craft.trackedLocations.get(key));
Set<Object> clone = new HashSet<>();
for (Object tracked : mlocs) {
if (tracked != null) {
if (tracked instanceof MovecraftLocation) {
MovecraftLocation newTracked = ((MovecraftLocation)tracked).translate(displacement.getX(),displacement.getY(),displacement.getZ());
clone.add(newTracked);
}
if (tracked instanceof HitBox) {
HitBox newBox = (SetHitBox)(craft.translateBox(((SetHitBox)tracked), displacement));
clone.add(newBox);
}
}
}
if (!clone.isEmpty() || clone != null) {
(craft).setTrackedLocs(clone,key);
}
}
}
long time = System.nanoTime();
World oldWorld = craft.getWorld();
final Set<Material> passthroughBlocks = new HashSet<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

Expand All @@ -45,7 +47,6 @@ public interface Craft {

@Deprecated
void setProcessing(boolean processing);

/**
* Gets a HitBox representing the current locations that this craft controls
*
Expand All @@ -58,7 +59,6 @@ public interface Craft {
* Sets the HitBox representing the current locations that this craft controls
*/
void setHitBox(@NotNull HitBox hitBox);

/**
* Gets the CraftType used to determine the Craft's behaviours
*
Expand All @@ -67,6 +67,94 @@ public interface Craft {
@NotNull
CraftType getType();


Map<Object, Collection<Object>> trackedLocations = new HashMap<>();

Map<String, Object> craftTags = new HashMap<>();

/**
* Attaches a Key-Value Pair to a Craft Object.
*/
void setDataTag(@NotNull String key, @Nullable Object data);

/**
* Gets the Value of and then Removes the Key, if Present (Otherwise Returns Null).
*/
@Nullable
Object removeDataTag(String key);

/**
* Gets a Tag Value from a Keys.
*/
@Nullable
Object getDataTag(String key);

/**
* Gets all Tag Values from all Keys.
*/
@NotNull
Collection<Object> getAllTagValues();

/**
* Checks if the Craft has a certain Key out of all Keys
*/
@NotNull
boolean hasDataKey(String key);

/**
* Checks if the Craft has a certain Value from all Keys
*/
@NotNull
boolean hasDataValue(Object value);

/**
* Translates the given HitBox by a MovecraftLocation (Which is Treated as a Vector)
*/
@NotNull
HitBox translateBox(HitBox box, MovecraftLocation vec);

/**
* Rotates the given HitBox around an Axis by a MovecraftRotation
*/
@NotNull
HitBox rotateBox(HitBox box, MovecraftLocation axis, MovecraftRotation rotation);

/**
* Checks if an Object (MovecraftLocation / HitBox) is currently being Tracked.
*/
@NotNull
boolean isTracking(@NotNull Object tracked);

/**
* Replaces/Updates a Key of Tracked MovecraftLocation or HitBox
*/
void setTrackedLocs(@NotNull Collection<Object> tracked, @NotNull Object key);
/**
* Adds a Tracked MovecraftLocation or HitBox to a Key
*/
void addTrackedLoc(@NotNull Object tracked, @NotNull Object key);
/**
* Removes a Tracked MovecraftLocation or HitBox from a Key
*/
void removeTrackedLoc(@NotNull Object tracked, @NotNull Object key);
/**
* Gets all Tracked MovecraftLocations (Including from Tracked HitBoxes) from all Keys
*/
Set<Object> getAllTrackedLocation();

/**
* Gets all Tracked MovecraftLocations (Including from Tracked HitBoxes) or HitBox from a particular key
*/
@Nullable
Set<MovecraftLocation> getTrackedLocs(@NotNull Object key);

/**
* Gets a HitBox representing the current locations that this craft controls
*
* @return the crafts current HitBox
*/


@Deprecated(forRemoval = true) @NotNull
default World getW(){
return this.getWorld();
Expand Down