Skip to content

Commit ac821dc

Browse files
Merge pull request ArchipelagoMW#2 from Extra-2-Dew/i-hate-this
Item sending & receiving works
2 parents 98a55f9 + eede314 commit ac821dc

9 files changed

+774
-230
lines changed

Code/APCommand.cs

+63-61
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,72 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using Archipelago.MultiClient.Net;
6-
using Archipelago.MultiClient.Net.MessageLog.Messages;
7-
using Archipelago.MultiClient.Net.Packets;
1+
using Archipelago.MultiClient.Net.Packets;
82
using ModCore;
9-
using UnityEngine;
103

114
namespace ArchipelagoRandomizer
125
{
13-
14-
public class APCommand
15-
{
16-
[DebugMenuCommand(commandName: "archipelago", commandAliases: ["ap"], caseSensitive:true)]
17-
private void SendAPCommand(string[] args)
18-
{
19-
if (args.Length == 0)
20-
{
21-
DebugMenuManager.LogToConsole("USAGE:\n" +
22-
" ap /connect {server:port} {slot} {password}: Connect to an Archipelago server\n" +
23-
" ap !hint {item}: Hint the location of an item\n" +
24-
" ap !release: Release all items remaining in your world to other worlds (if you have permission)\n" +
25-
" ap !collect: Receive all items that belong to your world (if you have permission)\n" +
26-
"You can also simply type to chat with other players.", DebugMenuManager.TextColor.Error);
27-
return;
28-
}
29-
if (args[0] == "/connect")
30-
{
31-
if (args.Length < 3)
32-
{
33-
DebugMenuManager.LogToConsole("USAGE:\n" +
34-
" ap /connect {server:port} {slot} {password (if needed)}\n" +
35-
"Examples:" +
36-
"ap /connect localhost:38281 PlayerIttle\n" +
37-
"ap /connect archipelago.gg:12345 PlayerIttle mYPassWord", DebugMenuManager.TextColor.Error);
38-
return;
39-
}
40-
string server = args[1];
41-
string slot = args[2];
42-
string password = "";
43-
if (args.Length >= 4) password = args[3];
44-
if (Archipelago.TryCreateSession(server, slot, password, out string message))
45-
{
46-
DebugMenuManager.LogToConsole(message, DebugMenuManager.TextColor.Success);
47-
}
48-
else
49-
{
50-
DebugMenuManager.LogToConsole(message, DebugMenuManager.TextColor.Error);
51-
}
526

53-
return;
54-
}
55-
56-
if (Archipelago.session == null)
57-
{
58-
DebugMenuManager.LogToConsole("No session active. Please connect with 'ap /connect' first.", DebugMenuManager.TextColor.Error);
59-
return;
60-
}
7+
public class APCommand
8+
{
9+
private static APCommand instance;
10+
public static APCommand Instance { get { return instance; } }
6111

62-
string combinedArgs = string.Join(" ", args).TrimEnd();
12+
public APCommand()
13+
{
14+
instance = this;
15+
DebugMenuManager.AddCommands(this);
16+
}
6317

64-
if (combinedArgs == "") return;
18+
[DebugMenuCommand(commandName: "archipelago", commandAliases: ["ap"], caseSensitive: true)]
19+
private void SendAPCommand(string[] args)
20+
{
21+
if (args.Length == 0)
22+
{
23+
DebugMenuManager.LogToConsole("USAGE:\n" +
24+
" ap /connect {server:port} {slot} {password}: Connect to an ArchipelagoHandler server\n" +
25+
" ap !hint {item}: Hint the location of an item\n" +
26+
" ap !release: Release all items remaining in your world to other worlds (if you have permission)\n" +
27+
" ap !collect: Receive all items that belong to your world (if you have permission)\n" +
28+
"You can also simply type to chat with other players.", DebugMenuManager.TextColor.Error);
29+
return;
30+
}
31+
if (args[0] == "/connect")
32+
{
33+
if (args.Length < 3)
34+
{
35+
DebugMenuManager.LogToConsole("USAGE:\n" +
36+
" ap /connect {server:port} {slot} {password (if needed)}\n" +
37+
"Examples:" +
38+
"ap /connect localhost:38281 PlayerIttle\n" +
39+
"ap /connect archipelago.gg:12345 PlayerIttle mYPassWord", DebugMenuManager.TextColor.Error);
40+
return;
41+
}
42+
string server = args[1];
43+
string slot = args[2];
44+
string password = "";
45+
if (args.Length >= 4) password = args[3];
46+
if (APHandler.Instance.TryCreateSession(server, slot, password, out string message))
47+
{
48+
DebugMenuManager.LogToConsole(message, DebugMenuManager.TextColor.Success);
49+
}
50+
else
51+
{
52+
DebugMenuManager.LogToConsole(message, DebugMenuManager.TextColor.Error);
53+
}
6554

66-
Archipelago.session.Socket.SendPacket(new SayPacket() { Text = combinedArgs });
67-
}
68-
69-
}
55+
return;
56+
}
57+
58+
if (APHandler.Session == null)
59+
{
60+
DebugMenuManager.LogToConsole("No session active. Please connect with 'ap /connect' first.", DebugMenuManager.TextColor.Error);
61+
return;
62+
}
63+
64+
string combinedArgs = string.Join(" ", args).TrimEnd();
65+
66+
if (combinedArgs == "") return;
67+
68+
APHandler.Session.Socket.SendPacket(new SayPacket() { Text = combinedArgs });
69+
}
70+
71+
}
7072
}

