generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Added a CCA synced component for command tags; allows for checking for command tags in the client + Added `has_command_tag` entity condition type
- Loading branch information
Showing
8 changed files
with
215 additions
and
1 deletion.
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
27 changes: 27 additions & 0 deletions
27
src/main/java/io/github/apace100/apoli/component/CommandTagComponent.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,27 @@ | ||
package io.github.apace100.apoli.component; | ||
|
||
import dev.onyxstudios.cca.api.v3.component.ComponentKey; | ||
import dev.onyxstudios.cca.api.v3.component.ComponentRegistry; | ||
import dev.onyxstudios.cca.api.v3.component.load.ServerLoadAwareComponent; | ||
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent; | ||
import io.github.apace100.apoli.Apoli; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.util.Set; | ||
|
||
public interface CommandTagComponent extends AutoSyncedComponent, ServerLoadAwareComponent { | ||
|
||
ComponentKey<CommandTagComponent> KEY = ComponentRegistry.getOrCreate(Apoli.identifier("command_tags"), CommandTagComponent.class); | ||
|
||
Set<String> getTags(); | ||
|
||
@ApiStatus.Internal | ||
void setTags(Set<String> commandTags); | ||
|
||
@ApiStatus.Internal | ||
boolean addTag(String commandTag); | ||
|
||
@ApiStatus.Internal | ||
boolean removeTag(String commandTag); | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/io/github/apace100/apoli/component/CommandTagComponentImpl.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,80 @@ | ||
package io.github.apace100.apoli.component; | ||
|
||
import io.github.apace100.apoli.mixin.EntityAccessor; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.nbt.NbtElement; | ||
import net.minecraft.nbt.NbtList; | ||
import net.minecraft.nbt.NbtString; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class CommandTagComponentImpl implements CommandTagComponent { | ||
|
||
private final Set<String> commandTags; | ||
private final Entity provider; | ||
|
||
public CommandTagComponentImpl(Entity provider) { | ||
this.commandTags = new HashSet<>(); | ||
this.provider = provider; | ||
} | ||
|
||
@Override | ||
public Set<String> getTags() { | ||
return commandTags; | ||
} | ||
|
||
@Override | ||
public void setTags(Set<String> commandTags) { | ||
this.commandTags.clear(); | ||
this.commandTags.addAll(commandTags); | ||
} | ||
|
||
@Override | ||
public boolean addTag(String commandTag) { | ||
return this.commandTags.add(commandTag); | ||
} | ||
|
||
@Override | ||
public boolean removeTag(String commandTag) { | ||
return this.commandTags.remove(commandTag); | ||
} | ||
|
||
@Override | ||
public void loadServerside() { | ||
|
||
Set<String> originalCommandTags = ((EntityAccessor) provider).getOriginalCommandTags(); | ||
|
||
if (!commandTags.equals(originalCommandTags)) { | ||
this.setTags(originalCommandTags); | ||
CommandTagComponent.KEY.sync(provider); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void readFromNbt(NbtCompound tag) { | ||
|
||
NbtList commandTagsNbt = tag.getList("Tags", NbtElement.STRING_TYPE); | ||
this.commandTags.clear(); | ||
|
||
for (int i = 0; i < commandTagsNbt.size(); ++i) { | ||
this.commandTags.add(commandTagsNbt.getString(i)); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void writeToNbt(NbtCompound tag) { | ||
|
||
NbtList commandTagsNbt = new NbtList(); | ||
this.commandTags.stream() | ||
.map(NbtString::of) | ||
.forEach(commandTagsNbt::add); | ||
|
||
tag.put("Tags", commandTagsNbt); | ||
|
||
} | ||
|
||
} |
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
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
42 changes: 42 additions & 0 deletions
42
.../java/io/github/apace100/apoli/power/factory/condition/entity/HasCommandTagCondition.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,42 @@ | ||
package io.github.apace100.apoli.power.factory.condition.entity; | ||
|
||
import io.github.apace100.apoli.Apoli; | ||
import io.github.apace100.apoli.power.factory.condition.ConditionFactory; | ||
import io.github.apace100.apoli.util.IdentifierAlias; | ||
import io.github.apace100.calio.data.SerializableData; | ||
import io.github.apace100.calio.data.SerializableDataTypes; | ||
import net.minecraft.entity.Entity; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class HasCommandTagCondition { | ||
|
||
public static boolean condition(SerializableData.Instance data, Entity entity) { | ||
|
||
Set<String> specifiedCommandTags = new HashSet<>(); | ||
Set<String> commandTags = entity.getCommandTags(); | ||
|
||
data.ifPresent("command_tag", specifiedCommandTags::add); | ||
data.ifPresent("command_tags", specifiedCommandTags::addAll); | ||
|
||
return specifiedCommandTags.isEmpty() ? !commandTags.isEmpty() | ||
: !Collections.disjoint(commandTags, specifiedCommandTags); | ||
|
||
} | ||
|
||
public static ConditionFactory<Entity> getFactory() { | ||
IdentifierAlias.addPathAlias("has_tag", "has_command_tag"); | ||
return new ConditionFactory<>( | ||
Apoli.identifier("has_command_tag"), | ||
new SerializableData() | ||
.add("tag", SerializableDataTypes.STRING, null) | ||
.addFunctionedDefault("command_tag", SerializableDataTypes.STRING, data -> data.get("tag")) | ||
.add("tags", SerializableDataTypes.STRINGS, null) | ||
.addFunctionedDefault("command_tags", SerializableDataTypes.STRINGS, data -> data.get("tags")), | ||
HasCommandTagCondition::condition | ||
); | ||
} | ||
|
||
} |
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