From c411a800f64af154aea167a06e3a39fdaadb9bea Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Fri, 5 Jun 2020 19:03:29 +0200 Subject: [PATCH] Work on server data and registries. --- .../components/BlockColorComponent.java | 2 +- .../bootstrap/components/IInitComponent.java | 2 +- .../components/ItemColorComponent.java | 2 +- src/main/java/appeng/core/AELog.java | 4 +- src/main/java/appeng/core/AppEng.java | 59 ++++-- src/main/java/appeng/core/Registration.java | 93 ++++----- .../features/registries/PlayerRegistry.java | 20 +- .../appeng/core/worlddata/CompassData.java | 7 +- .../core/worlddata/IOnWorldStartable.java | 30 --- .../core/worlddata/IOnWorldStoppable.java | 30 --- .../core/worlddata/IWorldGridStorageData.java | 5 +- .../core/worlddata/IWorldPlayerData.java | 11 +- .../core/worlddata/IWorldPlayerMapping.java | 54 ------ .../appeng/core/worlddata/PlayerData.java | 166 ++++++++-------- .../appeng/core/worlddata/PlayerMapping.java | 69 ------- .../worlddata/PlayerMappingsInitializer.java | 92 --------- .../appeng/core/worlddata/StorageData.java | 183 +++++++++--------- .../java/appeng/core/worlddata/WorldData.java | 128 +++--------- src/main/java/appeng/hooks/TickHandler.java | 2 - src/main/java/appeng/me/GridStorage.java | 63 ++---- .../java/appeng/me/GridStorageSearch.java | 77 -------- .../java/appeng/me/cache/SecurityCache.java | 2 +- .../appeng/me/helpers/AENetworkProxy.java | 2 +- .../java/appeng/worldgen/MeteoritePlacer.java | 3 +- 24 files changed, 329 insertions(+), 777 deletions(-) delete mode 100644 src/main/java/appeng/core/worlddata/IOnWorldStartable.java delete mode 100644 src/main/java/appeng/core/worlddata/IOnWorldStoppable.java delete mode 100644 src/main/java/appeng/core/worlddata/IWorldPlayerMapping.java delete mode 100644 src/main/java/appeng/core/worlddata/PlayerMapping.java delete mode 100644 src/main/java/appeng/core/worlddata/PlayerMappingsInitializer.java delete mode 100644 src/main/java/appeng/me/GridStorageSearch.java diff --git a/src/main/java/appeng/bootstrap/components/BlockColorComponent.java b/src/main/java/appeng/bootstrap/components/BlockColorComponent.java index 61b2306ee..f03dd5ca7 100644 --- a/src/main/java/appeng/bootstrap/components/BlockColorComponent.java +++ b/src/main/java/appeng/bootstrap/components/BlockColorComponent.java @@ -39,7 +39,7 @@ public class BlockColorComponent implements IInitComponent } @Override - public void initialize( Dist dist ) + public void initialize() { Minecraft.getInstance().getBlockColors().register( this.blockColor, this.block ); } diff --git a/src/main/java/appeng/bootstrap/components/IInitComponent.java b/src/main/java/appeng/bootstrap/components/IInitComponent.java index 178a82963..bafb239bb 100644 --- a/src/main/java/appeng/bootstrap/components/IInitComponent.java +++ b/src/main/java/appeng/bootstrap/components/IInitComponent.java @@ -27,5 +27,5 @@ import appeng.bootstrap.IBootstrapComponent; @FunctionalInterface public interface IInitComponent extends IBootstrapComponent { - void initialize( Dist dist ); + void initialize(); } diff --git a/src/main/java/appeng/bootstrap/components/ItemColorComponent.java b/src/main/java/appeng/bootstrap/components/ItemColorComponent.java index 83e506c76..e4e04cf0d 100644 --- a/src/main/java/appeng/bootstrap/components/ItemColorComponent.java +++ b/src/main/java/appeng/bootstrap/components/ItemColorComponent.java @@ -39,7 +39,7 @@ public class ItemColorComponent implements IInitComponent } @Override - public void initialize( Dist dist ) + public void initialize() { Minecraft.getInstance().getItemColors().register( this.itemColor, this.item ); } diff --git a/src/main/java/appeng/core/AELog.java b/src/main/java/appeng/core/AELog.java index 0e9c464a6..2b0651190 100644 --- a/src/main/java/appeng/core/AELog.java +++ b/src/main/java/appeng/core/AELog.java @@ -22,6 +22,7 @@ package appeng.core; import javax.annotation.Nonnull; import appeng.tile.AEBaseTile; +import appeng.util.Platform; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -56,8 +57,7 @@ public final class AELog */ private static Logger getLogger() { -// FIXME return Platform.isServer() ? SERVER : CLIENT; - throw new IllegalStateException(); + return Platform.isServer() ? SERVER : CLIENT; } /** diff --git a/src/main/java/appeng/core/AppEng.java b/src/main/java/appeng/core/AppEng.java index 7f2b5133d..c081795e5 100644 --- a/src/main/java/appeng/core/AppEng.java +++ b/src/main/java/appeng/core/AppEng.java @@ -24,12 +24,16 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import appeng.bootstrap.components.IClientSetupComponent; +import appeng.bootstrap.components.IInitComponent; import appeng.client.ClientHelper; import appeng.client.render.model.AutoRotatingModel; import appeng.client.render.model.AutoRotatingModelLoader; import appeng.client.render.model.GlassModelLoader; import appeng.client.render.model.SkyCompassModelLoader; import appeng.core.stats.AdvancementTriggers; +import appeng.core.worlddata.WorldData; +import appeng.hooks.TickHandler; +import appeng.parts.PartPlacement; import appeng.server.ServerHelper; import com.google.gson.Gson; import net.minecraft.block.Block; @@ -45,6 +49,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.model.ModelLoaderRegistry; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.CrashReportExtender; import net.minecraftforge.fml.DistExecutor; @@ -58,6 +63,10 @@ import appeng.core.crash.ModCrashEnhancement; import appeng.services.export.ExportConfig; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent; +import net.minecraftforge.fml.event.server.FMLServerStartedEvent; +import net.minecraftforge.fml.event.server.FMLServerStoppedEvent; +import net.minecraftforge.fml.event.server.FMLServerStoppingEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import javax.annotation.Nonnull; @@ -125,9 +134,22 @@ public final class AppEng // Register client-only events DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> modEventBus.addListener(this::clientSetup)); DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> modEventBus.addListener(registration::modelRegistryEvent)); + + MinecraftForge.EVENT_BUS.addListener( TickHandler.INSTANCE::unloadWorld ); + MinecraftForge.EVENT_BUS.addListener( TickHandler.INSTANCE::onTick ); + MinecraftForge.EVENT_BUS.addListener( this::serverAboutToStart ); + MinecraftForge.EVENT_BUS.addListener( this::serverStopped ); + MinecraftForge.EVENT_BUS.addListener( this::serverStopping ); + + MinecraftForge.EVENT_BUS.register( new PartPlacement() ); } private void commonSetup(FMLCommonSetupEvent event) { + + ApiDefinitions definitions = Api.INSTANCE.definitions(); + definitions.getRegistry().getBootstrapComponents( IInitComponent.class ).forEachRemaining(IInitComponent::initialize); + + Registration.setupInternalRegistries(); } @OnlyIn(Dist.CLIENT) @@ -283,26 +305,23 @@ public final class AppEng // // imcHandler.handleIMCEvent( event ); // } -// -// @EventHandler -// private void serverAboutToStart( final FMLServerAboutToStartEvent evt ) -// { -// WorldData.onServerAboutToStart( evt.getServer() ); -// } -// -// @EventHandler -// private void serverStopping( final FMLServerStoppingEvent event ) -// { -// WorldData.instance().onServerStopping(); -// } -// -// @EventHandler -// private void serverStopped( final FMLServerStoppedEvent event ) -// { -// WorldData.instance().onServerStoppped(); -// TickHandler.INSTANCE.shutdown(); -// } -// + + private void serverAboutToStart( final FMLServerStartedEvent evt ) + { + WorldData.onServerAboutToStart( evt.getServer() ); + } + + private void serverStopping( final FMLServerStoppingEvent event ) + { + WorldData.instance().onServerStopping(); + } + + private void serverStopped( final FMLServerStoppedEvent event ) + { + WorldData.instance().onServerStoppped(); + TickHandler.INSTANCE.shutdown(); + } + // @EventHandler // private void serverStarting( final FMLServerStartingEvent evt ) // { diff --git a/src/main/java/appeng/core/Registration.java b/src/main/java/appeng/core/Registration.java index 4cc8bbb3e..bd1ee20b6 100644 --- a/src/main/java/appeng/core/Registration.java +++ b/src/main/java/appeng/core/Registration.java @@ -19,6 +19,15 @@ package appeng.core; +import appeng.api.features.IRegistryContainer; +import appeng.api.networking.IGridCacheRegistry; +import appeng.api.networking.crafting.ICraftingGrid; +import appeng.api.networking.energy.IEnergyGrid; +import appeng.api.networking.pathing.IPathingGrid; +import appeng.api.networking.security.ISecurityGrid; +import appeng.api.networking.spatial.ISpatialCache; +import appeng.api.networking.storage.IStorageGrid; +import appeng.api.networking.ticking.ITickManager; import appeng.bootstrap.IModelRegistry; import appeng.bootstrap.components.*; import appeng.client.gui.implementations.GuiGrinder; @@ -32,7 +41,13 @@ import appeng.client.render.model.SkyCompassModel; import appeng.client.render.tesr.SkyChestTESR; import appeng.container.implementations.ContainerGrinder; import appeng.container.implementations.ContainerSkyChest; +import appeng.core.features.registries.cell.BasicCellHandler; +import appeng.core.features.registries.cell.BasicItemCellGuiHandler; +import appeng.core.features.registries.cell.CreativeCellHandler; import appeng.core.stats.AeStats; +import appeng.core.stats.PartItemPredicate; +import appeng.fluids.registries.BasicFluidCellGuiHandler; +import appeng.me.cache.*; import appeng.recipes.conditions.FeaturesEnabled; import appeng.recipes.game.DisassembleRecipe; import appeng.recipes.ingredients.PartIngredientSerializer; @@ -46,6 +61,7 @@ import net.minecraft.inventory.container.ContainerType; import net.minecraft.item.Item; import net.minecraft.item.crafting.IRecipeSerializer; import net.minecraft.particles.ParticleType; +import net.minecraft.stats.Stats; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; @@ -150,50 +166,39 @@ final class Registration // { // registry.addNewSubItemResolver( new AEItemResolver() ); // } -// -// public void initialize( @Nonnull final FMLInitializationEvent event, @Nonnull final File recipeDirectory ) -// { -// Preconditions.checkNotNull( event ); -// Preconditions.checkNotNull( recipeDirectory ); -// Preconditions.checkArgument( !recipeDirectory.isFile() ); -// -// final Api api = Api.INSTANCE; -// final IRegistryContainer registries = api.registries(); -// -// ApiDefinitions definitions = api.definitions(); -// definitions.getRegistry().getBootstrapComponents( IInitComponent.class ).forEachRemaining( b -> b.initialize( event.getSide() ) ); -// -// MinecraftForge.EVENT_BUS.register( TickHandler.INSTANCE ); -// -// MinecraftForge.EVENT_BUS.register( new PartPlacement() ); -// -// final IGridCacheRegistry gcr = registries.gridCache(); -// gcr.registerGridCache( ITickManager.class, TickManagerCache.class ); -// gcr.registerGridCache( IEnergyGrid.class, EnergyGridCache.class ); -// gcr.registerGridCache( IPathingGrid.class, PathGridCache.class ); -// gcr.registerGridCache( IStorageGrid.class, GridStorageCache.class ); -// gcr.registerGridCache( P2PCache.class, P2PCache.class ); -// gcr.registerGridCache( ISpatialCache.class, SpatialPylonCache.class ); -// gcr.registerGridCache( ISecurityGrid.class, SecurityCache.class ); -// gcr.registerGridCache( ICraftingGrid.class, CraftingGridCache.class ); -// -// registries.cell().addCellHandler( new BasicCellHandler() ); -// registries.cell().addCellHandler( new CreativeCellHandler() ); -// registries.cell().addCellGuiHandler( new BasicItemCellGuiHandler() ); -// registries.cell().addCellGuiHandler( new BasicFluidCellGuiHandler() ); -// -// api.definitions().materials().matterBall().maybeStack( 1 ).ifPresent( ammoStack -> -// { -// final double weight = 32; -// -// registries.matterCannon().registerAmmo( ammoStack, weight ); -// } ); -// -// PartItemPredicate.register(); -// Stats.register(); -// this.advancementTriggers = new AdvancementTriggers( new CriterionTrigggerRegistry() ); -// } -// + + public static void setupInternalRegistries() + { + final Api api = Api.INSTANCE; + final IRegistryContainer registries = api.registries(); + + final IGridCacheRegistry gcr = registries.gridCache(); + gcr.registerGridCache( ITickManager.class, TickManagerCache.class ); + gcr.registerGridCache( IEnergyGrid.class, EnergyGridCache.class ); + gcr.registerGridCache( IPathingGrid.class, PathGridCache.class ); + gcr.registerGridCache( IStorageGrid.class, GridStorageCache.class ); + gcr.registerGridCache( P2PCache.class, P2PCache.class ); + gcr.registerGridCache( ISpatialCache.class, SpatialPylonCache.class ); + gcr.registerGridCache( ISecurityGrid.class, SecurityCache.class ); + gcr.registerGridCache( ICraftingGrid.class, CraftingGridCache.class ); + + registries.cell().addCellHandler( new BasicCellHandler() ); + registries.cell().addCellHandler( new CreativeCellHandler() ); + registries.cell().addCellGuiHandler( new BasicItemCellGuiHandler() ); + registries.cell().addCellGuiHandler( new BasicFluidCellGuiHandler() ); + + api.definitions().materials().matterBall().maybeStack( 1 ).ifPresent( ammoStack -> + { + final double weight = 32; + + registries.matterCannon().registerAmmo( ammoStack, weight ); + } ); + + PartItemPredicate.register(); + // FIXME Stats.register(); + // FIXME this.advancementTriggers = new AdvancementTriggers( new CriterionTrigggerRegistry() ); + } + // @SubscribeEvent // public void registerBiomes( RegistryEvent.Register event ) // { diff --git a/src/main/java/appeng/core/features/registries/PlayerRegistry.java b/src/main/java/appeng/core/features/registries/PlayerRegistry.java index 14822e4ca..909bc9079 100644 --- a/src/main/java/appeng/core/features/registries/PlayerRegistry.java +++ b/src/main/java/appeng/core/features/registries/PlayerRegistry.java @@ -21,6 +21,7 @@ package appeng.core.features.registries; import javax.annotation.Nullable; +import appeng.core.AppEng; import com.mojang.authlib.GameProfile; import net.minecraft.entity.player.PlayerEntity; @@ -28,6 +29,8 @@ import net.minecraft.entity.player.PlayerEntity; import appeng.api.features.IPlayerRegistry; import appeng.core.worlddata.WorldData; +import java.util.UUID; + public class PlayerRegistry implements IPlayerRegistry { @@ -40,7 +43,7 @@ public class PlayerRegistry implements IPlayerRegistry return -1; } - return WorldData.instance().playerData().getPlayerID( username ); + return WorldData.instance().playerData().getMePlayerId( username ); } @Override @@ -53,6 +56,19 @@ public class PlayerRegistry implements IPlayerRegistry @Override public PlayerEntity findPlayer( final int playerID ) { - return WorldData.instance().playerData().getPlayerFromID( playerID ); + UUID profileId = WorldData.instance().playerData().getProfileId(playerID); + if (profileId == null) { + return null; + } + + for( final PlayerEntity player : AppEng.proxy.getPlayers() ) + { + if( player.getUniqueID().equals( profileId ) ) + { + return player; + } + } + + return null; } } diff --git a/src/main/java/appeng/core/worlddata/CompassData.java b/src/main/java/appeng/core/worlddata/CompassData.java index 33a54225a..22da52fba 100644 --- a/src/main/java/appeng/core/worlddata/CompassData.java +++ b/src/main/java/appeng/core/worlddata/CompassData.java @@ -33,7 +33,7 @@ import appeng.services.CompassService; * @version rv3 - 30.05.2015 * @since rv3 30.05.2015 */ -final class CompassData implements IWorldCompassData, IOnWorldStoppable +final class CompassData implements IWorldCompassData { @Nonnull private final CompassService service; @@ -52,9 +52,4 @@ final class CompassData implements IWorldCompassData, IOnWorldStoppable return this.service; } - @Override - public void onWorldStop() - { - this.service.kill(); - } } diff --git a/src/main/java/appeng/core/worlddata/IOnWorldStartable.java b/src/main/java/appeng/core/worlddata/IOnWorldStartable.java deleted file mode 100644 index b6c5c325d..000000000 --- a/src/main/java/appeng/core/worlddata/IOnWorldStartable.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.core.worlddata; - - -/** - * @author thatsIch - * @version rv3 - 30.05.2015 - * @since rv3 30.05.2015 - */ -public interface IOnWorldStartable -{ - void onWorldStart(); -} diff --git a/src/main/java/appeng/core/worlddata/IOnWorldStoppable.java b/src/main/java/appeng/core/worlddata/IOnWorldStoppable.java deleted file mode 100644 index 7a36e771f..000000000 --- a/src/main/java/appeng/core/worlddata/IOnWorldStoppable.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.core.worlddata; - - -/** - * @author thatsIch - * @version rv3 - 30.05.2015 - * @since rv3 30.05.2015 - */ -public interface IOnWorldStoppable -{ - void onWorldStop(); -} diff --git a/src/main/java/appeng/core/worlddata/IWorldGridStorageData.java b/src/main/java/appeng/core/worlddata/IWorldGridStorageData.java index c579a3249..55afdab51 100644 --- a/src/main/java/appeng/core/worlddata/IWorldGridStorageData.java +++ b/src/main/java/appeng/core/worlddata/IWorldGridStorageData.java @@ -32,15 +32,12 @@ import appeng.me.GridStorage; */ public interface IWorldGridStorageData { - @Nullable GridStorage getGridStorage( long storageID ); @Nonnull GridStorage getNewGridStorage(); - long nextGridStorage(); - void destroyGridStorage( long id ); - int getNextOrderedValue( String name ); + int getNextOrderedValue( String name, int firstValue ); } diff --git a/src/main/java/appeng/core/worlddata/IWorldPlayerData.java b/src/main/java/appeng/core/worlddata/IWorldPlayerData.java index 6bece8a54..6e27c2f19 100644 --- a/src/main/java/appeng/core/worlddata/IWorldPlayerData.java +++ b/src/main/java/appeng/core/worlddata/IWorldPlayerData.java @@ -25,6 +25,8 @@ import com.mojang.authlib.GameProfile; import net.minecraft.entity.player.PlayerEntity; +import java.util.UUID; + /** * @author thatsIch @@ -33,8 +35,13 @@ import net.minecraft.entity.player.PlayerEntity; */ public interface IWorldPlayerData { + /** + * Gets the UUID of the Minecraft profile associated with the given ME player id. + * @param playerID An ME player id. + * @return Null if the ME player id is unknown, otherwise the unique id of the Minecraft profile it originates from. + */ @Nullable - PlayerEntity getPlayerFromID( int playerID ); + UUID getProfileId(int playerID ); - int getPlayerID( GameProfile profile ); + int getMePlayerId(GameProfile profile ); } diff --git a/src/main/java/appeng/core/worlddata/IWorldPlayerMapping.java b/src/main/java/appeng/core/worlddata/IWorldPlayerMapping.java deleted file mode 100644 index eca35002c..000000000 --- a/src/main/java/appeng/core/worlddata/IWorldPlayerMapping.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.core.worlddata; - - -import java.util.Optional; -import java.util.UUID; - -import javax.annotation.Nonnull; - - -/** - * @author thatsIch - * @version rv3 - 30.05.2015 - * @since rv3 30.05.2015 - */ -public interface IWorldPlayerMapping -{ - /** - * Tries to retrieve the UUID of a player. - * Might not be stored inside of the map. - * Should not happen though. - * - * @param id ID of the to be searched player - * - * @return maybe the UUID of the searched player - */ - @Nonnull - Optional get( int id ); - - /** - * Put in new players when they join the server - * - * @param id id of new player - * @param uuid UUID of new player - */ - void put( int id, @Nonnull UUID uuid ); -} diff --git a/src/main/java/appeng/core/worlddata/PlayerData.java b/src/main/java/appeng/core/worlddata/PlayerData.java index eae571ef8..c469c1a5f 100644 --- a/src/main/java/appeng/core/worlddata/PlayerData.java +++ b/src/main/java/appeng/core/worlddata/PlayerData.java @@ -19,19 +19,19 @@ package appeng.core.worlddata; -import java.util.Optional; -import java.util.UUID; +import appeng.core.AELog; +import appeng.core.AppEng; +import com.google.common.base.Preconditions; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.mojang.authlib.GameProfile; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.world.storage.WorldSavedData; import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import com.electronwill.nightconfig.core.file.CommentedFileConfig; -import com.google.common.base.Preconditions; -import com.mojang.authlib.GameProfile; - -import net.minecraft.entity.player.PlayerEntity; - -import appeng.core.AppEng; +import java.util.Map; +import java.util.UUID; /** @@ -43,97 +43,83 @@ import appeng.core.AppEng; * @version rv3 - 30.05.2015 * @since rv3 30.05.2015 */ -final class PlayerData implements IWorldPlayerData, IOnWorldStartable, IOnWorldStoppable -{ - private static final String LAST_PLAYER_CATEGORY = "Counters"; - private static final String LAST_PLAYER_KEY = "lastPlayer"; - private static final int LAST_PLAYER_DEFAULT = 0; +final class PlayerData extends WorldSavedData implements IWorldPlayerData { - private final CommentedFileConfig config; - // FIXME private final IWorldPlayerMapping playerMapping; + public static final String NAME = AppEng.MOD_ID + "_players"; + public static final String TAG_PLAYER_IDS = "playerIds"; + public static final String TAG_PROFILE_IDS = "profileIds"; - private int lastPlayerID; + private final BiMap mapping = HashBiMap.create(); - public PlayerData( @Nonnull final CommentedFileConfig configFile ) - { - Preconditions.checkNotNull( configFile ); + // Caches the highest assigned player id + 1 + private int nextPlayerId = 0; - this.config = configFile; + public PlayerData() { + super(NAME); + } - // FIXME this.playerMapping = new PlayerMapping( config, "players." ); - } + @Nullable + @Override + public UUID getProfileId(final int playerId) { + return this.mapping.inverse().get(playerId); + } - @Nullable - @Override - public PlayerEntity getPlayerFromID( final int playerID ) - { -// FIXME final Optional maybe = this.playerMapping.get( playerID ); -// FIXME -// FIXME if( maybe.isPresent() ) -// FIXME { -// FIXME final UUID uuid = maybe.get(); -// FIXME for( final PlayerEntity player : AppEng.proxy.getPlayers() ) -// FIXME { -// FIXME if( player.getUniqueID().equals( uuid ) ) -// FIXME { -// FIXME return player; -// FIXME } -// FIXME } -// FIXME } + @Override + public int getMePlayerId(@Nonnull final GameProfile profile) { + Preconditions.checkNotNull(profile); - return null; - } + final UUID uuid = profile.getId(); + Integer playerId = mapping.get(uuid); - @Override - public int getPlayerID( @Nonnull final GameProfile profile ) - { - return -1; -// FIXME Preconditions.checkNotNull( profile ); -// FIXME Preconditions.checkNotNull( this.config.getCategory( "players" ) ); -// FIXME Preconditions.checkState( profile.isComplete() ); -// FIXME -// FIXME final ConfigCategory players = this.config.getCategory( "players" ); -// FIXME final String uuid = profile.getId().toString(); -// FIXME final Property maybePlayerID = players.get( uuid ); -// FIXME -// FIXME if( maybePlayerID != null && maybePlayerID.isIntValue() ) -// FIXME { -// FIXME return maybePlayerID.getInt(); -// FIXME } -// FIXME else -// FIXME { -// FIXME final int newPlayerID = this.nextPlayer(); -// FIXME final Property newPlayer = new Property( uuid, String.valueOf( newPlayerID ), Property.Type.INTEGER ); -// FIXME players.put( uuid, newPlayer ); -// FIXME this.playerMapping.put( newPlayerID, profile.getId() ); // add to reverse map -// FIXME this.config.save(); -// FIXME -// FIXME return newPlayerID; -// FIXME } - } + if (playerId == null) { + playerId = this.nextPlayerId++; + this.mapping.put(profile.getId(), playerId); + markDirty(); - private int nextPlayer() - { -// FIXME final int r = this.lastPlayerID; -// FIXME this.lastPlayerID++; -// FIXME this.config.get( LAST_PLAYER_CATEGORY, LAST_PLAYER_KEY, this.lastPlayerID ).set( this.lastPlayerID ); -// FIXME return r; - return 0; - } + AELog.info("Assigning ME player id {} to Minecraft profile {} ({})", playerId, profile.getId(), profile.getName()); + } - @Override - public void onWorldStart() - { - // FIXME this.lastPlayerID = this.config.get( LAST_PLAYER_CATEGORY, LAST_PLAYER_KEY, LAST_PLAYER_DEFAULT ).getInt( LAST_PLAYER_DEFAULT ); + return playerId; + } - this.config.save(); - } + @Override + public void read(CompoundNBT nbt) { + int[] playerIds = nbt.getIntArray(TAG_PLAYER_IDS); + long[] profileIds = nbt.getLongArray(TAG_PROFILE_IDS); - @Override - public void onWorldStop() - { - this.config.save(); + if (playerIds.length * 2 != profileIds.length) { + throw new IllegalStateException("Plaer ID mapping is corrupted. " + + playerIds.length + " player IDs vs. " + profileIds.length + + " profile IDs (latter must be 2 * the former)"); + } + + this.mapping.clear(); + int highestPlayerId = -1; + for (int i = 0; i < playerIds.length; i++) { + int playerId = playerIds[i]; + UUID profileId = new UUID(profileIds[i * 2], profileIds[i * 2 + 1]); + highestPlayerId = Math.max(playerId, highestPlayerId); + mapping.put(profileId, playerId); + AELog.debug("AE player ID {} is assigned to profile ID {}", playerId, profileId); + } + this.nextPlayerId = highestPlayerId + 1; + } + + @Override + public CompoundNBT write(CompoundNBT compound) { + int index = 0; + int[] playerIds = new int[mapping.size()]; + long[] profileIds = new long[mapping.size() * 2]; + for (Map.Entry entry : mapping.entrySet()) { + profileIds[index * 2] = entry.getKey().getMostSignificantBits(); + profileIds[index * 2 + 1] = entry.getKey().getLeastSignificantBits(); + playerIds[index++] = entry.getValue(); + } + + compound.putIntArray(TAG_PLAYER_IDS, playerIds); + compound.putLongArray(TAG_PROFILE_IDS, profileIds); + + return compound; + } - this.lastPlayerID = 0; - } } diff --git a/src/main/java/appeng/core/worlddata/PlayerMapping.java b/src/main/java/appeng/core/worlddata/PlayerMapping.java deleted file mode 100644 index 99a73df27..000000000 --- a/src/main/java/appeng/core/worlddata/PlayerMapping.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.core.worlddata; - - -import java.util.Map; -import java.util.Optional; -import java.util.UUID; - -import javax.annotation.Nonnull; - -import com.electronwill.nightconfig.core.Config; -import com.google.common.base.Preconditions; - - -/** - * Wrapper class for the player mappings. - * Will grant access to a pre initialized player map - * based on the "players" category in the settings.cfg - */ -final class PlayerMapping implements IWorldPlayerMapping -{ - /** - * View of player mappings, is not immutable, - * since it needs to be edited upon runtime, - * cause new players can join - */ - private final Map mappings; - - public PlayerMapping( final Config config, String path ) - { - final PlayerMappingsInitializer init = new PlayerMappingsInitializer( config, path ); - - this.mappings = init.getPlayerMappings(); - } - - @Nonnull - @Override - public Optional get( final int id ) - { - final UUID maybe = this.mappings.get( id ); - - return Optional.ofNullable( maybe ); - } - - @Override - public void put( final int id, @Nonnull final UUID uuid ) - { - Preconditions.checkNotNull( uuid ); - - this.mappings.put( id, uuid ); - } -} diff --git a/src/main/java/appeng/core/worlddata/PlayerMappingsInitializer.java b/src/main/java/appeng/core/worlddata/PlayerMappingsInitializer.java deleted file mode 100644 index 9ab05821f..000000000 --- a/src/main/java/appeng/core/worlddata/PlayerMappingsInitializer.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.core.worlddata; - - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import com.electronwill.nightconfig.core.Config; - -import appeng.core.AELog; - - -/** - * Initializes a map of ID to UUID from the player list in the settings.cfg - */ -class PlayerMappingsInitializer -{ - /** - * Internal immutable mapping - */ - // FIXME private final Map playerMappings; - - /** - * Creates the initializer for the player mappings. - * The map will be filled upon construction - * and will only be filled with valid entries. - * If an invalid entry is found, an warning is printed, - * mostly due to migration problems from 1.7.2 to 1.7.10 - * where the UUIDs were introduced. - * - * @param playerList the category for the player list, generally extracted using the "players" tag - */ - PlayerMappingsInitializer(final Config config, String prefix ) - { -// FIXME // Matcher for UUIDs -// FIXME final UUIDMatcher matcher = new UUIDMatcher(); -// FIXME -// FIXME // Initial capacity for mappings -// FIXME final int capacity = playerList.size(); -// FIXME -// FIXME // Mappings for the IDs is a regular HashMap -// FIXME this.playerMappings = new HashMap<>( capacity ); -// FIXME -// FIXME // Iterates through every pair of UUID to ID -// FIXME for( final Map.Entry entry : playerList.getValues().entrySet() ) -// FIXME { -// FIXME final String maybeUUID = entry.getKey(); -// FIXME final int id = entry.getValue().getInt(); -// FIXME -// FIXME if( matcher.isUUID( maybeUUID ) ) -// FIXME { -// FIXME final UUID uuidString = UUID.fromString( maybeUUID ); -// FIXME -// FIXME this.playerMappings.put( id, uuidString ); -// FIXME } -// FIXME else -// FIXME { -// FIXME AELog.warn( -// FIXME "The configuration for players contained an outdated entry instead an expected UUID " + maybeUUID + " for the player " + id + ". Please clean this up." ); -// FIXME } -// FIXME } - } - - /** - * Getter - * - * @return Immutable map of the players mappings of their ID to their UUID - */ - public Map getPlayerMappings() - { - return Collections.emptyMap(); //this.playerMappings; //FIXME - } -} diff --git a/src/main/java/appeng/core/worlddata/StorageData.java b/src/main/java/appeng/core/worlddata/StorageData.java index 5eb2419ff..271c30100 100644 --- a/src/main/java/appeng/core/worlddata/StorageData.java +++ b/src/main/java/appeng/core/worlddata/StorageData.java @@ -19,20 +19,15 @@ package appeng.core.worlddata; -import java.lang.ref.WeakReference; -import java.util.Map; -import java.util.WeakHashMap; +import appeng.core.AELog; +import appeng.core.AppEng; +import appeng.me.GridStorage; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.world.storage.WorldSavedData; import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.electronwill.nightconfig.core.CommentedConfig; -import com.electronwill.nightconfig.core.file.CommentedFileConfig; -import com.google.common.base.Preconditions; - -import appeng.core.AELog; -import appeng.me.GridStorage; -import appeng.me.GridStorageSearch; +import java.util.HashMap; +import java.util.Map; /** @@ -40,24 +35,26 @@ import appeng.me.GridStorageSearch; * @version rv3 - 30.05.2015 * @since rv3 30.05.2015 */ -final class StorageData implements IWorldGridStorageData, IOnWorldStartable, IOnWorldStoppable +final class StorageData extends WorldSavedData implements IWorldGridStorageData { - private static final String LAST_GRID_STORAGE_CATEGORY = "Counters"; - private static final String LAST_GRID_STORAGE_KEY = "lastGridStorage"; - private static final int LAST_GRID_STORAGE_DEFAULT = 0; - private static final String GRID_STORAGE_CATEGORY = "gridstorage"; + public static final String NAME = AppEng.MOD_ID + "_storage"; - private final Map> loadedStorage = new WeakHashMap<>( 10 ); - private final CommentedFileConfig config; + private static final String TAG_NEXT_ID = "nextId"; + public static final String TAG_ORDERED_VALUES = "orderedValues"; + public static final String TAG_STORAGE = "storage"; - private long lastGridStorage; + private final Map storage = new HashMap<>(); - public StorageData( @Nonnull final CommentedFileConfig settingsFile ) - { - Preconditions.checkNotNull( settingsFile ); + // id that will be assigned to the next new grid, this needs to be persisted + // because grids can be removed and we do not want to re-assign the same id twice + private long nextGridId; - this.config = settingsFile; + // Stores once-per-save values. I.e. which storage press to drop next in a spawned meteor + private final Map orderedValues = new HashMap<>(); + + public StorageData() { + super(NAME); } /** @@ -67,25 +64,18 @@ final class StorageData implements IWorldGridStorageData, IOnWorldStartable, IOn * * @return corresponding grid storage */ - @Nullable @Override public GridStorage getGridStorage( final long storageID ) { - return null; -// FIXME final GridStorageSearch gss = new GridStorageSearch( storageID ); -// FIXME final WeakReference result = this.loadedStorage.get( gss ); -// FIXME -// FIXME if( result == null || result.get() == null ) -// FIXME { -// FIXME final String id = String.valueOf( storageID ); -// FIXME final String data = this.config.get( "gridstorage", id, "" ).getString(); -// FIXME final GridStorage thisStorage = new GridStorage( data, storageID, gss ); -// FIXME gss.setGridStorage( new WeakReference<>( thisStorage ) ); -// FIXME this.loadedStorage.put( gss, new WeakReference<>( gss ) ); -// FIXME return thisStorage; -// FIXME } -// FIXME -// FIXME return result.get().getGridStorage().get(); + GridStorage result = storage.get(storageID); + + if( result == null ) + { + result = new GridStorage(storageID); + storage.put(storageID, result); + } + + return result; } /** @@ -95,73 +85,80 @@ final class StorageData implements IWorldGridStorageData, IOnWorldStartable, IOn @Override public GridStorage getNewGridStorage() { - final long storageID = this.nextGridStorage(); - final GridStorageSearch gss = new GridStorageSearch( storageID ); - final GridStorage newStorage = new GridStorage( storageID, gss ); - gss.setGridStorage( new WeakReference<>( newStorage ) ); - this.loadedStorage.put( gss, new WeakReference<>( gss ) ); - - return newStorage; - } - - @Override - public long nextGridStorage() - { -// FIXME final long r = this.lastGridStorage; -// FIXME this.lastGridStorage++; -// FIXME this.config.get( "Counters", "lastGridStorage", this.lastGridStorage ).set( Long.toString( this.lastGridStorage ) ); -// FIXME return r; - return 0; + return getGridStorage(nextGridId++); } @Override public void destroyGridStorage( final long id ) { -// FIXME final String stringID = String.valueOf( id ); -// FIXME this.config.getCategory( "gridstorage" ).remove( stringID ); + this.storage.remove(id); } @Override - public int getNextOrderedValue( final String name ) + public int getNextOrderedValue( final String name, int firstValue ) { -// FIXME final Property p = this.config.get( "orderedValues", name, 0 ); -// FIXME final int myValue = p.getInt(); -// FIXME p.set( myValue + 1 ); -// FIXME return myValue; - return 0; + return orderedValues.merge(name, firstValue, (oldValue, value) -> oldValue + 1); } @Override - public void onWorldStart() - { -// FIXME final String lastString = this.config.get( LAST_GRID_STORAGE_CATEGORY, LAST_GRID_STORAGE_KEY, LAST_GRID_STORAGE_DEFAULT ).getString(); -// FIXME -// FIXME try -// FIXME { -// FIXME this.lastGridStorage = Long.parseLong( lastString ); -// FIXME } -// FIXME catch( final NumberFormatException err ) -// FIXME { -// FIXME AELog.warn( "The config contained a value which was not represented as a Long: %s", lastString ); -// FIXME -// FIXME this.lastGridStorage = 0; -// FIXME } + public void read(CompoundNBT tag) { + + nextGridId = tag.getLong(TAG_NEXT_ID); + + // Load serialized grid storage + CompoundNBT storageTag = tag.getCompound(TAG_STORAGE); + for (String storageIdStr : storageTag.keySet()) { + long storageId; + try { + storageId = Long.parseLong(storageIdStr); + } catch (NumberFormatException e) { + AELog.warn("Unable to load grid storage with malformed id: '{}'", storageIdStr); + continue; + } + storage.put(storageId, new GridStorage(storageId, storageTag.getCompound(storageIdStr))); + } + + // Load ordered values map + CompoundNBT orderedValuesTag = tag.getCompound(TAG_ORDERED_VALUES); + this.orderedValues.clear(); + for (String key : orderedValuesTag.keySet()) { + this.orderedValues.put(key, orderedValuesTag.getInt(key)); + } + } @Override - public void onWorldStop() - { -// FIXME // populate new data -// FIXME for( final GridStorageSearch gs : this.loadedStorage.keySet() ) -// FIXME { -// FIXME final GridStorage thisStorage = gs.getGridStorage().get(); -// FIXME if( thisStorage != null && thisStorage.getGrid() != null && !thisStorage.getGrid().isEmpty() ) -// FIXME { -// FIXME final String value = thisStorage.getValue(); -// FIXME this.config.get( GRID_STORAGE_CATEGORY, String.valueOf( thisStorage.getID() ), value ).set( value ); -// FIXME } -// FIXME } -// FIXME -// FIXME this.config.save(); + public CompoundNBT write(CompoundNBT tag) { + + tag.putLong(TAG_NEXT_ID, nextGridId); + + // Save serialized grid storage + CompoundNBT storageTag = new CompoundNBT(); + for (Map.Entry entry : storage.entrySet()) { + + GridStorage gridStorage = entry.getValue(); + + if (gridStorage.getGrid() == null || gridStorage.getGrid().isEmpty()) { + continue; + } + + try { + entry.getValue().saveState(); + } catch (Exception e) { + AELog.warn("Failed to save state of Grid {}, storing last known value instead.", entry.getKey(), e); + } + storageTag.put(String.valueOf(entry.getKey()), entry.getValue().dataObject()); + } + tag.put(TAG_STORAGE, storageTag); + + // Save ordered values + CompoundNBT orderedValuesTag = new CompoundNBT(); + for (Map.Entry entry : orderedValues.entrySet()) { + orderedValuesTag.putInt(entry.getKey(), entry.getValue()); + } + tag.put(TAG_ORDERED_VALUES, orderedValuesTag); + + return tag; } + } diff --git a/src/main/java/appeng/core/worlddata/WorldData.java b/src/main/java/appeng/core/worlddata/WorldData.java index 958ef5450..63dc5c515 100644 --- a/src/main/java/appeng/core/worlddata/WorldData.java +++ b/src/main/java/appeng/core/worlddata/WorldData.java @@ -19,30 +19,14 @@ package appeng.core.worlddata; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.util.List; -import java.util.concurrent.ThreadFactory; +import com.google.common.base.Preconditions; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.dimension.DimensionType; +import net.minecraft.world.server.ServerWorld; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import com.electronwill.nightconfig.core.file.CommentedFileConfig; -import com.electronwill.nightconfig.toml.TomlFormat; -import com.electronwill.nightconfig.toml.TomlParser; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.world.dimension.Dimension; -import net.minecraft.world.dimension.DimensionType; -import net.minecraft.world.server.ServerWorld; -import net.minecraftforge.common.DimensionManager; - -import appeng.core.AEConfig; -import appeng.services.CompassService; -import appeng.services.compass.CompassThreadFactory; - /** * Singleton access to anything related to world-based data. @@ -57,11 +41,10 @@ import appeng.services.compass.CompassThreadFactory; */ public final class WorldData implements IWorldData { - private static final String AE2_DIRECTORY_NAME = "AE2"; - private static final String SETTING_FILE_NAME = "settings.cfg"; - private static final String SPAWNDATA_DIR_NAME = "spawndata"; - private static final String COMPASS_DIR_NAME = "compass"; + /** + * Is null while no MinecraftServer exists. + */ @Nullable private static IWorldData instance; @@ -70,44 +53,29 @@ public final class WorldData implements IWorldData private final IWorldCompassData compassData; private final IWorldSpawnData spawnData; - private final List startables; - private final List stoppables; - - private final File ae2directory; - private final File spawnDirectory; - private final File compassDirectory; - - private final CommentedFileConfig sharedConfig; - - private WorldData( @Nonnull final File worldDirectory ) + private WorldData( @Nonnull final MinecraftServer server ) { - Preconditions.checkNotNull( worldDirectory ); - Preconditions.checkArgument( worldDirectory.isDirectory() ); + Preconditions.checkNotNull( server ); - this.ae2directory = new File( worldDirectory, AE2_DIRECTORY_NAME ); - this.spawnDirectory = new File( this.ae2directory, SPAWNDATA_DIR_NAME ); - this.compassDirectory = new File( this.ae2directory, COMPASS_DIR_NAME ); + // Attach shared data to the server's overworld dimension + ServerWorld overworld = server.getWorld(DimensionType.OVERWORLD); + if (overworld == null) { + throw new IllegalStateException("The server doesn't have an Overworld dimension we could store our data on!"); + } - final File settingsFile = new File( this.ae2directory, SETTING_FILE_NAME ); - this.sharedConfig = CommentedFileConfig.of( settingsFile, TomlFormat.instance() ); - this.sharedConfig.load(); + final PlayerData playerData = overworld.getSavedData().getOrCreate(PlayerData::new, PlayerData.NAME); + final StorageData storageData = overworld.getSavedData().getOrCreate(StorageData::new, StorageData.NAME); - final PlayerData playerData = new PlayerData( this.sharedConfig ); - final StorageData storageData = new StorageData( this.sharedConfig ); - - final ThreadFactory compassThreadFactory = new CompassThreadFactory(); - final CompassService compassService = new CompassService( this.compassDirectory, compassThreadFactory ); - final CompassData compassData = new CompassData( this.compassDirectory, compassService ); - - final IWorldSpawnData spawnData = new SpawnData( this.spawnDirectory ); +// final ThreadFactory compassThreadFactory = new CompassThreadFactory(); +// final CompassService compassService = new CompassService( this.compassDirectory, compassThreadFactory ); +// final CompassData compassData = new CompassData( this.compassDirectory, compassService ); +// +// final IWorldSpawnData spawnData = new SpawnData( this.spawnDirectory ); this.playerData = playerData; this.storageData = storageData; - this.compassData = compassData; - this.spawnData = spawnData; - - this.startables = Lists.newArrayList( playerData, storageData ); - this.stoppables = Lists.newArrayList( playerData, storageData, compassData ); + this.compassData = null; // compassData; + this.spawnData = null; // spawnData; } /** @@ -131,65 +99,19 @@ public final class WorldData implements IWorldData */ public static void onServerAboutToStart( MinecraftServer server ) { - // FIXME: Possibly replace this with WorldData - File worldDirectory = null; - ServerWorld overworld = DimensionManager.getWorld(server, DimensionType.OVERWORLD, false, false); - if (overworld != null) { - worldDirectory = overworld.getSaveHandler().getWorldDirectory(); - } - if( worldDirectory == null ) - { - worldDirectory = server.getActiveAnvilConverter().getSaveLoader( server.getFolderName(), server ).getWorldDirectory(); - } - final WorldData newInstance = new WorldData( worldDirectory ); - - instance = newInstance; - newInstance.onServerStarting(); - } - - private void onServerStarting() - { - // check if ae2 folder already exists, else create - if( !this.ae2directory.isDirectory() && !this.ae2directory.mkdir() ) - { - throw new IllegalStateException( "Failed to create " + this.ae2directory.getAbsolutePath() ); - } - - // check if compass folder already exists, else create - if( !this.compassDirectory.isDirectory() && !this.compassDirectory.mkdir() ) - { - throw new IllegalStateException( "Failed to create " + this.compassDirectory.getAbsolutePath() ); - } - - // check if spawn data dir already exists, else create - if( !this.spawnDirectory.isDirectory() && !this.spawnDirectory.mkdir() ) - { - throw new IllegalStateException( "Failed to create " + this.spawnDirectory.getAbsolutePath() ); - } - - for( final IOnWorldStartable startable : this.startables ) - { - startable.onWorldStart(); - } - - this.startables.clear(); + instance = new WorldData(server); } @Override public void onServerStopping() { - for( final IOnWorldStoppable stoppable : this.stoppables ) - { - stoppable.onWorldStop(); - } + compassData.service().kill(); } @Override public void onServerStoppped() { Preconditions.checkNotNull( instance ); - - this.stoppables.clear(); instance = null; } diff --git a/src/main/java/appeng/hooks/TickHandler.java b/src/main/java/appeng/hooks/TickHandler.java index adce34e85..8e443ee2a 100644 --- a/src/main/java/appeng/hooks/TickHandler.java +++ b/src/main/java/appeng/hooks/TickHandler.java @@ -144,7 +144,6 @@ public class TickHandler this.getRepo().clear(); } - @SubscribeEvent public void unloadWorld( final WorldEvent.Unload ev ) { if( Platform.isServer() ) // for no there is no reason to care about this on the client... @@ -170,7 +169,6 @@ public class TickHandler } } - @SubscribeEvent public void onTick( final TickEvent ev ) { diff --git a/src/main/java/appeng/me/GridStorage.java b/src/main/java/appeng/me/GridStorage.java index 877d74ba0..077bdcc28 100644 --- a/src/main/java/appeng/me/GridStorage.java +++ b/src/main/java/appeng/me/GridStorage.java @@ -19,19 +19,13 @@ package appeng.me; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.lang.ref.WeakReference; -import java.util.WeakHashMap; - -import net.minecraft.nbt.CompressedStreamTools; -import net.minecraft.nbt.CompoundNBT; - import appeng.api.networking.IGrid; import appeng.api.networking.IGridStorage; -import appeng.core.AELog; import appeng.core.worlddata.WorldData; +import net.minecraft.nbt.CompoundNBT; + +import java.lang.ref.WeakReference; +import java.util.WeakHashMap; public class GridStorage implements IGridStorage @@ -39,49 +33,30 @@ public class GridStorage implements IGridStorage private final long myID; private final CompoundNBT data; - private final GridStorageSearch mySearchEntry; // keep myself in the list until I'm private final WeakHashMap divided = new WeakHashMap<>(); private WeakReference internalGrid = null; - // lost... - /** * for use with world settings * * @param id ID of grid storage - * @param gss grid storage search */ - public GridStorage( final long id, final GridStorageSearch gss ) + public GridStorage( final long id ) { this.myID = id; - this.mySearchEntry = gss; this.data = new CompoundNBT(); } /** * for use with world settings * - * @param input array of bytes string + * @param data The Grid data. * @param id ID of grid storage - * @param gss grid storage search */ - public GridStorage( final String input, final long id, final GridStorageSearch gss ) + public GridStorage( final long id, final CompoundNBT data ) { this.myID = id; - this.mySearchEntry = gss; - CompoundNBT myTag = null; - - try - { - final byte[] byteData = javax.xml.bind.DatatypeConverter.parseBase64Binary( input ); - myTag = CompressedStreamTools.readCompressed( new ByteArrayInputStream( byteData ) ); - } - catch( final Throwable t ) - { - myTag = new CompoundNBT(); - } - - this.data = myTag; + this.data = data; } /** @@ -90,30 +65,16 @@ public class GridStorage implements IGridStorage public GridStorage() { this.myID = 0; - this.mySearchEntry = null; this.data = new CompoundNBT(); } - public String getValue() + public void saveState() { - // FIXME final Grid currentGrid = (Grid) this.getGrid(); - // FIXME if( currentGrid != null ) - // FIXME { - // FIXME currentGrid.saveState(); - // FIXME } - - try + final Grid currentGrid = (Grid) this.getGrid(); + if( currentGrid != null ) { - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - CompressedStreamTools.writeCompressed( this.data, out ); - return javax.xml.bind.DatatypeConverter.printBase64Binary( out.toByteArray() ); + currentGrid.saveState(); } - catch( final IOException e ) - { - AELog.debug( e ); - } - - return ""; } public IGrid getGrid() diff --git a/src/main/java/appeng/me/GridStorageSearch.java b/src/main/java/appeng/me/GridStorageSearch.java deleted file mode 100644 index cd8875ca2..000000000 --- a/src/main/java/appeng/me/GridStorageSearch.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.me; - - -import java.lang.ref.WeakReference; - - -public class GridStorageSearch -{ - - private final long id; - private WeakReference gridStorage; - - /** - * for use with the world settings - * - * @param id ID of grid storage search - */ - public GridStorageSearch( final long id ) - { - this.id = id; - } - - @Override - public int hashCode() - { - return ( (Long) this.id ).hashCode(); - } - - @Override - public boolean equals( final Object obj ) - { - if( obj == null ) - { - return false; - } - if( this.getClass() != obj.getClass() ) - { - return false; - } - - final GridStorageSearch other = (GridStorageSearch) obj; - if( this.id == other.id ) - { - return true; - } - - return false; - } - - public WeakReference getGridStorage() - { - return this.gridStorage; - } - - public void setGridStorage( final WeakReference gridStorage ) - { - this.gridStorage = gridStorage; - } -} diff --git a/src/main/java/appeng/me/cache/SecurityCache.java b/src/main/java/appeng/me/cache/SecurityCache.java index d946b5072..1bbf5a627 100644 --- a/src/main/java/appeng/me/cache/SecurityCache.java +++ b/src/main/java/appeng/me/cache/SecurityCache.java @@ -156,7 +156,7 @@ public class SecurityCache implements ISecurityGrid Preconditions.checkNotNull( perm ); final GameProfile profile = player.getGameProfile(); - final int playerID = WorldData.instance().playerData().getPlayerID( profile ); + final int playerID = WorldData.instance().playerData().getMePlayerId( profile ); return this.hasPermission( playerID, perm ); } diff --git a/src/main/java/appeng/me/helpers/AENetworkProxy.java b/src/main/java/appeng/me/helpers/AENetworkProxy.java index 2e3c08f6f..3df74dfc6 100644 --- a/src/main/java/appeng/me/helpers/AENetworkProxy.java +++ b/src/main/java/appeng/me/helpers/AENetworkProxy.java @@ -166,7 +166,7 @@ public class AENetworkProxy implements IGridBlock else if( this.node != null && this.owner != null ) { final GameProfile profile = this.owner.getGameProfile(); - final int playerID = WorldData.instance().playerData().getPlayerID( profile ); + final int playerID = WorldData.instance().playerData().getMePlayerId( profile ); this.node.setPlayerID( playerID ); this.owner = null; diff --git a/src/main/java/appeng/worldgen/MeteoritePlacer.java b/src/main/java/appeng/worldgen/MeteoritePlacer.java index 71ea36233..ca1b1e523 100644 --- a/src/main/java/appeng/worldgen/MeteoritePlacer.java +++ b/src/main/java/appeng/worldgen/MeteoritePlacer.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; +import appeng.core.Api; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; @@ -237,7 +238,7 @@ public final class MeteoritePlacer if( Math.random() > PRESSES_SPAWN_CHANCE ) { - r = WorldData.instance().storageData().getNextOrderedValue( "presses" ); + r = WorldData.instance().storageData().getNextOrderedValue( "presses", 0 ); } else {