Add GameModeTrait

This commit is contained in:
fullwall 2018-11-12 14:53:52 +08:00
parent f47912f001
commit 3ca12de138
3 changed files with 33 additions and 1 deletions

View File

@ -75,6 +75,7 @@ import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.FollowTrait;
import net.citizensnpcs.trait.GameModeTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.HorseModifiers;
import net.citizensnpcs.trait.LookClose;
@ -564,7 +565,7 @@ public class NPCCommands {
Messaging.sendErrorTr(sender, Messages.GAMEMODE_INVALID, args.getString(1));
return;
}
player.setGameMode(mode);
npc.getTrait(GameModeTrait.class).setGameMode(mode);
Messaging.sendTr(sender, Messages.GAMEMODE_SET, mode.name().toLowerCase());
}

View File

@ -25,6 +25,7 @@ import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.FollowTrait;
import net.citizensnpcs.trait.GameModeTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.HorseModifiers;
import net.citizensnpcs.trait.LookClose;
@ -57,6 +58,7 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(CurrentLocation.class));
registerTrait(TraitInfo.create(Equipment.class));
registerTrait(TraitInfo.create(FollowTrait.class));
registerTrait(TraitInfo.create(GameModeTrait.class));
registerTrait(TraitInfo.create(Gravity.class));
registerTrait(TraitInfo.create(HorseModifiers.class));
registerTrait(TraitInfo.create(Inventory.class));

View File

@ -0,0 +1,29 @@
package net.citizensnpcs.trait;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
@TraitName("gamemodetrait")
public class GameModeTrait extends Trait {
@Persist
private GameMode mode;
public GameModeTrait() {
super("gamemodetrait");
}
@Override
public void run() {
if (npc.isSpawned() && npc.getEntity() instanceof Player && mode != null) {
((Player) npc.getEntity()).setGameMode(mode);
}
}
public void setGameMode(GameMode mode) {
this.mode = mode;
}
}