Skip to content

Commit

Permalink
Address review:
Browse files Browse the repository at this point in the history
Better names
Remove at end, to save copying performance
Avoid potential memory leak when dominant is switched in and out quickly
  • Loading branch information
anonymous123-code committed Oct 14, 2023
1 parent 5eb322a commit d33ba5d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface ManaSpark extends SparkEntity {
/**
* Gets a collection of all Sparks this is tranfering to.
*/
Collection<ManaSpark> getTransfers();
Collection<ManaSpark> getOutgoingTransfers();

/**
* Registers the Spark passed in as a Spark meant for mana to be transfered towards.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class ManaSparkEntity extends SparkBaseEntity implements ManaSpark {
private static final String TAG_UPGRADE = "upgrade";
private static final EntityDataAccessor<Integer> UPGRADE = SynchedEntityData.defineId(ManaSparkEntity.class, EntityDataSerializers.INT);

private final Set<ManaSpark> transfers = Collections.newSetFromMap(new WeakHashMap<>());
private final Set<ManaSpark> outgoingTransfers = Collections.newSetFromMap(new WeakHashMap<>());

private final ArrayList<ManaSpark> transfersTowardsSelfToRegister = new ArrayList<>();

Expand Down Expand Up @@ -106,7 +106,7 @@ public void tick() {
var receiver = getAttachedManaReceiver();

SparkUpgradeType upgrade = getUpgrade();
Collection<ManaSpark> transfers = getTransfers();
Collection<ManaSpark> transfers = getOutgoingTransfers();

switch (upgrade) {
case DISPERSIVE -> {
Expand Down Expand Up @@ -176,7 +176,7 @@ public void tick() {
updateTransfers();
}
if (!transfersTowardsSelfToRegister.isEmpty()) {
transfersTowardsSelfToRegister.remove(0).registerTransfer(this);
transfersTowardsSelfToRegister.remove(transfersTowardsSelfToRegister.size() - 1).registerTransfer(this);
}
}
// Recessive does not need to be handled because recessive sparks get notified in all relevant cases
Expand Down Expand Up @@ -228,6 +228,7 @@ public void tick() {

@Override
public void updateTransfers() {
transfersTowardsSelfToRegister.clear();
switch (getUpgrade()) {
case RECESSIVE -> {
var otherSparks = SparkHelper.getSparksAround(level(), getX(), getY() + (getBbHeight() / 2), getZ(), getNetwork());
Expand All @@ -238,7 +239,7 @@ public void updateTransfers() {
&& otherUpgrade != SparkUpgradeType.DOMINANT
&& otherUpgrade != SparkUpgradeType.RECESSIVE
&& otherUpgrade != SparkUpgradeType.ISOLATED) {
transfers.add(otherSpark);
outgoingTransfers.add(otherSpark);
}
}
}
Expand Down Expand Up @@ -301,7 +302,7 @@ public InteractionResult interact(Player player, InteractionHand hand) {
setUpgrade(SparkUpgradeType.NONE);

// Recalculate transfers, recessive and dominant will register the proper transfers
transfers.clear();
outgoingTransfers.clear();
notifyOthers(getNetwork());
} else {
dropAndKill();
Expand Down Expand Up @@ -363,7 +364,7 @@ public ManaReceiver getAttachedManaReceiver() {
}

private void filterTransfers() {
Iterator<ManaSpark> iter = transfers.iterator();
Iterator<ManaSpark> iter = outgoingTransfers.iterator();
while (iter.hasNext()) {
ManaSpark spark = iter.next();
SparkUpgradeType upgr = getUpgrade();
Expand All @@ -385,20 +386,20 @@ private void filterTransfers() {
}

@Override
public Collection<ManaSpark> getTransfers() {
return transfers;
public Collection<ManaSpark> getOutgoingTransfers() {
return outgoingTransfers;
}

private boolean hasTransfer(ManaSpark entity) {
return transfers.contains(entity);
return outgoingTransfers.contains(entity);
}

@Override
public void registerTransfer(ManaSpark entity) {
if (hasTransfer(entity)) {
return;
}
transfers.add(entity);
outgoingTransfers.add(entity);
filterTransfers();
}

Expand Down

0 comments on commit d33ba5d

Please sign in to comment.