fix: generated equals and hashCode for events, entities and entityIDs

This commit is contained in:
Yannik Bretschneider 2021-05-02 00:03:09 +02:00
parent 889e4cf233
commit be88744bc9
8 changed files with 135 additions and 1 deletions

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.entities;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import java.util.Objects;
/** Represents an abstract entity. */
public abstract class Entity {
/** Whether or not the entity is currently active in the game */
@ -44,5 +46,18 @@ public abstract class Entity {
public void setPosition(IntVector2 position) {
this.position = position;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Entity entity = (Entity) o;
return active == entity.active && Objects.equals(position, entity.position) && Objects.equals(id, entity.id);
}
@Override
public int hashCode() {
return Objects.hash(active, position, id);
}
}

View File

@ -1,5 +1,7 @@
package uulm.teamname.marvelous.gamelibrary.entities;
import java.util.Objects;
/** Represents a distinct identification for every {@link Entity} in a game. */
public class EntityID {
/** The index of the entity */
@ -42,4 +44,18 @@ public class EntityID {
public String toString() {
return "["+type.toString()+":"+id+"]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EntityID entityID = (EntityID) o;
return id == entityID.id && type == entityID.type;
}
@Override
public int hashCode() {
return Objects.hash(id, type);
}
}

View File

@ -4,7 +4,11 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
/** Represents a character event for: {@link EventType#MeleeAttackEvent}, {@link EventType#RangedAttackEvent}, {@link EventType#MoveEvent}, {@link EventType#ExchangeInfinityStoneEvent}, {@link EventType#UseInfinityStoneEvent}. */
import java.util.Objects;
/**
* Represents a character event for: {@link EventType#MeleeAttackEvent}, {@link EventType#RangedAttackEvent}, {@link EventType#MoveEvent}, {@link EventType#ExchangeInfinityStoneEvent}, {@link EventType#UseInfinityStoneEvent}.
*/
public class CharacterEvent extends Event {
public EntityID originEntity = null;
public EntityID targetEntity = null;
@ -42,4 +46,18 @@ public class CharacterEvent extends Event {
this.stoneType = stoneType;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
CharacterEvent that = (CharacterEvent) o;
return Objects.equals(originEntity, that.originEntity) && Objects.equals(targetEntity, that.targetEntity) && Objects.equals(originField, that.originField) && Objects.equals(targetField, that.targetField) && Objects.equals(amount, that.amount) && stoneType == that.stoneType;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), originEntity, targetEntity, originField, targetField, amount, stoneType);
}
}

View File

@ -1,9 +1,24 @@
package uulm.teamname.marvelous.gamelibrary.events;
import java.util.HashMap;
import java.util.Objects;
/** Represents a custom event ({@link EventType#CustomEvent}). */
public class CustomEvent extends Event {
public String teamIdentifier;
public HashMap<String, Object> customContent;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
CustomEvent that = (CustomEvent) o;
return Objects.equals(teamIdentifier, that.teamIdentifier) && Objects.equals(customContent, that.customContent);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), teamIdentifier, customContent);
}
}

View File

@ -3,6 +3,8 @@ package uulm.teamname.marvelous.gamelibrary.events;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import java.util.Objects;
/** Represents an entity event for: {@link EventType#DestroyedEntityEvent}, {@link EventType#TakenDamageEvent}, {@link EventType#ConsumedAPEvent}, {@link EventType#ConsumedMPEvent}, {@link EventType#SpawnEntityEvent}, {@link EventType#HealedEvent}. */
public class EntityEvent extends Event {
public EntityID targetEntity = null;
@ -23,4 +25,18 @@ public class EntityEvent extends Event {
this.amount = amount;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
EntityEvent that = (EntityEvent) o;
return Objects.equals(targetEntity, that.targetEntity) && Objects.equals(targetField, that.targetField) && Objects.equals(amount, that.amount);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), targetEntity, targetField, amount);
}
}

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.events;
import uulm.teamname.marvelous.gamelibrary.json.ingame.MessageStructure;
import java.util.Objects;
/** Represents an abstract event sent inside a {@link MessageStructure} between client and server. */
public abstract class Event {
public EventType type;
@ -10,4 +12,17 @@ public abstract class Event {
this.type = type;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Event event = (Event) o;
return type == event.type;
}
@Override
public int hashCode() {
return Objects.hash(type);
}
}

View File

@ -4,6 +4,9 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
import java.util.Arrays;
import java.util.Objects;
/** Represents a game event for: {@link EventType#RoundSetupEvent}, {@link EventType#TurnEvent}, {@link EventType#WinEvent}, {@link EventType#TurnTimeoutEvent}, {@link EventType#TimeoutWarningEvent}, {@link EventType#TimeoutEvent}, {@link EventType#DisconnectEvent}. */
public class GameEvent extends Event {
public int roundCount;
@ -15,4 +18,20 @@ public class GameEvent extends Event {
public String message;
public int timeLeft;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
GameEvent gameEvent = (GameEvent) o;
return roundCount == gameEvent.roundCount && turnCount == gameEvent.turnCount && playerWon == gameEvent.playerWon && timeLeft == gameEvent.timeLeft && Arrays.equals(characterOrder, gameEvent.characterOrder) && Objects.equals(nextCharacter, gameEvent.nextCharacter) && Objects.equals(message, gameEvent.message);
}
@Override
public int hashCode() {
int result = Objects.hash(super.hashCode(), roundCount, turnCount, nextCharacter, playerWon, message, timeLeft);
result = 31 * result + Arrays.hashCode(characterOrder);
return result;
}
}

View File

@ -3,10 +3,30 @@ package uulm.teamname.marvelous.gamelibrary.events;
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import java.util.Arrays;
import java.util.Objects;
/** Represents the game state event ({@link EventType#GameStateEvent}). */
public class GameStateEvent extends Event {
public Entity[] entities;
public EntityID[] turnOrder;
public EntityID activeCharacter;
public boolean winCondition;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
GameStateEvent that = (GameStateEvent) o;
return winCondition == that.winCondition && Arrays.equals(entities, that.entities) && Arrays.equals(turnOrder, that.turnOrder) && Objects.equals(activeCharacter, that.activeCharacter);
}
@Override
public int hashCode() {
int result = Objects.hash(super.hashCode(), activeCharacter, winCondition);
result = 31 * result + Arrays.hashCode(entities);
result = 31 * result + Arrays.hashCode(turnOrder);
return result;
}
}