Skip to content

Commit

Permalink
feat(Events): Added new Events
Browse files Browse the repository at this point in the history
Added ProfileCreatedEvent
Added SPropertyAddEvent
Added SPropertyRemoveEvent
  • Loading branch information
GeorgeV220 committed Apr 11, 2023
1 parent 803fafe commit 7a7ff89
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 61 deletions.
@@ -1,8 +1,6 @@
package com.georgev22.skinoverlay.handler.profile;

import com.georgev22.library.maps.HashObjectMap;
import com.georgev22.library.maps.ObjectMap;
import com.georgev22.skinoverlay.SkinOverlay;
import com.georgev22.skinoverlay.handler.SGameProfile;
import com.georgev22.skinoverlay.handler.SProperty;
import com.georgev22.skinoverlay.utilities.player.PlayerObject;
Expand All @@ -17,31 +15,21 @@

public class SGameProfileBungee extends SGameProfile {

private final String name;
private final UUID uuid;
private final ObjectMap<String, SProperty> properties;
private final SkinOverlay skinOverlay = SkinOverlay.getInstance();

public SGameProfileBungee(String name, UUID uuid) {
super(name, uuid);
this.name = name;
this.uuid = uuid;
this.properties = new HashObjectMap<>();
}

public SGameProfileBungee(String name, UUID uuid, ObjectMap<String, SProperty> properties) {
super(name, uuid, properties);
this.name = name;
this.uuid = uuid;
this.properties = properties;
}

@Override
public void apply() {
PlayerObject playerObject = skinOverlay.getPlayer(uuid).orElseThrow();
PlayerObject playerObject = skinOverlay.getPlayer(getUUID()).orElseThrow();
LoginResult initialHandler = ((InitialHandler) ((ProxiedPlayer) playerObject.player()).getPendingConnection()).getLoginProfile();
List<Property> bungeeProperties = new ArrayList<>();
this.properties.forEach((s, sProperty) -> bungeeProperties.add(new Property(sProperty.name(), sProperty.value(), sProperty.signature())));
this.getProperties().forEach((s, sProperty) -> bungeeProperties.add(new Property(sProperty.name(), sProperty.value(), sProperty.signature())));
initialHandler.setProperties(bungeeProperties.toArray(Property[]::new));
}
}
@@ -0,0 +1,83 @@
package com.georgev22.skinoverlay.event.events.profile;

import com.georgev22.library.maps.UnmodifiableObjectMap;
import com.georgev22.skinoverlay.event.Event;
import com.georgev22.skinoverlay.handler.SGameProfile;
import com.georgev22.skinoverlay.handler.SProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.UUID;

/**
* An event that is fired when a new SGameProfile is created.
*/
public class ProfileCreatedEvent implements Event {

private final String name;
private final UUID uuid;
private final UnmodifiableObjectMap<String, SProperty> properties;
private final SGameProfile profile;
private final boolean async;

/**
* Constructs a new ProfileCreatedEvent with the given SGameProfile and whether the event should be run asynchronously.
*
* @param sGameProfile the SGameProfile that was created
* @param async whether the event should be run asynchronously
*/
public ProfileCreatedEvent(@NotNull SGameProfile sGameProfile, boolean async) {
this.name = sGameProfile.getName();
this.uuid = sGameProfile.getUUID();
this.properties = sGameProfile.getProperties();
this.profile = sGameProfile;
this.async = async;
}

/**
* Returns the name associated with the created profile.
*
* @return the name associated with the created profile
*/
public String getName() {
return name;
}

/**
* Returns the UUID associated with the created profile.
*
* @return the UUID associated with the created profile
*/
public UUID getUUID() {
return uuid;
}

/**
* Returns an unmodifiable view of the properties associated with the created profile.
*
* @return an unmodifiable view of the properties associated with the created profile
*/
@UnmodifiableView
public UnmodifiableObjectMap<String, SProperty> getProperties() {
return properties;
}

/**
* Returns the SGameProfile that was created.
*
* @return the SGameProfile that was created
*/
public SGameProfile getProfile() {
return profile;
}

/**
* Returns whether this event should be run asynchronously.
*
* @return {@code true} if this event should be run asynchronously, {@code false} otherwise
*/
@Override
public boolean runAsync() {
return async;
}
}
@@ -0,0 +1,95 @@
package com.georgev22.skinoverlay.event.events.profile.property;

import com.georgev22.skinoverlay.event.Cancellable;
import com.georgev22.skinoverlay.event.Event;
import com.georgev22.skinoverlay.handler.SProperty;

/**
* An event that is fired when a SProperty is added.
*/
public class SPropertyAddEvent implements Event, Cancellable {

private String propertyName;
private SProperty property;
private final boolean async;
private boolean cancelled;

/**
* Constructs a new SPropertyAddEvent with the given property name, SProperty, and whether the event should be run asynchronously.
*
* @param propertyName the name of the property that was added
* @param property the SProperty that was added
* @param async whether the event should be run asynchronously
*/
public SPropertyAddEvent(String propertyName, SProperty property, boolean async) {
this.propertyName = propertyName;
this.property = property;
this.async = async;
}

/**
* Returns the name of the property that was added.
*
* @return the name of the property that was added
*/
public String getPropertyName() {
return propertyName;
}

/**
* Returns the SProperty that was added.
*
* @return the SProperty that was added
*/
public SProperty getProperty() {
return property;
}

/**
* Sets the name of the property that was added.
*
* @param propertyName the new name of the property that was added
*/
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}

