Skip to content

Instantly share code, notes, and snippets.

@intronate67
Created December 17, 2016 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intronate67/db38487e456a81ab7c49d7bb7068deed to your computer and use it in GitHub Desktop.
Save intronate67/db38487e456a81ab7c49d7bb7068deed to your computer and use it in GitHub Desktop.
/*This file is part of Disguise, licensed under the MIT License (MIT).
*
* Copyright (c) 2016 Hunter Sharpe
* Copyright (c) contributors
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.huntersharpe.Disguise.data;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Objects;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
import org.spongepowered.api.data.merge.MergeFunction;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.api.entity.EntityType;
import org.spongepowered.api.entity.EntityTypes;
import java.util.Optional;
import java.util.UUID;
public class DisguiseData extends AbstractData<DisguiseData, ImmutableDisguiseData> {
private EntityType entityType;
private UUID identifier;
public DisguiseData(){
new DisguiseData(EntityTypes.PIG, UUID.fromString("none"));
}
public DisguiseData(EntityType entityType, UUID identifier){
this.entityType = entityType;
this.identifier = identifier;
}
public Value<EntityType> entityTypeValue(){
return Sponge.getRegistry().getValueFactory().createValue(DisguiseKeys.ENTITY_TYPE, this.entityType);
}
public Value<UUID> identifierValue(){
return Sponge.getRegistry().getValueFactory().createValue(DisguiseKeys.IDENTIFIER, this.identifier);
}
@Override
protected void registerGettersAndSetters(){
registerFieldGetter(DisguiseKeys.ENTITY_TYPE, () -> this.entityType);
registerFieldSetter(DisguiseKeys.ENTITY_TYPE, value -> this.entityType = value);
registerKeyValue(DisguiseKeys.ENTITY_TYPE, this::entityTypeValue);
registerFieldGetter(DisguiseKeys.IDENTIFIER, () -> this.identifier);
registerFieldSetter(DisguiseKeys.IDENTIFIER, value -> this.identifier = checkNotNull(value));
registerKeyValue(DisguiseKeys.IDENTIFIER, this::identifierValue);
}
@Override
public Optional<DisguiseData> fill(DataHolder dataHolder, MergeFunction overlap){
return Optional.of(this);
}
@Override
public Optional<DisguiseData> from(DataContainer container){
if(!container.contains(DisguiseKeys.IDENTIFIER.getQuery(), DisguiseKeys.ENTITY_TYPE.getQuery())){
return Optional.empty();
}
final EntityType entityType = (EntityType)container.get(DisguiseKeys.ENTITY_TYPE.getQuery()).get();
final UUID identifier = (UUID)container.get(DisguiseKeys.IDENTIFIER.getQuery()).get();
this.entityType = entityType;
this.identifier = identifier;
return Optional.of(this);
}
@Override
public DisguiseData copy(){
return new DisguiseData(this.entityType, this.identifier);
}
@Override
public ImmutableDisguiseData asImmutable(){
return new ImmutableDisguiseData(this.entityType, this.identifier);
}
@Override
public int getContentVersion() {
return 1;
}
@Override
public DataContainer toContainer() {
return super.toContainer()
.set(DisguiseKeys.ENTITY_TYPE, this.entityType)
.set(DisguiseKeys.IDENTIFIER, this.identifier);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("entitytype", this.entityType)
.add("identifier", this.identifier)
.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment