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

Per player power patch #71

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fd8d864
Permission-based power modifiers
CamperSamu Jul 3, 2022
4948d96
Merge branch 'master' into per-player-power-patch
CamperSamu Jul 3, 2022
6274004
Fix imports
CamperSamu Jul 4, 2022
e3be841
Merge branch 'per-player-power-patch' into patch-1
CamperSamu Jul 6, 2022
bc9cda4
Merge pull request #2 from CamperSamu/patch-1
CamperSamu Jul 6, 2022
384539b
This _should_ work, backing up.
CamperSamu Jul 6, 2022
97cfd5d
Remove base power pt.1
CamperSamu Jul 6, 2022
f4c6b8e
Merge branch 'master' into per-player-power-patch
CamperSamu Jul 10, 2022
09790d6
fix power
CamperSamu Jul 10, 2022
7bb9da6
forgor a null check
CamperSamu Jul 10, 2022
1f116a8
Removed Base Power (commented out), added Faction#calculateRequiredPo…
CamperSamu Jul 10, 2022
29b21ea
Null-proofing Command.java
CamperSamu Jul 10, 2022
e3f9d2c
Fix User I/O (oops)
CamperSamu Jul 10, 2022
104e6f1
Added a generic Player Death event
CamperSamu Jul 10, 2022
ff46fb4
Added a generic Player Death event pt.2 + config option to apply deat…
CamperSamu Jul 10, 2022
66a96fc
Fixed the required power calculation (whoops)
CamperSamu Jul 10, 2022
c38700e
Default power config re-balance
CamperSamu Jul 10, 2022
6911ab5
Merge branch 'ickerio:master' into per-player-power-patch
CamperSamu Jul 12, 2022
c35111a
Fix ArithmeticException when connecting via FabricProxy-Lite with hac…
CamperSamu Jul 17, 2022
8892a87
Merge remote-tracking branch 'origin/per-player-power-patch' into per…
CamperSamu Jul 17, 2022
cb22339
Merge branch 'master' into per-player-power-patch
CamperSamu Jul 28, 2022
4d83169
One day I'll stop messing up conflicts lol
CamperSamu Jul 28, 2022
d33c59e
Fix comment in Message and remove migrator
BlueZeeKing Jul 30, 2022
6795b08
Combine player death events
BlueZeeKing Jul 30, 2022
6e852df
Merge branch 'master' into per-player-power-patch
CamperSamu Aug 3, 2022
f0a87d5
fix: Power penalty/bonus not working correctly
CamperSamu Aug 3, 2022
fabc217
fix: Wrong power reward/penalty message
CamperSamu Aug 3, 2022
e115b1f
Remove redundant check if member is null
BlueZeeKing Aug 3, 2022
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {

modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation "me.lucko:fabric-permissions-api:${project.lucko_permissions_version}"
compileOnly "net.luckperms:api:${project.lpapi_version}"
modImplementation "eu.pb4:placeholder-api:${project.papi_version}"
compileOnly "us.dynmap:DynmapCoreAPI:${project.dynmap_api_version}"
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ archives_base_name = factions
# Dependencies
fabric_version=0.58.5+1.19.1
lucko_permissions_version=0.1-SNAPSHOT
lpapi_version = 5.4
dynmap_api_version=3.4-beta-3
papi_version=2.0.0-beta.7+1.19
14 changes: 14 additions & 0 deletions src/main/java/io/icker/factions/api/events/PlayerEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public class PlayerEvents {
}
});

/**
* Called when a player dies
*/
public static final Event<PlayerDeath> ON_PLAYER_DEATH = EventFactory.createArrayBacked(PlayerDeath.class, callbacks -> (player, source) -> {
for (PlayerDeath callback : callbacks) {
callback.onPlayerDeath(player, source);
}
});

/**
* Called on a power reward will be given
*/
Expand Down Expand Up @@ -101,6 +110,11 @@ public interface KilledByPlayer {
void onKilledByPlayer(ServerPlayerEntity player, DamageSource source);
}

@FunctionalInterface
public interface PlayerDeath {
void onPlayerDeath(ServerPlayerEntity player, DamageSource source);
}