/**
* Sets the SProperty that was added.
*
* @param property the new SProperty that was added
*/
public void setProperty(SProperty property) {
this.property = property;
}

/**
* Cancels the event.
*
* @return {@code true} if the event was successfully cancelled, {@code false} otherwise
*/
@Override
public boolean cancel() {
return cancelled = true;
}

/**
* Returns whether the event has been cancelled.
*
* @return {@code true} if the event has been cancelled, {@code false} otherwise
*/
@Override
public boolean isCancelled() {
return cancelled;
}

/**
* Returns whether this event should be run asynchronously.
*
* @return {@code true} if this event should be run asynchronously, {@code false} otherwise
*/
@Override
public boolean runAsync() {
return async;
}
}
@@ -0,0 +1,77 @@
package com.georgev22.skinoverlay.event.events.profile.property;

import com.georgev22.skinoverlay.event.Cancellable;
import com.georgev22.skinoverlay.event.Event;
import com.georgev22.skinoverlay.handler.SProperty;

/**
* An event that is fired when a SProperty is removed.
*/
public class SPropertyRemoveEvent implements Event, Cancellable {

private final String propertyName;
private final SProperty property;
private final boolean async;
private boolean cancelled;

/**
* Constructs a new SPropertyRemoveEvent with the given property name, SProperty, and whether the event should be run asynchronously.
*
* @param propertyName the name of the property that was removed
* @param property the SProperty that was removed
* @param async whether the event should be run asynchronously
*/
public SPropertyRemoveEvent(String propertyName, SProperty property, boolean async) {
this.propertyName = propertyName;
this.property = property;
this.async = async;
}

/**
* Returns the name of the property that was removed.
*
* @return the name of the property that was removed
*/
public String getPropertyName() {
return propertyName;
}

/**
* Returns the SProperty that was removed.
*
* @return the SProperty that was removed
*/
public SProperty getProperty() {
return property;
}

/**
* Cancels the event.
*
* @return {@code true} if the event was successfully cancelled, {@code false} otherwise
*/
@Override
public boolean cancel() {
return cancelled = true;
}

/**
* Returns whether the event has been cancelled.
*
* @return {@code true} if the event has been cancelled, {@code false} otherwise
*/
@Override
public boolean isCancelled() {
return cancelled;
}

/**
* Returns whether this event should be run asynchronously.
*
* @return {@code true} if this event should be run asynchronously, {@code false} otherwise
*/
@Override
public boolean runAsync() {
return async;
}
}
Expand Up @@ -3,6 +3,10 @@
import com.georgev22.library.maps.HashObjectMap;
import com.georgev22.library.maps.ObjectMap;
import com.georgev22.library.maps.UnmodifiableObjectMap;
import com.georgev22.skinoverlay.SkinOverlay;
import com.georgev22.skinoverlay.event.events.profile.ProfileCreatedEvent;
import com.georgev22.skinoverlay.event.events.profile.property.SPropertyAddEvent;
import com.georgev22.skinoverlay.event.events.profile.property.SPropertyRemoveEvent;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.UUID;
Expand All @@ -12,6 +16,8 @@
*/
public abstract class SGameProfile {

protected final SkinOverlay skinOverlay = SkinOverlay.getInstance();

/**
* The player's name.
*/
Expand All @@ -37,6 +43,7 @@ public SGameProfile(String name, UUID uuid) {
this.name = name;
this.uuid = uuid;
this.properties = new HashObjectMap<>();
this.skinOverlay.getEventManager().fireEvent(new ProfileCreatedEvent(this, false));
}

/**
Expand All @@ -50,6 +57,7 @@ public SGameProfile(String name, UUID uuid, ObjectMap<String, SProperty> propert
this.name = name;
this.uuid = uuid;
this.properties = properties;
this.skinOverlay.getEventManager().fireEvent(new ProfileCreatedEvent(this, false));
}

/**
Expand Down Expand Up @@ -91,7 +99,12 @@ public SGameProfile addProperty(String propertyName, SProperty sProperty) {
if (sProperty == null) {
return this;
}
this.properties.append(propertyName, sProperty);
SPropertyAddEvent sPropertyAddEvent = new SPropertyAddEvent(propertyName, sProperty, false);
skinOverlay.getEventManager().fireEvent(sPropertyAddEvent);
if (sPropertyAddEvent.isCancelled()) {
return this;
}
this.properties.append(sPropertyAddEvent.getPropertyName(), sPropertyAddEvent.getProperty());
this.apply();
return this;
}
Expand All @@ -106,7 +119,12 @@ public SGameProfile removeProperty(String propertyName) {
if (!this.properties.containsKey(propertyName)) {
return this;
}
this.properties.remove(propertyName);
SPropertyRemoveEvent sPropertyRemoveEvent = new SPropertyRemoveEvent(propertyName, this.properties.get(propertyName), false);
skinOverlay.getEventManager().fireEvent(sPropertyRemoveEvent);
if (sPropertyRemoveEvent.isCancelled()) {
return this;
}
this.properties.remove(sPropertyRemoveEvent.getPropertyName());
this.apply();
return this;
}
Expand All @@ -122,4 +140,13 @@ public SProperty getProperty(String propertyName) {
}

public abstract void apply();

@Override
public String toString() {
return "SGameProfile{" +
"name='" + name + '\'' +
", uuid=" + uuid +
", properties=" + properties +
'}';
}
}

0 comments on commit 7a7ff89

Please sign in to comment.