hytale-server/com/hypixel/hytale/builtin/buildertools/prefablist/PrefabSavePage.java

121 lines
5.5 KiB
Java

package com.hypixel.hytale.builtin.buildertools.prefablist;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.codec.codecs.EnumCodec;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.protocol.packets.interface_.CustomPageLifetime;
import com.hypixel.hytale.protocol.packets.interface_.CustomUIEventBindingType;
import com.hypixel.hytale.protocol.packets.interface_.Page;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.entity.entities.player.pages.InteractiveCustomUIPage;
import com.hypixel.hytale.server.core.ui.builder.EventData;
import com.hypixel.hytale.server.core.ui.builder.UICommandBuilder;
import com.hypixel.hytale.server.core.ui.builder.UIEventBuilder;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import javax.annotation.Nonnull;
public class PrefabSavePage extends InteractiveCustomUIPage<PrefabSavePage.PageData> {
@Nonnull
private static final Message MESSAGE_SERVER_BUILDER_TOOLS_PREFAB_SAVE_NAME_REQUIRED = Message.translation("server.builderTools.prefabSave.nameRequired");
public PrefabSavePage(@Nonnull PlayerRef playerRef) {
super(playerRef, CustomPageLifetime.CanDismissOrCloseThroughInteraction, PrefabSavePage.PageData.CODEC);
}
@Override
public void build(
@Nonnull Ref<EntityStore> ref, @Nonnull UICommandBuilder commandBuilder, @Nonnull UIEventBuilder eventBuilder, @Nonnull Store<EntityStore> store
) {
commandBuilder.append("Pages/PrefabSavePage.ui");
commandBuilder.set("#Entities #CheckBox.Value", true);
commandBuilder.set("#Empty #CheckBox.Value", false);
commandBuilder.set("#Overwrite #CheckBox.Value", false);
commandBuilder.set("#FromClipboard #CheckBox.Value", false);
eventBuilder.addEventBinding(
CustomUIEventBindingType.Activating,
"#SaveButton",
new EventData()
.append("Action", PrefabSavePage.Action.Save.name())
.append("@Name", "#NameInput.Value")
.append("@Entities", "#Entities #CheckBox.Value")
.append("@Empty", "#Empty #CheckBox.Value")
.append("@Overwrite", "#Overwrite #CheckBox.Value")
.append("@FromClipboard", "#FromClipboard #CheckBox.Value")
);
eventBuilder.addEventBinding(CustomUIEventBindingType.Activating, "#CancelButton", new EventData().append("Action", PrefabSavePage.Action.Cancel.name()));
}
public void handleDataEvent(@Nonnull Ref<EntityStore> ref, @Nonnull Store<EntityStore> store, @Nonnull PrefabSavePage.PageData data) {
Player playerComponent = store.getComponent(ref, Player.getComponentType());
assert playerComponent != null;
switch (data.action) {
case Save:
if (data.name == null || data.name.isBlank()) {
this.playerRef.sendMessage(MESSAGE_SERVER_BUILDER_TOOLS_PREFAB_SAVE_NAME_REQUIRED);
this.sendUpdate();
return;
}
playerComponent.getPageManager().setPage(ref, store, Page.None);
BuilderToolsPlugin.addToQueue(playerComponent, this.playerRef, (r, s, componentAccessor) -> {
if (data.fromClipboard) {
s.save(r, data.name, true, data.overwrite, componentAccessor);
} else {
s.saveFromSelection(r, data.name, true, data.overwrite, data.entities, data.empty, componentAccessor);
}
});
break;
case Cancel:
playerComponent.getPageManager().setPage(ref, store, Page.None);
}
}
public static enum Action {
Save,
Cancel;
}
protected static class PageData {
public static final String NAME = "@Name";
public static final String ENTITIES = "@Entities";
public static final String EMPTY = "@Empty";
public static final String OVERWRITE = "@Overwrite";
public static final String FROM_CLIPBOARD = "@FromClipboard";
public static final BuilderCodec<PrefabSavePage.PageData> CODEC = BuilderCodec.builder(PrefabSavePage.PageData.class, PrefabSavePage.PageData::new)
.append(
new KeyedCodec<>("Action", new EnumCodec<>(PrefabSavePage.Action.class, EnumCodec.EnumStyle.LEGACY)),
(o, action) -> o.action = action,
o -> o.action
)
.add()
.append(new KeyedCodec<>("@Name", Codec.STRING), (o, name) -> o.name = name, o -> o.name)
.add()
.append(new KeyedCodec<>("@Entities", Codec.BOOLEAN), (o, entities) -> o.entities = entities, o -> o.entities)
.add()
.append(new KeyedCodec<>("@Empty", Codec.BOOLEAN), (o, empty) -> o.empty = empty, o -> o.empty)
.add()
.append(new KeyedCodec<>("@Overwrite", Codec.BOOLEAN), (o, overwrite) -> o.overwrite = overwrite, o -> o.overwrite)
.add()
.append(new KeyedCodec<>("@FromClipboard", Codec.BOOLEAN), (o, fromClipboard) -> o.fromClipboard = fromClipboard, o -> o.fromClipboard)
.add()
.build();
public PrefabSavePage.Action action;
public String name;
public boolean entities = true;
public boolean empty = false;
public boolean overwrite = false;
public boolean fromClipboard = false;
public PageData() {
}
}
}