Skip to content
Draft

War #75

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/io/icker/factions/FactionsMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void onInitialize() {
ServerManager.register();
SoundManager.register();
WorldManager.register();
WarManager.register();

CommandRegistrationCallback.EVENT.register(FactionsMod::registerCommands);
}
Expand Down Expand Up @@ -74,6 +75,7 @@ private static void registerCommands(CommandDispatcher<ServerCommandSource> disp
new ModifyCommand(),
new RankCommand(),
new SafeCommand(),
new WarCommand(),
};

for (Command command : commands) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/icker/factions/api/events/RelationshipEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.icker.factions.api.persistents.Relationship;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.network.ServerPlayerEntity;

/**
* All events related to relationships (UNIMPLEMENTED)
Expand All @@ -26,6 +27,12 @@ public final class RelationshipEvents {
}
});

public static final Event<OnTrespassing> ON_TRESPASSING = EventFactory.createArrayBacked(OnTrespassing.class, callbacks -> (player) -> {
for (OnTrespassing callback : callbacks) {
callback.onTrespassing(player);
}
});

@FunctionalInterface
public interface NewDecleration {
void onNewDecleration(Relationship relationship);
Expand All @@ -40,4 +47,9 @@ public interface NewMutual {
public interface EndMutual {
void onEndMutual(Relationship relationship, Relationship.Status oldStatus);
}

@FunctionalInterface
public interface OnTrespassing {
void onTrespassing(ServerPlayerEntity player);
}
}
26 changes: 26 additions & 0 deletions src/main/java/io/icker/factions/api/persistents/Faction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import io.icker.factions.database.Field;
import io.icker.factions.database.Name;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Predicate;

@Name("Faction")
public class Faction {
Expand Down Expand Up @@ -68,6 +72,7 @@ public String getKey() {
return id.toString();
}

@Nullable
public static Faction get(UUID id) {
return STORE.get(id);
}
Expand Down Expand Up @@ -169,6 +174,19 @@ public List<User> getUsers() {
return User.getByFaction(id);
}

public void sendCommandTree(PlayerManager playerManager) {
sendCommandTree(playerManager, user -> true);
}

public void sendCommandTree(PlayerManager playerManager, Predicate<User> filter) {
getUsers().stream().filter(filter).forEach(user -> {
ServerPlayerEntity player = playerManager.getPlayer(user.getID());
if (player != null) {
playerManager.sendCommandTree(player);
}
});
}

public List<Claim> getClaims() {
return Claim.getByFaction(id);
}
Expand Down Expand Up @@ -210,6 +228,10 @@ public boolean isMutualAllies(UUID target) {
return rel.status == Relationship.Status.ALLY && getReverse(rel).status == Relationship.Status.ALLY;
}

public boolean atWarWith(Faction target) {
return War.getByFactions(this, target) != null;
}

public List<Relationship> getMutualAllies() {
return relationships.stream().filter(rel -> isMutualAllies(rel.target)).toList();
}
Expand All @@ -222,6 +244,10 @@ public List<Relationship> getEnemiesOf() {
return relationships.stream().filter(rel -> getReverse(rel).status == Relationship.Status.ENEMY).toList();
}

public List<War> getWars() {
return War.getByFaction(this);
}

public void removeRelationship(UUID target) {
relationships = new ArrayList<>(relationships.stream().filter(rel -> !rel.target.equals(target)).toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ public class Relationship {
public enum Status {
ALLY,
NEUTRAL,
ENEMY,
ENEMY
}

@Field("Target")
public UUID target;

@Field("Aggression")
public int aggression;

@Field("Status")
public Status status;

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/icker/factions/api/persistents/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.icker.factions.api.persistents;

import io.icker.factions.FactionsMod;
import io.icker.factions.api.events.FactionEvents;
import io.icker.factions.database.Database;
import io.icker.factions.database.Field;
Expand Down Expand Up @@ -53,10 +54,16 @@ public enum SoundMode {
@Field("Sounds")
public SoundMode sounds = SoundMode.ALL;

@Field("Lives")
public int lives = FactionsMod.CONFIG.WAR != null ? FactionsMod.CONFIG.WAR.NUM_LIVES : 3;

public boolean autoclaim = false;
public boolean bypass = false;
public String language = "en_us";

public boolean isTrespassing = false;
public int startedTrespassing = 0;

private User spoof;

public User(UUID id) {
Expand Down
159 changes: 159 additions & 0 deletions src/main/java/io/icker/factions/api/persistents/War.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package io.icker.factions.api.persistents;

import io.icker.factions.FactionsMod;
import io.icker.factions.database.Database;
import io.icker.factions.database.Field;
import io.icker.factions.database.Name;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

@Name("War")
public class War {
private static final HashMap<UUID, War> STORE = Database.load(War.class, War::getID);

@Field("ID")
private UUID id;

@Field("Source")
private UUID source;

@Field("Target")
private UUID target;

@Field("SourceTeam")
private ArrayList<UUID> sourceTeam = new ArrayList<>();

@Field("TargetTeam")
private ArrayList<UUID> targetTeam = new ArrayList<>();

@Field("Name")
private String name;

public boolean sourceReadyToEnd = false;
public boolean targetReadyToEnd = false;

public War(Faction source, Faction target) {
this.source = source.getID();
this.target = target.getID();
this.targetTeam.add(target.getID());
this.sourceTeam.add(source.getID());
this.id = UUID.randomUUID();
name = String.format("The %s-%s war", source.getName(), target.getName());

for (Faction faction : getFactions()) {
for (User user : faction.getUsers()) {
user.lives = FactionsMod.CONFIG.WAR.NUM_LIVES;
}
}
}

public War() {}

public String getKey() {
return id.toString();
}

public static War get(UUID id) {
return STORE.get(id);
}

public static War getByFactions(Faction source, Faction target) {
UUID id = STORE
.keySet()
.stream()
.filter(warKey -> {
War war = get(warKey);
return (war.getSourceTeam().contains(source) && war.getTargetTeam().contains(target)) || (war.getSourceTeam().contains(target) && war.getTargetTeam().contains(source));
})
.findFirst()
.orElse(null);

if (id == null) return null;
return War.get(id);
}

public static List<War> getByFaction(Faction source) {
return STORE
.keySet()
.stream()
.filter(warKey -> {
War war = get(warKey);
return war.getSourceTeam().contains(source) || war.getTargetTeam().contains(source);
})
.map(War::get)
.toList();

}

public static War getByName(String name) {
FactionsMod.LOGGER.info(all().size());
return all().stream().filter(war -> war.getName().equals(name)).findFirst().orElse(null);
}

public static List<War> all() {
return STORE.keySet().stream().map(War::get).toList();
}

public static void add(War war) {
STORE.put(war.getID(), war);
}

public UUID getID() {
return id;
}

public void addSource(Faction source) {
for (User user : source.getUsers()) {
user.lives = FactionsMod.CONFIG.WAR.NUM_LIVES;
}
this.sourceTeam.add(source.getID());
}

public List<Faction> getSourceTeam() {
return sourceTeam.stream().map(Faction::get).toList();
}

public Faction getSource() {
return Faction.get(source);
}

public void addTarget(Faction target) {
for (User user : target.getUsers()) {
user.lives = FactionsMod.CONFIG.WAR.NUM_LIVES;
}
this.targetTeam.add(target.getID());
}

public List<Faction> getTargetTeam() {
return targetTeam.stream().map(Faction::get).toList();
}

public Faction getTarget() {
return Faction.get(target);
}

public String getName() {
return name;
}

public List<Faction> getFactions() {
return Stream.concat(getTargetTeam().stream(), getSourceTeam().stream()).toList();
}

public void end() {
STORE.remove(this.getID());
for (Faction faction : getFactions()) {
for (User user : faction.getUsers()) {
user.lives = FactionsMod.CONFIG.WAR.NUM_LIVES;
}
}
}

public static void save() {
Database.save(War.class, STORE.values().stream().toList());
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/icker/factions/command/AdminCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ private int power(CommandContext<ServerCommandSource> context) throws CommandSyn
return 1;
}

private int lives(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayer();

ServerPlayerEntity targetEntity = EntityArgumentType.getPlayer(context, "player");
User target = User.get(targetEntity.getUuid());

int numLives = IntegerArgumentType.getInteger(context, "numLives");

target.lives = numLives;

new Message("Set %s's lives to %d", targetEntity.getName().getString(), numLives).send(player, false);
new Message("Your lives have been set to %d by %s", numLives, player.getName().getString()).send(targetEntity, false);

return 1;
}

private int spoof(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayer();
Expand Down Expand Up @@ -140,6 +157,17 @@ public LiteralCommandNode<ServerCommandSource> getNode() {
)
.executes(this::clearSpoof)
)
.then(
CommandManager.literal("lives")
.requires(Requires.hasPerms("factions.admin.lives", FactionsMod.CONFIG.REQUIRED_BYPASS_LEVEL))
.then(
CommandManager.argument("numLives", IntegerArgumentType.integer())
.then(
CommandManager.argument("player", EntityArgumentType.player())
.executes(this::lives)
)
)
)
.build();
}
}
9 changes: 5 additions & 4 deletions src/main/java/io/icker/factions/command/DeclareCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package io.icker.factions.command;

import java.util.Locale;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.LiteralCommandNode;

import io.icker.factions.api.persistents.Faction;
import io.icker.factions.api.persistents.Relationship;
import io.icker.factions.util.Command;
Expand All @@ -16,6 +13,8 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Formatting;

import java.util.Locale;

public class DeclareCommand implements Command {
private int ally(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return updateRelationship(context, Relationship.Status.ALLY);
Expand Down Expand Up @@ -57,8 +56,10 @@ private int updateRelationship(CommandContext<ServerCommandSource> context, Rela
Relationship rev = targetFaction.getRelationship(sourceFaction.getID());
sourceFaction.setRelationship(rel);

rel.aggression = 0;

Message msgStatus = rel.status == Relationship.Status.ALLY ? new Message("allies").format(Formatting.GREEN)
: rel.status == Relationship.Status.ENEMY ? new Message("enemies").format(Formatting.RED)
: rel.status == Relationship.Status.ENEMY ? new Message("enemies").format(Formatting.RED)
: new Message("neutral");

if (rel.status == rev.status) {
Expand Down
Loading