33import com .github .litermc .vschunkloader .Constants ;
44import com .github .litermc .vschunkloader .VSCApi ;
55import com .github .litermc .vschunkloader .config .Config ;
6- import com .github .litermc .vschunkloader .platform .PlatformHelper ;
76import com .github .litermc .vschunkloader .util .TaskUtil ;
87import com .github .litermc .vtil .api .assemble .ShipAllocator ;
9- import com .github .litermc .vtil .util . LevelUtil ;
8+ import com .github .litermc .vtil .api . attachment . IServerTickListener ;
109
1110import net .minecraft .resources .ResourceLocation ;
12- import net .minecraft .server .MinecraftServer ;
1311import net .minecraft .server .level .ServerLevel ;
1412
1513import com .fasterxml .jackson .annotation .JsonAutoDetect ;
1614import com .fasterxml .jackson .annotation .JsonGetter ;
1715import com .fasterxml .jackson .annotation .JsonSetter ;
1816import org .valkyrienskies .core .api .ships .LoadedServerShip ;
1917import org .valkyrienskies .core .api .ships .ServerShip ;
20- import org .valkyrienskies .core .api .ships .ServerTickListener ;
21- import org .valkyrienskies .core .apigame .world .ServerShipWorldCore ;
22- import org .valkyrienskies .mod .common .VSGameUtilsKt ;
23-
24- import java .util .Collection ;
25- import java .util .Collections ;
26- import java .util .HashSet ;
27- import java .util .Set ;
18+ import org .valkyrienskies .core .impl .game .ships .ShipData ;
2819
2920@ JsonAutoDetect (
3021 fieldVisibility = JsonAutoDetect .Visibility .NONE ,
3122 isGetterVisibility = JsonAutoDetect .Visibility .NONE ,
3223 getterVisibility = JsonAutoDetect .Visibility .NONE ,
3324 setterVisibility = JsonAutoDetect .Visibility .NONE
3425)
35- public final class AmmoShipAttachment implements ServerTickListener {
26+ public final class AmmoShipAttachment implements IServerTickListener {
3627 private static final ResourceLocation AMMO_SHIP_TICKET = new ResourceLocation (Constants .MOD_ID , "ammo_ship" );
3728
38- private final MinecraftServer server ;
39- private final ServerShipWorldCore world ;
40- private long shipId ;
4129 private int activatedTick ;
4230 private boolean unmanaged = false ;
4331 private boolean deactivated = false ;
4432
45- public AmmoShipAttachment () {
46- this (-1 );
47- }
48-
49- private AmmoShipAttachment (final long shipId ) {
50- this .server = PlatformHelper .get ().getCurrentServer ();
51- this .world = VSGameUtilsKt .getShipObjectWorld (this .server );
52- this .shipId = shipId ;
53- }
33+ public AmmoShipAttachment () {}
5434
5535 public static AmmoShipAttachment create (final ServerShip ship ) {
56- final AmmoShipAttachment attachment = new AmmoShipAttachment (ship .getId ());
57- ship .saveAttachment (AmmoShipAttachment .class , attachment );
36+ final AmmoShipAttachment attachment = new AmmoShipAttachment ();
37+ if (ship instanceof final LoadedServerShip loadedShip ) {
38+ loadedShip .setAttachment (attachment );
39+ return attachment ;
40+ } else if (ship instanceof final ShipData shipData ) {
41+ final var attachmentHolder = shipData .getAttachmentHolder ();
42+ attachmentHolder .setAttachment (attachment );
43+ } else {
44+ throw new IllegalArgumentException ("ship is neither LoadedServerShip nor ShipData" );
45+ }
5846 return attachment ;
5947 }
6048
61- @ JsonGetter ("shipId" )
62- private long getShipId () {
63- return this .shipId ;
64- }
65-
66- @ JsonSetter ("shipId" )
67- private void setShipId (final long shipId ) {
68- this .shipId = shipId ;
69- }
70-
7149 @ JsonGetter ("activatedTick" )
72- private long getActivatedTick () {
50+ private int getActivatedTick () {
7351 return this .activatedTick ;
7452 }
7553
@@ -79,15 +57,15 @@ private void setActivatedTick(final int activatedTick) {
7957 }
8058
8159 @ Override
82- public void onServerTick () {
60+ public void onServerTick (final ServerLevel level , final LoadedServerShip ship ) {
8361 if (this .activatedTick == 0 ) {
84- this .checkActivate ();
62+ this .checkActivate (level , ship );
8563 return ;
8664 }
8765 if (this .activatedTick > Config .ammoMaxActivateSeconds * 20 ) {
8866 if (!this .deactivated ) {
8967 this .deactivated = true ;
90- this .deactivate ();
68+ this .deactivate (level , ship );
9169 }
9270 return ;
9371 }
@@ -98,41 +76,24 @@ public void managerTick() {
9876 this .unmanaged = false ;
9977 }
10078
101- private ServerShip getShip () {
102- final ServerShip ship = this .world .getLoadedShips ().getById (this .shipId );
103- if (ship != null ) {
104- return ship ;
105- }
106- return this .world .getAllShips ().getById (this .shipId );
107- }
108-
109- private void checkActivate () {
79+ private void checkActivate (final ServerLevel level , final LoadedServerShip ship ) {
11080 if (this .unmanaged ) {
111- this .activate ();
81+ this .activate (level , ship );
11282 return ;
11383 }
11484 this .unmanaged = true ;
11585 }
11686
117- private void activate () {
87+ private void activate (final ServerLevel level , final LoadedServerShip ship ) {
11888 this .activatedTick = 1 ;
119- VSCApi .forceLoad (this . server , this . shipId , AMMO_SHIP_TICKET , true );
89+ VSCApi .forceLoad (level . getServer (), ship . getId () , AMMO_SHIP_TICKET , true );
12090 }
12191
122- private void deactivate () {
123- VSCApi .forceLoad (this . server , this . shipId , AMMO_SHIP_TICKET , false );
92+ private void deactivate (final ServerLevel level , final LoadedServerShip ship ) {
93+ VSCApi .forceLoad (level . getServer (), ship . getId () , AMMO_SHIP_TICKET , false );
12494 if (!Config .removeAmmoAfterExpired ) {
12595 return ;
12696 }
127- final ServerShip ship = this .getShip ();
128- if (ship == null ) {
129- return ;
130- }
131- final ServerLevel level = LevelUtil .getLevel (ship .getChunkClaimDimension ());
132- if (level == null ) {
133- this .world .deleteShip (ship );
134- return ;
135- }
13697 TaskUtil .queueTickStart (() -> ShipAllocator .get (level .getServer ()).putShip (ship ));
13798 }
13899}
0 commit comments