use of net.countercraft.movecraft.events.FuelBurnEvent in project Movecraft by APDevTeam.
the class TranslationTask method callFuelEvent.
@NotNull
private static FuelBurnEvent callFuelEvent(@NotNull Craft craft, @NotNull ItemStack burningFuel) {
var v = craft.getType().getObjectProperty(CraftType.FUEL_TYPES);
if (!(v instanceof Map<?, ?>))
throw new IllegalStateException("FUEL_TYPES must be of type Map");
var fuelTypes = (Map<?, ?>) v;
for (var e : fuelTypes.entrySet()) {
if (!(e.getKey() instanceof Material))
throw new IllegalStateException("Keys in FUEL_TYPES must be of type Material");
if (!(e.getValue() instanceof Double))
throw new IllegalStateException("Values in FUEL_TYPES must be of type Double");
}
double fuelBurnRate = (double) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_FUEL_BURN_RATE, craft.getWorld());
return submitEvent(new FuelBurnEvent(craft, (double) fuelTypes.get(burningFuel.getType()), fuelBurnRate));
}
use of net.countercraft.movecraft.events.FuelBurnEvent in project Movecraft by APDevTeam.
the class AsyncTask method checkFuel.
protected boolean checkFuel() {
// check for fuel, burn some from a furnace if needed. Blocks of coal are supported, in addition to coal and charcoal
double fuelBurnRate = (double) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_FUEL_BURN_RATE, craft.getWorld());
if (fuelBurnRate == 0.0 || craft instanceof SinkingCraft)
return true;
if (craft.getBurningFuel() >= fuelBurnRate) {
double burningFuel = craft.getBurningFuel();
// call event
final FuelBurnEvent event = new FuelBurnEvent(craft, burningFuel, fuelBurnRate);
Bukkit.getPluginManager().callEvent(event);
if (event.getBurningFuel() != burningFuel)
burningFuel = event.getBurningFuel();
if (event.getFuelBurnRate() != fuelBurnRate)
fuelBurnRate = event.getFuelBurnRate();
craft.setBurningFuel(burningFuel - fuelBurnRate);
return true;
}
Block fuelHolder = null;
var v = craft.getType().getObjectProperty(CraftType.FUEL_TYPES);
if (!(v instanceof Map<?, ?>))
throw new IllegalStateException("FUEL_TYPES must be of type Map");
var fuelTypes = (Map<?, ?>) v;
for (var e : fuelTypes.entrySet()) {
if (!(e.getKey() instanceof Material))
throw new IllegalStateException("Keys in FUEL_TYPES must be of type Material");
if (!(e.getValue() instanceof Double))
throw new IllegalStateException("Values in FUEL_TYPES must be of type Double");
}
for (MovecraftLocation bTest : craft.getHitBox()) {
Block b = craft.getWorld().getBlockAt(bTest.getX(), bTest.getY(), bTest.getZ());
if (Tags.FURNACES.contains(b.getType())) {
InventoryHolder inventoryHolder = (InventoryHolder) b.getState();
for (ItemStack stack : inventoryHolder.getInventory()) {
if (stack == null || !fuelTypes.containsKey(stack.getType()))
continue;
fuelHolder = b;
break;
}
}
}
if (fuelHolder == null) {
return false;
}
InventoryHolder inventoryHolder = (InventoryHolder) fuelHolder.getState();
for (ItemStack iStack : inventoryHolder.getInventory()) {
if (iStack == null)
continue;
if (!fuelTypes.containsKey(iStack.getType()))
continue;
double burningFuel = (double) fuelTypes.get(iStack.getType());
// call event
final FuelBurnEvent event = new FuelBurnEvent(craft, burningFuel, fuelBurnRate);
Bukkit.getPluginManager().callEvent(event);
if (event.getBurningFuel() != burningFuel)
burningFuel = event.getBurningFuel();
if (event.getFuelBurnRate() != fuelBurnRate)
fuelBurnRate = event.getFuelBurnRate();
if (burningFuel == 0.0) {
continue;
}
int amount = iStack.getAmount();
int minAmount = 1;
if (burningFuel < fuelBurnRate) {
minAmount = (int) fuelBurnRate;
}
if (Tags.BUCKETS.contains(iStack.getType())) {
// If buckets are accepted as fuel, replace with an empty bucket
iStack.setType(Material.BUCKET);
} else if (amount == minAmount) {
inventoryHolder.getInventory().remove(iStack);
} else if (amount < minAmount) {
inventoryHolder.getInventory().remove(iStack);
final ItemStack secStack = inventoryHolder.getInventory().getItem(inventoryHolder.getInventory().first(iStack.getType()));
secStack.setAmount(secStack.getAmount() - (minAmount - amount));
} else {
iStack.setAmount(amount - minAmount);
}
craft.setBurningFuel(craft.getBurningFuel() + burningFuel);
break;
}
return true;
}
use of net.countercraft.movecraft.events.FuelBurnEvent in project Movecraft by APDevTeam.
the class TranslationTask method get.
@Override
public Effect get() {
var preTranslationResult = preTranslationValidators.stream().reduce(MonadicPredicate::and).orElseThrow().validate(craft);
if (!preTranslationResult.isSucess()) {
return () -> craft.getAudience().sendMessage(Component.text(preTranslationResult.getMessage()));
}
var preTranslateEvent = WorldManager.INSTANCE.executeMain(() -> {
var event = new CraftPreTranslateEvent(craft, translation.getX(), translation.getY(), translation.getZ(), craft.getWorld());
Bukkit.getServer().getPluginManager().callEvent(event);
return event;
});
if (preTranslateEvent.isCancelled()) {
return () -> craft.getAudience().sendMessage(Component.text(preTranslateEvent.getFailMessage()));
}
translation = new MovecraftLocation(preTranslateEvent.getDx(), preTranslateEvent.getDy(), preTranslateEvent.getDz());
destinationWorld = CachedMovecraftWorld.of(preTranslateEvent.getWorld());
// TODO: Portal movement
// TODO: Gravity
var destinationLocations = new SetHitBox();
var collisions = new SetHitBox();
var phaseLocations = new SetHitBox();
var harvestLocations = new SetHitBox();
var fuelSources = new ArrayList<FurnaceInventory>();
for (var originLocation : craft.getHitBox()) {
var originMaterial = craft.getMovecraftWorld().getMaterial(originLocation);
// Remove air from hitboxes
if (originMaterial.isAir())
continue;
if (Tags.FURNACES.contains(originMaterial)) {
var state = craft.getMovecraftWorld().getState(originLocation);
if (state instanceof FurnaceInventory)
fuelSources.add((FurnaceInventory) state);
}
var destination = originLocation.add(translation);
destinationLocations.add(destination);
// previous locations cannot collide
if (craft.getMovecraftWorld().equals(destinationWorld) && craft.getHitBox().contains(destination)) {
continue;
}
var destinationMaterial = destinationWorld.getMaterial(destination);
if (craft.getType().getMaterialSetProperty(CraftType.PASSTHROUGH_BLOCKS).contains(destinationMaterial)) {
phaseLocations.add(destination);
continue;
}
if (craft.getType().getMaterialSetProperty(CraftType.HARVEST_BLOCKS).contains(destinationMaterial) && craft.getType().getMaterialSetProperty(CraftType.HARVESTER_BLADE_BLOCKS).contains(originMaterial)) {
harvestLocations.add(destination);
continue;
}
collisions.add(destination);
}
double fuelBurnRate = (double) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_FUEL_BURN_RATE, preTranslateEvent.getWorld());
Effect fuelBurnEffect;
if (craft.getBurningFuel() >= fuelBurnRate) {
// call event
final FuelBurnEvent event = new FuelBurnEvent(craft, craft.getBurningFuel(), fuelBurnRate);
submitEvent(event);
fuelBurnEffect = () -> craft.setBurningFuel(event.getBurningFuel() - event.getFuelBurnRate());
} else {
var fuelSource = findFuelHolders(craft.getType(), fuelSources);
if (fuelSource == null) {
return () -> craft.getAudience().sendMessage(I18nSupport.getInternationalisedComponent("Translation - Failed Craft out of fuel"));
}
callFuelEvent(craft, findFuelStack(craft.getType(), fuelSource));
// TODO: Take Fuel
fuelBurnEffect = () -> Bukkit.getLogger().info("This is where we'd take ur fuel, if we had some");
}
var translationResult = translationValidators.stream().reduce(TetradicPredicate::and).orElseThrow().validate(translation, destinationWorld, destinationLocations, craft.getType());
if (!translationResult.isSucess()) {
return () -> craft.getAudience().sendMessage(Component.text(translationResult.getMessage()));
}
// Direct float comparison due to check for statically initialized value
callCollisionEvent(craft, collisions, preTranslateEvent.getWorld());
if (craft.getType().getFloatProperty(CraftType.COLLISION_EXPLOSION) <= 0F && !collisions.isEmpty()) {
// TODO: collision highlights
return () -> craft.getAudience().sendMessage(Component.text(String.format(I18nSupport.getInternationalisedString("Translation - Failed Craft is obstructed") + " @ %d,%d,%d,%s", 0, 0, 0, "not_implemented")));
}
Effect fluidBoxEffect = fluidBox(craft, translation);
var translateEvent = callTranslateEvent(craft, destinationLocations, preTranslateEvent.getWorld());
// TODO: Sinking?
// TODO: Collision explosion
// TODO: phase blocks
Effect movementEffect = moveCraft();
// TODO: un-phase blocks
Effect teleportEffect = new TeleportationEffect(craft, translation, translateEvent.getWorld());
return fuelBurnEffect.andThen(fluidBoxEffect).andThen(movementEffect).andThen(teleportEffect);
}
Aggregations