Skip to content

Commit

Permalink
Merge pull request #34 from tales-pw/add-gameobject-clone
Browse files Browse the repository at this point in the history
Add gameobject clone
  • Loading branch information
xunto authored Feb 24, 2022
2 parents e6d17dc + 4b3b7cc commit f306f91
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dependencies {
testCompile 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:3.+"

shade 'pw.tales:cofdsystem:5.0.1'
shade 'pw.tales:cofdsystem:5.4.0'
shade 'com.squareup.okhttp3:okhttp:3.11.0'
shade('com.google.inject:guice:4.1.0') {
exclude group: "com.google.guava", module: "guava"
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pw/tales/cofdsystem/mod/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Config(modid = TalesSystem.MOD_ID)
public class ModConfig {

public static String systemApiUrl = "https://napi.tales.pw";
public static String systemApiUrl = "https://api.tales.pw";
public static String systemApiToken = "server_token";

public static String accountsApiUrl = "http://auth.tales.pw";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pw.tales.cofdsystem.mod.client.modules.go_relation_entity.command;

import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.ArrayList;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer;
Expand All @@ -24,15 +26,23 @@ public ClientEntityGOBindCommand(TargetsList targets) {
public void wrappedExecute(
MinecraftServer server,
ICommandSender sender,
String[] args
String[] arrayArgs
) throws CommandException {
ArrayList<String> args = Lists.newArrayList(arrayArgs);

boolean clone = false;
if (args.contains("-c")) {
clone = true;
args.remove("-c");
}

String dn = "";
if (args.length > 0) {
dn = args[0];
if (!args.isEmpty()) {
dn = args.remove(0);
}

TalesSystem.network.sendToServer(
new EntityGOBindMessage(dn, this.targets.getLoadedEntities())
new EntityGOBindMessage(dn, clone, this.targets.getLoadedEntities())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,37 @@
public class EntityGOBindMessage extends TargetsMessage {

private String dn;
private boolean clone;

public EntityGOBindMessage() {
super();
}

public EntityGOBindMessage(String dn, Entity[] entities) {
public EntityGOBindMessage(String dn, boolean clone, Entity[] entities) {
super(entities);
this.dn = dn;
this.clone = clone;
}

public String getDn() {
return dn;
}

public boolean isClone() {
return this.clone;
}

@Override
public void fromBytes(ByteBuf buf) {
super.fromBytes(buf);
this.dn = MessageUtils.readString(buf);
this.clone = buf.readBoolean();
}

@Override
public void toBytes(ByteBuf buf) {
super.toBytes(buf);
MessageUtils.writeString(buf, this.dn);
buf.writeBoolean(this.clone);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.inject.Inject;
import java.util.concurrent.CompletableFuture;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
Expand All @@ -14,6 +15,7 @@
import pw.tales.cofdsystem.mod.common.modules.go_relation_entity.network.messages.EntityGOBindMessage;
import pw.tales.cofdsystem.mod.common.network.TalesMessageHandler;
import pw.tales.cofdsystem.mod.server.modules.go_relation_entity.GOEntityRelation;
import pw.tales.cofdsystem.mod.server.modules.go_source_local.LocalGOModule;
import pw.tales.cofdsystem.mod.server.modules.operators.OperatorsModule;

public class EntityGOBindHandler extends TalesMessageHandler<EntityGOBindMessage> {
Expand All @@ -22,6 +24,10 @@ public class EntityGOBindHandler extends TalesMessageHandler<EntityGOBindMessage
@Inject
public static GOEntityRelation goEntityRelation;

@SuppressWarnings("java:S1444")
@Inject
public static LocalGOModule localStorageModule;

@Override
public boolean checkPermission(MinecraftServer server, EntityPlayerMP player) {
return PermissionAPI.hasPermission(
Expand All @@ -30,6 +36,36 @@ public boolean checkPermission(MinecraftServer server, EntityPlayerMP player) {
);
}

private void notifyUseClone(EntityPlayerMP sender, GameObject go, GameObject clone) {
TextComponentTranslation startMsg = new TextComponentTranslation(
"command.gameobject.use.fetch.clone",
clone,
go
);
startMsg.getStyle().setColor(TextFormatting.GREEN);
sender.sendMessage(startMsg);
}

public void notifyStart(ICommandSender sender, Entity target, String dn) {
TextComponentTranslation startMsg = new TextComponentTranslation(
"command.gameobject.use.fetch.attempt",
target.getDisplayName(),
dn
);
startMsg.getStyle().setColor(TextFormatting.GREEN);
sender.sendMessage(startMsg);
}

public void notifySuccess(ICommandSender sender, Entity target, GameObject gameObject) {
TextComponentTranslation successMsg = new TextComponentTranslation(
"command.gameobject.use.fetch.success",
target.getDisplayName(),
gameObject
);
successMsg.getStyle().setColor(TextFormatting.GREEN);
sender.sendMessage(successMsg);
}

@Override
public void process(EntityPlayerMP player, EntityGOBindMessage message) throws CommandException {
// Who to bind
Expand All @@ -47,25 +83,29 @@ public void process(EntityPlayerMP player, EntityGOBindMessage message) throws C

// To whom to bind
String dn = message.getDn();
CompletableFuture<GameObject> future;
CompletableFuture<GameObject> gameObjectFuture;

if (dn.equals("")) {
future = CompletableFuture.completedFuture(null);
gameObjectFuture = CompletableFuture.completedFuture(null);
} else {
future = goEntityRelation.getGameObject(dn);
gameObjectFuture = goEntityRelation.getGameObject(dn);
}

// Notify command is working
TextComponentTranslation startMsg = new TextComponentTranslation(
"command.gameobject.use.fetch.attempt",
finalTarget.getDisplayName(),
dn
);
startMsg.getStyle().setColor(TextFormatting.GREEN);
player.sendMessage(startMsg);
this.notifyStart(player, finalTarget, dn);

// Use game object's clone
if (message.isClone()) {
gameObjectFuture = gameObjectFuture.thenApply(go -> {
GameObject clone = localStorageModule.cloneGameObject(go);
this.notifyUseClone(player, go, clone);
return clone;
});
}

Utils.merge(
goEntityRelation.getGameObject(finalTarget).exceptionally(e -> null),
future
gameObjectFuture
).thenApply(entry -> {
// Unbind old
GameObject oldGO = entry.getLeft();
Expand All @@ -81,14 +121,7 @@ public void process(EntityPlayerMP player, EntityGOBindMessage message) throws C
goEntityRelation.attach(finalTarget, newGO);
}

// Notify about success
TextComponentTranslation successMsg = new TextComponentTranslation(
"command.gameobject.use.fetch.success",
finalTarget.getDisplayName(),
newGO
);
successMsg.getStyle().setColor(TextFormatting.GREEN);
player.sendMessage(successMsg);
this.notifySuccess(player, finalTarget, newGO);

return newGO;
}).exceptionally(e -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ public void save(GameObject gameObject, Array<Trait> update, Array<Trait> remove
gameObject.version
);
}

public GameObject cloneGameObject(GameObject gameObject) {
GameObject clone = gameObject.clone();
this.save(clone);
TalesSystem.logger.info(
"{}, clone of {}, create and saved to {}",
clone,
gameObject,
this
);
return clone;
}
}
5 changes: 3 additions & 2 deletions src/main/resources/assets/tales_system/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ command.configure.builder_not_found=This attack not found
command.error.no_arguments=Required argument pool_size is not specified
command.error.pool_size_border=Pool size should between 0 and 50.
command.error.pool_size_integer=Pool size should be integer.
command.gameobject.use.failure.multiple=While technicly possible, multiple targets are forbidden.
command.gameobject.use.failure.multiple=While technically possible, multiple targets are forbidden.
command.gameobject.use.failure.no_target=No target to relate to character.
command.gameobject.use.fetch.attempt=%s is in process of relating to %s
command.gameobject.use.fetch.attempt=%s is in process of relating to dn=%s
command.gameobject.use.fetch.clone=Using %s, clone of %s
command.gameobject.use.fetch.failure=%s can't be related to %s
command.gameobject.use.fetch.success=%s successfully related to %s
command.gameobject.use.no_dn=You should specify character dn.
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/tales_system/lang/ru_ru.lang
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ command.error.pool_size_border=Размер пула должен быть от
command.error.pool_size_integer=Размер пула должен быть числом
command.gameobject.use.failure.multiple=Хоть и технически возможно, несколько целей для отображения запрещены.
command.gameobject.use.failure.no_target=Не выбрано ни одной цели для отоборажения в персонажа.
command.gameobject.use.fetch.attempt=%s находится в процессе отображения в %s
command.gameobject.use.fetch.attempt=%s находится в процессе отображения в dn=%s
command.gameobject.use.fetch.clone=Используется %s, клон %s
command.gameobject.use.fetch.failure=%s не может быть отображен в %s
command.gameobject.use.fetch.success=%s успешно отображен в %s
command.gameobject.use.no_dn=Укажите DN персонажа.
Expand Down

0 comments on commit f306f91

Please sign in to comment.