Skip to content

Commit

Permalink
feat: Добавлен выбор модпаков
Browse files Browse the repository at this point in the history
- Принимает путь, имя папки или название модпака, который нужно загрузить.
- Взрывы, вызванные из модов, теперь тоже отлавливаются калбеком.
  • Loading branch information
MaXFeeD committed Dec 26, 2023
1 parent 61beb45 commit bf9c513
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 28 deletions.
58 changes: 53 additions & 5 deletions src/main/java/com/reider745/InnerCoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.zhekasmirnov.innercore.modpack.ModPack;
import com.zhekasmirnov.innercore.modpack.ModPackContext;
import com.zhekasmirnov.innercore.modpack.ModPackDirectory;
import com.zhekasmirnov.innercore.modpack.ModPackFactory;
import com.zhekasmirnov.innercore.modpack.ModPackStorage;
import com.zhekasmirnov.innercore.ui.LoadingUI;
import com.zhekasmirnov.innercore.utils.FileTools;
import com.zhekasmirnov.mcpe161.InnerCore;
Expand All @@ -42,6 +42,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;
Expand All @@ -55,6 +56,7 @@ public enum MethodHandling {

public static final int PROTOCOL = 422;
public static final int EXIT_CODE_NO_INTERNAL_PACKAGE = 32;
public static final int EXIT_CODE_NO_MODPACK = 33;

public static String dataPath;
public static InnerCoreServer singleton;
Expand Down Expand Up @@ -235,8 +237,8 @@ public void preload(Server server) throws Exception {
try {
System.exit(EXIT_CODE_NO_INTERNAL_PACKAGE);
} catch (SecurityException e) {
throw new RuntimeException("EXIT_CODE_NO_INTERNAL_PACKAGE");
}
throw new RuntimeException("EXIT_CODE_NO_INTERNAL_PACKAGE");
}
}
}
Expand All @@ -246,10 +248,9 @@ public void preload(Server server) throws Exception {
// Required to be called before modpack instantiation
FileTools.init();

ModPack innerCoreModPack = ModPackFactory.getInstance().createFromDirectory(innerCoreDirectory);
ModPackContext.getInstance().setCurrentModPack(innerCoreModPack);
ModPackContext.getInstance().setCurrentModPack(getModpack());

for (ModPackDirectory directoryWithMods : innerCoreModPack
for (ModPackDirectory directoryWithMods : ModPackContext.getInstance().getCurrentModPack()
.getDirectoriesOfType(ModPackDirectory.DirectoryType.MODS)) {
directoryWithMods.assureDirectoryRoot();
for (File potentialIcmodFile : directoryWithMods.getLocation().listFiles()) {
Expand Down Expand Up @@ -357,6 +358,53 @@ public boolean getPropertyBoolean(String variable, Object defaultValue) {
}
}

private ModPack getModpack() {
String modpackRequirement = getPropertyString("modpack");
ModPackStorage storage = ModPackContext.getInstance().getStorage();
storage.rebuildModPackList();

if (modpackRequirement == null) {
return storage.getDefaultModPack();
}

List<ModPack> modpacks = storage.getAllModPacks();
for (ModPack modpack : modpacks) {
if (modpackRequirement.equals(modpack.getRootDirectory().getAbsolutePath())) {
return modpack;
}
if (modpackRequirement.equals(modpack.getRootDirectory().getName())) {
return modpack;
}
}
for (ModPack modpack : modpacks) {
if (modpackRequirement.equals(modpack.getManifest().getPackName())) {
return modpack;
}
if (modpackRequirement.equals(modpack.getManifest().getDisplayedName())) {
return modpack;
}
}

Logger.critical(
"Unable to find specified modpack, please enter modpack name, folder name or relative path instead. " +
"It is possible that your hosting provider dynamically updates paths, preventing you from specifying absolute one.");
if (modpacks.size() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("Available modpacks: ");
for (ModPack modpack : modpacks) {
builder.append(modpack.getRootDirectory().getName());
}
builder.append('.');
Logger.critical(builder.toString());
}

try {
System.exit(EXIT_CODE_NO_MODPACK);
} catch (SecurityException e) {
}
throw new RuntimeException("EXIT_CODE_NO_MODPACK");
}

public static boolean isLegacyWorkbench() {
return singleton.getPropertyBoolean("use-legacy-workbench-override", true);
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/reider745/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public void onEntityDeath(EntityDeathEvent event) {
final Entity entity = event.getEntity();
final EntityDamageEvent damageEvent = entity.getLastDamageCause();
final Entity attacker = damageEvent instanceof EntityDamageByEntityEvent damageByEntityEvent
? damageByEntityEvent.getDamager() : null;
? damageByEntityEvent.getDamager()
: null;

NativeCallback.onEntityDied(entity.getId(), attacker != null ? attacker.getId() : -1,
convertDamageCauseToEnum(damageEvent != null ? damageEvent.getCause() : DamageCause.CUSTOM));
Expand All @@ -212,7 +213,8 @@ public void onPlayerDeath(PlayerDeathEvent event) {
final Entity entity = event.getEntity();
final EntityDamageEvent damageEvent = entity.getLastDamageCause();
final Entity attacker = damageEvent instanceof EntityDamageByEntityEvent damageByEntityEvent
? damageByEntityEvent.getDamager() : null;
? damageByEntityEvent.getDamager()
: null;

consumeEvent(event, () -> NativeCallback.onEntityDied(entity.getId(), attacker != null ? attacker.getId() : -1,
convertDamageCauseToEnum(damageEvent != null ? damageEvent.getCause() : DamageCause.CUSTOM)));
Expand All @@ -223,8 +225,7 @@ public void onBlockExplosion(BlockExplosionPrimeEvent event) {
final Location pos = event.getBlock().getLocation();

consumeEvent(event, () -> NativeCallback.onExplode((float) pos.x, (float) pos.y, (float) pos.z,
(float) event.getForce(), -1, event.isIncendiary(), event.isBlockBreaking(),
(float) event.getFireChance()));
(float) event.getForce(), -1, event.isIncendiary(), event.isBlockBreaking(), Float.MAX_VALUE));
}

@EventHandler
Expand All @@ -233,7 +234,7 @@ public void onEntityExplosion(EntityExplosionPrimeEvent event) {
final Position pos = entity.getPosition();

consumeEvent(event, () -> NativeCallback.onExplode((float) pos.x, (float) pos.y, (float) pos.z,
(float) event.getForce(), entity.getId(), false, event.isBlockBreaking(), 0f));
(float) event.getForce(), entity.getId(), false, event.isBlockBreaking(), Float.MAX_VALUE));
}

@EventHandler
Expand Down Expand Up @@ -306,7 +307,8 @@ public void onBlockUpdate(BlockGrowEvent event) {

// TODO: onItemDispensed

// TODO: onEnchantPostAttack, onEnchantPostHurt, onEnchantGetDamageBonus, onEnchantGetProtectionBonus
// TODO: onEnchantPostAttack, onEnchantPostHurt, onEnchantGetDamageBonus,
// onEnchantGetProtectionBonus

// TODO: onWorkbenchCraft

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/reider745/world/BlockSourceMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.item.EntityItem;
import cn.nukkit.event.entity.ExplosionPrimeEvent;
import cn.nukkit.event.block.BlockExplosionPrimeEvent;
import cn.nukkit.item.Item;
import cn.nukkit.level.Explosion;
import cn.nukkit.level.Level;
Expand Down Expand Up @@ -176,15 +176,19 @@ public static void addToTickingQueue(Level pointer, int x, int y, int z, int run
int unknown /* = 0 */) {
}

public static void explode(Level pointer, float x, float y, float z, float power, boolean fire) {
ExplosionPrimeEvent event = new ExplosionPrimeEvent(null, power);
public static void explode(Level level, float x, float y, float z, float power, boolean fire) {
final Block block = level.getBlock((int) x, (int) y, (int) z);
final BlockExplosionPrimeEvent event = new BlockExplosionPrimeEvent(block, power, 0d);
event.setIncendiary(fire);
Server.getInstance().getPluginManager().callEvent(event);

if (event.isCancelled()) {
return;
}
Position position = new Position(x, y, z);
position.setLevel(pointer);
Explosion explosion = new Explosion(position, event.getForce(), (Entity) null);

final Position position = new Position(x, y, z);
position.setLevel(level);
final Explosion explosion = new Explosion(position, event.getForce(), block);
if (event.isBlockBreaking()) {
explosion.explodeA();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public FileLoader(File file) {
this.file = file;

try {
data = FileTools.readJSON(file.getAbsolutePath());
if (file.exists()) {
data = FileTools.readJSON(file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
data = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zhekasmirnov.innercore.modpack;

import com.zhekasmirnov.horizon.runtime.logger.Logger;
import com.zhekasmirnov.innercore.modpack.installation.ModPackExtractionTarget;
import com.zhekasmirnov.innercore.modpack.installation.ModpackInstallationSource;
import com.zhekasmirnov.innercore.modpack.strategy.extract.DirectoryExtractStrategy;
Expand Down Expand Up @@ -80,12 +81,15 @@ public ModPackJsAdapter getJsAdapter() {

public boolean reloadAndValidateManifest() {
try {
if (!getManifestFile().exists()) {
return false;
}
declaredDirectories.clear();
manifest.loadFile(getManifestFile());
declaredDirectories.addAll(manifest.createDeclaredDirectoriesForModPack(this));
return manifest.getPackName() != null;
} catch (IOException | JSONException exception) {
exception.printStackTrace();
Logger.warning("ModPack.reloadAndValidateManifest()", exception);
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/innercore/config.json

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/resources/innercore/modpack.json

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/resources/zotecore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ pack: Inner Core Test
pack-version: 2.3.0b115 test
pack-version-code: 152

# Sets the list of mods and configs to load, you can specify
# folder name of a modpack from /modpacks or a path.
# modpack: StoneBlock

# Use vanilla crafting table interface, instead of custom one.
# Attention: currently not supported and custom recipes will not be dislayed.
# use-legacy-workbench-override: off
Expand Down

0 comments on commit bf9c513

Please sign in to comment.