@FunctionalInterface
public interface PowerTick {
void onPowerTick(ServerPlayerEntity player);
Expand Down
119 changes: 58 additions & 61 deletions src/main/java/io/icker/factions/api/persistents/Faction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import io.icker.factions.database.Name;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.NotNull;

import java.util.*;

@Name("Faction")
public class Faction {
private static final HashMap<UUID, Faction> STORE = Database.load(Faction.class, Faction::getID);
@Field("Invites")
public ArrayList<UUID> invites = new ArrayList<>();

@Field("ID")
private UUID id;
Expand All @@ -35,37 +38,25 @@ public class Faction {
@Field("Open")
private boolean open;

@Field("Power")
private int power;

@Field("Home")
private Home home;

@Field("Safe")
private SimpleInventory safe = new SimpleInventory(54);

@Field("Invites")
public ArrayList<UUID> invites = new ArrayList<>();

@Field("Relationships")
private ArrayList<Relationship> relationships = new ArrayList<>();

public Faction(String name, String description, String motd, Formatting color, boolean open, int power) {
public Faction(String name, String description, String motd, Formatting color, boolean open) {
this.id = UUID.randomUUID();
this.name = name;
this.motd = motd;
this.description = description;
this.color = color.getName();
this.open = open;
this.power = power;
}

@SuppressWarnings("unused")
public Faction() {}

@SuppressWarnings("unused")
public String getKey() {
return id.toString();
public Faction() {
}

public static Faction get(UUID id) {
Expand All @@ -74,10 +65,10 @@ public static Faction get(UUID id) {

public static Faction getByName(String name) {
return STORE.values()
.stream()
.filter(f -> f.name.equals(name))
.findFirst()
.orElse(null);
.stream()
.filter(f -> f.name.equals(name))
.findFirst()
.orElse(null);
}

public static void add(Faction faction) {
Expand All @@ -91,9 +82,14 @@ public static Collection<Faction> all() {
@SuppressWarnings("unused")
public static List<Faction> allBut(UUID id) {
return STORE.values()
.stream()
.filter(f -> f.id != id)
.toList();
.stream()
.filter(f -> f.id != id)
.toList();
}

@SuppressWarnings("unused")
public String getKey() {
return id.toString();
}

public UUID getID() {
Expand All @@ -104,67 +100,60 @@ public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
FactionEvents.MODIFY.invoker().onModify(this);
}

public Formatting getColor() {
return Formatting.byName(color);
}

public void setColor(Formatting color) {
this.color = color.getName();
FactionEvents.MODIFY.invoker().onModify(this);
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
FactionEvents.MODIFY.invoker().onModify(this);
}

public String getMOTD() {
return motd;
}

public void setMOTD(String motd) {
this.motd = motd;
FactionEvents.MODIFY.invoker().onModify(this);
}

public int getPower() {
return power;
return getUsers().stream().mapToInt(User::getPower).sum();
}

public SimpleInventory getSafe() {
return safe;
}

public boolean isOpen() {
return open;
}

public void setName(String name) {
this.name = name;
FactionEvents.MODIFY.invoker().onModify(this);
}

public void setDescription(String description) {
this.description = description;
FactionEvents.MODIFY.invoker().onModify(this);
}

public void setMOTD(String motd) {
this.motd = motd;
FactionEvents.MODIFY.invoker().onModify(this);
@SuppressWarnings("unused")
public void setSafe(SimpleInventory safe) {
this.safe = safe;
}

public void setColor(Formatting color) {
this.color = color.getName();
FactionEvents.MODIFY.invoker().onModify(this);
public boolean isOpen() {
return open;
}

public void setOpen(boolean open) {
this.open = open;
FactionEvents.MODIFY.invoker().onModify(this);
}

public int adjustPower(int adjustment) {
int maxPower = calculateMaxPower();
int newPower = Math.min(Math.max(0, power + adjustment), maxPower);
int oldPower = this.power;

if (newPower == oldPower) return 0;

power = newPower;
FactionEvents.POWER_CHANGE.invoker().onPowerChange(this, oldPower);
return Math.abs(newPower - oldPower);
}

public List<User> getUsers() {
return User.getByFaction(id);
}
Expand All @@ -175,8 +164,7 @@ public List<Claim> getClaims() {

public void removeAllClaims() {
Claim.getByFaction(id)
.stream()
.forEach(Claim::remove);
.forEach(Claim::remove);
FactionEvents.REMOVE_ALL_CLAIMS.invoker().onRemoveAllClaims(this);
}

Expand Down Expand Up @@ -208,7 +196,7 @@ public Relationship getReverse(Relationship rel) {
public boolean isMutualAllies(UUID target) {
Relationship rel = getRelationship(target);
return rel.status == Relationship.Status.ALLY && getReverse(rel).status == Relationship.Status.ALLY;
}
}

public List<Relationship> getMutualAllies() {
return relationships.stream().filter(rel -> isMutualAllies(rel.target)).toList();
Expand All @@ -218,6 +206,7 @@ public List<Relationship> getEnemiesWith() {
return relationships.stream().filter(rel -> rel.status == Relationship.Status.ENEMY).toList();
}

@SuppressWarnings("unused") //util
public List<Relationship> getEnemiesOf() {
return relationships.stream().filter(rel -> getReverse(rel).status == Relationship.Status.ENEMY).toList();
}
Expand All @@ -226,7 +215,7 @@ public void removeRelationship(UUID target) {
relationships = new ArrayList<>(relationships.stream().filter(rel -> !rel.target.equals(target)).toList());
}

public void setRelationship(Relationship relationship) {
public void setRelationship(@NotNull Relationship relationship) {
if (getRelationship(relationship.target) != null) {
removeRelationship(relationship.target);
}
Expand All @@ -246,6 +235,15 @@ public void remove() {
FactionEvents.DISBAND.invoker().onDisband(this);
}

public int calculateMaxPower() {
//we need to remove base power otherwise is problematic
int maxPower = 0;
for (final User user : getUsers()) {
maxPower += user.getMaxPower();
}
return maxPower;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -258,8 +256,7 @@ public static void save() {
Database.save(Faction.class, STORE.values().stream().toList());
}

// TODO(samu): import per-player power patch
public int calculateMaxPower(){
return FactionsMod.CONFIG.POWER.BASE + (getUsers().size() * FactionsMod.CONFIG.POWER.MEMBER);
public int calculateRequiredPower(){
return (getClaims().size()) * FactionsMod.CONFIG.POWER.CLAIM_WEIGHT;
}
}
48 changes: 47 additions & 1 deletion src/main/java/io/icker/factions/api/persistents/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import io.icker.factions.database.Database;
import io.icker.factions.database.Field;
import io.icker.factions.database.Name;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static io.icker.factions.FactionsMod.CONFIG;
import static io.icker.factions.util.PermissionUtil.getPermissionPower;

@Name("User")
public class User {
private static final HashMap<UUID, User> STORE = Database.load(User.class, User::getID);
Expand Down Expand Up @@ -44,6 +48,10 @@ public enum SoundMode {
@Field("Rank")
public Rank rank;

@Field("Power")
private int power;

// NOTE(CamperSamu): This should be private and have getters and setters with events in the API
@Field("Radar")
public boolean radar = false;

Expand All @@ -61,11 +69,12 @@ public enum SoundMode {

public User(UUID id) {
this.id = id;
power = getMaxPower();
}

public User() {}

@SuppressWarnings("unused")
@SuppressWarnings("unused") //util
public String getKey() {
return id.toString();
}
Expand All @@ -88,6 +97,7 @@ public static void add(User user) {
STORE.put(user.id, user);
}

// NOTE(CamperSamu): Consider refactoring this to uuid(), following the record-like naming convention
public UUID getID() {
return id;
}
Expand Down Expand Up @@ -146,4 +156,40 @@ public static void save() {
Database.save(User.class, STORE.values().stream().toList());
}

//region Power Management
public static int getMaxPower(final @NotNull UUID userUUID) {
final var user = get(userUUID);
return user != null ? user.getMaxPower() : 0;
}

public int getMaxPower() {
return CONFIG.POWER.MEMBER + getPermissionPower(getID());
}

public static int getPower(final @NotNull UUID userUUID) {
return get(userUUID).getPower();
}

public int getPower() {
return power;
}

@SuppressWarnings("unused") //util
public static int setPower(final @NotNull UUID userUUID, final int newPower) {
return get(userUUID).setPower(newPower);
}

public int setPower(final int newPower) {
return power = newPower;
BlueZeeKing marked this conversation as resolved.
Show resolved Hide resolved
}

@SuppressWarnings("unused") //util
public static int addPower(final @NotNull UUID userUUID, final int powerModifier) {
return get(userUUID).addPower(powerModifier);
}

public int addPower(final int powerModifier) {
return power += powerModifier;
}
//endregion
}
Loading