Code/APHandler.cs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using Archipelago.MultiClient.Net;
2+
using Archipelago.MultiClient.Net.Enums;
3+
using Archipelago.MultiClient.Net.Helpers;
4+
using Archipelago.MultiClient.Net.MessageLog.Messages;
5+
using Archipelago.MultiClient.Net.Models;
6+
using ModCore;
7+
using System;
8+
9+
namespace ArchipelagoRandomizer
10+
{
11+
public class APHandler
12+
{
13+
private static APHandler instance;
14+
private const int baseId = 238492834;
15+
private ArchipelagoSession session;
16+
private PlayerInfo currentPlayer;
17+
18+
public static APHandler Instance { get { return instance; } }
19+
public static ArchipelagoSession Session { get { return instance.session; } }
20+
public PlayerInfo CurrentPlayer { get { return currentPlayer; } }
21+
22+
public APHandler()
23+
{
24+
instance = this;
25+
}
26+
27+
public bool TryCreateSession(string url, string slot, string password, out string message)
28+
{
29+
if (session != null)
30+
{
31+
session.MessageLog.OnMessageReceived -= OnReceiveMessage;
32+
}
33+
try
34+
{
35+
session = ArchipelagoSessionFactory.CreateSession(url);
36+
}
37+
catch (Exception ex)
38+
{
39+
message = ex.Message;
40+
return false;
41+
}
42+
43+
LoginResult result;
44+
45+
try
46+
{
47+
result = session.TryConnectAndLogin("Ittle Dew 2", slot, ItemsHandlingFlags.AllItems, password: password);
48+
}
49+
catch (Exception ex)
50+
{
51+
result = new LoginFailure(ex.GetBaseException().Message);
52+
}
53+
54+
if (!result.Successful)
55+
{
56+
LoginFailure failure = (LoginFailure)result;
57+
string errorMessage = $"Failed to connect to {url} as {slot}:";
58+
foreach (string error in failure.Errors)
59+
{
60+
errorMessage += $"\n {error}";
61+
}
62+
foreach (ConnectionRefusedError error in failure.ErrorCodes)
63+
{
64+
errorMessage += $"\n {error}";
65+
}
66+
message = errorMessage;
67+
return false;
68+
}
69+
70+
var loginSuccess = (LoginSuccessful)result;
71+
currentPlayer = session.Players.GetPlayerInfo(session.ConnectionInfo.Slot);
72+
session.MessageLog.OnMessageReceived += OnReceiveMessage;
73+
message = "Successfully connected!\n" +
74+
"Now that you are connected, you can use !help to list commands to run via the server.";
75+
session.Items.ItemReceived += OnReceivedItem;
76+
return true;
77+
}
78+
79+
private void OnReceivedItem(ReceivedItemsHelper helper)
80+
{
81+
ItemInfo receivedItem = session.Items.AllItemsReceived[session.Items.AllItemsReceived.Count - 1];
82+
int itemOffset = (int)receivedItem.ItemId - baseId;
83+
ItemRandomizer.Instance.ItemReceived(itemOffset, receivedItem.Player.Name);
84+
}
85+
86+
public void LocationChecked(int offset)
87+
{
88+
if (session == null)
89+
{
90+
Plugin.Log.LogError("Attempted to interact with Archipelago server, but no session has been started yet!");
91+
return;
92+
}
93+
94+
int id = baseId + offset;
95+
session.Locations.CompleteLocationChecksAsync((completed) =>
96+
{
97+
if (completed)
98+
{
99+
string locationName = session.Locations.GetLocationNameFromId(id);
100+
Plugin.Log.LogInfo($"Checked location: {locationName} ({offset})");
101+
}
102+
}, id);
103+
}
104+
105+
private void OnReceiveMessage(LogMessage message)
106+
{
107+
DebugMenuManager.LogToConsole(message.ToString());
108+
}
109+
}
110+
}

Code/Archipelago.cs

-71
This file was deleted.

Code/ItemDataForRandomizer.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using UnityEngine;
2+
3+
namespace ArchipelagoRandomizer
4+
{
5+
public class ItemDataForRandomizer : MonoBehaviour
6+
{
7+
public Entity Entity { get; set; }
8+
public Item Item { get; set; }
9+
public string SaveFlag { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)